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