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.

100 lines
3.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. )
  10. func TestTxCompressedData(t *testing.T) {
  11. var sk babyjub.PrivateKey
  12. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  13. assert.Nil(t, err)
  14. tx := PoolL2Tx{
  15. FromIdx: 2,
  16. ToIdx: 3,
  17. Amount: big.NewInt(4),
  18. TokenID: 5,
  19. Nonce: 6,
  20. ToBJJ: sk.Public(),
  21. }
  22. txCompressedData, err := tx.TxCompressedData()
  23. assert.Nil(t, err)
  24. // test vector value generated from javascript implementation
  25. expectedStr := "1766847064778421992193717128424891165872736891548909569553540449389241871"
  26. assert.Equal(t, expectedStr, txCompressedData.String())
  27. expected, ok := new(big.Int).SetString(expectedStr, 10)
  28. assert.True(t, ok)
  29. assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
  30. assert.Equal(t, "10000000000060000000500040000000000030000000000020001c60be60f", hex.EncodeToString(txCompressedData.Bytes())[1:])
  31. tx = PoolL2Tx{
  32. FromIdx: 7,
  33. ToIdx: 8,
  34. Amount: big.NewInt(9),
  35. TokenID: 10,
  36. Nonce: 11,
  37. Fee: 12,
  38. ToBJJ: sk.Public(),
  39. }
  40. txCompressedData, err = tx.TxCompressedDataV2()
  41. assert.Nil(t, err)
  42. // test vector value generated from javascript implementation
  43. expectedStr = "6571340879233176732837827812956721483162819083004853354503"
  44. assert.Equal(t, expectedStr, txCompressedData.String())
  45. expected, ok = new(big.Int).SetString(expectedStr, 10)
  46. assert.True(t, ok)
  47. assert.Equal(t, expected.Bytes(), txCompressedData.Bytes())
  48. assert.Equal(t, "10c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes())[1:])
  49. }
  50. func TestHashToSign(t *testing.T) {
  51. var sk babyjub.PrivateKey
  52. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  53. assert.Nil(t, err)
  54. ethAddr := ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370")
  55. tx := PoolL2Tx{
  56. FromIdx: 2,
  57. ToIdx: 3,
  58. Amount: big.NewInt(4),
  59. TokenID: 5,
  60. Nonce: 6,
  61. ToBJJ: sk.Public(),
  62. RqToEthAddr: ethAddr,
  63. RqToBJJ: sk.Public(),
  64. }
  65. toSign, err := tx.HashToSign()
  66. assert.Nil(t, err)
  67. assert.Equal(t, "14526446928649310956370997581245770629723313742905751117262272426489782809503", toSign.String())
  68. }
  69. func TestVerifyTxSignature(t *testing.T) {
  70. var sk babyjub.PrivateKey
  71. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  72. assert.Nil(t, err)
  73. ethAddr := ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370")
  74. tx := PoolL2Tx{
  75. FromIdx: 2,
  76. ToIdx: 3,
  77. Amount: big.NewInt(4),
  78. TokenID: 5,
  79. Nonce: 6,
  80. ToBJJ: sk.Public(),
  81. RqToEthAddr: ethAddr,
  82. RqToBJJ: sk.Public(),
  83. }
  84. toSign, err := tx.HashToSign()
  85. assert.Nil(t, err)
  86. assert.Equal(t, "14526446928649310956370997581245770629723313742905751117262272426489782809503", toSign.String())
  87. sig := sk.SignPoseidon(toSign)
  88. tx.Signature = sig
  89. assert.True(t, tx.VerifySignature(sk.Public()))
  90. }