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.

148 lines
4.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 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 := "1766847064778421992193717128424891165872736891548909569553540445094274575"
  38. assert.Equal(t, expectedStr, txCompressedData.String())
  39. assert.Equal(t, "010000000000060000000500040000000000030000000000020000c60be60f", 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. ToEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  113. }
  114. toSign, err := tx.HashToSign()
  115. assert.Nil(t, err)
  116. assert.Equal(t, "1469900657138253851938022936440971384682713995864967090251961124784132925291", toSign.String())
  117. }
  118. func TestVerifyTxSignature(t *testing.T) {
  119. var sk babyjub.PrivateKey
  120. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  121. assert.Nil(t, err)
  122. tx := PoolL2Tx{
  123. FromIdx: 2,
  124. ToIdx: 3,
  125. Amount: big.NewInt(4),
  126. TokenID: 5,
  127. Nonce: 6,
  128. ToBJJ: sk.Public(),
  129. RqToEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  130. RqToBJJ: sk.Public(),
  131. }
  132. toSign, err := tx.HashToSign()
  133. assert.Nil(t, err)
  134. assert.Equal(t, "18645218094210271622244722988708640202588315450486586312909439859037906375295", toSign.String())
  135. sig := sk.SignPoseidon(toSign)
  136. tx.Signature = sig.Compress()
  137. assert.True(t, tx.VerifySignature(sk.Public()))
  138. }