Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

305 рядки
6.5 KiB

  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. "bytes"
  7. "crypto/rand"
  8. "math/big"
  9. "testing"
  10. )
  11. func TestGFp2Invert(t *testing.T) {
  12. pool := new(bnPool)
  13. a := newGFp2(pool)
  14. a.x.SetString("23423492374", 10)
  15. a.y.SetString("12934872398472394827398470", 10)
  16. inv := newGFp2(pool)
  17. inv.Invert(a, pool)
  18. b := newGFp2(pool).Mul(inv, a, pool)
  19. if b.x.Int64() != 0 || b.y.Int64() != 1 {
  20. t.Fatalf("bad result for a^-1*a: %s %s", b.x, b.y)
  21. }
  22. a.Put(pool)
  23. b.Put(pool)
  24. inv.Put(pool)
  25. if c := pool.Count(); c > 0 {
  26. t.Errorf("Pool count non-zero: %d\n", c)
  27. }
  28. }
  29. func isZero(n *big.Int) bool {
  30. return new(big.Int).Mod(n, p).Int64() == 0
  31. }
  32. func isOne(n *big.Int) bool {
  33. return new(big.Int).Mod(n, p).Int64() == 1
  34. }
  35. func TestGFp6Invert(t *testing.T) {
  36. pool := new(bnPool)
  37. a := newGFp6(pool)
  38. a.x.x.SetString("239487238491", 10)
  39. a.x.y.SetString("2356249827341", 10)
  40. a.y.x.SetString("082659782", 10)
  41. a.y.y.SetString("182703523765", 10)
  42. a.z.x.SetString("978236549263", 10)
  43. a.z.y.SetString("64893242", 10)
  44. inv := newGFp6(pool)
  45. inv.Invert(a, pool)
  46. b := newGFp6(pool).Mul(inv, a, pool)
  47. if !isZero(b.x.x) ||
  48. !isZero(b.x.y) ||
  49. !isZero(b.y.x) ||
  50. !isZero(b.y.y) ||
  51. !isZero(b.z.x) ||
  52. !isOne(b.z.y) {
  53. t.Fatalf("bad result for a^-1*a: %s", b)
  54. }
  55. a.Put(pool)
  56. b.Put(pool)
  57. inv.Put(pool)
  58. if c := pool.Count(); c > 0 {
  59. t.Errorf("Pool count non-zero: %d\n", c)
  60. }
  61. }
  62. func TestGFp12Invert(t *testing.T) {
  63. pool := new(bnPool)
  64. a := newGFp12(pool)
  65. a.x.x.x.SetString("239846234862342323958623", 10)
  66. a.x.x.y.SetString("2359862352529835623", 10)
  67. a.x.y.x.SetString("928836523", 10)
  68. a.x.y.y.SetString("9856234", 10)
  69. a.x.z.x.SetString("235635286", 10)
  70. a.x.z.y.SetString("5628392833", 10)
  71. a.y.x.x.SetString("252936598265329856238956532167968", 10)
  72. a.y.x.y.SetString("23596239865236954178968", 10)
  73. a.y.y.x.SetString("95421692834", 10)
  74. a.y.y.y.SetString("236548", 10)
  75. a.y.z.x.SetString("924523", 10)
  76. a.y.z.y.SetString("12954623", 10)
  77. inv := newGFp12(pool)
  78. inv.Invert(a, pool)
  79. b := newGFp12(pool).Mul(inv, a, pool)
  80. if !isZero(b.x.x.x) ||
  81. !isZero(b.x.x.y) ||
  82. !isZero(b.x.y.x) ||
  83. !isZero(b.x.y.y) ||
  84. !isZero(b.x.z.x) ||
  85. !isZero(b.x.z.y) ||
  86. !isZero(b.y.x.x) ||
  87. !isZero(b.y.x.y) ||
  88. !isZero(b.y.y.x) ||
  89. !isZero(b.y.y.y) ||
  90. !isZero(b.y.z.x) ||
  91. !isOne(b.y.z.y) {
  92. t.Fatalf("bad result for a^-1*a: %s", b)
  93. }
  94. a.Put(pool)
  95. b.Put(pool)
  96. inv.Put(pool)
  97. if c := pool.Count(); c > 0 {
  98. t.Errorf("Pool count non-zero: %d\n", c)
  99. }
  100. }
  101. func TestCurveImpl(t *testing.T) {
  102. pool := new(bnPool)
  103. g := &curvePoint{
  104. pool.Get().SetInt64(1),
  105. pool.Get().SetInt64(-2),
  106. pool.Get().SetInt64(1),
  107. pool.Get().SetInt64(0),
  108. }
  109. x := pool.Get().SetInt64(32498273234)
  110. X := newCurvePoint(pool).Mul(g, x, pool)
  111. y := pool.Get().SetInt64(98732423523)
  112. Y := newCurvePoint(pool).Mul(g, y, pool)
  113. s1 := newCurvePoint(pool).Mul(X, y, pool).MakeAffine(pool)
  114. s2 := newCurvePoint(pool).Mul(Y, x, pool).MakeAffine(pool)
  115. if s1.x.Cmp(s2.x) != 0 ||
  116. s2.x.Cmp(s1.x) != 0 {
  117. t.Errorf("DH points don't match: (%s, %s) (%s, %s)", s1.x, s1.y, s2.x, s2.y)
  118. }
  119. pool.Put(x)
  120. X.Put(pool)
  121. pool.Put(y)
  122. Y.Put(pool)
  123. s1.Put(pool)
  124. s2.Put(pool)
  125. g.Put(pool)
  126. if c := pool.Count(); c > 0 {
  127. t.Errorf("Pool count non-zero: %d\n", c)
  128. }
  129. }
  130. func TestOrderG1(t *testing.T) {
  131. g := new(G1).ScalarBaseMult(Order)
  132. if !g.p.IsInfinity() {
  133. t.Error("G1 has incorrect order")
  134. }
  135. one := new(G1).ScalarBaseMult(new(big.Int).SetInt64(1))
  136. g.Add(g, one)
  137. g.p.MakeAffine(nil)
  138. if g.p.x.Cmp(one.p.x) != 0 || g.p.y.Cmp(one.p.y) != 0 {
  139. t.Errorf("1+0 != 1 in G1")
  140. }
  141. }
  142. func TestOrderG2(t *testing.T) {
  143. g := new(G2).ScalarBaseMult(Order)
  144. if !g.p.IsInfinity() {
  145. t.Error("G2 has incorrect order")
  146. }
  147. one := new(G2).ScalarBaseMult(new(big.Int).SetInt64(1))
  148. g.Add(g, one)
  149. g.p.MakeAffine(nil)
  150. if g.p.x.x.Cmp(one.p.x.x) != 0 ||
  151. g.p.x.y.Cmp(one.p.x.y) != 0 ||
  152. g.p.y.x.Cmp(one.p.y.x) != 0 ||
  153. g.p.y.y.Cmp(one.p.y.y) != 0 {
  154. t.Errorf("1+0 != 1 in G2")
  155. }
  156. }
  157. func TestOrderGT(t *testing.T) {
  158. gt := Pair(&G1{curveGen}, &G2{twistGen})
  159. g := new(GT).ScalarMult(gt, Order)
  160. if !g.p.IsOne() {
  161. t.Error("GT has incorrect order")
  162. }
  163. }
  164. func TestBilinearity(t *testing.T) {
  165. for i := 0; i < 2; i++ {
  166. a, p1, _ := RandomG1(rand.Reader)
  167. b, p2, _ := RandomG2(rand.Reader)
  168. e1 := Pair(p1, p2)
  169. e2 := Pair(&G1{curveGen}, &G2{twistGen})
  170. e2.ScalarMult(e2, a)
  171. e2.ScalarMult(e2, b)
  172. minusE2 := new(GT).Neg(e2)
  173. e1.Add(e1, minusE2)
  174. if !e1.p.IsOne() {
  175. t.Fatalf("bad pairing result: %s", e1)
  176. }
  177. }
  178. }
  179. func TestG1Marshal(t *testing.T) {
  180. g := new(G1).ScalarBaseMult(new(big.Int).SetInt64(1))
  181. form := g.Marshal()
  182. _, ok := new(G1).Unmarshal(form)
  183. if !ok {
  184. t.Fatalf("failed to unmarshal")
  185. }
  186. g.ScalarBaseMult(Order)
  187. form = g.Marshal()
  188. g2, ok := new(G1).Unmarshal(form)
  189. if !ok {
  190. t.Fatalf("failed to unmarshal ∞")
  191. }
  192. if !g2.p.IsInfinity() {
  193. t.Fatalf("∞ unmarshaled incorrectly")
  194. }
  195. }
  196. func TestG2Marshal(t *testing.T) {
  197. g := new(G2).ScalarBaseMult(new(big.Int).SetInt64(1))
  198. form := g.Marshal()
  199. _, ok := new(G2).Unmarshal(form)
  200. if !ok {
  201. t.Fatalf("failed to unmarshal")
  202. }
  203. g.ScalarBaseMult(Order)
  204. form = g.Marshal()
  205. g2, ok := new(G2).Unmarshal(form)
  206. if !ok {
  207. t.Fatalf("failed to unmarshal ∞")
  208. }
  209. if !g2.p.IsInfinity() {
  210. t.Fatalf("∞ unmarshaled incorrectly")
  211. }
  212. }
  213. func TestG1Identity(t *testing.T) {
  214. g := new(G1).ScalarBaseMult(new(big.Int).SetInt64(0))
  215. if !g.p.IsInfinity() {
  216. t.Error("failure")
  217. }
  218. }
  219. func TestG2Identity(t *testing.T) {
  220. g := new(G2).ScalarBaseMult(new(big.Int).SetInt64(0))
  221. if !g.p.IsInfinity() {
  222. t.Error("failure")
  223. }
  224. }
  225. func TestTripartiteDiffieHellman(t *testing.T) {
  226. a, _ := rand.Int(rand.Reader, Order)
  227. b, _ := rand.Int(rand.Reader, Order)
  228. c, _ := rand.Int(rand.Reader, Order)
  229. pa, _ := new(G1).Unmarshal(new(G1).ScalarBaseMult(a).Marshal())
  230. qa, _ := new(G2).Unmarshal(new(G2).ScalarBaseMult(a).Marshal())
  231. pb, _ := new(G1).Unmarshal(new(G1).ScalarBaseMult(b).Marshal())
  232. qb, _ := new(G2).Unmarshal(new(G2).ScalarBaseMult(b).Marshal())
  233. pc, _ := new(G1).Unmarshal(new(G1).ScalarBaseMult(c).Marshal())
  234. qc, _ := new(G2).Unmarshal(new(G2).ScalarBaseMult(c).Marshal())
  235. k1 := Pair(pb, qc)
  236. k1.ScalarMult(k1, a)
  237. k1Bytes := k1.Marshal()
  238. k2 := Pair(pc, qa)
  239. k2.ScalarMult(k2, b)
  240. k2Bytes := k2.Marshal()
  241. k3 := Pair(pa, qb)
  242. k3.ScalarMult(k3, c)
  243. k3Bytes := k3.Marshal()
  244. if !bytes.Equal(k1Bytes, k2Bytes) || !bytes.Equal(k2Bytes, k3Bytes) {
  245. t.Errorf("keys didn't agree")
  246. }
  247. }
  248. func BenchmarkPairing(b *testing.B) {
  249. for i := 0; i < b.N; i++ {
  250. Pair(&G1{curveGen}, &G2{twistGen})
  251. }
  252. }