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.

320 lines
9.7 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 TestPointFromSignAndy(t *testing.T) {
  171. x := utils.NewIntFromString(
  172. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  173. y := utils.NewIntFromString(
  174. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  175. p := &Point{X: x, Y: y}
  176. sign := PointCoordSign(p.X)
  177. p2, err := PointFromSignAndY(sign, p.Y)
  178. assert.Equal(t, nil, err)
  179. assert.Equal(t, p.X.String(), p2.X.String())
  180. assert.Equal(t, p.Y.String(), p2.Y.String())
  181. }
  182. func TestCompressDecompress1(t *testing.T) {
  183. x := utils.NewIntFromString(
  184. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  185. y := utils.NewIntFromString(
  186. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  187. p := &Point{X: x, Y: y}
  188. buf := p.Compress()
  189. assert.Equal(t, "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85", hex.EncodeToString(buf[:]))
  190. p2, err := NewPoint().Decompress(buf)
  191. assert.Equal(t, nil, err)
  192. assert.Equal(t, p.X.String(), p2.X.String())
  193. assert.Equal(t, p.Y.String(), p2.Y.String())
  194. }
  195. func TestCompressDecompress2(t *testing.T) {
  196. x := utils.NewIntFromString(
  197. "6890855772600357754907169075114257697580319025794532037257385534741338397365")
  198. y := utils.NewIntFromString(
  199. "4338620300185947561074059802482547481416142213883829469920100239455078257889")
  200. p := &Point{X: x, Y: y}
  201. buf := p.Compress()
  202. assert.Equal(t, "e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709", hex.EncodeToString(buf[:]))
  203. p2, err := NewPoint().Decompress(buf)
  204. assert.Equal(t, nil, err)
  205. assert.Equal(t, p.X.String(), p2.X.String())
  206. assert.Equal(t, p.Y.String(), p2.Y.String())
  207. }
  208. func TestCompressDecompressRnd(t *testing.T) {
  209. for i := 0; i < 64; i++ {
  210. p1 := NewPoint().Mul(big.NewInt(int64(i)), B8)
  211. buf := p1.Compress()
  212. p2, err := NewPoint().Decompress(buf)
  213. assert.Equal(t, nil, err)
  214. assert.Equal(t, p1, p2)
  215. }
  216. }
  217. func BenchmarkBabyjub(b *testing.B) {
  218. const n = 256
  219. rnd := rand.New(rand.NewSource(42))
  220. var badpoints [n]*Point
  221. for i := 0; i < n; i++ {
  222. x := new(big.Int).Rand(rnd, constants.Q)
  223. y := new(big.Int).Rand(rnd, constants.Q)
  224. badpoints[i] = &Point{X: x, Y: y}
  225. }
  226. var points [n]*Point
  227. baseX := utils.NewIntFromString(
  228. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  229. baseY := utils.NewIntFromString(
  230. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  231. base := &Point{X: baseX, Y: baseY}
  232. for i := 0; i < n; i++ {
  233. s := new(big.Int).Rand(rnd, constants.Q)
  234. points[i] = NewPoint().Mul(s, base)
  235. }
  236. var scalars [n]*big.Int
  237. for i := 0; i < n; i++ {
  238. scalars[i] = new(big.Int).Rand(rnd, constants.Q)
  239. }
  240. b.Run("AddConst", func(b *testing.B) {
  241. p0 := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
  242. p1 := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
  243. p2 := NewPoint()
  244. for i := 0; i < b.N; i++ {
  245. p2.Add(p0, p1)
  246. }
  247. })
  248. b.Run("AddRnd", func(b *testing.B) {
  249. res := NewPoint()
  250. for i := 0; i < b.N; i++ {
  251. res.Add(points[i%(n/2)], points[i%(n/2)+1])
  252. }
  253. })
  254. b.Run("MulRnd", func(b *testing.B) {
  255. res := NewPoint()
  256. for i := 0; i < b.N; i++ {
  257. res.Mul(scalars[i%n], points[i%n])
  258. }
  259. })
  260. b.Run("Compress", func(b *testing.B) {
  261. for i := 0; i < b.N; i++ {
  262. points[i%n].Compress()
  263. }
  264. })
  265. b.Run("InCurve", func(b *testing.B) {
  266. for i := 0; i < b.N; i++ {
  267. badpoints[i%n].InCurve()
  268. }
  269. })
  270. b.Run("InSubGroup", func(b *testing.B) {
  271. for i := 0; i < b.N; i++ {
  272. points[i%n].InCurve()
  273. }
  274. })
  275. }