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.

536 lines
19 KiB

  1. // Package common 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 accoumulated 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
  116. RqTxCompressedDataV2 []*big.Int `json:"rqTxCompressedDataV2"` // big.Int (max 251 bits), len: [maxTx]
  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, currentNumBatch *big.Int) *ZKInputs {
  275. zki := &ZKInputs{}
  276. zki.Metadata.MaxFeeIdxs = maxFeeIdxs
  277. zki.Metadata.MaxLevels = uint32(48) //nolint:gomnd
  278. zki.Metadata.NLevels = nLevels
  279. zki.Metadata.MaxL1Tx = maxL1Tx
  280. zki.Metadata.MaxTx = maxTx
  281. zki.Metadata.ChainID = chainID
  282. // General
  283. zki.CurrentNumBatch = currentNumBatch
  284. zki.OldLastIdx = big.NewInt(0)
  285. zki.OldStateRoot = big.NewInt(0)
  286. zki.GlobalChainID = big.NewInt(int64(chainID))
  287. zki.FeeIdxs = newSlice(maxFeeIdxs)
  288. zki.FeePlanTokens = newSlice(maxFeeIdxs)
  289. // Txs
  290. zki.TxCompressedData = newSlice(maxTx)
  291. zki.TxCompressedDataV2 = newSlice(maxTx)
  292. zki.MaxNumBatch = newSlice(maxTx)
  293. zki.FromIdx = newSlice(maxTx)
  294. zki.AuxFromIdx = newSlice(maxTx)
  295. zki.ToIdx = newSlice(maxTx)
  296. zki.AuxToIdx = newSlice(maxTx)
  297. zki.ToBJJAy = newSlice(maxTx)
  298. zki.ToEthAddr = newSlice(maxTx)
  299. zki.AmountF = newSlice(maxTx)
  300. zki.OnChain = newSlice(maxTx)
  301. zki.NewAccount = newSlice(maxTx)
  302. // L1
  303. zki.DepositAmountF = newSlice(maxTx)
  304. zki.FromEthAddr = newSlice(maxTx)
  305. zki.FromBJJCompressed = make([][256]*big.Int, maxTx)
  306. for i := 0; i < len(zki.FromBJJCompressed); i++ {
  307. // zki.FromBJJCompressed[i] = newSlice(256)
  308. for j := 0; j < 256; j++ {
  309. zki.FromBJJCompressed[i][j] = big.NewInt(0)
  310. }
  311. }
  312. // L2
  313. zki.RqOffset = newSlice(maxTx)
  314. zki.RqTxCompressedDataV2 = newSlice(maxTx)
  315. zki.RqToEthAddr = newSlice(maxTx)
  316. zki.RqToBJJAy = newSlice(maxTx)
  317. zki.S = newSlice(maxTx)
  318. zki.R8x = newSlice(maxTx)
  319. zki.R8y = newSlice(maxTx)
  320. // State MerkleTree Leafs transitions
  321. zki.TokenID1 = newSlice(maxTx)
  322. zki.Nonce1 = newSlice(maxTx)
  323. zki.Sign1 = newSlice(maxTx)
  324. zki.Ay1 = newSlice(maxTx)
  325. zki.Balance1 = newSlice(maxTx)
  326. zki.EthAddr1 = newSlice(maxTx)
  327. zki.Siblings1 = make([][]*big.Int, maxTx)
  328. for i := 0; i < len(zki.Siblings1); i++ {
  329. zki.Siblings1[i] = newSlice(nLevels + 1)
  330. }
  331. zki.IsOld0_1 = newSlice(maxTx)
  332. zki.OldKey1 = newSlice(maxTx)
  333. zki.OldValue1 = newSlice(maxTx)
  334. zki.TokenID2 = newSlice(maxTx)
  335. zki.Nonce2 = newSlice(maxTx)
  336. zki.Sign2 = newSlice(maxTx)
  337. zki.Ay2 = newSlice(maxTx)
  338. zki.Balance2 = newSlice(maxTx)
  339. zki.EthAddr2 = newSlice(maxTx)
  340. zki.Siblings2 = make([][]*big.Int, maxTx)
  341. for i := 0; i < len(zki.Siblings2); i++ {
  342. zki.Siblings2[i] = newSlice(nLevels + 1)
  343. }
  344. zki.NewExit = newSlice(maxTx)
  345. zki.IsOld0_2 = newSlice(maxTx)
  346. zki.OldKey2 = newSlice(maxTx)
  347. zki.OldValue2 = newSlice(maxTx)
  348. zki.TokenID3 = newSlice(maxFeeIdxs)
  349. zki.Nonce3 = newSlice(maxFeeIdxs)
  350. zki.Sign3 = newSlice(maxFeeIdxs)
  351. zki.Ay3 = newSlice(maxFeeIdxs)
  352. zki.Balance3 = newSlice(maxFeeIdxs)
  353. zki.EthAddr3 = newSlice(maxFeeIdxs)
  354. zki.Siblings3 = make([][]*big.Int, maxFeeIdxs)
  355. for i := 0; i < len(zki.Siblings3); i++ {
  356. zki.Siblings3[i] = newSlice(nLevels + 1)
  357. }
  358. // Intermediate States
  359. zki.ISOnChain = newSlice(maxTx - 1)
  360. zki.ISOutIdx = newSlice(maxTx - 1)
  361. zki.ISStateRoot = newSlice(maxTx - 1)
  362. zki.ISExitRoot = newSlice(maxTx - 1)
  363. zki.ISAccFeeOut = make([][]*big.Int, maxTx-1)
  364. for i := 0; i < len(zki.ISAccFeeOut); i++ {
  365. zki.ISAccFeeOut[i] = newSlice(maxFeeIdxs)
  366. }
  367. zki.ISStateRootFee = newSlice(maxFeeIdxs - 1)
  368. zki.ISInitStateRootFee = big.NewInt(0)
  369. zki.ISFinalAccFee = newSlice(maxFeeIdxs)
  370. return zki
  371. }
  372. // newSlice returns a []*big.Int slice of length n with values initialized at
  373. // 0.
  374. // Is used to initialize all *big.Ints of the ZKInputs data structure, so when
  375. // the transactions are processed and the ZKInputs filled, there is no need to
  376. // set all the elements, and if a transaction does not use a parameter, can be
  377. // leaved as it is in the ZKInputs, as will be 0, so later when using the
  378. // ZKInputs to generate the zkSnark proof there is no 'nil'/'null' values.
  379. func newSlice(n uint32) []*big.Int {
  380. s := make([]*big.Int, n)
  381. for i := 0; i < len(s); i++ {
  382. s[i] = big.NewInt(0)
  383. }
  384. return s
  385. }
  386. // HashGlobalData returns the HashGlobalData
  387. func (z ZKInputs) HashGlobalData() (*big.Int, error) {
  388. b, err := z.ToHashGlobalData()
  389. if err != nil {
  390. return nil, tracerr.Wrap(err)
  391. }
  392. h := sha256.New()
  393. _, err = h.Write(b)
  394. if err != nil {
  395. return nil, tracerr.Wrap(err)
  396. }
  397. r := new(big.Int).SetBytes(h.Sum(nil))
  398. v := r.Mod(r, cryptoConstants.Q)
  399. return v, nil
  400. }
  401. // ToHashGlobalData returns the data to be hashed in the method HashGlobalData
  402. func (z ZKInputs) ToHashGlobalData() ([]byte, error) {
  403. var b []byte
  404. bytesMaxLevels := int(z.Metadata.MaxLevels / 8) //nolint:gomnd
  405. bytesNLevels := int(z.Metadata.NLevels / 8) //nolint:gomnd
  406. // [MAX_NLEVELS bits] oldLastIdx
  407. oldLastIdx := make([]byte, bytesMaxLevels)
  408. oldLastIdxBytes := z.OldLastIdx.Bytes()
  409. copy(oldLastIdx[len(oldLastIdx)-len(oldLastIdxBytes):], oldLastIdxBytes)
  410. b = append(b, oldLastIdx...)
  411. // [MAX_NLEVELS bits] newLastIdx
  412. newLastIdx := make([]byte, bytesMaxLevels)
  413. newLastIdxBytes, err := z.Metadata.NewLastIdxRaw.Bytes()
  414. if err != nil {
  415. return nil, tracerr.Wrap(err)
  416. }
  417. copy(newLastIdx, newLastIdxBytes[len(newLastIdxBytes)-bytesMaxLevels:])
  418. b = append(b, newLastIdx...)
  419. // [256 bits] oldStRoot
  420. oldStateRoot := make([]byte, 32)
  421. copy(oldStateRoot, z.OldStateRoot.Bytes())
  422. b = append(b, oldStateRoot...)
  423. // [256 bits] newStateRoot
  424. newStateRoot := make([]byte, 32)
  425. copy(newStateRoot, z.Metadata.NewStateRootRaw.Bytes())
  426. b = append(b, newStateRoot...)
  427. // [256 bits] newExitRoot
  428. newExitRoot := make([]byte, 32)
  429. copy(newExitRoot, z.Metadata.NewExitRootRaw.Bytes())
  430. b = append(b, newExitRoot...)
  431. // [MAX_L1_TX * (2 * MAX_NLEVELS + 528) bits] L1TxsData
  432. l1TxDataLen := (2*z.Metadata.MaxLevels + 528)
  433. l1TxsDataLen := (z.Metadata.MaxL1Tx * l1TxDataLen)
  434. l1TxsData := make([]byte, l1TxsDataLen/8) //nolint:gomnd
  435. for i := 0; i < len(z.Metadata.L1TxsData); i++ {
  436. dataLen := int(l1TxDataLen) / 8 //nolint:gomnd
  437. pos0 := i * dataLen
  438. pos1 := i*dataLen + dataLen
  439. copy(l1TxsData[pos0:pos1], z.Metadata.L1TxsData[i])
  440. }
  441. b = append(b, l1TxsData...)
  442. var l1TxsDataAvailability []byte
  443. for i := 0; i < len(z.Metadata.L1TxsDataAvailability); i++ {
  444. l1TxsDataAvailability = append(l1TxsDataAvailability, z.Metadata.L1TxsDataAvailability[i]...)
  445. }
  446. b = append(b, l1TxsDataAvailability...)
  447. // [MAX_TX*(2*NLevels + 48) bits] L2TxsData
  448. var l2TxsData []byte
  449. l2TxDataLen := 2*z.Metadata.NLevels + 48 //nolint:gomnd
  450. l2TxsDataLen := (z.Metadata.MaxTx * l2TxDataLen)
  451. expectedL2TxsDataLen := l2TxsDataLen / 8 //nolint:gomnd
  452. for i := 0; i < len(z.Metadata.L2TxsData); i++ {
  453. l2TxsData = append(l2TxsData, z.Metadata.L2TxsData[i]...)
  454. }
  455. if len(l2TxsData) > int(expectedL2TxsDataLen) {
  456. return nil, tracerr.Wrap(fmt.Errorf("len(l2TxsData): %d, expected: %d", len(l2TxsData), expectedL2TxsDataLen))
  457. }
  458. b = append(b, l2TxsData...)
  459. l2TxsPadding := make([]byte, (int(z.Metadata.MaxTx)-len(z.Metadata.L1TxsDataAvailability)-len(z.Metadata.L2TxsData))*int(l2TxDataLen)/8) //nolint:gomnd
  460. b = append(b, l2TxsPadding...)
  461. // [NLevels * MAX_TOKENS_FEE bits] feeTxsData
  462. for i := 0; i < len(z.FeeIdxs); i++ {
  463. feeIdx := make([]byte, bytesNLevels) //nolint:gomnd
  464. feeIdxBytes := z.FeeIdxs[i].Bytes()
  465. copy(feeIdx[len(feeIdx)-len(feeIdxBytes):], feeIdxBytes[:])
  466. b = append(b, feeIdx...)
  467. }
  468. // [16 bits] chainID
  469. var chainID [2]byte
  470. binary.BigEndian.PutUint16(chainID[:], z.Metadata.ChainID)
  471. b = append(b, chainID[:]...)
  472. // [32 bits] currentNumBatch
  473. currNumBatchBytes := z.CurrentNumBatch.Bytes()
  474. var currNumBatch [4]byte
  475. copy(currNumBatch[4-len(currNumBatchBytes):], currNumBatchBytes)
  476. b = append(b, currNumBatch[:]...)
  477. return b, nil
  478. }