github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/crypto/bn256/cloudflare/example_test.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package bn256
    19  
    20  import (
    21  	"crypto/rand"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestExamplePair(t *testing.T) {
    28  	// This implements the tripartite Diffie-Hellman algorithm from "A One
    29  	// Round Protocol for Tripartite Diffie-Hellman", A. Joux.
    30  	// http://www.springerlink.com/content/cddc57yyva0hburb/fulltext.pdf
    31  
    32  	// Each of three parties, a, b and c, generate a private value.
    33  	a, _ := rand.Int(rand.Reader, Order)
    34  	b, _ := rand.Int(rand.Reader, Order)
    35  	c, _ := rand.Int(rand.Reader, Order)
    36  
    37  	// Then each party calculates g₁ and g₂ times their private value.
    38  	pa := new(G1).ScalarBaseMult(a)
    39  	qa := new(G2).ScalarBaseMult(a)
    40  
    41  	pb := new(G1).ScalarBaseMult(b)
    42  	qb := new(G2).ScalarBaseMult(b)
    43  
    44  	pc := new(G1).ScalarBaseMult(c)
    45  	qc := new(G2).ScalarBaseMult(c)
    46  
    47  	// Now each party exchanges its public values with the other two and
    48  	// all parties can calculate the shared key.
    49  	k1 := Pair(pb, qc)
    50  	k1.ScalarMult(k1, a)
    51  
    52  	k2 := Pair(pc, qa)
    53  	k2.ScalarMult(k2, b)
    54  
    55  	k3 := Pair(pa, qb)
    56  	k3.ScalarMult(k3, c)
    57  
    58  	// k1, k2 and k3 will all be equal.
    59  
    60  	require.Equal(t, k1, k2)
    61  	require.Equal(t, k1, k3)
    62  
    63  	require.Equal(t, len(np), 4) //Avoid gometalinter varcheck err on np
    64  }