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.

164 lines
4.9 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. Ax: big.NewInt(9876),
  18. Ay: big.NewInt(6789),
  19. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  20. }
  21. b, err := leaf.Bytes()
  22. assert.Nil(t, err)
  23. l1, err := LeafFromBytes(b)
  24. assert.Nil(t, err)
  25. assert.Equal(t, leaf, l1)
  26. e, err := leaf.BigInts()
  27. assert.Nil(t, err)
  28. assert.True(t, cryptoUtils.CheckBigIntInField(e[0]))
  29. assert.True(t, cryptoUtils.CheckBigIntInField(e[1]))
  30. assert.True(t, cryptoUtils.CheckBigIntInField(e[2]))
  31. assert.True(t, cryptoUtils.CheckBigIntInField(e[3]))
  32. assert.True(t, cryptoUtils.CheckBigIntInField(e[4]))
  33. assert.Equal(t, "1000", e[1].String())
  34. assert.Equal(t, "9876", e[2].String())
  35. assert.Equal(t, "6789", 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. // 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. //
  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. // assert.True(t, cryptoUtils.CheckBigIntInField(e[4]))
  69. //
  70. // l2, err := LeafFromBigInts(e)
  71. // assert.Nil(t, err)
  72. // assert.Equal(t, leaf, l2)
  73. // }
  74. // }
  75. func TestLeafErrNotInFF(t *testing.T) {
  76. z := big.NewInt(0)
  77. // Q-1 should not give error
  78. r := new(big.Int).Sub(cryptoConstants.Q, big.NewInt(1))
  79. e := [5]*big.Int{z, z, r, r, r}
  80. _, err := LeafFromBigInts(e)
  81. assert.Nil(t, err)
  82. // Q should give error
  83. r = cryptoConstants.Q
  84. e = [5]*big.Int{z, z, r, r, r}
  85. _, err = LeafFromBigInts(e)
  86. assert.NotNil(t, err)
  87. assert.Equal(t, ErrNotInFF, err)
  88. // Q+1 should give error
  89. r = new(big.Int).Add(cryptoConstants.Q, big.NewInt(1))
  90. e = [5]*big.Int{z, z, r, r, r}
  91. _, err = LeafFromBigInts(e)
  92. assert.NotNil(t, err)
  93. assert.Equal(t, ErrNotInFF, err)
  94. }
  95. func TestLeafErrNumOverflowNonce(t *testing.T) {
  96. // check limit
  97. leaf := &Leaf{
  98. TokenID: TokenID(1),
  99. Nonce: uint64(math.Pow(2, 40) - 1),
  100. Balance: big.NewInt(1000),
  101. Ax: big.NewInt(9876),
  102. Ay: big.NewInt(6789),
  103. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  104. }
  105. _, err := leaf.Bytes()
  106. assert.Nil(t, err)
  107. // force value overflow
  108. leaf.Nonce = uint64(math.Pow(2, 40))
  109. b, err := leaf.Bytes()
  110. assert.NotNil(t, err)
  111. assert.Equal(t, fmt.Errorf("%s Nonce", ErrNumOverflow), err)
  112. _, err = LeafFromBytes(b)
  113. assert.Nil(t, err)
  114. b[9] = 1
  115. _, err = LeafFromBytes(b)
  116. assert.NotNil(t, err)
  117. assert.Equal(t, fmt.Errorf("%s Nonce", ErrNumOverflow), err)
  118. }
  119. func TestLeafErrNumOverflowBalance(t *testing.T) {
  120. // check limit
  121. leaf := &Leaf{
  122. TokenID: TokenID(1),
  123. Nonce: uint64(math.Pow(2, 40) - 1),
  124. Balance: new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil), big.NewInt(1)),
  125. Ax: big.NewInt(9876),
  126. Ay: big.NewInt(6789),
  127. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  128. }
  129. assert.Equal(t, "6277101735386680763835789423207666416102355444464034512895", leaf.Balance.String())
  130. _, err := leaf.Bytes()
  131. assert.Nil(t, err)
  132. // force value overflow
  133. leaf.Balance = new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil)
  134. assert.Equal(t, "6277101735386680763835789423207666416102355444464034512896", leaf.Balance.String())
  135. b, err := leaf.Bytes()
  136. assert.NotNil(t, err)
  137. assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
  138. _, err = LeafFromBytes(b)
  139. assert.Nil(t, err)
  140. b[56] = 1
  141. _, err = LeafFromBytes(b)
  142. assert.NotNil(t, err)
  143. assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
  144. }