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.

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