example_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. "testing"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestExamplePair(t *testing.T) {
  11. // This implements the tripartite Diffie-Hellman algorithm from "A One
  12. // Round Protocol for Tripartite Diffie-Hellman", A. Joux.
  13. // http://www.springerlink.com/content/cddc57yyva0hburb/fulltext.pdf
  14. // Each of three parties, a, b and c, generate a private value.
  15. a, _ := rand.Int(rand.Reader, Order)
  16. b, _ := rand.Int(rand.Reader, Order)
  17. c, _ := rand.Int(rand.Reader, Order)
  18. // Then each party calculates g₁ and g₂ times their private value.
  19. pa := new(G1).ScalarBaseMult(a)
  20. qa := new(G2).ScalarBaseMult(a)
  21. pb := new(G1).ScalarBaseMult(b)
  22. qb := new(G2).ScalarBaseMult(b)
  23. pc := new(G1).ScalarBaseMult(c)
  24. qc := new(G2).ScalarBaseMult(c)
  25. // Now each party exchanges its public values with the other two and
  26. // all parties can calculate the shared key.
  27. k1 := Pair(pb, qc)
  28. k1.ScalarMult(k1, a)
  29. k2 := Pair(pc, qa)
  30. k2.ScalarMult(k2, b)
  31. k3 := Pair(pa, qb)
  32. k3.ScalarMult(k3, c)
  33. // k1, k2 and k3 will all be equal.
  34. require.Equal(t, k1, k2)
  35. require.Equal(t, k1, k3)
  36. require.Equal(t, len(np), 4) //Avoid gometalinter varcheck err on np
  37. }