pairing_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package bls12381
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/ethereum/go-ethereum/common"
  6. )
  7. func TestPairingExpected(t *testing.T) {
  8. bls := NewPairingEngine()
  9. G1, G2 := bls.G1, bls.G2
  10. GT := bls.GT()
  11. expected, err := GT.FromBytes(
  12. common.FromHex("" +
  13. "0f41e58663bf08cf068672cbd01a7ec73baca4d72ca93544deff686bfd6df543d48eaa24afe47e1efde449383b676631" +
  14. "04c581234d086a9902249b64728ffd21a189e87935a954051c7cdba7b3872629a4fafc05066245cb9108f0242d0fe3ef" +
  15. "03350f55a7aefcd3c31b4fcb6ce5771cc6a0e9786ab5973320c806ad360829107ba810c5a09ffdd9be2291a0c25a99a2" +
  16. "11b8b424cd48bf38fcef68083b0b0ec5c81a93b330ee1a677d0d15ff7b984e8978ef48881e32fac91b93b47333e2ba57" +
  17. "06fba23eb7c5af0d9f80940ca771b6ffd5857baaf222eb95a7d2809d61bfe02e1bfd1b68ff02f0b8102ae1c2d5d5ab1a" +
  18. "19f26337d205fb469cd6bd15c3d5a04dc88784fbb3d0b2dbdea54d43b2b73f2cbb12d58386a8703e0f948226e47ee89d" +
  19. "018107154f25a764bd3c79937a45b84546da634b8f6be14a8061e55cceba478b23f7dacaa35c8ca78beae9624045b4b6" +
  20. "01b2f522473d171391125ba84dc4007cfbf2f8da752f7c74185203fcca589ac719c34dffbbaad8431dad1c1fb597aaa5" +
  21. "193502b86edb8857c273fa075a50512937e0794e1e65a7617c90d8bd66065b1fffe51d7a579973b1315021ec3c19934f" +
  22. "1368bb445c7c2d209703f239689ce34c0378a68e72a6b3b216da0e22a5031b54ddff57309396b38c881c4c849ec23e87" +
  23. "089a1c5b46e5110b86750ec6a532348868a84045483c92b7af5af689452eafabf1a8943e50439f1d59882a98eaa0170f" +
  24. "1250ebd871fc0a92a7b2d83168d0d727272d441befa15c503dd8e90ce98db3e7b6d194f60839c508a84305aaca1789b6",
  25. ),
  26. )
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. r := bls.AddPair(G1.One(), G2.One()).Result()
  31. if !r.Equal(expected) {
  32. t.Fatal("bad pairing")
  33. }
  34. if !GT.IsValid(r) {
  35. t.Fatal("element is not in correct subgroup")
  36. }
  37. }
  38. func TestPairingNonDegeneracy(t *testing.T) {
  39. bls := NewPairingEngine()
  40. G1, G2 := bls.G1, bls.G2
  41. g1Zero, g2Zero, g1One, g2One := G1.Zero(), G2.Zero(), G1.One(), G2.One()
  42. GT := bls.GT()
  43. // e(g1^a, g2^b) != 1
  44. bls.Reset()
  45. {
  46. bls.AddPair(g1One, g2One)
  47. e := bls.Result()
  48. if e.IsOne() {
  49. t.Fatal("pairing result is not expected to be one")
  50. }
  51. if !GT.IsValid(e) {
  52. t.Fatal("pairing result is not valid")
  53. }
  54. }
  55. // e(g1^a, 0) == 1
  56. bls.Reset()
  57. {
  58. bls.AddPair(g1One, g2Zero)
  59. e := bls.Result()
  60. if !e.IsOne() {
  61. t.Fatal("pairing result is expected to be one")
  62. }
  63. }
  64. // e(0, g2^b) == 1
  65. bls.Reset()
  66. {
  67. bls.AddPair(g1Zero, g2One)
  68. e := bls.Result()
  69. if !e.IsOne() {
  70. t.Fatal("pairing result is expected to be one")
  71. }
  72. }
  73. //
  74. bls.Reset()
  75. {
  76. bls.AddPair(g1Zero, g2One)
  77. bls.AddPair(g1One, g2Zero)
  78. bls.AddPair(g1Zero, g2Zero)
  79. e := bls.Result()
  80. if !e.IsOne() {
  81. t.Fatal("pairing result is expected to be one")
  82. }
  83. }
  84. //
  85. bls.Reset()
  86. {
  87. expected, err := GT.FromBytes(
  88. common.FromHex("" +
  89. "0f41e58663bf08cf068672cbd01a7ec73baca4d72ca93544deff686bfd6df543d48eaa24afe47e1efde449383b676631" +
  90. "04c581234d086a9902249b64728ffd21a189e87935a954051c7cdba7b3872629a4fafc05066245cb9108f0242d0fe3ef" +
  91. "03350f55a7aefcd3c31b4fcb6ce5771cc6a0e9786ab5973320c806ad360829107ba810c5a09ffdd9be2291a0c25a99a2" +
  92. "11b8b424cd48bf38fcef68083b0b0ec5c81a93b330ee1a677d0d15ff7b984e8978ef48881e32fac91b93b47333e2ba57" +
  93. "06fba23eb7c5af0d9f80940ca771b6ffd5857baaf222eb95a7d2809d61bfe02e1bfd1b68ff02f0b8102ae1c2d5d5ab1a" +
  94. "19f26337d205fb469cd6bd15c3d5a04dc88784fbb3d0b2dbdea54d43b2b73f2cbb12d58386a8703e0f948226e47ee89d" +
  95. "018107154f25a764bd3c79937a45b84546da634b8f6be14a8061e55cceba478b23f7dacaa35c8ca78beae9624045b4b6" +
  96. "01b2f522473d171391125ba84dc4007cfbf2f8da752f7c74185203fcca589ac719c34dffbbaad8431dad1c1fb597aaa5" +
  97. "193502b86edb8857c273fa075a50512937e0794e1e65a7617c90d8bd66065b1fffe51d7a579973b1315021ec3c19934f" +
  98. "1368bb445c7c2d209703f239689ce34c0378a68e72a6b3b216da0e22a5031b54ddff57309396b38c881c4c849ec23e87" +
  99. "089a1c5b46e5110b86750ec6a532348868a84045483c92b7af5af689452eafabf1a8943e50439f1d59882a98eaa0170f" +
  100. "1250ebd871fc0a92a7b2d83168d0d727272d441befa15c503dd8e90ce98db3e7b6d194f60839c508a84305aaca1789b6",
  101. ),
  102. )
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. bls.AddPair(g1Zero, g2One)
  107. bls.AddPair(g1One, g2Zero)
  108. bls.AddPair(g1Zero, g2Zero)
  109. bls.AddPair(g1One, g2One)
  110. e := bls.Result()
  111. if !e.Equal(expected) {
  112. t.Fatal("bad pairing")
  113. }
  114. }
  115. }
  116. func TestPairingBilinearity(t *testing.T) {
  117. bls := NewPairingEngine()
  118. g1, g2 := bls.G1, bls.G2
  119. gt := bls.GT()
  120. // e(a*G1, b*G2) = e(G1, G2)^c
  121. {
  122. a, b := big.NewInt(17), big.NewInt(117)
  123. c := new(big.Int).Mul(a, b)
  124. G1, G2 := g1.One(), g2.One()
  125. e0 := bls.AddPair(G1, G2).Result()
  126. P1, P2 := g1.New(), g2.New()
  127. g1.MulScalar(P1, G1, a)
  128. g2.MulScalar(P2, G2, b)
  129. e1 := bls.AddPair(P1, P2).Result()
  130. gt.Exp(e0, e0, c)
  131. if !e0.Equal(e1) {
  132. t.Fatal("bad pairing, 1")
  133. }
  134. }
  135. // e(a * G1, b * G2) = e((a + b) * G1, G2)
  136. {
  137. // scalars
  138. a, b := big.NewInt(17), big.NewInt(117)
  139. c := new(big.Int).Mul(a, b)
  140. // LHS
  141. G1, G2 := g1.One(), g2.One()
  142. g1.MulScalar(G1, G1, c)
  143. bls.AddPair(G1, G2)
  144. // RHS
  145. P1, P2 := g1.One(), g2.One()
  146. g1.MulScalar(P1, P1, a)
  147. g2.MulScalar(P2, P2, b)
  148. bls.AddPairInv(P1, P2)
  149. // should be one
  150. if !bls.Check() {
  151. t.Fatal("bad pairing, 2")
  152. }
  153. }
  154. // e(a * G1, b * G2) = e((a + b) * G1, G2)
  155. {
  156. // scalars
  157. a, b := big.NewInt(17), big.NewInt(117)
  158. c := new(big.Int).Mul(a, b)
  159. // LHS
  160. G1, G2 := g1.One(), g2.One()
  161. g2.MulScalar(G2, G2, c)
  162. bls.AddPair(G1, G2)
  163. // RHS
  164. H1, H2 := g1.One(), g2.One()
  165. g1.MulScalar(H1, H1, a)
  166. g2.MulScalar(H2, H2, b)
  167. bls.AddPairInv(H1, H2)
  168. // should be one
  169. if !bls.Check() {
  170. t.Fatal("bad pairing, 3")
  171. }
  172. }
  173. }
  174. func TestPairingMulti(t *testing.T) {
  175. // e(G1, G2) ^ t == e(a01 * G1, a02 * G2) * e(a11 * G1, a12 * G2) * ... * e(an1 * G1, an2 * G2)
  176. // where t = sum(ai1 * ai2)
  177. bls := NewPairingEngine()
  178. g1, g2 := bls.G1, bls.G2
  179. numOfPair := 100
  180. targetExp := new(big.Int)
  181. // RHS
  182. for i := 0; i < numOfPair; i++ {
  183. // (ai1 * G1, ai2 * G2)
  184. a1, a2 := randScalar(q), randScalar(q)
  185. P1, P2 := g1.One(), g2.One()
  186. g1.MulScalar(P1, P1, a1)
  187. g2.MulScalar(P2, P2, a2)
  188. bls.AddPair(P1, P2)
  189. // accumulate targetExp
  190. // t += (ai1 * ai2)
  191. a1.Mul(a1, a2)
  192. targetExp.Add(targetExp, a1)
  193. }
  194. // LHS
  195. // e(t * G1, G2)
  196. T1, T2 := g1.One(), g2.One()
  197. g1.MulScalar(T1, T1, targetExp)
  198. bls.AddPairInv(T1, T2)
  199. if !bls.Check() {
  200. t.Fatal("fail multi pairing")
  201. }
  202. }
  203. func TestPairingEmpty(t *testing.T) {
  204. bls := NewPairingEngine()
  205. if !bls.Check() {
  206. t.Fatal("empty check should be accepted")
  207. }
  208. if !bls.Result().IsOne() {
  209. t.Fatal("empty pairing result should be one")
  210. }
  211. }
  212. func BenchmarkPairing(t *testing.B) {
  213. bls := NewPairingEngine()
  214. g1, g2, gt := bls.G1, bls.G2, bls.GT()
  215. bls.AddPair(g1.One(), g2.One())
  216. e := gt.New()
  217. t.ResetTimer()
  218. for i := 0; i < t.N; i++ {
  219. e = bls.calculate()
  220. }
  221. _ = e
  222. }