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.

255 lines
6.6 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, err := k.SignMimc7(msg)
  41. assert.NoError(t, err)
  42. assert.Equal(t,
  43. "11384336176656855268977457483345535180380036354188103142384839473266348197733",
  44. sig.R8.X.String())
  45. assert.Equal(t,
  46. "15383486972088797283337779941324724402501462225528836549661220478783371668959",
  47. sig.R8.Y.String())
  48. assert.Equal(t,
  49. "2523202440825208709475937830811065542425109372212752003460238913256192595070",
  50. sig.S.String())
  51. err = pk.VerifyMimc7(msg, sig)
  52. assert.NoError(t, err)
  53. sigBuf := sig.Compress()
  54. sig2, err := new(Signature).Decompress(sigBuf)
  55. assert.NoError(t, err)
  56. assert.Equal(t, ""+
  57. "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
  58. "7ed40dab29bf993c928e789d007387998901a24913d44fddb64b1f21fc149405",
  59. hex.EncodeToString(sigBuf[:]))
  60. err = pk.VerifyMimc7(msg, sig2)
  61. assert.NoError(t, err)
  62. }
  63. func TestSignVerifyPoseidon(t *testing.T) {
  64. var k PrivateKey
  65. _, err := hex.Decode(k[:],
  66. []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  67. require.Nil(t, err)
  68. msgBuf, err := hex.DecodeString("00010203040506070809")
  69. if err != nil {
  70. panic(err)
  71. }
  72. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  73. pk := k.Public()
  74. assert.Equal(t,
  75. "13277427435165878497778222415993513565335242147425444199013288855685581939618",
  76. pk.X.String())
  77. assert.Equal(t,
  78. "13622229784656158136036771217484571176836296686641868549125388198837476602820",
  79. pk.Y.String())
  80. sig, err := k.SignPoseidon(msg)
  81. assert.NoError(t, err)
  82. assert.Equal(t,
  83. "11384336176656855268977457483345535180380036354188103142384839473266348197733",
  84. sig.R8.X.String())
  85. assert.Equal(t,
  86. "15383486972088797283337779941324724402501462225528836549661220478783371668959",
  87. sig.R8.Y.String())
  88. assert.Equal(t,
  89. "1672775540645840396591609181675628451599263765380031905495115170613215233181",
  90. sig.S.String())
  91. err = pk.VerifyPoseidon(msg, sig)
  92. assert.NoError(t, err)
  93. sigBuf := sig.Compress()
  94. sig2, err := new(Signature).Decompress(sigBuf)
  95. assert.NoError(t, err)
  96. assert.Equal(t, ""+
  97. "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
  98. "9d043ece562a8f82bfc0adb640c0107a7d3a27c1c7c1a6179a0da73de5c1b203",
  99. hex.EncodeToString(sigBuf[:]))
  100. err = pk.VerifyPoseidon(msg, sig2)
  101. assert.NoError(t, err)
  102. }
  103. func TestCompressDecompress(t *testing.T) {
  104. var k PrivateKey
  105. _, err := hex.Decode(k[:],
  106. []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  107. require.Nil(t, err)
  108. pk := k.Public()
  109. for i := 0; i < 64; i++ {
  110. msgBuf, err := hex.DecodeString(fmt.Sprintf("000102030405060708%02d", i))
  111. if err != nil {
  112. panic(err)
  113. }
  114. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  115. sig, err := k.SignMimc7(msg)
  116. assert.NoError(t, err)
  117. sigBuf := sig.Compress()
  118. sig2, err := new(Signature).Decompress(sigBuf)
  119. assert.NoError(t, err)
  120. err = pk.VerifyMimc7(msg, sig2)
  121. assert.NoError(t, err)
  122. }
  123. }
  124. func TestSignatureCompScannerValuer(t *testing.T) {
  125. privK := NewRandPrivKey()
  126. var err error
  127. sig, err := privK.SignPoseidon(big.NewInt(674238462))
  128. assert.NoError(t, err)
  129. var value driver.Valuer //nolint:gosimple // this is done to ensure interface compatibility
  130. value = sig.Compress()
  131. sig, err = privK.SignPoseidon(big.NewInt(1))
  132. assert.NoError(t, err)
  133. scan := sig.Compress()
  134. fromDB, err := value.Value()
  135. assert.NoError(t, err)
  136. assert.Nil(t, scan.Scan(fromDB))
  137. assert.Equal(t, value, scan)
  138. }
  139. func TestSignatureScannerValuer(t *testing.T) {
  140. privK := NewRandPrivKey()
  141. var value driver.Valuer
  142. var scan sql.Scanner
  143. var err error
  144. value, err = privK.SignPoseidon(big.NewInt(674238462))
  145. assert.NoError(t, err)
  146. scan, err = privK.SignPoseidon(big.NewInt(1))
  147. assert.NoError(t, err)
  148. fromDB, err := value.Value()
  149. assert.NoError(t, err)
  150. assert.Nil(t, scan.Scan(fromDB))
  151. assert.Equal(t, value, scan)
  152. }
  153. func TestPublicKeyScannerValuer(t *testing.T) {
  154. privKValue := NewRandPrivKey()
  155. pubKValue := privKValue.Public()
  156. privKScan := NewRandPrivKey()
  157. pubKScan := privKScan.Public()
  158. var value driver.Valuer
  159. var scan sql.Scanner
  160. value = pubKValue
  161. scan = pubKScan
  162. fromDB, err := value.Value()
  163. assert.Nil(t, err)
  164. assert.Nil(t, scan.Scan(fromDB))
  165. assert.Equal(t, value, scan)
  166. }
  167. func TestPublicKeyCompScannerValuer(t *testing.T) {
  168. privKValue := NewRandPrivKey()
  169. pubKCompValue := privKValue.Public().Compress()
  170. privKScan := NewRandPrivKey()
  171. pubKCompScan := privKScan.Public().Compress()
  172. var value driver.Valuer
  173. var scan sql.Scanner
  174. value = &pubKCompValue
  175. scan = &pubKCompScan
  176. fromDB, err := value.Value()
  177. assert.Nil(t, err)
  178. assert.Nil(t, scan.Scan(fromDB))
  179. assert.Equal(t, value, scan)
  180. }
  181. func BenchmarkBabyjubEddsa(b *testing.B) {
  182. var k PrivateKey
  183. _, err := hex.Decode(k[:],
  184. []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  185. require.Nil(b, err)
  186. pk := k.Public()
  187. const n = 256
  188. msgBuf, err := hex.DecodeString("00010203040506070809")
  189. if err != nil {
  190. panic(err)
  191. }
  192. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  193. var msgs [n]*big.Int
  194. for i := 0; i < n; i++ {
  195. msgs[i] = new(big.Int).Add(msg, big.NewInt(int64(i)))
  196. }
  197. var sigs [n]*Signature
  198. b.Run("SignMimc7", func(b *testing.B) {
  199. for i := 0; i < b.N; i++ {
  200. k.SignMimc7(msgs[i%n])
  201. }
  202. })
  203. for i := 0; i < n; i++ {
  204. sigs[i%n], _ = k.SignMimc7(msgs[i%n])
  205. }
  206. b.Run("VerifyMimc7", func(b *testing.B) {
  207. for i := 0; i < b.N; i++ {
  208. _ = pk.VerifyMimc7(msgs[i%n], sigs[i%n])
  209. }
  210. })
  211. b.Run("SignPoseidon", func(b *testing.B) {
  212. for i := 0; i < b.N; i++ {
  213. k.SignPoseidon(msgs[i%n])
  214. }
  215. })
  216. for i := 0; i < n; i++ {
  217. sigs[i%n], _ = k.SignPoseidon(msgs[i%n])
  218. }
  219. b.Run("VerifyPoseidon", func(b *testing.B) {
  220. for i := 0; i < b.N; i++ {
  221. _ = pk.VerifyPoseidon(msgs[i%n], sigs[i%n])
  222. }
  223. })
  224. }