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.

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