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.

226 lines
5.9 KiB

  1. package babyjub
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "math/big"
  6. "testing"
  7. "database/sql"
  8. "database/sql/driver"
  9. "github.com/iden3/go-iden3-crypto/constants"
  10. "github.com/iden3/go-iden3-crypto/utils"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func TestPublicKey(t *testing.T) {
  15. var k PrivateKey
  16. for i := 0; i < 32; i++ {
  17. k[i] = byte(i)
  18. }
  19. pk := k.Public()
  20. assert.True(t, pk.X.Cmp(constants.Q) == -1)
  21. assert.True(t, pk.Y.Cmp(constants.Q) == -1)
  22. }
  23. func TestSignVerifyMimc7(t *testing.T) {
  24. var k PrivateKey
  25. _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  26. require.Nil(t, err)
  27. msgBuf, err := hex.DecodeString("00010203040506070809")
  28. if err != nil {
  29. panic(err)
  30. }
  31. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  32. pk := k.Public()
  33. assert.Equal(t,
  34. "13277427435165878497778222415993513565335242147425444199013288855685581939618",
  35. pk.X.String())
  36. assert.Equal(t,
  37. "13622229784656158136036771217484571176836296686641868549125388198837476602820",
  38. pk.Y.String())
  39. sig := k.SignMimc7(msg)
  40. assert.Equal(t,
  41. "11384336176656855268977457483345535180380036354188103142384839473266348197733",
  42. sig.R8.X.String())
  43. assert.Equal(t,
  44. "15383486972088797283337779941324724402501462225528836549661220478783371668959",
  45. sig.R8.Y.String())
  46. assert.Equal(t,
  47. "2523202440825208709475937830811065542425109372212752003460238913256192595070",
  48. sig.S.String())
  49. ok := pk.VerifyMimc7(msg, sig)
  50. assert.Equal(t, true, ok)
  51. sigBuf := sig.Compress()
  52. sig2, err := new(Signature).Decompress(sigBuf)
  53. assert.Equal(t, nil, err)
  54. assert.Equal(t, ""+
  55. "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
  56. "7ed40dab29bf993c928e789d007387998901a24913d44fddb64b1f21fc149405",
  57. hex.EncodeToString(sigBuf[:]))
  58. ok = pk.VerifyMimc7(msg, sig2)
  59. assert.Equal(t, true, ok)
  60. }
  61. func TestSignVerifyPoseidon(t *testing.T) {
  62. var k PrivateKey
  63. _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  64. require.Nil(t, err)
  65. msgBuf, err := hex.DecodeString("00010203040506070809")
  66. if err != nil {
  67. panic(err)
  68. }
  69. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  70. pk := k.Public()
  71. assert.Equal(t,
  72. "13277427435165878497778222415993513565335242147425444199013288855685581939618",
  73. pk.X.String())
  74. assert.Equal(t,
  75. "13622229784656158136036771217484571176836296686641868549125388198837476602820",
  76. pk.Y.String())
  77. sig := k.SignPoseidon(msg)
  78. assert.Equal(t,
  79. "11384336176656855268977457483345535180380036354188103142384839473266348197733",
  80. sig.R8.X.String())
  81. assert.Equal(t,
  82. "15383486972088797283337779941324724402501462225528836549661220478783371668959",
  83. sig.R8.Y.String())
  84. assert.Equal(t,
  85. "1398758333392199195742243841591064350253744445503462896781493968760929513778",
  86. sig.S.String())
  87. ok := pk.VerifyPoseidon(msg, sig)
  88. assert.Equal(t, true, ok)
  89. sigBuf := sig.Compress()
  90. sig2, err := new(Signature).Decompress(sigBuf)
  91. assert.Equal(t, nil, err)
  92. assert.Equal(t, ""+
  93. "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
  94. "32f16b0f2f4c4e1169aa59685637e1429b6581a9531d058d65f4ab224eab1703",
  95. hex.EncodeToString(sigBuf[:]))
  96. ok = pk.VerifyPoseidon(msg, sig2)
  97. assert.Equal(t, true, ok)
  98. }
  99. func TestCompressDecompress(t *testing.T) {
  100. var k PrivateKey
  101. _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  102. require.Nil(t, err)
  103. pk := k.Public()
  104. for i := 0; i < 64; i++ {
  105. msgBuf, err := hex.DecodeString(fmt.Sprintf("000102030405060708%02d", i))
  106. if err != nil {
  107. panic(err)
  108. }
  109. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  110. sig := k.SignMimc7(msg)
  111. sigBuf := sig.Compress()
  112. sig2, err := new(Signature).Decompress(sigBuf)
  113. assert.Equal(t, nil, err)
  114. ok := pk.VerifyMimc7(msg, sig2)
  115. assert.Equal(t, true, ok)
  116. }
  117. }
  118. func TestSignatureCompScannerValuer(t *testing.T) {
  119. privK := NewRandPrivKey()
  120. var value driver.Valuer //nolint:gosimple this is done to ensure interface compability
  121. value = privK.SignPoseidon(big.NewInt(674238462)).Compress()
  122. scan := privK.SignPoseidon(big.NewInt(1)).Compress()
  123. fromDB, err := value.Value()
  124. assert.Nil(t, err)
  125. assert.Nil(t, scan.Scan(fromDB))
  126. assert.Equal(t, value, scan)
  127. }
  128. func TestSignatureScannerValuer(t *testing.T) {
  129. privK := NewRandPrivKey()
  130. var value driver.Valuer
  131. var scan sql.Scanner
  132. value = privK.SignPoseidon(big.NewInt(674238462))
  133. scan = privK.SignPoseidon(big.NewInt(1))
  134. fromDB, err := value.Value()
  135. assert.Nil(t, err)
  136. assert.Nil(t, scan.Scan(fromDB))
  137. assert.Equal(t, value, scan)
  138. }
  139. func TestPubKeyScannerValuer(t *testing.T) {
  140. privKValue := NewRandPrivKey()
  141. pubKValue := privKValue.Public()
  142. privKScan := NewRandPrivKey()
  143. pubKScan := privKScan.Public()
  144. var value driver.Valuer
  145. var scan sql.Scanner
  146. value = pubKValue
  147. scan = pubKScan
  148. fromDB, err := value.Value()
  149. assert.Nil(t, err)
  150. assert.Nil(t, scan.Scan(fromDB))
  151. assert.Equal(t, value, scan)
  152. }
  153. func BenchmarkBabyjubEddsa(b *testing.B) {
  154. var k PrivateKey
  155. _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  156. require.Nil(b, err)
  157. pk := k.Public()
  158. const n = 256
  159. msgBuf, err := hex.DecodeString("00010203040506070809")
  160. if err != nil {
  161. panic(err)
  162. }
  163. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  164. var msgs [n]*big.Int
  165. for i := 0; i < n; i++ {
  166. msgs[i] = new(big.Int).Add(msg, big.NewInt(int64(i)))
  167. }
  168. var sigs [n]*Signature
  169. b.Run("SignMimc7", func(b *testing.B) {
  170. for i := 0; i < b.N; i++ {
  171. k.SignMimc7(msgs[i%n])
  172. }
  173. })
  174. for i := 0; i < n; i++ {
  175. sigs[i%n] = k.SignMimc7(msgs[i%n])
  176. }
  177. b.Run("VerifyMimc7", func(b *testing.B) {
  178. for i := 0; i < b.N; i++ {
  179. pk.VerifyMimc7(msgs[i%n], sigs[i%n])
  180. }
  181. })
  182. b.Run("SignPoseidon", func(b *testing.B) {
  183. for i := 0; i < b.N; i++ {
  184. k.SignPoseidon(msgs[i%n])
  185. }
  186. })
  187. for i := 0; i < n; i++ {
  188. sigs[i%n] = k.SignPoseidon(msgs[i%n])
  189. }
  190. b.Run("VerifyPoseidon", func(b *testing.B) {
  191. for i := 0; i < b.N; i++ {
  192. pk.VerifyPoseidon(msgs[i%n], sigs[i%n])
  193. }
  194. })
  195. }