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