example_test.go 1.1 KB

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