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.

232 lines
7.4 KiB

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