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.

122 lines
3.5 KiB

  1. package common
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. "testing"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/iden3/go-iden3-crypto/babyjub"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestNonceParser(t *testing.T) {
  11. n := Nonce(1)
  12. nBytes, err := n.Bytes()
  13. assert.Nil(t, err)
  14. assert.Equal(t, 5, len(nBytes))
  15. assert.Equal(t, "0100000000", hex.EncodeToString(nBytes[:]))
  16. n2, err := NonceFromBytes(nBytes)
  17. assert.Nil(t, err)
  18. assert.Equal(t, n, n2)
  19. // value before overflow
  20. n = Nonce(1099511627775)
  21. nBytes, err = n.Bytes()
  22. assert.Nil(t, err)
  23. assert.Equal(t, 5, len(nBytes))
  24. assert.Equal(t, "ffffffffff", hex.EncodeToString(nBytes[:]))
  25. n2, err = NonceFromBytes(nBytes)
  26. assert.Nil(t, err)
  27. assert.Equal(t, n, n2)
  28. // expect value overflow
  29. n = Nonce(1099511627776)
  30. nBytes, err = n.Bytes()
  31. assert.NotNil(t, err)
  32. assert.Equal(t, ErrNonceOverflow, err)
  33. _, err = NonceFromBytes(nBytes)
  34. assert.Nil(t, err)
  35. }
  36. func TestTxCompressedData(t *testing.T) {
  37. var sk babyjub.PrivateKey
  38. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  39. assert.Nil(t, err)
  40. tx := PoolL2Tx{
  41. FromIdx: 2,
  42. ToIdx: 3,
  43. Amount: big.NewInt(4),
  44. TokenID: 5,
  45. Nonce: 6,
  46. ToBJJ: sk.Public(),
  47. }
  48. txCompressedData, err := tx.TxCompressedData()
  49. assert.Nil(t, err)
  50. // test vector value generated from javascript implementation
  51. assert.Equal(t, "1766847064778421992193717128424891165872736891548909569553540449389241871", txCompressedData.String())
  52. assert.Equal(t, "10000000000060000000500040000000000030000000000020001c60be60f", hex.EncodeToString(txCompressedData.Bytes())[1:])
  53. tx = PoolL2Tx{
  54. FromIdx: 7,
  55. ToIdx: 8,
  56. Amount: big.NewInt(9),
  57. TokenID: 10,
  58. Nonce: 11,
  59. Fee: 12,
  60. ToBJJ: sk.Public(),
  61. }
  62. txCompressedData, err = tx.TxCompressedDataV2()
  63. assert.Nil(t, err)
  64. // test vector value generated from javascript implementation
  65. assert.Equal(t, "6571340879233176732837827812956721483162819083004853354503", txCompressedData.String())
  66. assert.Equal(t, "10c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes())[1:])
  67. }
  68. func TestHashToSign(t *testing.T) {
  69. var sk babyjub.PrivateKey
  70. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  71. assert.Nil(t, err)
  72. ethAddr := ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370")
  73. tx := PoolL2Tx{
  74. FromIdx: 2,
  75. ToIdx: 3,
  76. Amount: big.NewInt(4),
  77. TokenID: 5,
  78. Nonce: 6,
  79. ToBJJ: sk.Public(),
  80. RqToEthAddr: ethAddr,
  81. RqToBJJ: sk.Public(),
  82. }
  83. toSign, err := tx.HashToSign()
  84. assert.Nil(t, err)
  85. assert.Equal(t, "14526446928649310956370997581245770629723313742905751117262272426489782809503", toSign.String())
  86. }
  87. func TestVerifyTxSignature(t *testing.T) {
  88. var sk babyjub.PrivateKey
  89. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  90. assert.Nil(t, err)
  91. ethAddr := ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370")
  92. tx := PoolL2Tx{
  93. FromIdx: 2,
  94. ToIdx: 3,
  95. Amount: big.NewInt(4),
  96. TokenID: 5,
  97. Nonce: 6,
  98. ToBJJ: sk.Public(),
  99. RqToEthAddr: ethAddr,
  100. RqToBJJ: sk.Public(),
  101. }
  102. toSign, err := tx.HashToSign()
  103. assert.Nil(t, err)
  104. assert.Equal(t, "14526446928649310956370997581245770629723313742905751117262272426489782809503", toSign.String())
  105. sig := sk.SignPoseidon(toSign)
  106. tx.Signature = sig
  107. assert.True(t, tx.VerifySignature(sk.Public()))
  108. }