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.

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