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.

251 lines
7.0 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 TestIdxParser(t *testing.T) {
  16. i := Idx(1)
  17. iBytes, err := i.Bytes()
  18. assert.Nil(t, err)
  19. assert.Equal(t, 6, len(iBytes))
  20. assert.Equal(t, "000000000001", hex.EncodeToString(iBytes[:]))
  21. i2, err := IdxFromBytes(iBytes[:])
  22. assert.Nil(t, err)
  23. assert.Equal(t, i, i2)
  24. i = Idx(100)
  25. assert.Equal(t, big.NewInt(100), i.BigInt())
  26. // value before overflow
  27. i = Idx(281474976710655)
  28. iBytes, err = i.Bytes()
  29. assert.Nil(t, err)
  30. assert.Equal(t, 6, len(iBytes))
  31. assert.Equal(t, "ffffffffffff", hex.EncodeToString(iBytes[:]))
  32. i2, err = IdxFromBytes(iBytes[:])
  33. assert.Nil(t, err)
  34. assert.Equal(t, i, i2)
  35. // expect value overflow
  36. i = Idx(281474976710656)
  37. iBytes, err = i.Bytes()
  38. assert.NotNil(t, err)
  39. assert.Equal(t, ErrIdxOverflow, err)
  40. }
  41. func TestNonceParser(t *testing.T) {
  42. n := Nonce(1)
  43. nBytes, err := n.Bytes()
  44. assert.Nil(t, err)
  45. assert.Equal(t, 5, len(nBytes))
  46. assert.Equal(t, "0000000001", hex.EncodeToString(nBytes[:]))
  47. n2 := NonceFromBytes(nBytes)
  48. assert.Equal(t, n, n2)
  49. // value before overflow
  50. n = Nonce(1099511627775)
  51. nBytes, err = n.Bytes()
  52. assert.Nil(t, err)
  53. assert.Equal(t, 5, len(nBytes))
  54. assert.Equal(t, "ffffffffff", hex.EncodeToString(nBytes[:]))
  55. n2 = NonceFromBytes(nBytes)
  56. assert.Equal(t, n, n2)
  57. // expect value overflow
  58. n = Nonce(1099511627776)
  59. nBytes, err = n.Bytes()
  60. assert.NotNil(t, err)
  61. assert.Equal(t, ErrNonceOverflow, err)
  62. }
  63. func TestAccount(t *testing.T) {
  64. var sk babyjub.PrivateKey
  65. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  66. assert.Nil(t, err)
  67. pk := sk.Public()
  68. account := &Account{
  69. TokenID: TokenID(1),
  70. Nonce: Nonce(1234),
  71. Balance: big.NewInt(1000),
  72. PublicKey: pk,
  73. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  74. }
  75. b, err := account.Bytes()
  76. assert.Nil(t, err)
  77. assert.Equal(t, byte(1), b[10])
  78. a1, err := AccountFromBytes(b)
  79. assert.Nil(t, err)
  80. assert.Equal(t, account, a1)
  81. e, err := account.BigInts()
  82. assert.Nil(t, err)
  83. assert.True(t, cryptoUtils.CheckBigIntInField(e[0]))
  84. assert.True(t, cryptoUtils.CheckBigIntInField(e[1]))
  85. assert.True(t, cryptoUtils.CheckBigIntInField(e[2]))
  86. assert.True(t, cryptoUtils.CheckBigIntInField(e[3]))
  87. assert.Equal(t, "1000", e[1].String())
  88. assert.Equal(t, pk.Y.String(), e[2].String())
  89. assert.Equal(t, new(big.Int).SetBytes(SwapEndianness(account.EthAddr.Bytes())).String(), e[3].String())
  90. a2, err := AccountFromBigInts(e)
  91. assert.Nil(t, err)
  92. assert.Equal(t, account, a2)
  93. assert.Equal(t, a1, a2)
  94. }
  95. func TestAccountLoop(t *testing.T) {
  96. // check that for different Address there is no problem
  97. for i := 0; i < 256; i++ {
  98. var sk babyjub.PrivateKey
  99. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  100. assert.Nil(t, err)
  101. pk := sk.Public()
  102. key, err := ethCrypto.GenerateKey()
  103. assert.Nil(t, err)
  104. address := ethCrypto.PubkeyToAddress(key.PublicKey)
  105. account := &Account{
  106. TokenID: TokenID(i),
  107. Nonce: Nonce(i),
  108. Balance: big.NewInt(1000),
  109. PublicKey: pk,
  110. EthAddr: address,
  111. }
  112. b, err := account.Bytes()
  113. assert.Nil(t, err)
  114. a1, err := AccountFromBytes(b)
  115. assert.Nil(t, err)
  116. assert.Equal(t, account, a1)
  117. e, err := account.BigInts()
  118. assert.Nil(t, err)
  119. assert.True(t, cryptoUtils.CheckBigIntInField(e[0]))
  120. assert.True(t, cryptoUtils.CheckBigIntInField(e[1]))
  121. assert.True(t, cryptoUtils.CheckBigIntInField(e[2]))
  122. assert.True(t, cryptoUtils.CheckBigIntInField(e[3]))
  123. a2, err := AccountFromBigInts(e)
  124. assert.Nil(t, err)
  125. assert.Equal(t, account, a2)
  126. }
  127. }
  128. func TestAccountHashValue(t *testing.T) {
  129. var sk babyjub.PrivateKey
  130. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  131. assert.Nil(t, err)
  132. pk := sk.Public()
  133. account := &Account{
  134. TokenID: TokenID(1),
  135. Nonce: Nonce(1234),
  136. Balance: big.NewInt(1000),
  137. PublicKey: pk,
  138. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  139. }
  140. v, err := account.HashValue()
  141. assert.Nil(t, err)
  142. assert.Equal(t, "9478468711598093334066833736294178928569163287501434518121324135729106649559", v.String())
  143. }
  144. func TestAccountErrNotInFF(t *testing.T) {
  145. z := big.NewInt(0)
  146. // Q-1 should not give error
  147. r := new(big.Int).Sub(cryptoConstants.Q, big.NewInt(1))
  148. e := [NLeafElems]*big.Int{z, z, r, r}
  149. _, err := AccountFromBigInts(e)
  150. assert.Nil(t, err)
  151. // Q should give error
  152. r = cryptoConstants.Q
  153. e = [NLeafElems]*big.Int{z, z, r, r}
  154. _, err = AccountFromBigInts(e)
  155. assert.NotNil(t, err)
  156. assert.Equal(t, ErrNotInFF, err)
  157. // Q+1 should give error
  158. r = new(big.Int).Add(cryptoConstants.Q, big.NewInt(1))
  159. e = [NLeafElems]*big.Int{z, z, r, r}
  160. _, err = AccountFromBigInts(e)
  161. assert.NotNil(t, err)
  162. assert.Equal(t, ErrNotInFF, err)
  163. }
  164. func TestAccountErrNumOverflowNonce(t *testing.T) {
  165. var sk babyjub.PrivateKey
  166. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  167. assert.Nil(t, err)
  168. pk := sk.Public()
  169. // check limit
  170. account := &Account{
  171. TokenID: TokenID(1),
  172. Nonce: Nonce(math.Pow(2, 40) - 1),
  173. Balance: big.NewInt(1000),
  174. PublicKey: pk,
  175. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  176. }
  177. _, err = account.Bytes()
  178. assert.Nil(t, err)
  179. // force value overflow
  180. account.Nonce = Nonce(math.Pow(2, 40))
  181. b, err := account.Bytes()
  182. assert.NotNil(t, err)
  183. assert.Equal(t, fmt.Errorf("%s Nonce", ErrNumOverflow), err)
  184. _, err = AccountFromBytes(b)
  185. assert.Nil(t, err)
  186. }
  187. func TestAccountErrNumOverflowBalance(t *testing.T) {
  188. var sk babyjub.PrivateKey
  189. _, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
  190. assert.Nil(t, err)
  191. pk := sk.Public()
  192. // check limit
  193. account := &Account{
  194. TokenID: TokenID(1),
  195. Nonce: Nonce(math.Pow(2, 40) - 1),
  196. Balance: new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil), big.NewInt(1)),
  197. PublicKey: pk,
  198. EthAddr: ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370"),
  199. }
  200. assert.Equal(t, "6277101735386680763835789423207666416102355444464034512895", account.Balance.String())
  201. _, err = account.Bytes()
  202. assert.Nil(t, err)
  203. // force value overflow
  204. account.Balance = new(big.Int).Exp(big.NewInt(2), big.NewInt(192), nil)
  205. assert.Equal(t, "6277101735386680763835789423207666416102355444464034512896", account.Balance.String())
  206. b, err := account.Bytes()
  207. assert.NotNil(t, err)
  208. assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
  209. _, err = AccountFromBytes(b)
  210. assert.Nil(t, err)
  211. b[56] = 1
  212. _, err = AccountFromBytes(b)
  213. assert.NotNil(t, err)
  214. assert.Equal(t, fmt.Errorf("%s Balance", ErrNumOverflow), err)
  215. }