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