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.

540 lines
19 KiB

  1. // Package common zk.go contains all the common data structures used at the
  2. // hermez-node, zk.go contains the zkSnark inputs used to generate the proof
  3. package common
  4. import (
  5. "crypto/sha256"
  6. "encoding/binary"
  7. "encoding/json"
  8. "fmt"
  9. "math/big"
  10. "github.com/hermeznetwork/hermez-node/log"
  11. "github.com/hermeznetwork/tracerr"
  12. cryptoConstants "github.com/iden3/go-iden3-crypto/constants"
  13. "github.com/iden3/go-merkletree"
  14. "github.com/mitchellh/mapstructure"
  15. )
  16. // ZKMetadata contains ZKInputs metadata that is not used directly in the
  17. // ZKInputs result, but to calculate values for Hash check
  18. type ZKMetadata struct {
  19. // Circuit parameters
  20. // absolute maximum of L1 or L2 transactions allowed
  21. MaxLevels uint32
  22. // merkle tree depth
  23. NLevels uint32
  24. // absolute maximum of L1 transaction allowed
  25. MaxL1Tx uint32
  26. // total txs allowed
  27. MaxTx uint32
  28. // Maximum number of Idxs where Fees can be send in a batch (currently
  29. // is constant for all circuits: 64)
  30. MaxFeeIdxs uint32
  31. L1TxsData [][]byte
  32. L1TxsDataAvailability [][]byte
  33. L2TxsData [][]byte
  34. ChainID uint16
  35. NewLastIdxRaw Idx
  36. NewStateRootRaw *merkletree.Hash
  37. NewExitRootRaw *merkletree.Hash
  38. }
  39. // ZKInputs represents the inputs that will be used to generate the zkSNARK
  40. // proof
  41. type ZKInputs struct {
  42. Metadata ZKMetadata `json:"-"`
  43. //
  44. // General
  45. //
  46. // CurrentNumBatch is the current batch number processed
  47. CurrentNumBatch *big.Int `json:"currentNumBatch"` // uint32
  48. // inputs for final `hashGlobalInputs`
  49. // OldLastIdx is the last index assigned to an account
  50. OldLastIdx *big.Int `json:"oldLastIdx"` // uint64 (max nLevels bits)
  51. // OldStateRoot is the current state merkle tree root
  52. OldStateRoot *big.Int `json:"oldStateRoot"` // Hash
  53. // GlobalChainID is the blockchain ID (0 for Ethereum mainnet). This
  54. // value can be get from the smart contract.
  55. GlobalChainID *big.Int `json:"globalChainID"` // uint16
  56. // FeeIdxs is an array of merkle tree indexes (Idxs) where the
  57. // coordinator will receive the accumulated fees
  58. FeeIdxs []*big.Int `json:"feeIdxs"` // uint64 (max nLevels bits), len: [maxFeeIdxs]
  59. // accumulate fees
  60. // FeePlanTokens contains all the tokenIDs for which the fees are being
  61. // accumulated and those fees accumulated will be paid to the FeeIdxs
  62. // array. The order of FeeIdxs & FeePlanTokens & State3 must match.
  63. // Coordinator fees are processed correlated such as:
  64. // [FeePlanTokens[i], FeeIdxs[i]]
  65. FeePlanTokens []*big.Int `json:"feePlanTokens"` // uint32 (max nLevels bits), len: [maxFeeIdxs]
  66. //
  67. // Txs (L1&L2)
  68. //
  69. // transaction L1-L2
  70. // TxCompressedData
  71. TxCompressedData []*big.Int `json:"txCompressedData"` // big.Int (max 251 bits), len: [maxTx]
  72. // TxCompressedDataV2, only used in L2Txs, in L1Txs is set to 0
  73. TxCompressedDataV2 []*big.Int `json:"txCompressedDataV2"` // big.Int (max 193 bits), len: [maxTx]
  74. // MaxNumBatch is the maximum allowed batch number when the transaction
  75. // can be processed
  76. MaxNumBatch []*big.Int `json:"maxNumBatch"` // big.Int (max 32 bits), len: [maxTx]
  77. // FromIdx
  78. FromIdx []*big.Int `json:"fromIdx"` // uint64 (max nLevels bits), len: [maxTx]
  79. // AuxFromIdx is the Idx of the new created account which is
  80. // consequence of a L1CreateAccountTx
  81. AuxFromIdx []*big.Int `json:"auxFromIdx"` // uint64 (max nLevels bits), len: [maxTx]
  82. // ToIdx
  83. ToIdx []*big.Int `json:"toIdx"` // uint64 (max nLevels bits), len: [maxTx]
  84. // AuxToIdx is the Idx of the Tx that has 'toIdx==0', is the
  85. // coordinator who will find which Idx corresponds to the 'toBJJAy' or
  86. // 'toEthAddr'
  87. AuxToIdx []*big.Int `json:"auxToIdx"` // uint64 (max nLevels bits), len: [maxTx]
  88. // ToBJJAy
  89. ToBJJAy []*big.Int `json:"toBjjAy"` // big.Int, len: [maxTx]
  90. // ToEthAddr
  91. ToEthAddr []*big.Int `json:"toEthAddr"` // ethCommon.Address, len: [maxTx]
  92. // AmountF encoded as float40
  93. AmountF []*big.Int `json:"amountF"` // uint40 len: [maxTx]
  94. // OnChain determines if is L1 (1/true) or L2 (0/false)
  95. OnChain []*big.Int `json:"onChain"` // bool, len: [maxTx]
  96. //
  97. // Txs/L1Txs
  98. //
  99. // NewAccount boolean (0/1) flag set 'true' when L1 tx creates a new
  100. // account (fromIdx==0)
  101. NewAccount []*big.Int `json:"newAccount"` // bool, len: [maxTx]
  102. // DepositAmountF encoded as float40
  103. DepositAmountF []*big.Int `json:"loadAmountF"` // uint40, len: [maxTx]
  104. // FromEthAddr
  105. FromEthAddr []*big.Int `json:"fromEthAddr"` // ethCommon.Address, len: [maxTx]
  106. // FromBJJCompressed boolean encoded where each value is a *big.Int
  107. FromBJJCompressed [][256]*big.Int `json:"fromBjjCompressed"` // bool array, len: [maxTx][256]
  108. //
  109. // Txs/L2Txs
  110. //
  111. // RqOffset relative transaction position to be linked. Used to perform
  112. // atomic transactions.
  113. RqOffset []*big.Int `json:"rqOffset"` // uint8 (max 3 bits), len: [maxTx]
  114. // transaction L2 request data
  115. // RqTxCompressedDataV2 big.Int (max 251 bits), len: [maxTx]
  116. RqTxCompressedDataV2 []*big.Int `json:"rqTxCompressedDataV2"`
  117. // RqToEthAddr
  118. RqToEthAddr []*big.Int `json:"rqToEthAddr"` // ethCommon.Address, len: [maxTx]
  119. // RqToBJJAy
  120. RqToBJJAy []*big.Int `json:"rqToBjjAy"` // big.Int, len: [maxTx]
  121. // transaction L2 signature
  122. // S
  123. S []*big.Int `json:"s"` // big.Int, len: [maxTx]
  124. // R8x
  125. R8x []*big.Int `json:"r8x"` // big.Int, len: [maxTx]
  126. // R8y
  127. R8y []*big.Int `json:"r8y"` // big.Int, len: [maxTx]
  128. //
  129. // State MerkleTree Leafs transitions
  130. //
  131. // state 1, value of the sender (from) account leaf. The values at the
  132. // moment pre-smtprocessor of the update (before updating the Sender
  133. // leaf).
  134. TokenID1 []*big.Int `json:"tokenID1"` // uint32, len: [maxTx]
  135. Nonce1 []*big.Int `json:"nonce1"` // uint64 (max 40 bits), len: [maxTx]
  136. Sign1 []*big.Int `json:"sign1"` // bool, len: [maxTx]
  137. Ay1 []*big.Int `json:"ay1"` // big.Int, len: [maxTx]
  138. Balance1 []*big.Int `json:"balance1"` // big.Int (max 192 bits), len: [maxTx]
  139. EthAddr1 []*big.Int `json:"ethAddr1"` // ethCommon.Address, len: [maxTx]
  140. Siblings1 [][]*big.Int `json:"siblings1"` // big.Int, len: [maxTx][nLevels + 1]
  141. // Required for inserts and deletes, values of the CircomProcessorProof
  142. // (smt insert proof)
  143. IsOld0_1 []*big.Int `json:"isOld0_1"` // bool, len: [maxTx]
  144. OldKey1 []*big.Int `json:"oldKey1"` // uint64 (max 40 bits), len: [maxTx]
  145. OldValue1 []*big.Int `json:"oldValue1"` // Hash, len: [maxTx]
  146. // state 2, value of the receiver (to) account leaf. The values at the
  147. // moment pre-smtprocessor of the update (before updating the Receiver
  148. // leaf).
  149. // If Tx is an Exit (tx.ToIdx=1), state 2 is used for the Exit Merkle
  150. // Proof of the Exit MerkleTree.
  151. TokenID2 []*big.Int `json:"tokenID2"` // uint32, len: [maxTx]
  152. Nonce2 []*big.Int `json:"nonce2"` // uint64 (max 40 bits), len: [maxTx]
  153. Sign2 []*big.Int `json:"sign2"` // bool, len: [maxTx]
  154. Ay2 []*big.Int `json:"ay2"` // big.Int, len: [maxTx]
  155. Balance2 []*big.Int `json:"balance2"` // big.Int (max 192 bits), len: [maxTx]
  156. EthAddr2 []*big.Int `json:"ethAddr2"` // ethCommon.Address, len: [maxTx]
  157. Siblings2 [][]*big.Int `json:"siblings2"` // big.Int, len: [maxTx][nLevels + 1]
  158. // NewExit determines if an exit transaction has to create a new leaf
  159. // in the exit tree. If already exists an exit leaf of an account in
  160. // the ExitTree, there is no 'new leaf' creation and 'NewExit' for that
  161. // tx is 0 (if is an 'insert' in the tree, NewExit=1, if is an 'update'
  162. // of an existing leaf, NewExit=0).
  163. NewExit []*big.Int `json:"newExit"` // bool, len: [maxTx]
  164. // Required for inserts and deletes, values of the CircomProcessorProof
  165. // (smt insert proof)
  166. IsOld0_2 []*big.Int `json:"isOld0_2"` // bool, len: [maxTx]
  167. OldKey2 []*big.Int `json:"oldKey2"` // uint64 (max 40 bits), len: [maxTx]
  168. OldValue2 []*big.Int `json:"oldValue2"` // Hash, len: [maxTx]
  169. // state 3, fee leafs states, value of the account leaf receiver of the
  170. // Fees fee tx. The values at the moment pre-smtprocessor of the update
  171. // (before updating the Receiver leaf).
  172. // The order of FeeIdxs & FeePlanTokens & State3 must match.
  173. TokenID3 []*big.Int `json:"tokenID3"` // uint32, len: [maxFeeIdxs]
  174. Nonce3 []*big.Int `json:"nonce3"` // uint64 (max 40 bits), len: [maxFeeIdxs]
  175. Sign3 []*big.Int `json:"sign3"` // bool, len: [maxFeeIdxs]
  176. Ay3 []*big.Int `json:"ay3"` // big.Int, len: [maxFeeIdxs]
  177. Balance3 []*big.Int `json:"balance3"` // big.Int (max 192 bits), len: [maxFeeIdxs]
  178. EthAddr3 []*big.Int `json:"ethAddr3"` // ethCommon.Address, len: [maxFeeIdxs]
  179. Siblings3 [][]*big.Int `json:"siblings3"` // Hash, len: [maxFeeIdxs][nLevels + 1]
  180. //
  181. // Intermediate States
  182. //
  183. // Intermediate States to parallelize witness computation
  184. // Note: the Intermediate States (IS) of the last transaction does not
  185. // exist. Meaning that transaction 3 (4th) will fill the parameters
  186. // FromIdx[3] and ISOnChain[3], but last transaction (maxTx-1) will fill
  187. // FromIdx[maxTx-1] but will not fill ISOnChain. That's why IS have
  188. // length of maxTx-1, while the other parameters have length of maxTx.
  189. // Last transaction does not need intermediate state since its output
  190. // will not be used.
  191. // decode-tx
  192. // ISOnChain indicates if tx is L1 (true (1)) or L2 (false (0))
  193. ISOnChain []*big.Int `json:"imOnChain"` // bool, len: [maxTx - 1]
  194. // ISOutIdx current index account for each Tx
  195. // Contains the index of the created account in case that the tx is of
  196. // account creation type.
  197. ISOutIdx []*big.Int `json:"imOutIdx"` // uint64 (max nLevels bits), len: [maxTx - 1]
  198. // rollup-tx
  199. // ISStateRoot root at the moment of the Tx (once processed), the state
  200. // root value once the Tx is processed into the state tree
  201. ISStateRoot []*big.Int `json:"imStateRoot"` // Hash, len: [maxTx - 1]
  202. // ISExitTree root at the moment (once processed) of the Tx the value
  203. // once the Tx is processed into the exit tree
  204. ISExitRoot []*big.Int `json:"imExitRoot"` // Hash, len: [maxTx - 1]
  205. // ISAccFeeOut accumulated fees once the Tx is processed. Contains the
  206. // array of FeeAccount Balances at each moment of each Tx processed.
  207. ISAccFeeOut [][]*big.Int `json:"imAccFeeOut"` // big.Int, len: [maxTx - 1][maxFeeIdxs]
  208. // fee-tx:
  209. // ISStateRootFee root at the moment of the Tx (once processed), the
  210. // state root value once the Tx is processed into the state tree
  211. ISStateRootFee []*big.Int `json:"imStateRootFee"` // Hash, len: [maxFeeIdxs - 1]
  212. // ISInitStateRootFee state root once all L1-L2 tx are processed
  213. // (before computing the fees-tx)
  214. ISInitStateRootFee *big.Int `json:"imInitStateRootFee"` // Hash
  215. // ISFinalAccFee final accumulated fees (before computing the fees-tx).
  216. // Contains the final values of the ISAccFeeOut parameter
  217. ISFinalAccFee []*big.Int `json:"imFinalAccFee"` // big.Int, len: [maxFeeIdxs]
  218. }
  219. func bigIntsToStrings(v interface{}) interface{} {
  220. switch c := v.(type) {
  221. case *big.Int:
  222. return c.String()
  223. case []*big.Int:
  224. r := make([]interface{}, len(c))
  225. for i := range c {
  226. r[i] = bigIntsToStrings(c[i])
  227. }
  228. return r
  229. case [256]*big.Int:
  230. r := make([]interface{}, len(c))
  231. for i := range c {
  232. r[i] = bigIntsToStrings(c[i])
  233. }
  234. return r
  235. case [][]*big.Int:
  236. r := make([]interface{}, len(c))
  237. for i := range c {
  238. r[i] = bigIntsToStrings(c[i])
  239. }
  240. return r
  241. case [][256]*big.Int:
  242. r := make([]interface{}, len(c))
  243. for i := range c {
  244. r[i] = bigIntsToStrings(c[i])
  245. }
  246. return r
  247. case map[string]interface{}:
  248. // avoid printing a warning when there is a struct type
  249. default:
  250. log.Warnf("bigIntsToStrings unexpected type: %T\n", v)
  251. }
  252. return nil
  253. }
  254. // MarshalJSON implements the json marshaler for ZKInputs
  255. func (z ZKInputs) MarshalJSON() ([]byte, error) {
  256. var m map[string]interface{}
  257. dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
  258. TagName: "json",
  259. Result: &m,
  260. })
  261. if err != nil {
  262. return nil, tracerr.Wrap(err)
  263. }
  264. err = dec.Decode(z)
  265. if err != nil {
  266. return nil, tracerr.Wrap(err)
  267. }
  268. for k, v := range m {
  269. m[k] = bigIntsToStrings(v)
  270. }
  271. return json.Marshal(m)
  272. }
  273. // NewZKInputs returns a pointer to an initialized struct of ZKInputs
  274. func NewZKInputs(chainID uint16, maxTx, maxL1Tx, maxFeeIdxs, nLevels uint32,
  275. currentNumBatch *big.Int) *ZKInputs {
  276. zki := &ZKInputs{}
  277. zki.Metadata.MaxFeeIdxs = maxFeeIdxs
  278. zki.Metadata.MaxLevels = uint32(48) //nolint:gomnd
  279. zki.Metadata.NLevels = nLevels
  280. zki.Metadata.MaxL1Tx = maxL1Tx
  281. zki.Metadata.MaxTx = maxTx
  282. zki.Metadata.ChainID = chainID
  283. // General
  284. zki.CurrentNumBatch = currentNumBatch
  285. zki.OldLastIdx = big.NewInt(0)
  286. zki.OldStateRoot = big.NewInt(0)
  287. zki.GlobalChainID = big.NewInt(int64(chainID))
  288. zki.FeeIdxs = newSlice(maxFeeIdxs)
  289. zki.FeePlanTokens = newSlice(maxFeeIdxs)
  290. // Txs
  291. zki.TxCompressedData = newSlice(maxTx)
  292. zki.TxCompressedDataV2 = newSlice(maxTx)
  293. zki.MaxNumBatch = newSlice(maxTx)
  294. zki.FromIdx = newSlice(maxTx)
  295. zki.AuxFromIdx = newSlice(maxTx)
  296. zki.ToIdx = newSlice(maxTx)
  297. zki.AuxToIdx = newSlice(maxTx)
  298. zki.ToBJJAy = newSlice(maxTx)
  299. zki.ToEthAddr = newSlice(maxTx)
  300. zki.AmountF = newSlice(maxTx)
  301. zki.OnChain = newSlice(maxTx)
  302. zki.NewAccount = newSlice(maxTx)
  303. // L1
  304. zki.DepositAmountF = newSlice(maxTx)
  305. zki.FromEthAddr = newSlice(maxTx)
  306. zki.FromBJJCompressed = make([][256]*big.Int, maxTx)
  307. for i := 0; i < len(zki.FromBJJCompressed); i++ {
  308. // zki.FromBJJCompressed[i] = newSlice(256)
  309. for j := 0; j < 256; j++ {
  310. zki.FromBJJCompressed[i][j] = big.NewInt(0)
  311. }
  312. }
  313. // L2
  314. zki.RqOffset = newSlice(maxTx)
  315. zki.RqTxCompressedDataV2 = newSlice(maxTx)
  316. zki.RqToEthAddr = newSlice(maxTx)
  317. zki.RqToBJJAy = newSlice(maxTx)
  318. zki.S = newSlice(maxTx)
  319. zki.R8x = newSlice(maxTx)
  320. zki.R8y = newSlice(maxTx)
  321. // State MerkleTree Leafs transitions
  322. zki.TokenID1 = newSlice(maxTx)
  323. zki.Nonce1 = newSlice(maxTx)
  324. zki.Sign1 = newSlice(maxTx)
  325. zki.Ay1 = newSlice(maxTx)
  326. zki.Balance1 = newSlice(maxTx)
  327. zki.EthAddr1 = newSlice(maxTx)
  328. zki.Siblings1 = make([][]*big.Int, maxTx)
  329. for i := 0; i < len(zki.Siblings1); i++ {
  330. zki.Siblings1[i] = newSlice(nLevels + 1)
  331. }
  332. zki.IsOld0_1 = newSlice(maxTx)
  333. zki.OldKey1 = newSlice(maxTx)
  334. zki.OldValue1 = newSlice(maxTx)
  335. zki.TokenID2 = newSlice(maxTx)
  336. zki.Nonce2 = newSlice(maxTx)
  337. zki.Sign2 = newSlice(maxTx)
  338. zki.Ay2 = newSlice(maxTx)
  339. zki.Balance2 = newSlice(maxTx)
  340. zki.EthAddr2 = newSlice(maxTx)
  341. zki.Siblings2 = make([][]*big.Int, maxTx)
  342. for i := 0; i < len(zki.Siblings2); i++ {
  343. zki.Siblings2[i] = newSlice(nLevels + 1)
  344. }
  345. zki.NewExit = newSlice(maxTx)
  346. zki.IsOld0_2 = newSlice(maxTx)
  347. zki.OldKey2 = newSlice(maxTx)
  348. zki.OldValue2 = newSlice(maxTx)
  349. zki.TokenID3 = newSlice(maxFeeIdxs)
  350. zki.Nonce3 = newSlice(maxFeeIdxs)
  351. zki.Sign3 = newSlice(maxFeeIdxs)
  352. zki.Ay3 = newSlice(maxFeeIdxs)
  353. zki.Balance3 = newSlice(maxFeeIdxs)
  354. zki.EthAddr3 = newSlice(maxFeeIdxs)
  355. zki.Siblings3 = make([][]*big.Int, maxFeeIdxs)
  356. for i := 0; i < len(zki.Siblings3); i++ {
  357. zki.Siblings3[i] = newSlice(nLevels + 1)
  358. }
  359. // Intermediate States
  360. zki.ISOnChain = newSlice(maxTx - 1)
  361. zki.ISOutIdx = newSlice(maxTx - 1)
  362. zki.ISStateRoot = newSlice(maxTx - 1)
  363. zki.ISExitRoot = newSlice(maxTx - 1)
  364. zki.ISAccFeeOut = make([][]*big.Int, maxTx-1)
  365. for i := 0; i < len(zki.ISAccFeeOut); i++ {
  366. zki.ISAccFeeOut[i] = newSlice(maxFeeIdxs)
  367. }
  368. zki.ISStateRootFee = newSlice(maxFeeIdxs - 1)
  369. zki.ISInitStateRootFee = big.NewInt(0)
  370. zki.ISFinalAccFee = newSlice(maxFeeIdxs)
  371. return zki
  372. }
  373. // newSlice returns a []*big.Int slice of length n with values initialized at
  374. // 0.
  375. // Is used to initialize all *big.Ints of the ZKInputs data structure, so when
  376. // the transactions are processed and the ZKInputs filled, there is no need to
  377. // set all the elements, and if a transaction does not use a parameter, can be
  378. // leaved as it is in the ZKInputs, as will be 0, so later when using the
  379. // ZKInputs to generate the zkSnark proof there is no 'nil'/'null' values.
  380. func newSlice(n uint32) []*big.Int {
  381. s := make([]*big.Int, n)
  382. for i := 0; i < len(s); i++ {
  383. s[i] = big.NewInt(0)
  384. }
  385. return s
  386. }
  387. // HashGlobalData returns the HashGlobalData
  388. func (z ZKInputs) HashGlobalData() (*big.Int, error) {
  389. b, err := z.ToHashGlobalData()
  390. if err != nil {
  391. return nil, tracerr.Wrap(err)
  392. }
  393. h := sha256.New()
  394. _, err = h.Write(b)
  395. if err != nil {
  396. return nil, tracerr.Wrap(err)
  397. }
  398. r := new(big.Int).SetBytes(h.Sum(nil))
  399. v := r.Mod(r, cryptoConstants.Q)
  400. return v, nil
  401. }
  402. // ToHashGlobalData returns the data to be hashed in the method HashGlobalData
  403. func (z ZKInputs) ToHashGlobalData() ([]byte, error) {
  404. var b []byte
  405. bytesMaxLevels := int(z.Metadata.MaxLevels / 8) //nolint:gomnd
  406. bytesNLevels := int(z.Metadata.NLevels / 8) //nolint:gomnd
  407. // [MAX_NLEVELS bits] oldLastIdx
  408. oldLastIdx := make([]byte, bytesMaxLevels)
  409. oldLastIdxBytes := z.OldLastIdx.Bytes()
  410. copy(oldLastIdx[len(oldLastIdx)-len(oldLastIdxBytes):], oldLastIdxBytes)
  411. b = append(b, oldLastIdx...)
  412. // [MAX_NLEVELS bits] newLastIdx
  413. newLastIdx := make([]byte, bytesMaxLevels)
  414. newLastIdxBytes, err := z.Metadata.NewLastIdxRaw.Bytes()
  415. if err != nil {
  416. return nil, tracerr.Wrap(err)
  417. }
  418. copy(newLastIdx, newLastIdxBytes[len(newLastIdxBytes)-bytesMaxLevels:])
  419. b = append(b, newLastIdx...)
  420. // [256 bits] oldStRoot
  421. oldStateRoot := make([]byte, 32)
  422. copy(oldStateRoot, z.OldStateRoot.Bytes())
  423. b = append(b, oldStateRoot...)
  424. // [256 bits] newStateRoot
  425. newStateRoot := make([]byte, 32)
  426. copy(newStateRoot, z.Metadata.NewStateRootRaw.Bytes())
  427. b = append(b, newStateRoot...)
  428. // [256 bits] newExitRoot
  429. newExitRoot := make([]byte, 32)
  430. copy(newExitRoot, z.Metadata.NewExitRootRaw.Bytes())
  431. b = append(b, newExitRoot...)
  432. // [MAX_L1_TX * (2 * MAX_NLEVELS + 528) bits] L1TxsData
  433. l1TxDataLen := (2*z.Metadata.MaxLevels + 528) //nolint:gomnd
  434. l1TxsDataLen := (z.Metadata.MaxL1Tx * l1TxDataLen)
  435. l1TxsData := make([]byte, l1TxsDataLen/8) //nolint:gomnd
  436. for i := 0; i < len(z.Metadata.L1TxsData); i++ {
  437. dataLen := int(l1TxDataLen) / 8 //nolint:gomnd
  438. pos0 := i * dataLen
  439. pos1 := i*dataLen + dataLen
  440. copy(l1TxsData[pos0:pos1], z.Metadata.L1TxsData[i])
  441. }
  442. b = append(b, l1TxsData...)
  443. var l1TxsDataAvailability []byte
  444. for i := 0; i < len(z.Metadata.L1TxsDataAvailability); i++ {
  445. l1TxsDataAvailability = append(l1TxsDataAvailability, z.Metadata.L1TxsDataAvailability[i]...)
  446. }
  447. b = append(b, l1TxsDataAvailability...)
  448. // [MAX_TX*(2*NLevels + 48) bits] L2TxsData
  449. var l2TxsData []byte
  450. l2TxDataLen := 2*z.Metadata.NLevels + 48 //nolint:gomnd
  451. l2TxsDataLen := (z.Metadata.MaxTx * l2TxDataLen)
  452. expectedL2TxsDataLen := l2TxsDataLen / 8 //nolint:gomnd
  453. for i := 0; i < len(z.Metadata.L2TxsData); i++ {
  454. l2TxsData = append(l2TxsData, z.Metadata.L2TxsData[i]...)
  455. }
  456. if len(l2TxsData) > int(expectedL2TxsDataLen) {
  457. return nil, tracerr.Wrap(fmt.Errorf("len(l2TxsData): %d, expected: %d",
  458. len(l2TxsData), expectedL2TxsDataLen))
  459. }
  460. b = append(b, l2TxsData...)
  461. l2TxsPadding := make([]byte,
  462. (int(z.Metadata.MaxTx)-len(z.Metadata.L1TxsDataAvailability)-
  463. len(z.Metadata.L2TxsData))*int(l2TxDataLen)/8) //nolint:gomnd
  464. b = append(b, l2TxsPadding...)
  465. // [NLevels * MAX_TOKENS_FEE bits] feeTxsData
  466. for i := 0; i < len(z.FeeIdxs); i++ {
  467. feeIdx := make([]byte, bytesNLevels) //nolint:gomnd
  468. feeIdxBytes := z.FeeIdxs[i].Bytes()
  469. copy(feeIdx[len(feeIdx)-len(feeIdxBytes):], feeIdxBytes[:])
  470. b = append(b, feeIdx...)
  471. }
  472. // [16 bits] chainID
  473. var chainID [2]byte
  474. binary.BigEndian.PutUint16(chainID[:], z.Metadata.ChainID)
  475. b = append(b, chainID[:]...)
  476. // [32 bits] currentNumBatch
  477. currNumBatchBytes := z.CurrentNumBatch.Bytes()
  478. var currNumBatch [4]byte
  479. copy(currNumBatch[4-len(currNumBatchBytes):], currNumBatchBytes)
  480. b = append(b, currNumBatch[:]...)
  481. return b, nil
  482. }