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.

89 lines
2.3 KiB

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