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.

222 lines
6.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. "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, "0x024f67ec893467419cdfacfc9152e55dc7ce16088952bf2b3442732fd046bd031c", poolL2Tx.TxID.String())
  22. }
  23. func TestTxCompressedData(t *testing.T) {
  24. chainID := uint16(0)
  25. var sk babyjub.PrivateKey
  26. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  27. assert.NoError(t, err)
  28. tx := PoolL2Tx{
  29. FromIdx: 2,
  30. ToIdx: 3,
  31. Amount: big.NewInt(4),
  32. TokenID: 5,
  33. Nonce: 6,
  34. ToBJJ: sk.Public().Compress(),
  35. }
  36. txCompressedData, err := tx.TxCompressedData(chainID)
  37. assert.NoError(t, err)
  38. // test vector value generated from javascript implementation
  39. expectedStr := "1766847064778421992193717128424891165872736891548909569553540445094274575"
  40. assert.Equal(t, expectedStr, txCompressedData.String())
  41. assert.Equal(t, "010000000000060000000500040000000000030000000000020000c60be60f", hex.EncodeToString(txCompressedData.Bytes()))
  42. // using a different chainID
  43. txCompressedData, err = tx.TxCompressedData(uint16(100))
  44. assert.NoError(t, err)
  45. expectedStr = "1766847064778421992193717128424891165872736891548909569553540874591004175"
  46. assert.Equal(t, expectedStr, txCompressedData.String())
  47. assert.Equal(t, "010000000000060000000500040000000000030000000000020064c60be60f", hex.EncodeToString(txCompressedData.Bytes()))
  48. txCompressedData, err = tx.TxCompressedData(uint16(65535))
  49. assert.NoError(t, err)
  50. expectedStr = "1766847064778421992193717128424891165872736891548909569553821915776017935"
  51. assert.Equal(t, expectedStr, txCompressedData.String())
  52. assert.Equal(t, "01000000000006000000050004000000000003000000000002ffffc60be60f", hex.EncodeToString(txCompressedData.Bytes()))
  53. tx = PoolL2Tx{
  54. RqFromIdx: 7,
  55. RqToIdx: 8,
  56. RqAmount: big.NewInt(9),
  57. RqTokenID: 10,
  58. RqNonce: 11,
  59. RqFee: 12,
  60. RqToBJJ: sk.Public().Compress(),
  61. }
  62. rqTxCompressedData, err := tx.RqTxCompressedDataV2()
  63. assert.NoError(t, err)
  64. // test vector value generated from javascript implementation
  65. expectedStr = "6571340879233176732837827812956721483162819083004853354503"
  66. assert.Equal(t, expectedStr, rqTxCompressedData.String())
  67. assert.Equal(t, "010c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(rqTxCompressedData.Bytes()))
  68. }
  69. func TestTxCompressedDataV2(t *testing.T) {
  70. var sk babyjub.PrivateKey
  71. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  72. assert.NoError(t, err)
  73. tx := PoolL2Tx{
  74. FromIdx: 7,
  75. ToIdx: 8,
  76. Amount: big.NewInt(9),
  77. TokenID: 10,
  78. Nonce: 11,
  79. Fee: 12,
  80. ToBJJ: sk.Public().Compress(),
  81. }
  82. txCompressedData, err := tx.TxCompressedDataV2()
  83. assert.NoError(t, err)
  84. // test vector value generated from javascript implementation
  85. expectedStr := "6571340879233176732837827812956721483162819083004853354503"
  86. assert.Equal(t, expectedStr, txCompressedData.String())
  87. expected, ok := new(big.Int).SetString(expectedStr, 10)
  88. assert.True(t, ok)
  89. assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
  90. assert.Equal(t, "010c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes()))
  91. }
  92. func TestRqTxCompressedDataV2(t *testing.T) {
  93. var sk babyjub.PrivateKey
  94. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  95. assert.NoError(t, err)
  96. tx := PoolL2Tx{
  97. RqFromIdx: 7,
  98. RqToIdx: 8,
  99. RqAmount: big.NewInt(9),
  100. RqTokenID: 10,
  101. RqNonce: 11,
  102. RqFee: 12,
  103. RqToBJJ: sk.Public().Compress(),
  104. }
  105. txCompressedData, err := tx.RqTxCompressedDataV2()
  106. assert.NoError(t, err)
  107. // test vector value generated from javascript implementation
  108. expectedStr := "6571340879233176732837827812956721483162819083004853354503"
  109. assert.Equal(t, expectedStr, txCompressedData.String())
  110. expected, ok := new(big.Int).SetString(expectedStr, 10)
  111. assert.True(t, ok)
  112. assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
  113. assert.Equal(t, "010c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes()))
  114. }
  115. func TestHashToSign(t *testing.T) {
  116. chainID := uint16(0)
  117. var sk babyjub.PrivateKey
  118. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  119. assert.NoError(t, err)
  120. tx := PoolL2Tx{
  121. FromIdx: 2,
  122. ToIdx: 3,
  123. Amount: big.NewInt(4),
  124. TokenID: 5,
  125. Nonce: 6,
  126. ToEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  127. }
  128. toSign, err := tx.HashToSign(chainID)
  129. assert.NoError(t, err)
  130. assert.Equal(t, "1469900657138253851938022936440971384682713995864967090251961124784132925291", toSign.String())
  131. }
  132. func TestVerifyTxSignature(t *testing.T) {
  133. chainID := uint16(0)
  134. var sk babyjub.PrivateKey
  135. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  136. assert.NoError(t, err)
  137. tx := PoolL2Tx{
  138. FromIdx: 2,
  139. ToIdx: 3,
  140. Amount: big.NewInt(4),
  141. TokenID: 5,
  142. Nonce: 6,
  143. ToBJJ: sk.Public().Compress(),
  144. RqToEthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  145. RqToBJJ: sk.Public().Compress(),
  146. }
  147. toSign, err := tx.HashToSign(chainID)
  148. assert.NoError(t, err)
  149. assert.Equal(t, "18645218094210271622244722988708640202588315450486586312909439859037906375295", toSign.String())
  150. sig := sk.SignPoseidon(toSign)
  151. tx.Signature = sig.Compress()
  152. assert.True(t, tx.VerifySignature(chainID, sk.Public().Compress()))
  153. }
  154. func TestDecompressEmptyBJJComp(t *testing.T) {
  155. pkComp := EmptyBJJComp
  156. pk, err := pkComp.Decompress()
  157. require.NoError(t, err)
  158. assert.Equal(t, "2957874849018779266517920829765869116077630550401372566248359756137677864698", pk.X.String())
  159. assert.Equal(t, "0", pk.Y.String())
  160. }
  161. func TestPoolL2TxID(t *testing.T) {
  162. tx0 := PoolL2Tx{
  163. FromIdx: 5,
  164. ToIdx: 5,
  165. Amount: big.NewInt(5),
  166. Fee: 126,
  167. TokenID: 5,
  168. Nonce: 5,
  169. }
  170. err := tx0.SetID()
  171. require.NoError(t, err)
  172. // differ TokenID
  173. tx1 := PoolL2Tx{
  174. FromIdx: 5,
  175. ToIdx: 5,
  176. Amount: big.NewInt(5),
  177. Fee: 126,
  178. TokenID: 4,
  179. Nonce: 5,
  180. }
  181. err = tx1.SetID()
  182. require.NoError(t, err)
  183. assert.NotEqual(t, tx0.TxID, tx1.TxID)
  184. // differ Nonce
  185. tx1 = PoolL2Tx{
  186. FromIdx: 5,
  187. ToIdx: 5,
  188. Amount: big.NewInt(5),
  189. Fee: 126,
  190. TokenID: 5,
  191. Nonce: 4,
  192. }
  193. err = tx1.SetID()
  194. require.NoError(t, err)
  195. assert.NotEqual(t, tx0.TxID, tx1.TxID)
  196. // differ Fee
  197. tx1 = PoolL2Tx{
  198. FromIdx: 5,
  199. ToIdx: 5,
  200. Amount: big.NewInt(5),
  201. Fee: 124,
  202. TokenID: 5,
  203. Nonce: 5,
  204. }
  205. err = tx1.SetID()
  206. require.NoError(t, err)
  207. assert.NotEqual(t, tx0.TxID, tx1.TxID)
  208. }