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. "2610057752638682202795145288373380503107623443963127956230801721756904484787",
  33. pk.X.String())
  34. assert.Equal(t,
  35. "16617171478497210597712478520507818259149717466230047843969353176573634386897",
  36. pk.Y.String())
  37. sig := k.SignMimc7(msg)
  38. assert.Equal(t,
  39. "4974729414807584049518234760796200867685098748448054182902488636762478901554",
  40. sig.R8.X.String())
  41. assert.Equal(t,
  42. "18714049394522540751536514815950425694461287643205706667341348804546050128733",
  43. sig.R8.Y.String())
  44. assert.Equal(t,
  45. "2171284143457722024136077617757713039502332290425057126942676527240038689549",
  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. "5dfb6f843c023fe3e52548ccf22e55c81b426f7af81b4f51f7152f2fcfc65f29"+
  54. "0dab19c5a0a75973cd75a54780de0c3a41ede6f57396fe99b5307fff3ce7cc04",
  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. }