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