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.

125 lines
3.7 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, "0000000001", 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. expectedStr := "1766847064778421992193717128424891165872736891548909569553540449389241871"
  48. assert.Equal(t, expectedStr, txCompressedData.String())
  49. expected, ok := new(big.Int).SetString(expectedStr, 10)
  50. assert.True(t, ok)
  51. assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
  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. expectedStr = "6571340879233176732837827812956721483162819083004853354503"
  66. assert.Equal(t, expectedStr, txCompressedData.String())
  67. expected, ok = new(big.Int).SetString(expectedStr, 10)
  68. assert.True(t, ok)
  69. assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
  70. assert.Equal(t, "10c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes())[1:])
  71. }
  72. func TestHashToSign(t *testing.T) {
  73. var sk babyjub.PrivateKey
  74. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  75. assert.Nil(t, err)
  76. ethAddr := ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370")
  77. tx := PoolL2Tx{
  78. FromIdx: 2,
  79. ToIdx: 3,
  80. Amount: big.NewInt(4),
  81. TokenID: 5,
  82. Nonce: 6,
  83. ToBJJ: sk.Public(),
  84. RqToEthAddr: ethAddr,
  85. RqToBJJ: sk.Public(),
  86. }
  87. toSign, err := tx.HashToSign()
  88. assert.Nil(t, err)
  89. assert.Equal(t, "14526446928649310956370997581245770629723313742905751117262272426489782809503", toSign.String())
  90. }
  91. func TestVerifyTxSignature(t *testing.T) {
  92. var sk babyjub.PrivateKey
  93. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  94. assert.Nil(t, err)
  95. ethAddr := ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370")
  96. tx := PoolL2Tx{
  97. FromIdx: 2,
  98. ToIdx: 3,
  99. Amount: big.NewInt(4),
  100. TokenID: 5,
  101. Nonce: 6,
  102. ToBJJ: sk.Public(),
  103. RqToEthAddr: ethAddr,
  104. RqToBJJ: sk.Public(),
  105. }
  106. toSign, err := tx.HashToSign()
  107. assert.Nil(t, err)
  108. assert.Equal(t, "14526446928649310956370997581245770629723313742905751117262272426489782809503", toSign.String())
  109. sig := sk.SignPoseidon(toSign)
  110. tx.Signature = sig
  111. assert.True(t, tx.VerifySignature(sk.Public()))
  112. }