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.

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