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 root value once the Tx is processed into the state tree
207	ISStateRoot []*big.Int `json:"imStateRoot"` // Hash, len: [nTx - 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: [nTx - 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: [nTx - 1][maxFeeIdxs]
214	// fee-tx
215	// ISStateRootFee root at the moment of the Tx (once processed), the state root value once the Tx is processed into the state tree
216	ISStateRootFee []*big.Int `json:"imStateRootFee"` // Hash, len: [maxFeeIdxs - 1]
217	// ISInitStateRootFee state root once all L1-L2 tx are processed (before computing the fees-tx)
218	ISInitStateRootFee *big.Int `json:"imInitStateRootFee"` // Hash
219	// ISFinalAccFee final accumulated fees (before computing the fees-tx).
220	// Contains the final values of the ISAccFeeOut parameter
221	ISFinalAccFee []*big.Int `json:"imFinalAccFee"` // big.Int, len: [maxFeeIdxs - 1]
222}
223
224func bigIntsToStrings(v interface{}) interface{} {
225	switch c := v.(type) {
226	case *big.Int:
227		return c.String()
228	case []*big.Int:
229		r := make([]interface{}, len(c))
230		for i := range c {
231			r[i] = bigIntsToStrings(c[i])
232		}
233		return r
234	case [256]*big.Int:
235		r := make([]interface{}, len(c))
236		for i := range c {
237			r[i] = bigIntsToStrings(c[i])
238		}
239		return r
240	case [][]*big.Int:
241		r := make([]interface{}, len(c))
242		for i := range c {
243			r[i] = bigIntsToStrings(c[i])
244		}
245		return r
246	case [][256]*big.Int:
247		r := make([]interface{}, len(c))
248		for i := range c {
249			r[i] = bigIntsToStrings(c[i])
250		}
251		return r
252	case map[string]interface{}:
253		// avoid printing a warning when there is a struct type
254	default:
255		log.Warnf("bigIntsToStrings unexpected type: %T\n", v)
256	}
257	return nil
258}
259
260// MarshalJSON implements the json marshaler for ZKInputs
261func (z ZKInputs) MarshalJSON() ([]byte, error) {
262	var m map[string]interface{}
263	dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
264		TagName: "json",
265		Result:  &m,
266	})
267	if err != nil {
268		return nil, tracerr.Wrap(err)
269	}
270	err = dec.Decode(z)
271	if err != nil {
272		return nil, tracerr.Wrap(err)
273	}
274
275	for k, v := range m {
276		m[k] = bigIntsToStrings(v)
277	}
278	return json.Marshal(m)
279}
280
281// NewZKInputs returns a pointer to an initialized struct of ZKInputs
282func NewZKInputs(nTx, maxL1Tx, maxTx, maxFeeIdxs, nLevels uint32, currentNumBatch *big.Int) *ZKInputs {
283	zki := &ZKInputs{}
284	zki.Metadata.NTx = nTx
285	zki.Metadata.MaxFeeIdxs = maxFeeIdxs
286	zki.Metadata.NLevels = nLevels
287	zki.Metadata.MaxLevels = uint32(48) //nolint:gomnd
288	zki.Metadata.MaxL1Tx = maxL1Tx
289	zki.Metadata.MaxTx = maxTx
290
291	// General
292	zki.CurrentNumBatch = currentNumBatch
293	zki.OldLastIdx = big.NewInt(0)
294	zki.OldStateRoot = big.NewInt(0)
295	zki.GlobalChainID = big.NewInt(0) // TODO pass by parameter
296	zki.FeeIdxs = newSlice(maxFeeIdxs)
297	zki.FeePlanTokens = newSlice(maxFeeIdxs)
298
299	// Txs
300	zki.TxCompressedData = newSlice(nTx)
301	zki.TxCompressedDataV2 = newSlice(nTx)
302	zki.MaxNumBatch = newSlice(nTx)
303	zki.FromIdx = newSlice(nTx)
304	zki.AuxFromIdx = newSlice(nTx)
305	zki.ToIdx = newSlice(nTx)
306	zki.AuxToIdx = newSlice(nTx)
307	zki.ToBJJAy = newSlice(nTx)
308	zki.ToEthAddr = newSlice(nTx)
309	zki.OnChain = newSlice(nTx)
310	zki.NewAccount = newSlice(nTx)
311
312	// L1
313	zki.DepositAmountF = newSlice(nTx)
314	zki.FromEthAddr = newSlice(nTx)
315	zki.FromBJJCompressed = make([][256]*big.Int, nTx)
316	for i := 0; i < len(zki.FromBJJCompressed); i++ {
317		// zki.FromBJJCompressed[i] = newSlice(256)
318		for j := 0; j < 256; j++ {
319			zki.FromBJJCompressed[i][j] = big.NewInt(0)
320		}
321	}
322
323	// L2
324	zki.RqOffset = newSlice(nTx)
325	zki.RqTxCompressedDataV2 = newSlice(nTx)
326	zki.RqToEthAddr = newSlice(nTx)
327	zki.RqToBJJAy = newSlice(nTx)
328	zki.S = newSlice(nTx)
329	zki.R8x = newSlice(nTx)
330	zki.R8y = newSlice(nTx)
331
332	// State MerkleTree Leafs transitions
333	zki.TokenID1 = newSlice(nTx)
334	zki.Nonce1 = newSlice(nTx)
335	zki.Sign1 = newSlice(nTx)
336	zki.Ay1 = newSlice(nTx)
337	zki.Balance1 = newSlice(nTx)
338	zki.EthAddr1 = newSlice(nTx)
339	zki.Siblings1 = make([][]*big.Int, nTx)
340	for i := 0; i < len(zki.Siblings1); i++ {
341		zki.Siblings1[i] = newSlice(nLevels + 1)
342	}
343	zki.IsOld0_1 = newSlice(nTx)
344	zki.OldKey1 = newSlice(nTx)
345	zki.OldValue1 = newSlice(nTx)
346
347	zki.TokenID2 = newSlice(nTx)
348	zki.Nonce2 = newSlice(nTx)
349	zki.Sign2 = newSlice(nTx)
350	zki.Ay2 = newSlice(nTx)
351	zki.Balance2 = newSlice(nTx)
352	zki.EthAddr2 = newSlice(nTx)
353	zki.Siblings2 = make([][]*big.Int, nTx)
354	for i := 0; i < len(zki.Siblings2); i++ {
355		zki.Siblings2[i] = newSlice(nLevels + 1)
356	}
357	zki.NewExit = newSlice(nTx)
358	zki.IsOld0_2 = newSlice(nTx)
359	zki.OldKey2 = newSlice(nTx)
360	zki.OldValue2 = newSlice(nTx)
361
362	zki.TokenID3 = newSlice(maxFeeIdxs)
363	zki.Nonce3 = newSlice(maxFeeIdxs)
364	zki.Sign3 = newSlice(maxFeeIdxs)
365	zki.Ay3 = newSlice(maxFeeIdxs)
366	zki.Balance3 = newSlice(maxFeeIdxs)
367	zki.EthAddr3 = newSlice(maxFeeIdxs)
368	zki.Siblings3 = make([][]*big.Int, maxFeeIdxs)
369	for i := 0; i < len(zki.Siblings3); i++ {
370		zki.Siblings3[i] = newSlice(nLevels + 1)
371	}
372
373	// Intermediate States
374	zki.ISOnChain = newSlice(nTx - 1)
375	zki.ISOutIdx = newSlice(nTx - 1)
376	zki.ISStateRoot = newSlice(nTx - 1)
377	zki.ISExitRoot = newSlice(nTx - 1)
378	zki.ISAccFeeOut = make([][]*big.Int, nTx-1)
379	for i := 0; i < len(zki.ISAccFeeOut); i++ {
380		zki.ISAccFeeOut[i] = newSlice(maxFeeIdxs)
381	}
382	zki.ISStateRootFee = newSlice(maxFeeIdxs - 1)
383	zki.ISInitStateRootFee = big.NewInt(0)
384	zki.ISFinalAccFee = newSlice(maxFeeIdxs - 1)
385
386	return zki
387}
388
389// newSlice returns a []*big.Int slice of length n with values initialized at
390// 0.
391// Is used to initialize all *big.Ints of the ZKInputs data structure, so when
392// the transactions are processed and the ZKInputs filled, there is no need to
393// set all the elements, and if a transaction does not use a parameter, can be
394// leaved as it is in the ZKInputs, as will be 0, so later when using the
395// ZKInputs to generate the zkSnark proof there is no 'nil'/'null' values.
396func newSlice(n uint32) []*big.Int {
397	s := make([]*big.Int, n)
398	for i := 0; i < len(s); i++ {
399		s[i] = big.NewInt(0)
400	}
401	return s
402}
403
404// HashGlobalData returns the HashGlobalData
405func (z ZKInputs) HashGlobalData() (*big.Int, error) {
406	b, err := z.ToHashGlobalData()
407	if err != nil {
408		return nil, tracerr.Wrap(err)
409	}
410
411	h := sha256.New()
412	_, err = h.Write(b)
413	if err != nil {
414		return nil, tracerr.Wrap(err)
415	}
416
417	r := new(big.Int).SetBytes(h.Sum(nil))
418	v := r.Mod(r, cryptoConstants.Q)
419
420	return v, nil
421}
422
423// ToHashGlobalData returns the data to be hashed in the method HashGlobalData
424func (z ZKInputs) ToHashGlobalData() ([]byte, error) {
425	var b []byte
426	bytesMaxLevels := int(z.Metadata.MaxLevels / 8) //nolint:gomnd
427
428	// [MAX_NLEVELS bits] oldLastIdx
429	oldLastIdx := make([]byte, bytesMaxLevels)
430	copy(oldLastIdx, z.OldLastIdx.Bytes())
431	b = append(b, SwapEndianness(oldLastIdx)...)
432
433	// [MAX_NLEVELS bits] newLastIdx
434	newLastIdx := make([]byte, bytesMaxLevels)
435	newLastIdxBytes, err := z.Metadata.NewLastIdxRaw.Bytes()
436	if err != nil {
437		return nil, tracerr.Wrap(err)
438	}
439	copy(newLastIdx, newLastIdxBytes[len(newLastIdxBytes)-bytesMaxLevels:])
440	b = append(b, newLastIdx...)
441
442	// [256 bits] oldStRoot
443	oldStateRoot := make([]byte, 32)
444	copy(oldStateRoot, z.OldStateRoot.Bytes())
445	b = append(b, oldStateRoot...)
446
447	// [256 bits] newStateRoot
448	newStateRoot := make([]byte, 32)
449	copy(newStateRoot, z.Metadata.NewStateRootRaw.Bytes())
450	b = append(b, newStateRoot...)
451
452	// [256 bits] newExitRoot
453	newExitRoot := make([]byte, 32)
454	copy(newExitRoot, z.Metadata.NewExitRootRaw.Bytes())
455	b = append(b, newExitRoot...)
456
457	// [MAX_L1_TX * (2 * MAX_NLEVELS + 480) bits] L1TxsData
458	l1TxDataLen := (2*z.Metadata.MaxLevels + 480)
459	l1TxsDataLen := (z.Metadata.MaxL1Tx * l1TxDataLen)
460	l1TxsData := make([]byte, l1TxsDataLen/8) //nolint:gomnd
461	for i := 0; i < len(z.Metadata.L1TxsData); i++ {
462		dataLen := int(l1TxDataLen) / 8 //nolint:gomnd
463		pos0 := i * dataLen
464		pos1 := i*dataLen + dataLen
465		copy(l1TxsData[pos0:pos1], z.Metadata.L1TxsData[i])
466	}
467	b = append(b, l1TxsData...)
468
469	var l1TxsDataAvailability []byte
470	for i := 0; i < len(z.Metadata.L1TxsDataAvailability); i++ {
471		l1TxsDataAvailability = append(l1TxsDataAvailability, z.Metadata.L1TxsDataAvailability[i]...)
472	}
473	b = append(b, l1TxsDataAvailability...)
474
475	// [MAX_TX*(2*NLevels + 24) bits] L2TxsData
476	var l2TxsData []byte
477	l2TxDataLen := 2*z.Metadata.NLevels + 24 //nolint:gomnd
478	l2TxsDataLen := (z.Metadata.MaxTx * l2TxDataLen)
479	expectedL2TxsDataLen := l2TxsDataLen / 8 //nolint:gomnd
480	for i := 0; i < len(z.Metadata.L2TxsData); i++ {
481		l2TxsData = append(l2TxsData, z.Metadata.L2TxsData[i]...)
482	}
483	if len(l2TxsData) > int(expectedL2TxsDataLen) {
484		return nil, tracerr.Wrap(fmt.Errorf("len(l2TxsData): %d, expected: %d", len(l2TxsData), expectedL2TxsDataLen))
485	}
486
487	b = append(b, l2TxsData...)
488	l2TxsPadding := make([]byte, (int(z.Metadata.MaxTx)-len(z.Metadata.L1TxsDataAvailability)-len(z.Metadata.L2TxsData))*int(l2TxDataLen)/8) //nolint:gomnd
489	b = append(b, l2TxsPadding...)
490
491	// [NLevels * MAX_TOKENS_FEE bits] feeTxsData
492	for i := 0; i < len(z.FeeIdxs); i++ {
493		var r []byte
494
495		padding := make([]byte, bytesMaxLevels/4) //nolint:gomnd
496		r = append(r, padding...)
497
498		feeIdx := make([]byte, bytesMaxLevels/2) //nolint:gomnd
499		feeIdxBytes := z.FeeIdxs[i].Bytes()
500		copy(feeIdx[len(feeIdx)-len(feeIdxBytes):], feeIdxBytes[:])
501		r = append(r, feeIdx...)
502		b = append(b, r...)
503	}
504
505	// [16 bits] chainID
506	var chainID [2]byte
507	binary.BigEndian.PutUint16(chainID[:], z.Metadata.ChainID)
508	b = append(b, chainID[:]...)
509
510	// [32 bits] currentNumBatch
511	currNumBatchBytes := z.CurrentNumBatch.Bytes()
512	var currNumBatch [4]byte
513	copy(currNumBatch[4-len(currNumBatchBytes):], currNumBatchBytes)
514	b = append(b, currNumBatch[:]...)
515
516	return b, nil
517}