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.

165 lines
4.4 KiB

  1. package common
  2. import (
  3. "fmt"
  4. "math"
  5. "math/big"
  6. "testing"
  7. ethCommon "github.com/ethereum/go-ethereum/common"
  8. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  9. cryptoConstants "github.com/iden3/go-iden3-crypto/constants"
  10. cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestLeaf(t *testing.T) {
  14. leaf := &Leaf{
  15. TokenID: TokenID(1),
  16. Nonce: uint64(1234),
  17. Balance: big.NewInt(1000),
  18. Ax: big.NewInt(9876),
  19. Ay: big.NewInt(6789),
  20. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  21. }
  22. b, err := leaf.Bytes()
  23. assert.Nil(t, err)
  24. l1, err := LeafFromBytes(b)
  25. assert.Nil(t, err)
  26. assert.Equal(t, leaf, l1)
  27. e, err := leaf.BigInts()
  28. assert.Nil(t, err)
  29. assert.True(t, cryptoUtils.CheckBigIntInField(e[0]))
  30. assert.True(t, cryptoUtils.CheckBigIntInField(e[1]))
  31. assert.True(t, cryptoUtils.CheckBigIntInField(e[2]))
  32. assert.True(t, cryptoUtils.CheckBigIntInField(e[3]))
  33. assert.True(t, cryptoUtils.CheckBigIntInField(e[4]))
  34. assert.Equal(t, "1000", e[1].String())
  35. assert.Equal(t, "9876", e[2].String())
  36. assert.Equal(t, "6789", e[3].String())
  37. l2, err := LeafFromBigInts(e)
  38. assert.Nil(t, err)
  39. assert.Equal(t, leaf, l2)
  40. assert.Equal(t, l1, l2)
  41. }
  42. func TestLeafLoop(t *testing.T) {
  43. // check that for different Address there is no problem
  44. for i := 0; i < 256; i++ {
  45. key, err := ethCrypto.GenerateKey()
  46. assert.Nil(t, err)
  47. address := ethCrypto.PubkeyToAddress(key.PublicKey)
  48. leaf := &Leaf{
  49. TokenID: TokenID(i),
  50. Nonce: uint64(i),
  51. Balance: big.NewInt(1000),
  52. Ax: big.NewInt(9876),
  53. Ay: big.NewInt(6789),
  54. EthAddr: address,
  55. }
  56. b, err := leaf.Bytes()
  57. assert.Nil(t, err)
  58. l1, err := LeafFromBytes(b)
  59. assert.Nil(t, err)
  60. assert.Equal(t, leaf, l1)
  61. e, err := leaf.BigInts()
  62. assert.Nil(t, err)
  63. assert.True(t, cryptoUtils.CheckBigIntInField(e[0]))
  64. assert.True(t, cryptoUtils.CheckBigIntInField(e[1]))
  65. assert.True(t, cryptoUtils.CheckBigIntInField(e[2]))
  66. assert.True(t, cryptoUtils.CheckBigIntInField(e[3]))
  67. assert.True(t, cryptoUtils.CheckBigIntInField(e[4]))
  68. l2, err := LeafFromBigInts(e)
  69. assert.Nil(t, err)
  70. assert.Equal(t, leaf, l2)
  71. }
  72. }
  73. func TestLeafErrNotInFF(t *testing.T) {
  74. z := big.NewInt(0)
  75. // Q-1 should not give error
  76. r := new(big.Int).Sub(cryptoConstants.Q, big.NewInt(1))
  77. e := [5]*big.Int{z, z, r, r, r}
  78. _, err := LeafFromBigInts(e)
  79. assert.Nil(t, err)
  80. // Q should give error
  81. r = cryptoConstants.Q
  82. e = [5]*big.Int{z, z, r, r, r}
  83. _, err = LeafFromBigInts(e)
  84. assert.NotNil(t, err)
  85. assert.Equal(t, ErrNotInFF, err)
  86. // Q+1 should give error
  87. r = new(big.Int).Add(cryptoConstants.Q, big.NewInt(1))
  88. e = [5]*big.Int{z, z, r, r, r}
  89. _, err = LeafFromBigInts(e)
  90. assert.NotNil(t, err)
  91. assert.Equal(t, ErrNotInFF, err)
  92. }
  93. func TestLeafErrNumOverflowNonce(t *testing.T) {
  94. // check limit
  95. leaf := &Leaf{
  96. TokenID: TokenID(1),
  97. Nonce: uint64(math.Pow(2, 40) - 1),
  98. Balance: big.NewInt(1000),
  99. Ax: big.NewInt(9876),
  100. Ay: big.NewInt(6789),
  101. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  102. }
  103. _, err := leaf.Bytes()
  104. assert.Nil(t, err)
  105. // force value overflow
  106. leaf.Nonce = uint64(math.Pow(2, 40))
  107. b, err := leaf.Bytes()
  108. assert.NotNil(t, err)
  109. assert.Equal(t, fmt.Errorf("%s Nonce", ErrNumOverflow), err)
  110. _, err = LeafFromBytes(b)
  111. assert.Nil(t, err)
  112. b[9] = 1
  113. _, err = LeafFromBytes(b)
  114. assert.NotNil(t, err)
  115. assert.Equal(t, fmt.Errorf("%s Nonce", ErrNumOverflow), err)
  116. }
  117. func TestLeafErrNumOverflowBalance(t *testing.T) {
  118. // check limit
  119. leaf := &Leaf{
  120. TokenID: TokenID(1),
  121. Nonce: uint64(math.Pow(2, 40) - 1),
  122. Balance: new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil), big.NewInt(1)),
  123. Ax: big.NewInt(9876),
  124. Ay: big.NewInt(6789),
  125. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  126. }
  127. assert.Equal(t, "6277101735386680763835789423207666416102355444464034512895", leaf.Balance.String())
  128. _, err := leaf.Bytes()
  129. assert.Nil(t, err)
  130. // force value overflow
  131. leaf.Balance = new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil)
  132. assert.Equal(t, "6277101735386680763835789423207666416102355444464034512896", leaf.Balance.String())
  133. b, err := leaf.Bytes()
  134. assert.NotNil(t, err)
  135. assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
  136. _, err = LeafFromBytes(b)
  137. assert.Nil(t, err)
  138. b[56] = 1
  139. _, err = LeafFromBytes(b)
  140. assert.NotNil(t, err)
  141. assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
  142. }