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.

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