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.

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