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.

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