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.

215 lines
5.6 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. "248298168863866362217836334079793350221620631973732197668910946177382043688",
  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. "28506bce274aa1b3f7e7c2fd7e4fe09bff8f9aa37a42def7994e98f322888c00",
  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 TestSignatureScannerValuer(t *testing.T) {
  119. privK := NewRandPrivKey()
  120. var value driver.Valuer
  121. var scan sql.Scanner
  122. value = privK.SignPoseidon(big.NewInt(674238462))
  123. scan = privK.SignPoseidon(big.NewInt(1))
  124. fromDB, err := value.Value()
  125. assert.NoError(t, err)
  126. assert.NoError(t, scan.Scan(fromDB))
  127. assert.Equal(t, value, scan)
  128. }
  129. func TestPubKeyScannerValuer(t *testing.T) {
  130. privKValue := NewRandPrivKey()
  131. pubKValue := privKValue.Public()
  132. privKScan := NewRandPrivKey()
  133. pubKScan := privKScan.Public()
  134. var value driver.Valuer
  135. var scan sql.Scanner
  136. value = pubKValue
  137. scan = pubKScan
  138. fromDB, err := value.Value()
  139. assert.NoError(t, err)
  140. assert.NoError(t, scan.Scan(fromDB))
  141. assert.Equal(t, value, scan)
  142. }
  143. func BenchmarkBabyjubEddsa(b *testing.B) {
  144. var k PrivateKey
  145. _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  146. require.Nil(b, err)
  147. pk := k.Public()
  148. const n = 256
  149. msgBuf, err := hex.DecodeString("00010203040506070809")
  150. if err != nil {
  151. panic(err)
  152. }
  153. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  154. var msgs [n]*big.Int
  155. for i := 0; i < n; i++ {
  156. msgs[i] = new(big.Int).Add(msg, big.NewInt(int64(i)))
  157. }
  158. var sigs [n]*Signature
  159. b.Run("SignMimc7", func(b *testing.B) {
  160. for i := 0; i < b.N; i++ {
  161. k.SignMimc7(msgs[i%n])
  162. }
  163. })
  164. for i := 0; i < n; i++ {
  165. sigs[i%n] = k.SignMimc7(msgs[i%n])
  166. }
  167. b.Run("VerifyMimc7", func(b *testing.B) {
  168. for i := 0; i < b.N; i++ {
  169. pk.VerifyMimc7(msgs[i%n], sigs[i%n])
  170. }
  171. })
  172. b.Run("SignPoseidon", func(b *testing.B) {
  173. for i := 0; i < b.N; i++ {
  174. k.SignPoseidon(msgs[i%n])
  175. }
  176. })
  177. for i := 0; i < n; i++ {
  178. sigs[i%n] = k.SignPoseidon(msgs[i%n])
  179. }
  180. b.Run("VerifyPoseidon", func(b *testing.B) {
  181. for i := 0; i < b.N; i++ {
  182. pk.VerifyPoseidon(msgs[i%n], sigs[i%n])
  183. }
  184. })
  185. }