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(chainID uint16, 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 zki.Metadata.ChainID = chainID
294
295 // General
296 zki.CurrentNumBatch = currentNumBatch
297 zki.OldLastIdx = big.NewInt(0)
298 zki.OldStateRoot = big.NewInt(0)
299 zki.GlobalChainID = big.NewInt(int64(chainID))
300 zki.FeeIdxs = newSlice(maxFeeIdxs)
301 zki.FeePlanTokens = newSlice(maxFeeIdxs)
302
303 // Txs
304 zki.TxCompressedData = newSlice(nTx)
305 zki.TxCompressedDataV2 = newSlice(nTx)
306 zki.MaxNumBatch = newSlice(nTx)
307 zki.FromIdx = newSlice(nTx)
308 zki.AuxFromIdx = newSlice(nTx)
309 zki.ToIdx = newSlice(nTx)
310 zki.AuxToIdx = newSlice(nTx)
311 zki.ToBJJAy = newSlice(nTx)
312 zki.ToEthAddr = newSlice(nTx)
313 zki.OnChain = newSlice(nTx)
314 zki.NewAccount = newSlice(nTx)
315
316 // L1
317 zki.DepositAmountF = newSlice(nTx)
318 zki.FromEthAddr = newSlice(nTx)
319 zki.FromBJJCompressed = make([][256]*big.Int, nTx)
320 for i := 0; i < len(zki.FromBJJCompressed); i++ {
321 // zki.FromBJJCompressed[i] = newSlice(256)
322 for j := 0; j < 256; j++ {
323 zki.FromBJJCompressed[i][j] = big.NewInt(0)
324 }
325 }
326
327 // L2
328 zki.RqOffset = newSlice(nTx)
329 zki.RqTxCompressedDataV2 = newSlice(nTx)
330 zki.RqToEthAddr = newSlice(nTx)
331 zki.RqToBJJAy = newSlice(nTx)
332 zki.S = newSlice(nTx)
333 zki.R8x = newSlice(nTx)
334 zki.R8y = newSlice(nTx)
335
336 // State MerkleTree Leafs transitions
337 zki.TokenID1 = newSlice(nTx)
338 zki.Nonce1 = newSlice(nTx)
339 zki.Sign1 = newSlice(nTx)
340 zki.Ay1 = newSlice(nTx)
341 zki.Balance1 = newSlice(nTx)
342 zki.EthAddr1 = newSlice(nTx)
343 zki.Siblings1 = make([][]*big.Int, nTx)
344 for i := 0; i < len(zki.Siblings1); i++ {
345 zki.Siblings1[i] = newSlice(nLevels + 1)
346 }
347 zki.IsOld0_1 = newSlice(nTx)
348 zki.OldKey1 = newSlice(nTx)
349 zki.OldValue1 = newSlice(nTx)
350
351 zki.TokenID2 = newSlice(nTx)
352 zki.Nonce2 = newSlice(nTx)
353 zki.Sign2 = newSlice(nTx)
354 zki.Ay2 = newSlice(nTx)
355 zki.Balance2 = newSlice(nTx)
356 zki.EthAddr2 = newSlice(nTx)
357 zki.Siblings2 = make([][]*big.Int, nTx)
358 for i := 0; i < len(zki.Siblings2); i++ {
359 zki.Siblings2[i] = newSlice(nLevels + 1)
360 }
361 zki.NewExit = newSlice(nTx)
362 zki.IsOld0_2 = newSlice(nTx)
363 zki.OldKey2 = newSlice(nTx)
364 zki.OldValue2 = newSlice(nTx)
365
366 zki.TokenID3 = newSlice(maxFeeIdxs)
367 zki.Nonce3 = newSlice(maxFeeIdxs)
368 zki.Sign3 = newSlice(maxFeeIdxs)
369 zki.Ay3 = newSlice(maxFeeIdxs)
370 zki.Balance3 = newSlice(maxFeeIdxs)
371 zki.EthAddr3 = newSlice(maxFeeIdxs)
372 zki.Siblings3 = make([][]*big.Int, maxFeeIdxs)
373 for i := 0; i < len(zki.Siblings3); i++ {
374 zki.Siblings3[i] = newSlice(nLevels + 1)
375 }
376
377 // Intermediate States
378 zki.ISOnChain = newSlice(nTx - 1)
379 zki.ISOutIdx = newSlice(nTx - 1)
380 zki.ISStateRoot = newSlice(nTx - 1)
381 zki.ISExitRoot = newSlice(nTx - 1)
382 zki.ISAccFeeOut = make([][]*big.Int, nTx-1)
383 for i := 0; i < len(zki.ISAccFeeOut); i++ {
384 zki.ISAccFeeOut[i] = newSlice(maxFeeIdxs)
385 }
386 zki.ISStateRootFee = newSlice(maxFeeIdxs - 1)
387 zki.ISInitStateRootFee = big.NewInt(0)
388 zki.ISFinalAccFee = newSlice(maxFeeIdxs)
389
390 return zki
391}
392
393// newSlice returns a []*big.Int slice of length n with values initialized at
394// 0.
395// Is used to initialize all *big.Ints of the ZKInputs data structure, so when
396// the transactions are processed and the ZKInputs filled, there is no need to
397// set all the elements, and if a transaction does not use a parameter, can be
398// leaved as it is in the ZKInputs, as will be 0, so later when using the
399// ZKInputs to generate the zkSnark proof there is no 'nil'/'null' values.
400func newSlice(n uint32) []*big.Int {
401 s := make([]*big.Int, n)
402 for i := 0; i < len(s); i++ {
403 s[i] = big.NewInt(0)
404 }
405 return s
406}
407
408// HashGlobalData returns the HashGlobalData
409func (z ZKInputs) HashGlobalData() (*big.Int, error) {
410 b, err := z.ToHashGlobalData()
411 if err != nil {
412 return nil, tracerr.Wrap(err)
413 }
414
415 h := sha256.New()
416 _, err = h.Write(b)
417 if err != nil {
418 return nil, tracerr.Wrap(err)
419 }
420
421 r := new(big.Int).SetBytes(h.Sum(nil))
422 v := r.Mod(r, cryptoConstants.Q)
423
424 return v, nil
425}
426
427// ToHashGlobalData returns the data to be hashed in the method HashGlobalData
428func (z ZKInputs) ToHashGlobalData() ([]byte, error) {
429 var b []byte
430 bytesMaxLevels := int(z.Metadata.MaxLevels / 8) //nolint:gomnd
431 bytesNLevels := int(z.Metadata.NLevels / 8) //nolint:gomnd
432
433 // [MAX_NLEVELS bits] oldLastIdx
434 oldLastIdx := make([]byte, bytesMaxLevels)
435 oldLastIdxBytes := z.OldLastIdx.Bytes()
436 copy(oldLastIdx[len(oldLastIdx)-len(oldLastIdxBytes):], oldLastIdxBytes)
437 b = append(b, oldLastIdx...)
438
439 // [MAX_NLEVELS bits] newLastIdx
440 newLastIdx := make([]byte, bytesMaxLevels)
441 newLastIdxBytes, err := z.Metadata.NewLastIdxRaw.Bytes()
442 if err != nil {
443 return nil, tracerr.Wrap(err)
444 }
445 copy(newLastIdx, newLastIdxBytes[len(newLastIdxBytes)-bytesMaxLevels:])
446 b = append(b, newLastIdx...)
447
448 // [256 bits] oldStRoot
449 oldStateRoot := make([]byte, 32)
450 copy(oldStateRoot, z.OldStateRoot.Bytes())
451 b = append(b, oldStateRoot...)
452
453 // [256 bits] newStateRoot
454 newStateRoot := make([]byte, 32)
455 copy(newStateRoot, z.Metadata.NewStateRootRaw.Bytes())
456 b = append(b, newStateRoot...)
457
458 // [256 bits] newExitRoot
459 newExitRoot := make([]byte, 32)
460 copy(newExitRoot, z.Metadata.NewExitRootRaw.Bytes())
461 b = append(b, newExitRoot...)
462
463 // [MAX_L1_TX * (2 * MAX_NLEVELS + 480) bits] L1TxsData
464 l1TxDataLen := (2*z.Metadata.MaxLevels + 480)
465 l1TxsDataLen := (z.Metadata.MaxL1Tx * l1TxDataLen)
466 l1TxsData := make([]byte, l1TxsDataLen/8) //nolint:gomnd
467 for i := 0; i < len(z.Metadata.L1TxsData); i++ {
468 dataLen := int(l1TxDataLen) / 8 //nolint:gomnd
469 pos0 := i * dataLen
470 pos1 := i*dataLen + dataLen
471 copy(l1TxsData[pos0:pos1], z.Metadata.L1TxsData[i])
472 }
473 b = append(b, l1TxsData...)
474
475 var l1TxsDataAvailability []byte
476 for i := 0; i < len(z.Metadata.L1TxsDataAvailability); i++ {
477 l1TxsDataAvailability = append(l1TxsDataAvailability, z.Metadata.L1TxsDataAvailability[i]...)
478 }
479 b = append(b, l1TxsDataAvailability...)
480
481 // [MAX_TX*(2*NLevels + 24) bits] L2TxsData
482 var l2TxsData []byte
483 l2TxDataLen := 2*z.Metadata.NLevels + 24 //nolint:gomnd
484 l2TxsDataLen := (z.Metadata.MaxTx * l2TxDataLen)
485 expectedL2TxsDataLen := l2TxsDataLen / 8 //nolint:gomnd
486 for i := 0; i < len(z.Metadata.L2TxsData); i++ {
487 l2TxsData = append(l2TxsData, z.Metadata.L2TxsData[i]...)
488 }
489 if len(l2TxsData) > int(expectedL2TxsDataLen) {
490 return nil, tracerr.Wrap(fmt.Errorf("len(l2TxsData): %d, expected: %d", len(l2TxsData), expectedL2TxsDataLen))
491 }
492
493 b = append(b, l2TxsData...)
494 l2TxsPadding := make([]byte, (int(z.Metadata.MaxTx)-len(z.Metadata.L1TxsDataAvailability)-len(z.Metadata.L2TxsData))*int(l2TxDataLen)/8) //nolint:gomnd
495 b = append(b, l2TxsPadding...)
496
497 // [NLevels * MAX_TOKENS_FEE bits] feeTxsData
498 for i := 0; i < len(z.FeeIdxs); i++ {
499 feeIdx := make([]byte, bytesNLevels) //nolint:gomnd
500 feeIdxBytes := z.FeeIdxs[i].Bytes()
501 copy(feeIdx[len(feeIdx)-len(feeIdxBytes):], feeIdxBytes[:])
502 b = append(b, feeIdx...)
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}