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.

158 lines
4.7 KiB

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