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.

244 lines
6.4 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 compatibility
  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 TestPublicKeyScannerValuer(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 TestPublicKeyCompScannerValuer(t *testing.T) {
  157. privKValue := NewRandPrivKey()
  158. pubKCompValue := privKValue.Public().Compress()
  159. privKScan := NewRandPrivKey()
  160. pubKCompScan := privKScan.Public().Compress()
  161. var value driver.Valuer
  162. var scan sql.Scanner
  163. value = &pubKCompValue
  164. scan = &pubKCompScan
  165. fromDB, err := value.Value()
  166. assert.Nil(t, err)
  167. assert.Nil(t, scan.Scan(fromDB))
  168. assert.Equal(t, value, scan)
  169. }
  170. func BenchmarkBabyjubEddsa(b *testing.B) {
  171. var k PrivateKey
  172. _, err := hex.Decode(k[:],
  173. []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  174. require.Nil(b, err)
  175. pk := k.Public()
  176. const n = 256
  177. msgBuf, err := hex.DecodeString("00010203040506070809")
  178. if err != nil {
  179. panic(err)
  180. }
  181. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  182. var msgs [n]*big.Int
  183. for i := 0; i < n; i++ {
  184. msgs[i] = new(big.Int).Add(msg, big.NewInt(int64(i)))
  185. }
  186. var sigs [n]*Signature
  187. b.Run("SignMimc7", func(b *testing.B) {
  188. for i := 0; i < b.N; i++ {
  189. k.SignMimc7(msgs[i%n])
  190. }
  191. })
  192. for i := 0; i < n; i++ {
  193. sigs[i%n] = k.SignMimc7(msgs[i%n])
  194. }
  195. b.Run("VerifyMimc7", func(b *testing.B) {
  196. for i := 0; i < b.N; i++ {
  197. pk.VerifyMimc7(msgs[i%n], sigs[i%n])
  198. }
  199. })
  200. b.Run("SignPoseidon", func(b *testing.B) {
  201. for i := 0; i < b.N; i++ {
  202. k.SignPoseidon(msgs[i%n])
  203. }
  204. })
  205. for i := 0; i < n; i++ {
  206. sigs[i%n] = k.SignPoseidon(msgs[i%n])
  207. }
  208. b.Run("VerifyPoseidon", func(b *testing.B) {
  209. for i := 0; i < b.N; i++ {
  210. pk.VerifyPoseidon(msgs[i%n], sigs[i%n])
  211. }
  212. })
  213. }