github.com/shopperooofficial/ethereum-protocol@v1.9.7/crypto/bn256/cloudflare/example_test.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package bn256
     6  
     7  import (
     8  	"crypto/rand"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestExamplePair(t *testing.T) {
    15  	// This implements the tripartite Diffie-Hellman algorithm from "A One
    16  	// Round Protocol for Tripartite Diffie-Hellman", A. Joux.
    17  	// http://www.springerlink.com/content/cddc57yyva0hburb/fulltext.pdf
    18  
    19  	// Each of three parties, a, b and c, generate a private value.
    20  	a, _ := rand.Int(rand.Reader, Order)
    21  	b, _ := rand.Int(rand.Reader, Order)
    22  	c, _ := rand.Int(rand.Reader, Order)
    23  
    24  	// Then each party calculates g₁ and g₂ times their private value.
    25  	pa := new(G1).ScalarBaseMult(a)
    26  	qa := new(G2).ScalarBaseMult(a)
    27  
    28  	pb := new(G1).ScalarBaseMult(b)
    29  	qb := new(G2).ScalarBaseMult(b)
    30  
    31  	pc := new(G1).ScalarBaseMult(c)
    32  	qc := new(G2).ScalarBaseMult(c)
    33  
    34  	// Now each party exchanges its public values with the other two and
    35  	// all parties can calculate the shared key.
    36  	k1 := Pair(pb, qc)
    37  	k1.ScalarMult(k1, a)
    38  
    39  	k2 := Pair(pc, qa)
    40  	k2.ScalarMult(k2, b)
    41  
    42  	k3 := Pair(pa, qb)
    43  	k3.ScalarMult(k3, c)
    44  
    45  	// k1, k2 and k3 will all be equal.
    46  
    47  	require.Equal(t, k1, k2)
    48  	require.Equal(t, k1, k3)
    49  
    50  	require.Equal(t, len(np), 4) //Avoid gometalinter varcheck err on np
    51  }