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.

133 lines
3.7 KiB

  1. package babyjub
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "fmt"
  6. "math/big"
  7. "testing"
  8. "github.com/iden3/go-iden3-crypto/constants"
  9. "github.com/iden3/go-iden3-crypto/utils"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func genInputs() (*PrivateKey, *big.Int) {
  13. k := NewRandPrivKey()
  14. fmt.Println("k", hex.EncodeToString(k[:]))
  15. msgBuf := [32]byte{}
  16. rand.Read(msgBuf[:])
  17. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf[:])
  18. msg.Mod(msg, constants.Q)
  19. fmt.Println("msg", msg)
  20. return &k, msg
  21. }
  22. func TestSignVerifyMimc7(t *testing.T) {
  23. var k PrivateKey
  24. hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  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. hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  62. msgBuf, err := hex.DecodeString("00010203040506070809")
  63. if err != nil {
  64. panic(err)
  65. }
  66. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  67. pk := k.Public()
  68. assert.Equal(t,
  69. "13277427435165878497778222415993513565335242147425444199013288855685581939618",
  70. pk.X.String())
  71. assert.Equal(t,
  72. "13622229784656158136036771217484571176836296686641868549125388198837476602820",
  73. pk.Y.String())
  74. sig := k.SignPoseidon(msg)
  75. assert.Equal(t,
  76. "11384336176656855268977457483345535180380036354188103142384839473266348197733",
  77. sig.R8.X.String())
  78. assert.Equal(t,
  79. "15383486972088797283337779941324724402501462225528836549661220478783371668959",
  80. sig.R8.Y.String())
  81. assert.Equal(t,
  82. "248298168863866362217836334079793350221620631973732197668910946177382043688",
  83. sig.S.String())
  84. ok := pk.VerifyPoseidon(msg, sig)
  85. assert.Equal(t, true, ok)
  86. sigBuf := sig.Compress()
  87. sig2, err := new(Signature).Decompress(sigBuf)
  88. assert.Equal(t, nil, err)
  89. assert.Equal(t, ""+
  90. "dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2"+
  91. "28506bce274aa1b3f7e7c2fd7e4fe09bff8f9aa37a42def7994e98f322888c00",
  92. hex.EncodeToString(sigBuf[:]))
  93. ok = pk.VerifyPoseidon(msg, sig2)
  94. assert.Equal(t, true, ok)
  95. }
  96. func TestCompressDecompress(t *testing.T) {
  97. var k PrivateKey
  98. hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  99. pk := k.Public()
  100. for i := 0; i < 64; i++ {
  101. msgBuf, err := hex.DecodeString(fmt.Sprintf("000102030405060708%02d", i))
  102. if err != nil {
  103. panic(err)
  104. }
  105. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  106. sig := k.SignMimc7(msg)
  107. sigBuf := sig.Compress()
  108. sig2, err := new(Signature).Decompress(sigBuf)
  109. assert.Equal(t, nil, err)
  110. ok := pk.VerifyMimc7(msg, sig2)
  111. assert.Equal(t, true, ok)
  112. }
  113. }