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.

185 lines
4.8 KiB

  1. package babyjub
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "math/big"
  6. "testing"
  7. "github.com/iden3/go-iden3-crypto/constants"
  8. "github.com/iden3/go-iden3-crypto/utils"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestPublicKey(t *testing.T) {
  13. var k PrivateKey
  14. for i := 0; i < 32; i++ {
  15. k[i] = byte(i)
  16. }
  17. pk := k.Public()
  18. assert.True(t, pk.X.Cmp(constants.Q) == -1)
  19. assert.True(t, pk.Y.Cmp(constants.Q) == -1)
  20. }
  21. func TestSignVerifyMimc7(t *testing.T) {
  22. var k PrivateKey
  23. _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  24. require.Nil(t, err)
  25. msgBuf, err := hex.DecodeString("00010203040506070809")
  26. if err != nil {
  27. panic(err)
  28. }
  29. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  30. pk := k.Public()
  31. assert.Equal(t,
  32. "13277427435165878497778222415993513565335242147425444199013288855685581939618",
  33. pk.X.String())
  34. assert.Equal(t,
  35. "13622229784656158136036771217484571176836296686641868549125388198837476602820",
  36. pk.Y.String())
  37. sig := k.SignMimc7(msg)
  38. assert.Equal(t,
  39. "11384336176656855268977457483345535180380036354188103142384839473266348197733",
  40. sig.R8.X.String())
  41. assert.Equal(t,
  42. "15383486972088797283337779941324724402501462225528836549661220478783371668959",
  43. sig.R8.Y.String())
  44. assert.Equal(t,
  45. "2523202440825208709475937830811065542425109372212752003460238913256192595070",
  46. sig.S.String())
  47. ok := pk.VerifyMimc7(msg, sig)
  48. assert.Equal(t, true, ok)
  49. sigBuf := sig.Compress()
  50. sig2, err := new(Signature).Decompress(sigBuf)
  51. assert.Equal(t, nil, err)
  52. assert.Equal(t, ""+
  53. "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
  54. "7ed40dab29bf993c928e789d007387998901a24913d44fddb64b1f21fc149405",
  55. hex.EncodeToString(sigBuf[:]))
  56. ok = pk.VerifyMimc7(msg, sig2)
  57. assert.Equal(t, true, ok)
  58. }
  59. func TestSignVerifyPoseidon(t *testing.T) {
  60. var k PrivateKey
  61. _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  62. require.Nil(t, err)
  63. msgBuf, err := hex.DecodeString("00010203040506070809")
  64. if err != nil {
  65. panic(err)
  66. }
  67. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  68. pk := k.Public()
  69. assert.Equal(t,
  70. "13277427435165878497778222415993513565335242147425444199013288855685581939618",
  71. pk.X.String())
  72. assert.Equal(t,
  73. "13622229784656158136036771217484571176836296686641868549125388198837476602820",
  74. pk.Y.String())
  75. sig := k.SignPoseidon(msg)
  76. assert.Equal(t,
  77. "11384336176656855268977457483345535180380036354188103142384839473266348197733",
  78. sig.R8.X.String())
  79. assert.Equal(t,
  80. "15383486972088797283337779941324724402501462225528836549661220478783371668959",
  81. sig.R8.Y.String())
  82. assert.Equal(t,
  83. "248298168863866362217836334079793350221620631973732197668910946177382043688",
  84. sig.S.String())
  85. ok := pk.VerifyPoseidon(msg, sig)
  86. assert.Equal(t, true, ok)
  87. sigBuf := sig.Compress()
  88. sig2, err := new(Signature).Decompress(sigBuf)
  89. assert.Equal(t, nil, err)
  90. assert.Equal(t, ""+
  91. "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
  92. "28506bce274aa1b3f7e7c2fd7e4fe09bff8f9aa37a42def7994e98f322888c00",
  93. hex.EncodeToString(sigBuf[:]))
  94. ok = pk.VerifyPoseidon(msg, sig2)
  95. assert.Equal(t, true, ok)
  96. }
  97. func TestCompressDecompress(t *testing.T) {
  98. var k PrivateKey
  99. _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  100. require.Nil(t, err)
  101. pk := k.Public()
  102. for i := 0; i < 64; i++ {
  103. msgBuf, err := hex.DecodeString(fmt.Sprintf("000102030405060708%02d", i))
  104. if err != nil {
  105. panic(err)
  106. }
  107. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  108. sig := k.SignMimc7(msg)
  109. sigBuf := sig.Compress()
  110. sig2, err := new(Signature).Decompress(sigBuf)
  111. assert.Equal(t, nil, err)
  112. ok := pk.VerifyMimc7(msg, sig2)
  113. assert.Equal(t, true, ok)
  114. }
  115. }
  116. func BenchmarkBabyjubEddsa(b *testing.B) {
  117. var k PrivateKey
  118. _, err := hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  119. require.Nil(b, err)
  120. pk := k.Public()
  121. const n = 256
  122. msgBuf, err := hex.DecodeString("00010203040506070809")
  123. if err != nil {
  124. panic(err)
  125. }
  126. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  127. var msgs [n]*big.Int
  128. for i := 0; i < n; i++ {
  129. msgs[i] = new(big.Int).Add(msg, big.NewInt(int64(i)))
  130. }
  131. var sigs [n]*Signature
  132. b.Run("SignMimc7", func(b *testing.B) {
  133. for i := 0; i < b.N; i++ {
  134. k.SignMimc7(msgs[i%n])
  135. }
  136. })
  137. for i := 0; i < n; i++ {
  138. sigs[i%n] = k.SignMimc7(msgs[i%n])
  139. }
  140. b.Run("VerifyMimc7", func(b *testing.B) {
  141. for i := 0; i < b.N; i++ {
  142. pk.VerifyMimc7(msgs[i%n], sigs[i%n])
  143. }
  144. })
  145. b.Run("SignPoseidon", func(b *testing.B) {
  146. for i := 0; i < b.N; i++ {
  147. k.SignPoseidon(msgs[i%n])
  148. }
  149. })
  150. for i := 0; i < n; i++ {
  151. sigs[i%n] = k.SignPoseidon(msgs[i%n])
  152. }
  153. b.Run("VerifyPoseidon", func(b *testing.B) {
  154. for i := 0; i < b.N; i++ {
  155. pk.VerifyPoseidon(msgs[i%n], sigs[i%n])
  156. }
  157. })
  158. }