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.

75 lines
2.0 KiB

  1. package common
  2. import (
  3. "encoding/hex"
  4. "math/big"
  5. "testing"
  6. "github.com/iden3/go-iden3-crypto/babyjub"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestNonceParser(t *testing.T) {
  10. n := Nonce(1)
  11. nBytes, err := n.Bytes()
  12. assert.Nil(t, err)
  13. assert.Equal(t, 5, len(nBytes))
  14. assert.Equal(t, "0100000000", hex.EncodeToString(nBytes[:]))
  15. n2, err := NonceFromBytes(nBytes)
  16. assert.Nil(t, err)
  17. assert.Equal(t, n, n2)
  18. // value before overflow
  19. n = Nonce(1099511627775)
  20. nBytes, err = n.Bytes()
  21. assert.Nil(t, err)
  22. assert.Equal(t, 5, len(nBytes))
  23. assert.Equal(t, "ffffffffff", hex.EncodeToString(nBytes[:]))
  24. n2, err = NonceFromBytes(nBytes)
  25. assert.Nil(t, err)
  26. assert.Equal(t, n, n2)
  27. // expect value overflow
  28. n = Nonce(1099511627776)
  29. nBytes, err = n.Bytes()
  30. assert.NotNil(t, err)
  31. assert.Equal(t, ErrNonceOverflow, err)
  32. _, err = NonceFromBytes(nBytes)
  33. assert.Nil(t, err)
  34. }
  35. func TestTxCompressedData(t *testing.T) {
  36. var sk babyjub.PrivateKey
  37. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  38. assert.Nil(t, err)
  39. tx := PoolL2Tx{
  40. FromIdx: 2,
  41. ToIdx: 3,
  42. Amount: big.NewInt(4),
  43. TokenID: 5,
  44. Nonce: 6,
  45. ToBJJ: sk.Public(),
  46. }
  47. txCompressedData, err := tx.TxCompressedData()
  48. assert.Nil(t, err)
  49. // test vector value generated from javascript implementation
  50. assert.Equal(t, "1766847064778421992193717128424891165872736891548909569553540449389241871", txCompressedData.String())
  51. assert.Equal(t, "10000000000060000000500040000000000030000000000020001c60be60f", hex.EncodeToString(txCompressedData.Bytes())[1:])
  52. tx = PoolL2Tx{
  53. FromIdx: 7,
  54. ToIdx: 8,
  55. Amount: big.NewInt(9),
  56. TokenID: 10,
  57. Nonce: 11,
  58. Fee: 12,
  59. ToBJJ: sk.Public(),
  60. }
  61. txCompressedData, err = tx.TxCompressedDataV2()
  62. assert.Nil(t, err)
  63. // test vector value generated from javascript implementation
  64. assert.Equal(t, "6571340879233176732837827812956721483162819083004853354503", txCompressedData.String())
  65. assert.Equal(t, "10c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes())[1:])
  66. }