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.

150 lines
4.8 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 TestNewPoolL2Tx(t *testing.T) {
  11. poolL2Tx := &PoolL2Tx{
  12. FromIdx: 87654,
  13. ToIdx: 300,
  14. Amount: big.NewInt(4),
  15. TokenID: 5,
  16. Nonce: 144,
  17. }
  18. poolL2Tx, err := NewPoolL2Tx(poolL2Tx)
  19. assert.Nil(t, err)
  20. assert.Equal(t, "0x020000000156660000000090", poolL2Tx.TxID.String())
  21. }
  22. func TestTxCompressedData(t *testing.T) {
  23. var sk babyjub.PrivateKey
  24. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  25. assert.Nil(t, err)
  26. tx := PoolL2Tx{
  27. FromIdx: 2,
  28. ToIdx: 3,
  29. Amount: big.NewInt(4),
  30. TokenID: 5,
  31. Nonce: 6,
  32. ToBJJ: sk.Public(),
  33. }
  34. txCompressedData, err := tx.TxCompressedData()
  35. assert.Nil(t, err)
  36. // test vector value generated from javascript implementation
  37. expectedStr := "1766847064778421992193717128424891165872736891548909569553540449389241871"
  38. assert.Equal(t, expectedStr, txCompressedData.String())
  39. assert.Equal(t, "010000000000060000000500040000000000030000000000020001c60be60f", hex.EncodeToString(txCompressedData.Bytes()))
  40. tx = PoolL2Tx{
  41. RqFromIdx: 7,
  42. RqToIdx: 8,
  43. RqAmount: big.NewInt(9),
  44. RqTokenID: 10,
  45. RqNonce: 11,
  46. RqFee: 12,
  47. RqToBJJ: sk.Public(),
  48. }
  49. txCompressedData, err = tx.RqTxCompressedDataV2()
  50. assert.Nil(t, err)
  51. // test vector value generated from javascript implementation
  52. expectedStr = "6571340879233176732837827812956721483162819083004853354503"
  53. assert.Equal(t, expectedStr, txCompressedData.String())
  54. assert.Equal(t, "010c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes()))
  55. }
  56. func TestTxCompressedDataV2(t *testing.T) {
  57. var sk babyjub.PrivateKey
  58. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  59. assert.Nil(t, err)
  60. tx := PoolL2Tx{
  61. FromIdx: 7,
  62. ToIdx: 8,
  63. Amount: big.NewInt(9),
  64. TokenID: 10,
  65. Nonce: 11,
  66. Fee: 12,
  67. ToBJJ: sk.Public(),
  68. }
  69. txCompressedData, err := tx.TxCompressedDataV2()
  70. assert.Nil(t, err)
  71. // test vector value generated from javascript implementation
  72. expectedStr := "6571340879233176732837827812956721483162819083004853354503"
  73. assert.Equal(t, expectedStr, txCompressedData.String())
  74. expected, ok := new(big.Int).SetString(expectedStr, 10)
  75. assert.True(t, ok)
  76. assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
  77. assert.Equal(t, "010c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes()))
  78. }
  79. func TestRqTxCompressedDataV2(t *testing.T) {
  80. var sk babyjub.PrivateKey
  81. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  82. assert.Nil(t, err)
  83. tx := PoolL2Tx{
  84. RqFromIdx: 7,
  85. RqToIdx: 8,
  86. RqAmount: big.NewInt(9),
  87. RqTokenID: 10,
  88. RqNonce: 11,
  89. RqFee: 12,
  90. RqToBJJ: sk.Public(),
  91. }
  92. txCompressedData, err := tx.RqTxCompressedDataV2()
  93. assert.Nil(t, err)
  94. // test vector value generated from javascript implementation
  95. expectedStr := "6571340879233176732837827812956721483162819083004853354503"
  96. assert.Equal(t, expectedStr, txCompressedData.String())
  97. expected, ok := new(big.Int).SetString(expectedStr, 10)
  98. assert.True(t, ok)
  99. assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
  100. assert.Equal(t, "010c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes()))
  101. }
  102. func TestHashToSign(t *testing.T) {
  103. var sk babyjub.PrivateKey
  104. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  105. assert.Nil(t, err)
  106. tx := PoolL2Tx{
  107. FromIdx: 2,
  108. ToIdx: 3,
  109. Amount: big.NewInt(4),
  110. TokenID: 5,
  111. Nonce: 6,
  112. ToBJJ: sk.Public(),
  113. RqToEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  114. RqToBJJ: sk.Public(),
  115. }
  116. toSign, err := tx.HashToSign()
  117. assert.Nil(t, err)
  118. assert.Equal(t, "13412877307445712067533842795279849753265998687662992184595695642580679868064", toSign.String())
  119. }
  120. func TestVerifyTxSignature(t *testing.T) {
  121. var sk babyjub.PrivateKey
  122. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  123. assert.Nil(t, err)
  124. tx := PoolL2Tx{
  125. FromIdx: 2,
  126. ToIdx: 3,
  127. Amount: big.NewInt(4),
  128. TokenID: 5,
  129. Nonce: 6,
  130. ToBJJ: sk.Public(),
  131. RqToEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  132. RqToBJJ: sk.Public(),
  133. }
  134. toSign, err := tx.HashToSign()
  135. assert.Nil(t, err)
  136. assert.Equal(t, "13412877307445712067533842795279849753265998687662992184595695642580679868064", toSign.String())
  137. sig := sk.SignPoseidon(toSign)
  138. tx.Signature = sig.Compress()
  139. assert.True(t, tx.VerifySignature(sk.Public()))
  140. }