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.

90 lines
2.4 KiB

  1. package babyjub
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "fmt"
  6. "github.com/iden3/go-iden3-crypto/constants"
  7. "github.com/iden3/go-iden3-crypto/utils"
  8. "github.com/stretchr/testify/assert"
  9. "math/big"
  10. "testing"
  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 TestSignVerify1(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 TestCompressDecompress(t *testing.T) {
  60. var k PrivateKey
  61. hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  62. pk := k.Public()
  63. for i := 0; i < 64; i++ {
  64. msgBuf, err := hex.DecodeString(fmt.Sprintf("000102030405060708%02d", i))
  65. if err != nil {
  66. panic(err)
  67. }
  68. msg := utils.SetBigIntFromLEBytes(new(big.Int), msgBuf)
  69. sig := k.SignMimc7(msg)
  70. sigBuf := sig.Compress()
  71. sig2, err := new(Signature).Decompress(sigBuf)
  72. assert.Equal(t, nil, err)
  73. ok := pk.VerifyMimc7(msg, sig2)
  74. assert.Equal(t, true, ok)
  75. }
  76. }