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