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.

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