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. // LoadAmountF encoded as float16
  95. LoadAmountF []*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 root value once the Tx is processed into the state tree
  181. ISStateRoot []*big.Int `json:"imStateRoot"` // Hash, len: [nTx - 1]
  182. // ISExitTree root at the moment (once processed) of the Tx the value
  183. // once the Tx is processed into the exit tree
  184. ISExitRoot []*big.Int `json:"imExitRoot"` // Hash, len: [nTx - 1]
  185. // ISAccFeeOut accumulated fees once the Tx is processed. Contains the
  186. // array of FeeAccount Balances at each moment of each Tx processed.
  187. ISAccFeeOut [][]*big.Int `json:"imAccFeeOut"` // big.Int, len: [nTx - 1][maxFeeIdxs]
  188. // fee-tx
  189. // ISStateRootFee root at the moment of the Tx (once processed), the state root value once the Tx is processed into the state tree
  190. ISStateRootFee []*big.Int `json:"imStateRootFee"` // Hash, len: [maxFeeIdxs - 1]
  191. // ISInitStateRootFee state root once all L1-L2 tx are processed (before computing the fees-tx)
  192. ISInitStateRootFee *big.Int `json:"imInitStateRootFee"` // Hash
  193. // ISFinalAccFee final accumulated fees (before computing the fees-tx).
  194. // Contains the final values of the ISAccFeeOut parameter
  195. ISFinalAccFee []*big.Int `json:"imFinalAccFee"` // big.Int, len: [maxFeeIdxs - 1]
  196. }
  197. func bigIntsToStrings(v interface{}) interface{} {
  198. switch c := v.(type) {
  199. case *big.Int:
  200. return c.String()
  201. case []*big.Int:
  202. r := make([]interface{}, len(c))
  203. for i := range c {
  204. r[i] = bigIntsToStrings(c[i])
  205. }
  206. return r
  207. case [256]*big.Int:
  208. r := make([]interface{}, len(c))
  209. for i := range c {
  210. r[i] = bigIntsToStrings(c[i])
  211. }
  212. return r
  213. case [][]*big.Int:
  214. r := make([]interface{}, len(c))
  215. for i := range c {
  216. r[i] = bigIntsToStrings(c[i])
  217. }
  218. return r
  219. case [][256]*big.Int:
  220. r := make([]interface{}, len(c))
  221. for i := range c {
  222. r[i] = bigIntsToStrings(c[i])
  223. }
  224. return r
  225. case map[string]interface{}:
  226. // avoid printing a warning when there is a struct type
  227. default:
  228. log.Warnf("bigIntsToStrings unexpected type: %T\n", v)
  229. }
  230. return nil
  231. }
  232. // MarshalJSON implements the json marshaler for ZKInputs
  233. func (z ZKInputs) MarshalJSON() ([]byte, error) {
  234. var m map[string]interface{}
  235. dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
  236. TagName: "json",
  237. Result: &m,
  238. })
  239. if err != nil {
  240. return nil, tracerr.Wrap(err)
  241. }
  242. err = dec.Decode(z)
  243. if err != nil {
  244. return nil, tracerr.Wrap(err)
  245. }
  246. for k, v := range m {
  247. m[k] = bigIntsToStrings(v)
  248. }
  249. return json.Marshal(m)
  250. }
  251. // NewZKInputs returns a pointer to an initialized struct of ZKInputs
  252. func NewZKInputs(nTx, maxL1Tx, maxTx, maxFeeIdxs, nLevels uint32, currentNumBatch *big.Int) *ZKInputs {
  253. zki := &ZKInputs{}
  254. zki.Metadata.NTx = nTx
  255. zki.Metadata.MaxFeeIdxs = maxFeeIdxs
  256. zki.Metadata.NLevels = nLevels
  257. zki.Metadata.MaxLevels = uint32(48) //nolint:gomnd
  258. zki.Metadata.MaxL1Tx = maxL1Tx
  259. zki.Metadata.MaxTx = maxTx
  260. // General
  261. zki.CurrentNumBatch = currentNumBatch
  262. zki.OldLastIdx = big.NewInt(0)
  263. zki.OldStateRoot = big.NewInt(0)
  264. zki.GlobalChainID = big.NewInt(0) // TODO pass by parameter
  265. zki.FeeIdxs = newSlice(maxFeeIdxs)
  266. zki.FeePlanTokens = newSlice(maxFeeIdxs)
  267. // Txs
  268. zki.TxCompressedData = newSlice(nTx)
  269. zki.TxCompressedDataV2 = newSlice(nTx)
  270. zki.MaxNumBatch = newSlice(nTx)
  271. zki.FromIdx = newSlice(nTx)
  272. zki.AuxFromIdx = newSlice(nTx)
  273. zki.ToIdx = newSlice(nTx)
  274. zki.AuxToIdx = newSlice(nTx)
  275. zki.ToBJJAy = newSlice(nTx)
  276. zki.ToEthAddr = newSlice(nTx)
  277. zki.OnChain = newSlice(nTx)
  278. zki.NewAccount = newSlice(nTx)
  279. // L1
  280. zki.LoadAmountF = newSlice(nTx)
  281. zki.FromEthAddr = newSlice(nTx)
  282. zki.FromBJJCompressed = make([][256]*big.Int, nTx)
  283. for i := 0; i < len(zki.FromBJJCompressed); i++ {
  284. // zki.FromBJJCompressed[i] = newSlice(256)
  285. for j := 0; j < 256; j++ {
  286. zki.FromBJJCompressed[i][j] = big.NewInt(0)
  287. }
  288. }
  289. // L2
  290. zki.RqOffset = newSlice(nTx)
  291. zki.RqTxCompressedDataV2 = newSlice(nTx)
  292. zki.RqToEthAddr = newSlice(nTx)
  293. zki.RqToBJJAy = newSlice(nTx)
  294. zki.S = newSlice(nTx)
  295. zki.R8x = newSlice(nTx)
  296. zki.R8y = newSlice(nTx)
  297. // State MerkleTree Leafs transitions
  298. zki.TokenID1 = newSlice(nTx)
  299. zki.Nonce1 = newSlice(nTx)
  300. zki.Sign1 = newSlice(nTx)
  301. zki.Ay1 = newSlice(nTx)
  302. zki.Balance1 = newSlice(nTx)
  303. zki.EthAddr1 = newSlice(nTx)
  304. zki.Siblings1 = make([][]*big.Int, nTx)
  305. for i := 0; i < len(zki.Siblings1); i++ {
  306. zki.Siblings1[i] = newSlice(nLevels + 1)
  307. }
  308. zki.IsOld0_1 = newSlice(nTx)
  309. zki.OldKey1 = newSlice(nTx)
  310. zki.OldValue1 = newSlice(nTx)
  311. zki.TokenID2 = newSlice(nTx)
  312. zki.Nonce2 = newSlice(nTx)
  313. zki.Sign2 = newSlice(nTx)
  314. zki.Ay2 = newSlice(nTx)
  315. zki.Balance2 = newSlice(nTx)
  316. zki.EthAddr2 = newSlice(nTx)
  317. zki.Siblings2 = make([][]*big.Int, nTx)
  318. for i := 0; i < len(zki.Siblings2); i++ {
  319. zki.Siblings2[i] = newSlice(nLevels + 1)
  320. }
  321. zki.NewExit = newSlice(nTx)
  322. zki.IsOld0_2 = newSlice(nTx)
  323. zki.OldKey2 = newSlice(nTx)
  324. zki.OldValue2 = newSlice(nTx)
  325. zki.TokenID3 = newSlice(maxFeeIdxs)
  326. zki.Nonce3 = newSlice(maxFeeIdxs)
  327. zki.Sign3 = newSlice(maxFeeIdxs)
  328. zki.Ay3 = newSlice(maxFeeIdxs)
  329. zki.Balance3 = newSlice(maxFeeIdxs)
  330. zki.EthAddr3 = newSlice(maxFeeIdxs)
  331. zki.Siblings3 = make([][]*big.Int, maxFeeIdxs)
  332. for i := 0; i < len(zki.Siblings3); i++ {
  333. zki.Siblings3[i] = newSlice(nLevels + 1)
  334. }
  335. // Intermediate States
  336. zki.ISOnChain = newSlice(nTx - 1)
  337. zki.ISOutIdx = newSlice(nTx - 1)
  338. zki.ISStateRoot = newSlice(nTx - 1)
  339. zki.ISExitRoot = newSlice(nTx - 1)
  340. zki.ISAccFeeOut = make([][]*big.Int, nTx-1)
  341. for i := 0; i < len(zki.ISAccFeeOut); i++ {
  342. zki.ISAccFeeOut[i] = newSlice(maxFeeIdxs)
  343. }
  344. zki.ISStateRootFee = newSlice(maxFeeIdxs - 1)
  345. zki.ISInitStateRootFee = big.NewInt(0)
  346. zki.ISFinalAccFee = newSlice(maxFeeIdxs - 1)
  347. return zki
  348. }
  349. // newSlice returns a []*big.Int slice of length n with values initialized at
  350. // 0.
  351. // Is used to initialize all *big.Ints of the ZKInputs data structure, so when
  352. // the transactions are processed and the ZKInputs filled, there is no need to
  353. // set all the elements, and if a transaction does not use a parameter, can be
  354. // leaved as it is in the ZKInputs, as will be 0, so later when using the
  355. // ZKInputs to generate the zkSnark proof there is no 'nil'/'null' values.
  356. func newSlice(n uint32) []*big.Int {
  357. s := make([]*big.Int, n)
  358. for i := 0; i < len(s); i++ {
  359. s[i] = big.NewInt(0)
  360. }
  361. return s
  362. }
  363. // HashGlobalData returns the HashGlobalData
  364. func (z ZKInputs) HashGlobalData() (*big.Int, error) {
  365. b, err := z.ToHashGlobalData()
  366. if err != nil {
  367. return nil, tracerr.Wrap(err)
  368. }
  369. h := sha256.New()
  370. _, err = h.Write(b)
  371. if err != nil {
  372. return nil, tracerr.Wrap(err)
  373. }
  374. r := new(big.Int).SetBytes(h.Sum(nil))
  375. v := r.Mod(r, cryptoConstants.Q)
  376. return v, nil
  377. }
  378. // ToHashGlobalData returns the data to be hashed in the method HashGlobalData
  379. func (z ZKInputs) ToHashGlobalData() ([]byte, error) {
  380. var b []byte
  381. bytesMaxLevels := int(z.Metadata.MaxLevels / 8) //nolint:gomnd
  382. // [MAX_NLEVELS bits] oldLastIdx
  383. oldLastIdx := make([]byte, bytesMaxLevels)
  384. copy(oldLastIdx, z.OldLastIdx.Bytes())
  385. b = append(b, SwapEndianness(oldLastIdx)...)
  386. // [MAX_NLEVELS bits] newLastIdx
  387. newLastIdx := make([]byte, bytesMaxLevels)
  388. newLastIdxBytes, err := z.Metadata.NewLastIdxRaw.Bytes()
  389. if err != nil {
  390. return nil, tracerr.Wrap(err)
  391. }
  392. copy(newLastIdx, newLastIdxBytes[len(newLastIdxBytes)-bytesMaxLevels:])
  393. b = append(b, newLastIdx...)
  394. // [256 bits] oldStRoot
  395. oldStateRoot := make([]byte, 32)
  396. copy(oldStateRoot, z.OldStateRoot.Bytes())
  397. b = append(b, oldStateRoot...)
  398. // [256 bits] newStateRoot
  399. newStateRoot := make([]byte, 32)
  400. copy(newStateRoot, z.Metadata.NewStateRootRaw.Bytes())
  401. b = append(b, newStateRoot...)
  402. // [256 bits] newExitRoot
  403. newExitRoot := make([]byte, 32)
  404. copy(newExitRoot, z.Metadata.NewExitRootRaw.Bytes())
  405. b = append(b, newExitRoot...)
  406. // [MAX_L1_TX * (2 * MAX_NLEVELS + 480) bits] L1TxsData
  407. l1TxDataLen := (2*z.Metadata.MaxLevels + 480)
  408. l1TxsDataLen := (z.Metadata.MaxL1Tx * l1TxDataLen)
  409. l1TxsData := make([]byte, l1TxsDataLen/8) //nolint:gomnd
  410. for i := 0; i < len(z.Metadata.L1TxsData); i++ {
  411. dataLen := int(l1TxDataLen) / 8 //nolint:gomnd
  412. pos0 := i * dataLen
  413. pos1 := i*dataLen + dataLen
  414. copy(l1TxsData[pos0:pos1], z.Metadata.L1TxsData[i])
  415. }
  416. b = append(b, l1TxsData...)
  417. var l1TxsDataAvailability []byte
  418. for i := 0; i < len(z.Metadata.L1TxsDataAvailability); i++ {
  419. l1TxsDataAvailability = append(l1TxsDataAvailability, z.Metadata.L1TxsDataAvailability[i]...)
  420. }
  421. b = append(b, l1TxsDataAvailability...)
  422. // [MAX_TX*(2*NLevels + 24) bits] L2TxsData
  423. var l2TxsData []byte
  424. l2TxDataLen := 2*z.Metadata.NLevels + 24 //nolint:gomnd
  425. l2TxsDataLen := (z.Metadata.MaxTx * l2TxDataLen)
  426. expectedL2TxsDataLen := l2TxsDataLen / 8 //nolint:gomnd
  427. for i := 0; i < len(z.Metadata.L2TxsData); i++ {
  428. l2TxsData = append(l2TxsData, z.Metadata.L2TxsData[i]...)
  429. }
  430. if len(l2TxsData) > int(expectedL2TxsDataLen) {
  431. return nil, tracerr.Wrap(fmt.Errorf("len(l2TxsData): %d, expected: %d", len(l2TxsData), expectedL2TxsDataLen))
  432. }
  433. b = append(b, l2TxsData...)
  434. l2TxsPadding := make([]byte, (int(z.Metadata.MaxTx)-len(z.Metadata.L1TxsDataAvailability)-len(z.Metadata.L2TxsData))*int(l2TxDataLen)/8) //nolint:gomnd
  435. b = append(b, l2TxsPadding...)
  436. // [NLevels * MAX_TOKENS_FEE bits] feeTxsData
  437. for i := 0; i < len(z.FeeIdxs); i++ {
  438. var r []byte
  439. padding := make([]byte, bytesMaxLevels/4) //nolint:gomnd
  440. r = append(r, padding...)
  441. feeIdx := make([]byte, bytesMaxLevels/2) //nolint:gomnd
  442. feeIdxBytes := z.FeeIdxs[i].Bytes()
  443. copy(feeIdx[len(feeIdx)-len(feeIdxBytes):], feeIdxBytes[:])
  444. r = append(r, feeIdx...)
  445. b = append(b, r...)
  446. }
  447. // [16 bits] chainID
  448. var chainID [2]byte
  449. binary.BigEndian.PutUint16(chainID[:], z.Metadata.ChainID)
  450. b = append(b, chainID[:]...)
  451. // [32 bits] currentNumBatch
  452. currNumBatchBytes := z.CurrentNumBatch.Bytes()
  453. var currNumBatch [4]byte
  454. copy(currNumBatch[4-len(currNumBatchBytes):], currNumBatchBytes)
  455. b = append(b, currNumBatch[:]...)
  456. return b, nil
  457. }