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.

196 lines
5.6 KiB

  1. package common
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "math"
  6. "math/big"
  7. "testing"
  8. ethCommon "github.com/ethereum/go-ethereum/common"
  9. ethCrypto "github.com/ethereum/go-ethereum/crypto"
  10. "github.com/iden3/go-iden3-crypto/babyjub"
  11. cryptoConstants "github.com/iden3/go-iden3-crypto/constants"
  12. cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestAccount(t *testing.T) {
  16. var sk babyjub.PrivateKey
  17. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  18. assert.Nil(t, err)
  19. pk := sk.Public()
  20. account := &Account{
  21. TokenID: TokenID(1),
  22. Nonce: uint64(1234),
  23. Balance: big.NewInt(1000),
  24. PublicKey: pk,
  25. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  26. }
  27. b, err := account.Bytes()
  28. assert.Nil(t, err)
  29. assert.Equal(t, byte(1), b[10])
  30. a1, err := AccountFromBytes(b)
  31. assert.Nil(t, err)
  32. assert.Equal(t, account, a1)
  33. e, err := account.BigInts()
  34. assert.Nil(t, err)
  35. assert.True(t, cryptoUtils.CheckBigIntInField(e[0]))
  36. assert.True(t, cryptoUtils.CheckBigIntInField(e[1]))
  37. assert.True(t, cryptoUtils.CheckBigIntInField(e[2]))
  38. assert.True(t, cryptoUtils.CheckBigIntInField(e[3]))
  39. assert.Equal(t, "1000", e[1].String())
  40. assert.Equal(t, pk.Y.String(), e[2].String())
  41. assert.Equal(t, new(big.Int).SetBytes(SwapEndianness(account.EthAddr.Bytes())).String(), e[3].String())
  42. a2, err := AccountFromBigInts(e)
  43. assert.Nil(t, err)
  44. assert.Equal(t, account, a2)
  45. assert.Equal(t, a1, a2)
  46. }
  47. func TestAccountLoop(t *testing.T) {
  48. // check that for different Address there is no problem
  49. for i := 0; i < 256; i++ {
  50. var sk babyjub.PrivateKey
  51. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  52. assert.Nil(t, err)
  53. pk := sk.Public()
  54. key, err := ethCrypto.GenerateKey()
  55. assert.Nil(t, err)
  56. address := ethCrypto.PubkeyToAddress(key.PublicKey)
  57. account := &Account{
  58. TokenID: TokenID(i),
  59. Nonce: uint64(i),
  60. Balance: big.NewInt(1000),
  61. PublicKey: pk,
  62. EthAddr: address,
  63. }
  64. b, err := account.Bytes()
  65. assert.Nil(t, err)
  66. a1, err := AccountFromBytes(b)
  67. assert.Nil(t, err)
  68. assert.Equal(t, account, a1)
  69. e, err := account.BigInts()
  70. assert.Nil(t, err)
  71. assert.True(t, cryptoUtils.CheckBigIntInField(e[0]))
  72. assert.True(t, cryptoUtils.CheckBigIntInField(e[1]))
  73. assert.True(t, cryptoUtils.CheckBigIntInField(e[2]))
  74. assert.True(t, cryptoUtils.CheckBigIntInField(e[3]))
  75. a2, err := AccountFromBigInts(e)
  76. assert.Nil(t, err)
  77. assert.Equal(t, account, a2)
  78. }
  79. }
  80. func TestAccountHashValue(t *testing.T) {
  81. var sk babyjub.PrivateKey
  82. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  83. assert.Nil(t, err)
  84. pk := sk.Public()
  85. account := &Account{
  86. TokenID: TokenID(1),
  87. Nonce: uint64(1234),
  88. Balance: big.NewInt(1000),
  89. PublicKey: pk,
  90. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  91. }
  92. v, err := account.HashValue()
  93. assert.Nil(t, err)
  94. assert.Equal(t, "6335844662301214382338419199835935731871537354006112711277201708185593574314", v.String())
  95. }
  96. func TestAccountErrNotInFF(t *testing.T) {
  97. z := big.NewInt(0)
  98. // Q-1 should not give error
  99. r := new(big.Int).Sub(cryptoConstants.Q, big.NewInt(1))
  100. e := [NLEAFELEMS]*big.Int{z, z, r, r}
  101. _, err := AccountFromBigInts(e)
  102. assert.Nil(t, err)
  103. // Q should give error
  104. r = cryptoConstants.Q
  105. e = [NLEAFELEMS]*big.Int{z, z, r, r}
  106. _, err = AccountFromBigInts(e)
  107. assert.NotNil(t, err)
  108. assert.Equal(t, ErrNotInFF, err)
  109. // Q+1 should give error
  110. r = new(big.Int).Add(cryptoConstants.Q, big.NewInt(1))
  111. e = [NLEAFELEMS]*big.Int{z, z, r, r}
  112. _, err = AccountFromBigInts(e)
  113. assert.NotNil(t, err)
  114. assert.Equal(t, ErrNotInFF, err)
  115. }
  116. func TestAccountErrNumOverflowNonce(t *testing.T) {
  117. var sk babyjub.PrivateKey
  118. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  119. assert.Nil(t, err)
  120. pk := sk.Public()
  121. // check limit
  122. account := &Account{
  123. TokenID: TokenID(1),
  124. Nonce: uint64(math.Pow(2, 40) - 1),
  125. Balance: big.NewInt(1000),
  126. PublicKey: pk,
  127. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  128. }
  129. _, err = account.Bytes()
  130. assert.Nil(t, err)
  131. // force value overflow
  132. account.Nonce = uint64(math.Pow(2, 40))
  133. b, err := account.Bytes()
  134. assert.NotNil(t, err)
  135. assert.Equal(t, fmt.Errorf("%s Nonce", ErrNumOverflow), err)
  136. _, err = AccountFromBytes(b)
  137. assert.Nil(t, err)
  138. }
  139. func TestAccountErrNumOverflowBalance(t *testing.T) {
  140. var sk babyjub.PrivateKey
  141. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  142. assert.Nil(t, err)
  143. pk := sk.Public()
  144. // check limit
  145. account := &Account{
  146. TokenID: TokenID(1),
  147. Nonce: uint64(math.Pow(2, 40) - 1),
  148. Balance: new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil), big.NewInt(1)),
  149. PublicKey: pk,
  150. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  151. }
  152. assert.Equal(t, "6277101735386680763835789423207666416102355444464034512895", account.Balance.String())
  153. _, err = account.Bytes()
  154. assert.Nil(t, err)
  155. // force value overflow
  156. account.Balance = new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil)
  157. assert.Equal(t, "6277101735386680763835789423207666416102355444464034512896", account.Balance.String())
  158. b, err := account.Bytes()
  159. assert.NotNil(t, err)
  160. assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
  161. _, err = AccountFromBytes(b)
  162. assert.Nil(t, err)
  163. b[56] = 1
  164. _, err = AccountFromBytes(b)
  165. assert.NotNil(t, err)
  166. assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
  167. }