You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
9.2 KiB

  1. package babyjub
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. "math/rand"
  6. "testing"
  7. "github.com/iden3/go-iden3-crypto/constants"
  8. "github.com/iden3/go-iden3-crypto/utils"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestAdd1(t *testing.T) {
  12. a := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
  13. b := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
  14. c := NewPoint().Add(a, b)
  15. // fmt.Printf("%v = 2 * %v", *c, *a)
  16. assert.Equal(t, "0", c.X.String())
  17. assert.Equal(t, "1", c.Y.String())
  18. }
  19. func TestAdd2(t *testing.T) {
  20. aX := utils.NewIntFromString(
  21. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  22. aY := utils.NewIntFromString(
  23. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  24. a := &Point{X: aX, Y: aY}
  25. bX := utils.NewIntFromString(
  26. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  27. bY := utils.NewIntFromString(
  28. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  29. b := &Point{X: bX, Y: bY}
  30. c := NewPoint().Add(a, b)
  31. // fmt.Printf("%v = 2 * %v", *c, *a)
  32. assert.Equal(t,
  33. "6890855772600357754907169075114257697580319025794532037257385534741338397365",
  34. c.X.String())
  35. assert.Equal(t,
  36. "4338620300185947561074059802482547481416142213883829469920100239455078257889",
  37. c.Y.String())
  38. }
  39. func TestAdd3(t *testing.T) {
  40. aX := utils.NewIntFromString(
  41. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  42. aY := utils.NewIntFromString(
  43. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  44. a := &Point{X: aX, Y: aY}
  45. bX := utils.NewIntFromString(
  46. "16540640123574156134436876038791482806971768689494387082833631921987005038935")
  47. bY := utils.NewIntFromString(
  48. "20819045374670962167435360035096875258406992893633759881276124905556507972311")
  49. b := &Point{X: bX, Y: bY}
  50. c := NewPoint().Add(a, b)
  51. // fmt.Printf("%v = 2 * %v", *c, *a)
  52. assert.Equal(t,
  53. "7916061937171219682591368294088513039687205273691143098332585753343424131937",
  54. c.X.String())
  55. assert.Equal(t,
  56. "14035240266687799601661095864649209771790948434046947201833777492504781204499",
  57. c.Y.String())
  58. }
  59. func TestAdd4(t *testing.T) {
  60. aX := utils.NewIntFromString(
  61. "0")
  62. aY := utils.NewIntFromString(
  63. "1")
  64. a := &Point{X: aX, Y: aY}
  65. bX := utils.NewIntFromString(
  66. "16540640123574156134436876038791482806971768689494387082833631921987005038935")
  67. bY := utils.NewIntFromString(
  68. "20819045374670962167435360035096875258406992893633759881276124905556507972311")
  69. b := &Point{X: bX, Y: bY}
  70. c := NewPoint().Add(a, b)
  71. // fmt.Printf("%v = 2 * %v", *c, *a)
  72. assert.Equal(t,
  73. "16540640123574156134436876038791482806971768689494387082833631921987005038935",
  74. c.X.String())
  75. assert.Equal(t,
  76. "20819045374670962167435360035096875258406992893633759881276124905556507972311",
  77. c.Y.String())
  78. }
  79. func TestInCurve1(t *testing.T) {
  80. p := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
  81. assert.Equal(t, true, p.InCurve())
  82. }
  83. func TestInCurve2(t *testing.T) {
  84. p := &Point{X: big.NewInt(1), Y: big.NewInt(0)}
  85. assert.Equal(t, false, p.InCurve())
  86. }
  87. func TestMul0(t *testing.T) {
  88. x := utils.NewIntFromString(
  89. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  90. y := utils.NewIntFromString(
  91. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  92. p := &Point{X: x, Y: y}
  93. s := utils.NewIntFromString("3")
  94. r2 := NewPoint().Add(p, p)
  95. r2 = NewPoint().Add(r2, p)
  96. r := NewPoint().Mul(s, p)
  97. assert.Equal(t, r2.X.String(), r.X.String())
  98. assert.Equal(t, r2.Y.String(), r.Y.String())
  99. assert.Equal(t,
  100. "19372461775513343691590086534037741906533799473648040012278229434133483800898",
  101. r.X.String())
  102. assert.Equal(t,
  103. "9458658722007214007257525444427903161243386465067105737478306991484593958249",
  104. r.Y.String())
  105. }
  106. func TestMul1(t *testing.T) {
  107. x := utils.NewIntFromString(
  108. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  109. y := utils.NewIntFromString(
  110. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  111. p := &Point{X: x, Y: y}
  112. s := utils.NewIntFromString(
  113. "14035240266687799601661095864649209771790948434046947201833777492504781204499")
  114. r := NewPoint().Mul(s, p)
  115. assert.Equal(t,
  116. "17070357974431721403481313912716834497662307308519659060910483826664480189605",
  117. r.X.String())
  118. assert.Equal(t,
  119. "4014745322800118607127020275658861516666525056516280575712425373174125159339",
  120. r.Y.String())
  121. }
  122. func TestMul2(t *testing.T) {
  123. x := utils.NewIntFromString(
  124. "6890855772600357754907169075114257697580319025794532037257385534741338397365")
  125. y := utils.NewIntFromString(
  126. "4338620300185947561074059802482547481416142213883829469920100239455078257889")
  127. p := &Point{X: x, Y: y}
  128. s := utils.NewIntFromString(
  129. "20819045374670962167435360035096875258406992893633759881276124905556507972311")
  130. r := NewPoint().Mul(s, p)
  131. assert.Equal(t,
  132. "13563888653650925984868671744672725781658357821216877865297235725727006259983",
  133. r.X.String())
  134. assert.Equal(t,
  135. "8442587202676550862664528699803615547505326611544120184665036919364004251662",
  136. r.Y.String())
  137. }
  138. func TestInCurve3(t *testing.T) {
  139. x := utils.NewIntFromString(
  140. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  141. y := utils.NewIntFromString(
  142. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  143. p := &Point{X: x, Y: y}
  144. assert.Equal(t, true, p.InCurve())
  145. }
  146. func TestInCurve4(t *testing.T) {
  147. x := utils.NewIntFromString(
  148. "6890855772600357754907169075114257697580319025794532037257385534741338397365")
  149. y := utils.NewIntFromString(
  150. "4338620300185947561074059802482547481416142213883829469920100239455078257889")
  151. p := &Point{X: x, Y: y}
  152. assert.Equal(t, true, p.InCurve())
  153. }
  154. func TestInSubGroup1(t *testing.T) {
  155. x := utils.NewIntFromString(
  156. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  157. y := utils.NewIntFromString(
  158. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  159. p := &Point{X: x, Y: y}
  160. assert.Equal(t, true, p.InSubGroup())
  161. }
  162. func TestInSubGroup2(t *testing.T) {
  163. x := utils.NewIntFromString(
  164. "6890855772600357754907169075114257697580319025794532037257385534741338397365")
  165. y := utils.NewIntFromString(
  166. "4338620300185947561074059802482547481416142213883829469920100239455078257889")
  167. p := &Point{X: x, Y: y}
  168. assert.Equal(t, true, p.InSubGroup())
  169. }
  170. func TestCompressDecompress1(t *testing.T) {
  171. x := utils.NewIntFromString(
  172. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  173. y := utils.NewIntFromString(
  174. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  175. p := &Point{X: x, Y: y}
  176. buf := p.Compress()
  177. assert.Equal(t, "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85", hex.EncodeToString(buf[:]))
  178. p2, err := NewPoint().Decompress(buf)
  179. assert.Equal(t, nil, err)
  180. assert.Equal(t, p.X.String(), p2.X.String())
  181. assert.Equal(t, p.Y.String(), p2.Y.String())
  182. }
  183. func TestCompressDecompress2(t *testing.T) {
  184. x := utils.NewIntFromString(
  185. "6890855772600357754907169075114257697580319025794532037257385534741338397365")
  186. y := utils.NewIntFromString(
  187. "4338620300185947561074059802482547481416142213883829469920100239455078257889")
  188. p := &Point{X: x, Y: y}
  189. buf := p.Compress()
  190. assert.Equal(t, "e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709", hex.EncodeToString(buf[:]))
  191. p2, err := NewPoint().Decompress(buf)
  192. assert.Equal(t, nil, err)
  193. assert.Equal(t, p.X.String(), p2.X.String())
  194. assert.Equal(t, p.Y.String(), p2.Y.String())
  195. }
  196. func TestCompressDecompressRnd(t *testing.T) {
  197. for i := 0; i < 64; i++ {
  198. p1 := NewPoint().Mul(big.NewInt(int64(i)), B8)
  199. buf := p1.Compress()
  200. p2, err := NewPoint().Decompress(buf)
  201. assert.Equal(t, nil, err)
  202. assert.Equal(t, p1, p2)
  203. }
  204. }
  205. func BenchmarkBabyjub(b *testing.B) {
  206. const n = 256
  207. rnd := rand.New(rand.NewSource(42))
  208. var badpoints [n]*Point
  209. for i := 0; i < n; i++ {
  210. x := new(big.Int).Rand(rnd, constants.Q)
  211. y := new(big.Int).Rand(rnd, constants.Q)
  212. badpoints[i] = &Point{X: x, Y: y}
  213. }
  214. var points [n]*Point
  215. baseX := utils.NewIntFromString(
  216. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  217. baseY := utils.NewIntFromString(
  218. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  219. base := &Point{X: baseX, Y: baseY}
  220. for i := 0; i < n; i++ {
  221. s := new(big.Int).Rand(rnd, constants.Q)
  222. points[i] = NewPoint().Mul(s, base)
  223. }
  224. var scalars [n]*big.Int
  225. for i := 0; i < n; i++ {
  226. scalars[i] = new(big.Int).Rand(rnd, constants.Q)
  227. }
  228. b.Run("AddConst", func(b *testing.B) {
  229. p0 := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
  230. p1 := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
  231. p2 := NewPoint()
  232. for i := 0; i < b.N; i++ {
  233. p2.Add(p0, p1)
  234. }
  235. })
  236. b.Run("AddRnd", func(b *testing.B) {
  237. res := NewPoint()
  238. for i := 0; i < b.N; i++ {
  239. res.Add(points[i%(n/2)], points[i%(n/2)+1])
  240. }
  241. })
  242. b.Run("MulRnd", func(b *testing.B) {
  243. res := NewPoint()
  244. for i := 0; i < b.N; i++ {
  245. res.Mul(scalars[i%n], points[i%n])
  246. }
  247. })
  248. b.Run("Compress", func(b *testing.B) {
  249. for i := 0; i < b.N; i++ {
  250. points[i%n].Compress()
  251. }
  252. })
  253. b.Run("InCurve", func(b *testing.B) {
  254. for i := 0; i < b.N; i++ {
  255. badpoints[i%n].InCurve()
  256. }
  257. })
  258. b.Run("InSubGroup", func(b *testing.B) {
  259. for i := 0; i < b.N; i++ {
  260. points[i%n].InCurve()
  261. }
  262. })
  263. }