github.com/smalaichami/go-bowhead@v0.0.0-20180311002552-16302db95eaa/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 // +build amd64,!appengine,!gccgo 6 7 package bn256 8 9 import ( 10 "crypto/rand" 11 ) 12 13 func ExamplePair() { 14 // This implements the tripartite Diffie-Hellman algorithm from "A One 15 // Round Protocol for Tripartite Diffie-Hellman", A. Joux. 16 // http://www.springerlink.com/content/cddc57yyva0hburb/fulltext.pdf 17 18 // Each of three parties, a, b and c, generate a private value. 19 a, _ := rand.Int(rand.Reader, Order) 20 b, _ := rand.Int(rand.Reader, Order) 21 c, _ := rand.Int(rand.Reader, Order) 22 23 // Then each party calculates g₁ and g₂ times their private value. 24 pa := new(G1).ScalarBaseMult(a) 25 qa := new(G2).ScalarBaseMult(a) 26 27 pb := new(G1).ScalarBaseMult(b) 28 qb := new(G2).ScalarBaseMult(b) 29 30 pc := new(G1).ScalarBaseMult(c) 31 qc := new(G2).ScalarBaseMult(c) 32 33 // Now each party exchanges its public values with the other two and 34 // all parties can calculate the shared key. 35 k1 := Pair(pb, qc) 36 k1.ScalarMult(k1, a) 37 38 k2 := Pair(pc, qa) 39 k2.ScalarMult(k2, b) 40 41 k3 := Pair(pa, qb) 42 k3.ScalarMult(k3, c) 43 44 // k1, k2 and k3 will all be equal. 45 }