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.

88 lines
2.3 KiB

  1. package babyjub
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "fmt"
  6. "github.com/stretchr/testify/assert"
  7. "math/big"
  8. "testing"
  9. )
  10. func genInputs() (*PrivateKey, *big.Int) {
  11. k := NewRandPrivKey()
  12. fmt.Println("k", hex.EncodeToString(k[:]))
  13. msgBuf := [32]byte{}
  14. rand.Read(msgBuf[:])
  15. msg := SetBigIntFromLEBytes(new(big.Int), msgBuf[:])
  16. msg.Mod(msg, Q)
  17. fmt.Println("msg", msg)
  18. return &k, msg
  19. }
  20. func TestSignVerify1(t *testing.T) {
  21. var k PrivateKey
  22. hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  23. msgBuf, err := hex.DecodeString("00010203040506070809")
  24. if err != nil {
  25. panic(err)
  26. }
  27. msg := SetBigIntFromLEBytes(new(big.Int), msgBuf)
  28. pk := k.Public()
  29. assert.Equal(t,
  30. "2610057752638682202795145288373380503107623443963127956230801721756904484787",
  31. pk.X.String())
  32. assert.Equal(t,
  33. "16617171478497210597712478520507818259149717466230047843969353176573634386897",
  34. pk.Y.String())
  35. sig := k.SignMimc7(msg)
  36. assert.Equal(t,
  37. "4974729414807584049518234760796200867685098748448054182902488636762478901554",
  38. sig.R8.X.String())
  39. assert.Equal(t,
  40. "18714049394522540751536514815950425694461287643205706667341348804546050128733",
  41. sig.R8.Y.String())
  42. assert.Equal(t,
  43. "2171284143457722024136077617757713039502332290425057126942676527240038689549",
  44. sig.S.String())
  45. ok := pk.VerifyMimc7(msg, sig)
  46. assert.Equal(t, true, ok)
  47. sigBuf := sig.Compress()
  48. sig2, err := new(Signature).Decompress(sigBuf)
  49. assert.Equal(t, nil, err)
  50. assert.Equal(t, ""+
  51. "5dfb6f843c023fe3e52548ccf22e55c81b426f7af81b4f51f7152f2fcfc65f29"+
  52. "0dab19c5a0a75973cd75a54780de0c3a41ede6f57396fe99b5307fff3ce7cc04",
  53. hex.EncodeToString(sigBuf[:]))
  54. ok = pk.VerifyMimc7(msg, sig2)
  55. assert.Equal(t, true, ok)
  56. }
  57. func TestCompressDecompress(t *testing.T) {
  58. var k PrivateKey
  59. hex.Decode(k[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  60. pk := k.Public()
  61. for i := 0; i < 64; i++ {
  62. msgBuf, err := hex.DecodeString(fmt.Sprintf("000102030405060708%02d", i))
  63. if err != nil {
  64. panic(err)
  65. }
  66. msg := SetBigIntFromLEBytes(new(big.Int), msgBuf)
  67. sig := k.SignMimc7(msg)
  68. sigBuf := sig.Compress()
  69. sig2, err := new(Signature).Decompress(sigBuf)
  70. assert.Equal(t, nil, err)
  71. ok := pk.VerifyMimc7(msg, sig2)
  72. assert.Equal(t, true, ok)
  73. }
  74. }