main_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package bn256
  2. import (
  3. "testing"
  4. "crypto/rand"
  5. )
  6. func TestRandomG2Marshal(t *testing.T) {
  7. for i := 0; i < 10; i++ {
  8. n, g2, err := RandomG2(rand.Reader)
  9. if err != nil {
  10. t.Error(err)
  11. continue
  12. }
  13. t.Logf("%d: %x\n", n, g2.Marshal())
  14. }
  15. }
  16. func TestPairings(t *testing.T) {
  17. a1 := new(G1).ScalarBaseMult(bigFromBase10("1"))
  18. a2 := new(G1).ScalarBaseMult(bigFromBase10("2"))
  19. a37 := new(G1).ScalarBaseMult(bigFromBase10("37"))
  20. an1 := new(G1).ScalarBaseMult(bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495616"))
  21. b0 := new(G2).ScalarBaseMult(bigFromBase10("0"))
  22. b1 := new(G2).ScalarBaseMult(bigFromBase10("1"))
  23. b2 := new(G2).ScalarBaseMult(bigFromBase10("2"))
  24. b27 := new(G2).ScalarBaseMult(bigFromBase10("27"))
  25. b999 := new(G2).ScalarBaseMult(bigFromBase10("999"))
  26. bn1 := new(G2).ScalarBaseMult(bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495616"))
  27. p1 := Pair(a1, b1)
  28. pn1 := Pair(a1, bn1)
  29. np1 := Pair(an1, b1)
  30. if pn1.String() != np1.String() {
  31. t.Error("Pairing mismatch: e(a, -b) != e(-a, b)")
  32. }
  33. if !PairingCheck([]*G1{a1, an1}, []*G2{b1, b1}) {
  34. t.Error("MultiAte check gave false negative!")
  35. }
  36. p0 := new(GT).Add(p1, pn1)
  37. p0_2 := Pair(a1, b0)
  38. if p0.String() != p0_2.String() {
  39. t.Error("Pairing mismatch: e(a, b) * e(a, -b) != 1")
  40. }
  41. p0_3 := new(GT).ScalarMult(p1, bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495617"))
  42. if p0.String() != p0_3.String() {
  43. t.Error("Pairing mismatch: e(a, b) has wrong order")
  44. }
  45. p2 := Pair(a2, b1)
  46. p2_2 := Pair(a1, b2)
  47. p2_3 := new(GT).ScalarMult(p1, bigFromBase10("2"))
  48. if p2.String() != p2_2.String() {
  49. t.Error("Pairing mismatch: e(a, b * 2) != e(a * 2, b)")
  50. }
  51. if p2.String() != p2_3.String() {
  52. t.Error("Pairing mismatch: e(a, b * 2) != e(a, b) ** 2")
  53. }
  54. if p2.String() == p1.String() {
  55. t.Error("Pairing is degenerate!")
  56. }
  57. if PairingCheck([]*G1{a1, a1}, []*G2{b1, b1}) {
  58. t.Error("MultiAte check gave false positive!")
  59. }
  60. p999 := Pair(a37, b27)
  61. p999_2 := Pair(a1, b999)
  62. if p999.String() != p999_2.String() {
  63. t.Error("Pairing mismatch: e(a * 37, b * 27) != e(a, b * 999)")
  64. }
  65. }