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.

424 lines
33 KiB

  1. package statedb
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "math/big"
  8. "os"
  9. "testing"
  10. ethCommon "github.com/ethereum/go-ethereum/common"
  11. "github.com/hermeznetwork/hermez-node/common"
  12. "github.com/iden3/go-iden3-crypto/babyjub"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. )
  16. func printZKInputs(t *testing.T, zki *common.ZKInputs) {
  17. s, err := json.Marshal(zki)
  18. require.Nil(t, err)
  19. h, err := zki.HashGlobalData()
  20. require.Nil(t, err)
  21. fmt.Println("\nCopy&Paste into js circom test:\n let zkInput = JSON.parse(`" + string(s) + "`);")
  22. // fmt.Println("\nZKInputs json:\n echo '" + string(s) + "' | jq")
  23. fmt.Printf(`
  24. const output={
  25. hashGlobalInputs: "%s",
  26. };
  27. await circuit.assertOut(w, output);
  28. `, h.String())
  29. fmt.Println("")
  30. }
  31. func TestZKInputsHashTestVector0(t *testing.T) {
  32. dir, err := ioutil.TempDir("", "tmpdb")
  33. require.Nil(t, err)
  34. defer assert.Nil(t, os.RemoveAll(dir))
  35. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  36. assert.Nil(t, err)
  37. // same values than in the js test
  38. bjj0, err := common.BJJFromStringWithChecksum("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7")
  39. assert.Nil(t, err)
  40. l1Txs := []common.L1Tx{
  41. {
  42. FromIdx: 0,
  43. DepositAmount: big.NewInt(16000000),
  44. Amount: big.NewInt(0),
  45. TokenID: 1,
  46. FromBJJ: bjj0,
  47. FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
  48. ToIdx: 0,
  49. Type: common.TxTypeCreateAccountDeposit,
  50. UserOrigin: true,
  51. },
  52. }
  53. l2Txs := []common.PoolL2Tx{
  54. {
  55. FromIdx: 256,
  56. ToIdx: 256,
  57. TokenID: 1,
  58. Amount: big.NewInt(1000),
  59. Nonce: 0,
  60. Fee: 126,
  61. Type: common.TxTypeTransfer,
  62. },
  63. }
  64. ptc := ProcessTxsConfig{
  65. NLevels: 32,
  66. MaxFeeTx: 8,
  67. MaxTx: 32,
  68. MaxL1Tx: 16,
  69. }
  70. // skip first batch to do the test with BatchNum=1
  71. _, err = sdb.ProcessTxs(ptc, nil, nil, nil, nil)
  72. require.Nil(t, err)
  73. ptOut, err := sdb.ProcessTxs(ptc, nil, l1Txs, nil, l2Txs)
  74. require.Nil(t, err)
  75. // check expected account keys values from tx inputs
  76. acc, err := sdb.GetAccount(common.Idx(256))
  77. require.Nil(t, err)
  78. assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", acc.PublicKey.Compress().String())
  79. assert.Equal(t, "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf", acc.EthAddr.Hex())
  80. // check that there no exist more accounts
  81. _, err = sdb.GetAccount(common.Idx(257))
  82. require.NotNil(t, err)
  83. ptOut.ZKInputs.FeeIdxs[0] = common.Idx(256).BigInt()
  84. toHash, err := ptOut.ZKInputs.ToHashGlobalData()
  85. assert.Nil(t, err)
  86. // value from js test vector
  87. expectedToHash := "0000000000ff000000000100000000000000000000000000000000000000000000000000000000000000000015ba488d749f6b891d29d0bf3a72481ec812e4d4ecef2bf7a3fc64f3c010444200000000000000000000000000000000000000000000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d700000000000028a00000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000010003e87e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000001"
  88. // checks are splitted to find the difference easier
  89. assert.Equal(t, expectedToHash[:1000], hex.EncodeToString(toHash)[:1000])
  90. assert.Equal(t, expectedToHash[1000:2000], hex.EncodeToString(toHash)[1000:2000])
  91. assert.Equal(t, expectedToHash[2000:], hex.EncodeToString(toHash)[2000:])
  92. h, err := ptOut.ZKInputs.HashGlobalData()
  93. require.Nil(t, err)
  94. // value from js test vector
  95. assert.Equal(t, "4356692423721763303547321618014315464040324829724049399065961225345730555597", h.String())
  96. }
  97. func TestZKInputsHashTestVector1(t *testing.T) {
  98. dir, err := ioutil.TempDir("", "tmpdb")
  99. require.Nil(t, err)
  100. defer assert.Nil(t, os.RemoveAll(dir))
  101. sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
  102. assert.Nil(t, err)
  103. // same values than in the js test
  104. bjj0, err := common.BJJFromStringWithChecksum("21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d7")
  105. assert.Nil(t, err)
  106. bjj1, err := common.BJJFromStringWithChecksum("093985b1993d9f743f9d7d943ed56f38601cb8b196db025f79650c4007c3054d")
  107. assert.Nil(t, err)
  108. l1Txs := []common.L1Tx{
  109. {
  110. FromIdx: 0,
  111. DepositAmount: big.NewInt(16000000),
  112. Amount: big.NewInt(0),
  113. TokenID: 1,
  114. FromBJJ: bjj0,
  115. FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
  116. ToIdx: 0,
  117. Type: common.TxTypeCreateAccountDeposit,
  118. UserOrigin: true,
  119. },
  120. {
  121. FromIdx: 0,
  122. DepositAmount: big.NewInt(16000000),
  123. Amount: big.NewInt(0),
  124. TokenID: 1,
  125. FromBJJ: bjj1,
  126. FromEthAddr: ethCommon.HexToAddress("0x2b5ad5c4795c026514f8317c7a215e218dccd6cf"),
  127. ToIdx: 0,
  128. Type: common.TxTypeCreateAccountDeposit,
  129. UserOrigin: true,
  130. },
  131. }
  132. l2Txs := []common.PoolL2Tx{
  133. {
  134. FromIdx: 257,
  135. ToIdx: 256,
  136. TokenID: 1,
  137. Amount: big.NewInt(1000),
  138. Nonce: 0,
  139. Fee: 137,
  140. Type: common.TxTypeTransfer,
  141. },
  142. }
  143. ptc := ProcessTxsConfig{
  144. NLevels: 32,
  145. MaxFeeTx: 8,
  146. MaxTx: 32,
  147. MaxL1Tx: 16,
  148. }
  149. // skip first batch to do the test with BatchNum=1
  150. _, err = sdb.ProcessTxs(ptc, nil, nil, nil, nil)
  151. require.Nil(t, err)
  152. ptOut, err := sdb.ProcessTxs(ptc, nil, l1Txs, nil, l2Txs)
  153. require.Nil(t, err)
  154. // check expected account keys values from tx inputs
  155. acc, err := sdb.GetAccount(common.Idx(256))
  156. require.Nil(t, err)
  157. assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", acc.PublicKey.Compress().String())
  158. assert.Equal(t, "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf", acc.EthAddr.Hex())
  159. acc, err = sdb.GetAccount(common.Idx(257))
  160. require.Nil(t, err)
  161. assert.Equal(t, "4d05c307400c65795f02db96b1b81c60386fd53e947d9d3f749f3d99b1853909", acc.PublicKey.Compress().String())
  162. assert.Equal(t, "0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF", acc.EthAddr.Hex())
  163. // check that there no exist more accounts
  164. _, err = sdb.GetAccount(common.Idx(258))
  165. require.NotNil(t, err)
  166. ptOut.ZKInputs.FeeIdxs[0] = common.Idx(257).BigInt()
  167. toHash, err := ptOut.ZKInputs.ToHashGlobalData()
  168. assert.Nil(t, err)
  169. // value from js test vector
  170. expectedToHash := "0000000000ff0000000001010000000000000000000000000000000000000000000000000000000000000000304a3f3aef4f416cca887aab7265227449077627138345c2eb25bf8ff946b09500000000000000000000000000000000000000000000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d700000000000028a00000000000010000000000002b5ad5c4795c026514f8317c7a215e218dccd6cf093985b1993d9f743f9d7d943ed56f38601cb8b196db025f79650c4007c3054d00000000000028a000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001010000010003e889000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010100000000000000000000000000000000000000000000000000000000000000000001"
  171. // checks are splitted to find the difference easier
  172. assert.Equal(t, expectedToHash[:1000], hex.EncodeToString(toHash)[:1000])
  173. assert.Equal(t, expectedToHash[1000:2000], hex.EncodeToString(toHash)[1000:2000])
  174. assert.Equal(t, expectedToHash[2000:], hex.EncodeToString(toHash)[2000:])
  175. h, err := ptOut.ZKInputs.HashGlobalData()
  176. require.Nil(t, err)
  177. // value from js test vector
  178. assert.Equal(t, "20293112365009290386650039345314592436395562810005523677125576447132206192598", h.String())
  179. }
  180. // TestZKInputs0:
  181. // - 1 L1Tx
  182. // - 1 L2Tx without fees
  183. // no Coordinator Idxs defined to receive the fees
  184. func TestZKInputs0(t *testing.T) {
  185. dir, err := ioutil.TempDir("", "tmpdb")
  186. require.Nil(t, err)
  187. defer assert.Nil(t, os.RemoveAll(dir))
  188. nLevels := 16
  189. sdb, err := NewStateDB(dir, TypeBatchBuilder, nLevels)
  190. assert.Nil(t, err)
  191. // same values than in the js test
  192. // skJsHex is equivalent to the 0000...0001 js private key in commonjs
  193. skJsHex := "7eb258e61862aae75c6c1d1f7efae5006ffc9e4d5596a6ff95f3df4ea209ea7f"
  194. skJs, err := hex.DecodeString(skJsHex)
  195. require.Nil(t, err)
  196. var sk0 babyjub.PrivateKey
  197. copy(sk0[:], skJs)
  198. bjj0 := sk0.Public()
  199. assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", bjj0.String())
  200. l1Txs := []common.L1Tx{
  201. {
  202. FromIdx: 0,
  203. DepositAmount: big.NewInt(16000000),
  204. Amount: big.NewInt(0),
  205. TokenID: 1,
  206. FromBJJ: bjj0,
  207. FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
  208. ToIdx: 0,
  209. Type: common.TxTypeCreateAccountDeposit,
  210. UserOrigin: true,
  211. },
  212. }
  213. l2Txs := []common.PoolL2Tx{
  214. {
  215. FromIdx: 256,
  216. ToIdx: 256,
  217. TokenID: 1,
  218. Amount: big.NewInt(1000),
  219. Nonce: 0,
  220. Fee: 0,
  221. Type: common.TxTypeTransfer,
  222. },
  223. }
  224. toSign, err := l2Txs[0].HashToSign()
  225. require.Nil(t, err)
  226. sig := sk0.SignPoseidon(toSign)
  227. l2Txs[0].Signature = sig.Compress()
  228. ptc := ProcessTxsConfig{
  229. NLevels: uint32(nLevels),
  230. MaxTx: 3,
  231. MaxL1Tx: 2,
  232. MaxFeeTx: 2,
  233. }
  234. // skip first batch to do the test with BatchNum=1
  235. _, err = sdb.ProcessTxs(ptc, nil, nil, nil, nil)
  236. require.Nil(t, err)
  237. ptOut, err := sdb.ProcessTxs(ptc, nil, l1Txs, nil, l2Txs)
  238. require.Nil(t, err)
  239. // check expected account keys values from tx inputs
  240. acc, err := sdb.GetAccount(common.Idx(256))
  241. require.Nil(t, err)
  242. assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", acc.PublicKey.Compress().String())
  243. assert.Equal(t, "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf", acc.EthAddr.Hex())
  244. // check that there no exist more accounts
  245. _, err = sdb.GetAccount(common.Idx(257))
  246. require.NotNil(t, err)
  247. debug := false
  248. // debug = true
  249. if debug {
  250. printZKInputs(t, ptOut.ZKInputs)
  251. }
  252. toHash, err := ptOut.ZKInputs.ToHashGlobalData()
  253. require.Nil(t, err)
  254. assert.Equal(t, "0000000000ff0000000001000000000000000000000000000000000000000000000000000000000000000000071a61ed5a1ac052b0d1086a330c540b55318a07f6b7989573b9bbbb5380d1a900000000000000000000000000000000000000000000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d700000000000028a0000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100010003e8000000000000000000000000000000000001", hex.EncodeToString(toHash))
  255. h, err := ptOut.ZKInputs.HashGlobalData()
  256. require.Nil(t, err)
  257. assert.Equal(t, "10273997725311869157325593477103834352520120955255334791164183491223442653056", h.String())
  258. s, err := json.Marshal(ptOut.ZKInputs)
  259. require.Nil(t, err)
  260. // the 'expected' data has been checked with the circom circuits
  261. expected := `{"auxFromIdx":["256","0","0"],"auxToIdx":["0","0","0"],"ay1":["15238403086306505038849621710779816852318505119327426213168494964113886299863","15238403086306505038849621710779816852318505119327426213168494964113886299863","0"],"ay2":["0","15238403086306505038849621710779816852318505119327426213168494964113886299863","0"],"ay3":["0","0"],"balance1":["16000000","16000000","0"],"balance2":["0","15999000","0"],"balance3":["0","0"],"currentNumBatch":"1","ethAddr1":["721457446580647751014191829380889690493307935711","721457446580647751014191829380889690493307935711","0"],"ethAddr2":["0","721457446580647751014191829380889690493307935711","0"],"ethAddr3":["0","0"],"feeIdxs":["0","0"],"feePlanTokens":["0","0"],"fromBjjCompressed":[["1","1","1","0","1","0","1","1","0","1","1","0","0","0","1","0","0","1","0","0","0","0","0","1","1","1","1","1","0","0","1","0","1","0","1","1","1","1","1","0","0","1","0","1","0","0","0","0","1","0","1","0","0","0","1","1","0","0","1","0","0","0","0","0","0","1","0","1","0","0","1","0","1","1","1","0","1","0","1","0","1","1","1","1","1","1","0","0","1","0","0","0","1","0","1","0","0","1","0","0","1","1","0","1","0","0","0","1","1","1","1","0","0","0","1","1","0","1","0","1","1","0","1","0","1","1","0","1","1","0","1","1","0","1","1","0","0","1","0","0","0","0","0","1","1","1","0","1","1","1","0","0","0","0","1","1","0","1","1","1","1","0","0","1","1","1","0","0","1","0","1","0","1","0","1","0","1","0","1","1","1","0","0","0","1","0","1","1","1","0","0","0","1","1","0","1","1","1","1","0","1","1","1","0","1","1","1","1","1","1","1","0","1","1","0","0","1","1","0","1","0","0","0","1","0","0","0","1","0","1","1","0","1","0","0","0","0","1","0","1","0","0","0","0","1","1","0","1","1","0","0","0","0","1","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]],"fromEthAddr":["721457446580647751014191829380889690493307935711","0","0"],"fromIdx":["0","256","0"],"globalChainID":"0","imAccFeeOut":[["0","0"],["0","0"]],"imExitRoot":["0","0"],"imFinalAccFee":["0","0"],"imInitStateRootFee":"3212803832159212591526550848126062808026208063555125878245901046146545013161","imOnChain":["1","0"],"imOutIdx":["256","256"],"imStateRoot":["2999178063326948609414231200730958862089790119006655219527433501846141543551","3212803832159212591526550848126062808026208063555125878245901046146545013161"],"imStateRootFee":["3212803832159212591526550848126062808026208063555125878245901046146545013161"],"isOld0_1":["1","0","0"],"isOld0_2":["0","0","0"],"loadAmountF":["10400","0","0"],"maxNumBatch":["0","0","0"],"newAccount":["1","0","0"],"newExit":["0","0","0"],"nonce1":["0","0","0"],"nonce2":["0","1","0"],"nonce3":["0","0"],"oldKey1":["0","0","0"],"oldKey2":["0","0","0"],"oldLastIdx":"255","oldStateRoot":"0","oldValue1":["0","0","0"],"oldValue2":["0","0","0"],"onChain":["1","0","0"],"r8x":["0","13339118088097183560380359255316479838355724395928453439485234854234470298884","0"],"r8y":["0","12062876403986777372637801733000285846673058725183957648593976028822138986587","0"],"rqOffset":["0","0","0"],"rqToBjjAy":["0","0","0"],"rqToEthAddr":["0","0","0"],"rqTxCompressedDataV2":["0","0","0"],"s":["0","1429292460142966038093363510339656828866419125109324886747095533117015974779","0"],"siblings1":[["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]],"siblings2":[["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]],"siblings3":[["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]],"sign1":["0","0","0"],"sign2":["0","0","0"],"sign3":["0","0"],"toBjjAy":["0","0","0"],"toEthAddr":["0","0","0"],"toIdx":["0","256","0"],"tokenID1":["1","1","0"],"tokenID2":["0","1","0"],"tokenID3":["0","0"],"txCompressedData":["1461501637330902918203684832716283019659255211535","1483802382529433561627630154640673862706524841487","3322668559"],"txCompressedDataV2":["0","5271525021049092038181634317484288","0"]}`
  262. assert.Equal(t, expected, string(s))
  263. }
  264. // TestZKInputs1:
  265. // - 2 L1Tx of CreateAccountDeposit
  266. // - 1 L2Tx with fees
  267. func TestZKInputs1(t *testing.T) {
  268. dir, err := ioutil.TempDir("", "tmpdb")
  269. require.Nil(t, err)
  270. defer assert.Nil(t, os.RemoveAll(dir))
  271. nLevels := 16
  272. sdb, err := NewStateDB(dir, TypeBatchBuilder, nLevels)
  273. assert.Nil(t, err)
  274. // same values than in the js test
  275. // skJsHex is equivalent to the 0000...0001 js private key in commonjs
  276. sk0JsHex := "7eb258e61862aae75c6c1d1f7efae5006ffc9e4d5596a6ff95f3df4ea209ea7f"
  277. sk0Js, err := hex.DecodeString(sk0JsHex)
  278. require.Nil(t, err)
  279. var sk0 babyjub.PrivateKey
  280. copy(sk0[:], sk0Js)
  281. bjj0 := sk0.Public()
  282. assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", bjj0.String())
  283. sk1JsHex := "c005700f76f4b4cec710805c21595688648524df0a9d467afae537b7a7118819"
  284. sk1Js, err := hex.DecodeString(sk1JsHex)
  285. require.Nil(t, err)
  286. var sk1 babyjub.PrivateKey
  287. copy(sk1[:], sk1Js)
  288. bjj1 := sk1.Public()
  289. assert.Equal(t, "4d05c307400c65795f02db96b1b81c60386fd53e947d9d3f749f3d99b1853909", bjj1.String())
  290. l1Txs := []common.L1Tx{
  291. {
  292. FromIdx: 0,
  293. DepositAmount: big.NewInt(16000000),
  294. Amount: big.NewInt(0),
  295. TokenID: 1,
  296. FromBJJ: bjj0,
  297. FromEthAddr: ethCommon.HexToAddress("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"),
  298. ToIdx: 0,
  299. Type: common.TxTypeCreateAccountDeposit,
  300. UserOrigin: true,
  301. },
  302. {
  303. FromIdx: 0,
  304. DepositAmount: big.NewInt(16000000),
  305. Amount: big.NewInt(0),
  306. TokenID: 1,
  307. FromBJJ: bjj1,
  308. FromEthAddr: ethCommon.HexToAddress("0x2b5ad5c4795c026514f8317c7a215e218dccd6cf"),
  309. ToIdx: 0,
  310. Type: common.TxTypeCreateAccountDeposit,
  311. UserOrigin: true,
  312. },
  313. }
  314. l2Txs := []common.PoolL2Tx{
  315. {
  316. FromIdx: 256,
  317. ToIdx: 256,
  318. TokenID: 1,
  319. Amount: big.NewInt(1000),
  320. Nonce: 0,
  321. Fee: 126,
  322. Type: common.TxTypeTransfer,
  323. },
  324. }
  325. toSign, err := l2Txs[0].HashToSign()
  326. require.Nil(t, err)
  327. sig := sk0.SignPoseidon(toSign)
  328. l2Txs[0].Signature = sig.Compress()
  329. ptc := ProcessTxsConfig{
  330. NLevels: uint32(nLevels),
  331. MaxTx: 3,
  332. MaxL1Tx: 2,
  333. MaxFeeTx: 2,
  334. }
  335. // skip first batch to do the test with BatchNum=1
  336. _, err = sdb.ProcessTxs(ptc, nil, nil, nil, nil)
  337. require.Nil(t, err)
  338. coordIdxs := []common.Idx{257}
  339. ptOut, err := sdb.ProcessTxs(ptc, coordIdxs, l1Txs, nil, l2Txs)
  340. require.Nil(t, err)
  341. // check expected account keys values from tx inputs
  342. acc, err := sdb.GetAccount(common.Idx(256))
  343. require.Nil(t, err)
  344. assert.Equal(t, "d746824f7d0ac5044a573f51b278acb56d823bec39551d1d7bf7378b68a1b021", acc.PublicKey.Compress().String())
  345. assert.Equal(t, "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf", acc.EthAddr.Hex())
  346. assert.Equal(t, "15999899", acc.Balance.String())
  347. acc, err = sdb.GetAccount(common.Idx(257))
  348. require.Nil(t, err)
  349. assert.Equal(t, "4d05c307400c65795f02db96b1b81c60386fd53e947d9d3f749f3d99b1853909", acc.PublicKey.Compress().String())
  350. assert.Equal(t, "0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF", acc.EthAddr.Hex())
  351. assert.Equal(t, "16000101", acc.Balance.String())
  352. // check that there no exist more accounts
  353. _, err = sdb.GetAccount(common.Idx(258))
  354. require.NotNil(t, err)
  355. debug := false
  356. // debug = true
  357. if debug {
  358. printZKInputs(t, ptOut.ZKInputs)
  359. }
  360. toHash, err := ptOut.ZKInputs.ToHashGlobalData()
  361. require.Nil(t, err)
  362. assert.Equal(t, "0000000000ff0000000001010000000000000000000000000000000000000000000000000000000000000000036d607b790b93bb1768d5390803b5a4a1f77e46755f57900930b14454faf95c00000000000000000000000000000000000000000000000000000000000000007e5f4552091a69125d5dfcb7b8c2659029395bdf21b0a1688b37f77b1d1d5539ec3b826db5ac78b2513f574a04c50a7d4f8246d700000000000028a00000000000010000000000002b5ad5c4795c026514f8317c7a215e218dccd6cf093985b1993d9f743f9d7d943ed56f38601cb8b196db025f79650c4007c3054d00000000000028a000000000000100000000000000000000000000000000000000000100010003e87e01010000000000000001", hex.EncodeToString(toHash))
  363. h, err := ptOut.ZKInputs.HashGlobalData()
  364. require.Nil(t, err)
  365. assert.Equal(t, "11039366437749764484706691779656824178800407917434257418748834397596260744468", h.String())
  366. s, err := json.Marshal(ptOut.ZKInputs)
  367. require.Nil(t, err)
  368. // the 'expected' data has been checked with the circom circuits
  369. expected := `{"auxFromIdx":["256","257","0"],"auxToIdx":["0","0","0"],"ay1":["15238403086306505038849621710779816852318505119327426213168494964113886299863","4172448640254579435434214421479401747968866348490029667576411173067925161293","15238403086306505038849621710779816852318505119327426213168494964113886299863"],"ay2":["0","0","15238403086306505038849621710779816852318505119327426213168494964113886299863"],"ay3":["4172448640254579435434214421479401747968866348490029667576411173067925161293","0"],"balance1":["16000000","16000000","16000000"],"balance2":["0","0","15998899"],"balance3":["16000000","0"],"currentNumBatch":"1","ethAddr1":["721457446580647751014191829380889690493307935711","247512291986854564435551364600938690683113101007","721457446580647751014191829380889690493307935711"],"ethAddr2":["0","0","721457446580647751014191829380889690493307935711"],"ethAddr3":["247512291986854564435551364600938690683113101007","0"],"feeIdxs":["257","0"],"feePlanTokens":["1","0"],"fromBjjCompressed":[["1","1","1","0","1","0","1","1","0","1","1","0","0","0","1","0","0","1","0","0","0","0","0","1","1","1","1","1","0","0","1","0","1","0","1","1","1","1","1","0","0","1","0","1","0","0","0","0","1","0","1","0","0","0","1","1","0","0","1","0","0","0","0","0","0","1","0","1","0","0","1","0","1","1","1","0","1","0","1","0","1","1","1","1","1","1","0","0","1","0","0","0","1","0","1","0","0","1","0","0","1","1","0","1","0","0","0","1","1","1","1","0","0","0","1","1","0","1","0","1","1","0","1","0","1","1","0","1","1","0","1","1","0","1","1","0","0","1","0","0","0","0","0","1","1","1","0","1","1","1","0","0","0","0","1","1","0","1","1","1","1","0","0","1","1","1","0","0","1","0","1","0","1","0","1","0","1","0","1","1","1","0","0","0","1","0","1","1","1","0","0","0","1","1","0","1","1","1","1","0","1","1","1","0","1","1","1","1","1","1","1","0","1","1","0","0","1","1","0","1","0","0","0","1","0","0","0","1","0","1","1","0","1","0","0","0","0","1","0","1","0","0","0","0","1","1","0","1","1","0","0","0","0","1","0","0"],["1","0","1","1","0","0","1","0","1","0","1","0","0","0","0","0","1","1","0","0","0","0","1","1","1","1","1","0","0","0","0","0","0","0","0","0","0","0","1","0","0","0","1","1","0","0","0","0","1","0","1","0","0","1","1","0","1","0","0","1","1","1","1","0","1","1","1","1","1","0","1","0","0","1","0","0","0","0","0","0","1","1","0","1","1","0","1","1","0","1","1","0","1","0","0","1","1","0","0","0","1","1","0","1","0","0","0","1","1","1","0","1","0","0","1","1","1","0","0","0","0","0","0","0","0","1","1","0","0","0","0","1","1","1","0","0","1","1","1","1","0","1","1","0","1","0","1","0","1","0","1","1","0","1","1","1","1","1","0","0","0","0","1","0","1","0","0","1","1","0","1","1","1","1","1","0","1","0","1","1","1","0","0","1","1","1","1","1","1","1","0","0","0","0","1","0","1","1","1","0","1","1","1","1","1","0","0","1","1","0","1","1","1","1","0","0","1","0","0","1","1","0","0","1","1","0","0","0","1","1","0","1","1","0","1","0","0","0","0","1","1","0","0","1","1","1","0","0","1","0","0","1","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]],"fromEthAddr":["721457446580647751014191829380889690493307935711","247512291986854564435551364600938690683113101007","0"],"fromIdx":["0","0","256"],"globalChainID":"0","imAccFeeOut":[["0","0"],["0","0"]],"imExitRoot":["0","0"],"imFinalAccFee":["101","0"],"imInitStateRootFee":"10660728613879129016661596154319504485937170756181586060561759832613498905432","imOnChain":["1","1"],"imOutIdx":["256","257"],"imStateRoot":["2999178063326948609414231200730958862089790119006655219527433501846141543551","13160175861809095962915811919507877524206523306071085047160493107056995190544"],"imStateRootFee":["1550190772280924834409423240867892593473592863918771212295716656664630983004"],"isOld0_1":["1","0","0"],"isOld0_2":["0","0","0"],"loadAmountF":["10400","10400","0"],"maxNumBatch":["0","0","0"],"newAccount":["1","1","0"],"newExit":["0","0","0"],"nonce1":["0","0","0"],"nonce2":["0","0","1"],"nonce3":["0","0"],"oldKey1":["0","256","0"],"oldKey2":["0","0","0"],"oldLastIdx":"255","oldStateRoot":"0","oldValue1":["0","9733782510199048326382833205201407219982604211594942097825192094127807440165","0"],"oldValue2":["0","0","0"],"onChain":["1","1","0"],"r8x":["0","0","16795818329006817411605347777151783287113601795569690834743955502344582990059"],"r8y":["0","0","21153110871938011270027204820675751452345817312713349225139208384264949654114"],"rqOffset":["0","0","0"],"rqToBjjAy":["0","0","0"],"rqToEthAddr":["0","0","0"],"rqTxCompressedDataV2":["0","0","0"],"s":["0","0","1550352334297856444344240780544275542131334387040150478134835458055364079268"],"siblings1":[["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["6975841694765113541634698345295957238501610055097872059913911260522365532165","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]],"siblings2":[["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["6975841694765113541634698345295957238501610055097872059913911260522365532165","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]],"siblings3":[["9827704113668630072730115158977131501210702363656902211840117643154933433410","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"],["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]],"sign1":["0","0","0"],"sign2":["0","0","0"],"sign3":["0","0"],"toBjjAy":["0","0","0"],"toEthAddr":["0","0","0"],"toIdx":["0","0","256"],"tokenID1":["1","1","1"],"tokenID2":["0","0","1"],"tokenID3":["1","0"],"txCompressedData":["1461501637330902918203684832716283019659255211535","1461501637330902918203684832716283019659255211535","869620039695611037216780722449287736442401358123623346624340758723552783"],"txCompressedDataV2":["0","0","3089511010385631938450432878260044363267416251956459471104"]}`
  370. assert.Equal(t, expected, string(s))
  371. }