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.

229 lines
6.0 KiB

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