github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/crypto/bn256/google/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  )
    23  
    24  func ExamplePair() {
    25  	// This implements the tripartite Diffie-Hellman algorithm from "A One
    26  	// Round Protocol for Tripartite Diffie-Hellman", A. Joux.
    27  	// http://www.springerlink.com/content/cddc57yyva0hburb/fulltext.pdf
    28  
    29  	// Each of three parties, a, b and c, generate a private value.
    30  	a, _ := rand.Int(rand.Reader, Order)
    31  	b, _ := rand.Int(rand.Reader, Order)
    32  	c, _ := rand.Int(rand.Reader, Order)
    33  
    34  	// Then each party calculates g₁ and g₂ times their private value.
    35  	pa := new(G1).ScalarBaseMult(a)
    36  	qa := new(G2).ScalarBaseMult(a)
    37  
    38  	pb := new(G1).ScalarBaseMult(b)
    39  	qb := new(G2).ScalarBaseMult(b)
    40  
    41  	pc := new(G1).ScalarBaseMult(c)
    42  	qc := new(G2).ScalarBaseMult(c)
    43  
    44  	// Now each party exchanges its public values with the other two and
    45  	// all parties can calculate the shared key.
    46  	k1 := Pair(pb, qc)
    47  	k1.ScalarMult(k1, a)
    48  
    49  	k2 := Pair(pc, qa)
    50  	k2.ScalarMult(k2, b)
    51  
    52  	k3 := Pair(pa, qb)
    53  	k3.ScalarMult(k3, c)
    54  
    55  	// k1, k2 and k3 will all be equal.
    56  }