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.

233 lines
7.6 KiB

  1. package babyjub
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. "testing"
  6. "github.com/iden3/go-iden3-crypto/utils"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestAdd1(t *testing.T) {
  10. a := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
  11. b := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
  12. c := NewPoint().Add(a, b)
  13. // fmt.Printf("%v = 2 * %v", *c, *a)
  14. assert.Equal(t, "0", c.X.String())
  15. assert.Equal(t, "1", c.Y.String())
  16. }
  17. func TestAdd2(t *testing.T) {
  18. aX := utils.NewIntFromString(
  19. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  20. aY := utils.NewIntFromString(
  21. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  22. a := &Point{X: aX, Y: aY}
  23. bX := utils.NewIntFromString(
  24. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  25. bY := utils.NewIntFromString(
  26. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  27. b := &Point{X: bX, Y: bY}
  28. c := NewPoint().Add(a, b)
  29. // fmt.Printf("%v = 2 * %v", *c, *a)
  30. assert.Equal(t,
  31. "6890855772600357754907169075114257697580319025794532037257385534741338397365",
  32. c.X.String())
  33. assert.Equal(t,
  34. "4338620300185947561074059802482547481416142213883829469920100239455078257889",
  35. c.Y.String())
  36. }
  37. func TestAdd3(t *testing.T) {
  38. aX := utils.NewIntFromString(
  39. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  40. aY := utils.NewIntFromString(
  41. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  42. a := &Point{X: aX, Y: aY}
  43. bX := utils.NewIntFromString(
  44. "16540640123574156134436876038791482806971768689494387082833631921987005038935")
  45. bY := utils.NewIntFromString(
  46. "20819045374670962167435360035096875258406992893633759881276124905556507972311")
  47. b := &Point{X: bX, Y: bY}
  48. c := NewPoint().Add(a, b)
  49. // fmt.Printf("%v = 2 * %v", *c, *a)
  50. assert.Equal(t,
  51. "7916061937171219682591368294088513039687205273691143098332585753343424131937",
  52. c.X.String())
  53. assert.Equal(t,
  54. "14035240266687799601661095864649209771790948434046947201833777492504781204499",
  55. c.Y.String())
  56. }
  57. func TestAdd4(t *testing.T) {
  58. aX := utils.NewIntFromString(
  59. "0")
  60. aY := utils.NewIntFromString(
  61. "1")
  62. a := &Point{X: aX, Y: aY}
  63. bX := utils.NewIntFromString(
  64. "16540640123574156134436876038791482806971768689494387082833631921987005038935")
  65. bY := utils.NewIntFromString(
  66. "20819045374670962167435360035096875258406992893633759881276124905556507972311")
  67. b := &Point{X: bX, Y: bY}
  68. c := NewPoint().Add(a, b)
  69. // fmt.Printf("%v = 2 * %v", *c, *a)
  70. assert.Equal(t,
  71. "16540640123574156134436876038791482806971768689494387082833631921987005038935",
  72. c.X.String())
  73. assert.Equal(t,
  74. "20819045374670962167435360035096875258406992893633759881276124905556507972311",
  75. c.Y.String())
  76. }
  77. func TestInCurve1(t *testing.T) {
  78. p := &Point{X: big.NewInt(0), Y: big.NewInt(1)}
  79. assert.Equal(t, true, p.InCurve())
  80. }
  81. func TestInCurve2(t *testing.T) {
  82. p := &Point{X: big.NewInt(1), Y: big.NewInt(0)}
  83. assert.Equal(t, false, p.InCurve())
  84. }
  85. func TestMul0(t *testing.T) {
  86. x := utils.NewIntFromString(
  87. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  88. y := utils.NewIntFromString(
  89. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  90. p := &Point{X: x, Y: y}
  91. s := utils.NewIntFromString("3")
  92. r2 := NewPoint().Add(p, p)
  93. r2 = NewPoint().Add(r2, p)
  94. r := NewPoint().Mul(s, p)
  95. assert.Equal(t, r2.X.String(), r.X.String())
  96. assert.Equal(t, r2.Y.String(), r.Y.String())
  97. assert.Equal(t,
  98. "19372461775513343691590086534037741906533799473648040012278229434133483800898",
  99. r.X.String())
  100. assert.Equal(t,
  101. "9458658722007214007257525444427903161243386465067105737478306991484593958249",
  102. r.Y.String())
  103. }
  104. func TestMul1(t *testing.T) {
  105. x := utils.NewIntFromString(
  106. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  107. y := utils.NewIntFromString(
  108. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  109. p := &Point{X: x, Y: y}
  110. s := utils.NewIntFromString(
  111. "14035240266687799601661095864649209771790948434046947201833777492504781204499")
  112. r := NewPoint().Mul(s, p)
  113. assert.Equal(t,
  114. "17070357974431721403481313912716834497662307308519659060910483826664480189605",
  115. r.X.String())
  116. assert.Equal(t,
  117. "4014745322800118607127020275658861516666525056516280575712425373174125159339",
  118. r.Y.String())
  119. }
  120. func TestMul2(t *testing.T) {
  121. x := utils.NewIntFromString(
  122. "6890855772600357754907169075114257697580319025794532037257385534741338397365")
  123. y := utils.NewIntFromString(
  124. "4338620300185947561074059802482547481416142213883829469920100239455078257889")
  125. p := &Point{X: x, Y: y}
  126. s := utils.NewIntFromString(
  127. "20819045374670962167435360035096875258406992893633759881276124905556507972311")
  128. r := NewPoint().Mul(s, p)
  129. assert.Equal(t,
  130. "13563888653650925984868671744672725781658357821216877865297235725727006259983",
  131. r.X.String())
  132. assert.Equal(t,
  133. "8442587202676550862664528699803615547505326611544120184665036919364004251662",
  134. r.Y.String())
  135. }
  136. func TestInCurve3(t *testing.T) {
  137. x := utils.NewIntFromString(
  138. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  139. y := utils.NewIntFromString(
  140. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  141. p := &Point{X: x, Y: y}
  142. assert.Equal(t, true, p.InCurve())
  143. }
  144. func TestInCurve4(t *testing.T) {
  145. x := utils.NewIntFromString(
  146. "6890855772600357754907169075114257697580319025794532037257385534741338397365")
  147. y := utils.NewIntFromString(
  148. "4338620300185947561074059802482547481416142213883829469920100239455078257889")
  149. p := &Point{X: x, Y: y}
  150. assert.Equal(t, true, p.InCurve())
  151. }
  152. func TestInSubGroup1(t *testing.T) {
  153. x := utils.NewIntFromString(
  154. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  155. y := utils.NewIntFromString(
  156. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  157. p := &Point{X: x, Y: y}
  158. assert.Equal(t, true, p.InSubGroup())
  159. }
  160. func TestInSubGroup2(t *testing.T) {
  161. x := utils.NewIntFromString(
  162. "6890855772600357754907169075114257697580319025794532037257385534741338397365")
  163. y := utils.NewIntFromString(
  164. "4338620300185947561074059802482547481416142213883829469920100239455078257889")
  165. p := &Point{X: x, Y: y}
  166. assert.Equal(t, true, p.InSubGroup())
  167. }
  168. func TestCompressDecompress1(t *testing.T) {
  169. x := utils.NewIntFromString(
  170. "17777552123799933955779906779655732241715742912184938656739573121738514868268")
  171. y := utils.NewIntFromString(
  172. "2626589144620713026669568689430873010625803728049924121243784502389097019475")
  173. p := &Point{X: x, Y: y}
  174. buf := p.Compress()
  175. assert.Equal(t, "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85", hex.EncodeToString(buf[:]))
  176. p2, err := NewPoint().Decompress(buf)
  177. assert.Equal(t, nil, err)
  178. assert.Equal(t, p.X.String(), p2.X.String())
  179. assert.Equal(t, p.Y.String(), p2.Y.String())
  180. }
  181. func TestCompressDecompress2(t *testing.T) {
  182. x := utils.NewIntFromString(
  183. "6890855772600357754907169075114257697580319025794532037257385534741338397365")
  184. y := utils.NewIntFromString(
  185. "4338620300185947561074059802482547481416142213883829469920100239455078257889")
  186. p := &Point{X: x, Y: y}
  187. buf := p.Compress()
  188. assert.Equal(t, "e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709", hex.EncodeToString(buf[:]))
  189. p2, err := NewPoint().Decompress(buf)
  190. assert.Equal(t, nil, err)
  191. assert.Equal(t, p.X.String(), p2.X.String())
  192. assert.Equal(t, p.Y.String(), p2.Y.String())
  193. }
  194. func TestCompressDecompressRnd(t *testing.T) {
  195. for i := 0; i < 64; i++ {
  196. p1 := NewPoint().Mul(big.NewInt(int64(i)), B8)
  197. buf := p1.Compress()
  198. p2, err := NewPoint().Decompress(buf)
  199. assert.Equal(t, nil, err)
  200. assert.Equal(t, p1, p2)
  201. }
  202. }