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
3//nolint:deadcode,structcheck,unused
4package common
5
6import "math/big"
7
8// circuit parameters
9// absolute maximum of L1 or L2 transactions allowed
10type nTx uint32
11
12// merkle tree depth
13type nLevels uint32
14
15// absolute maximum of L1 transaction allowed
16type maxL1Tx uint32
17
18//absolute maximum of fee transactions allowed
19type maxFeeTx uint32
20
21// ZKInputs represents the inputs that will be used to generate the zkSNARK proof
22type ZKInputs struct {
23 //
24 // General
25 //
26
27 // inputs for final `hashGlobalInputs`
28 // OldLastIdx is the last index assigned to an account
29 OldLastIdx *big.Int // uint64 (max nLevels bits)
30 // OldStateRoot is the current state merkle tree root
31 OldStateRoot *big.Int // Hash
32 // GlobalChainID is the blockchain ID (0 for Ethereum mainnet). This value can be get from the smart contract.
33 GlobalChainID *big.Int // uint16
34 // FeeIdxs is an array of merkle tree indexes where the coordinator will receive the accumulated fees
35 FeeIdxs []*big.Int // uint64 (max nLevels bits), len: [maxFeeTx]
36
37 // accumulate fees
38 // FeePlanTokens contains all the tokenIDs for which the fees are being accumulated
39 FeePlanTokens []*big.Int // uint32 (max 32 bits), len: [maxFeeTx]
40
41 //
42 // Txs (L1&L2)
43 //
44
45 // transaction L1-L2
46 // TxCompressedData
47 TxCompressedData []*big.Int // big.Int (max 251 bits), len: [nTx]
48 // TxCompressedDataV2, only used in L2Txs, in L1Txs is set to 0
49 TxCompressedDataV2 []*big.Int // big.Int (max 193 bits), len: [nTx]
50
51 // FromIdx
52 FromIdx []*big.Int // uint64 (max nLevels bits), len: [nTx]
53 // AuxFromIdx is the Idx of the new created account which is consequence of a L1CreateAccountTx
54 AuxFromIdx []*big.Int // uint64 (max nLevels bits), len: [nTx]
55
56 // ToIdx
57 ToIdx []*big.Int // uint64 (max nLevels bits), len: [nTx]
58 // 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'
59 AuxToIdx []*big.Int // uint64 (max nLevels bits), len: [nTx]
60 // ToBJJAy
61 ToBJJAy []*big.Int // big.Int, len: [nTx]
62 // ToEthAddr
63 ToEthAddr []*big.Int // ethCommon.Address, len: [nTx]
64
65 // OnChain determines if is L1 (1/true) or L2 (0/false)
66 OnChain []*big.Int // bool, len: [nTx]
67
68 //
69 // Txs/L1Txs
70 //
71 // NewAccount boolean (0/1) flag set 'true' when L1 tx creates a new account (fromIdx==0)
72 NewAccount []*big.Int // bool, len: [nTx]
73 // LoadAmountF encoded as float16
74 LoadAmountF []*big.Int // uint16, len: [nTx]
75 // FromEthAddr
76 FromEthAddr []*big.Int // ethCommon.Address, len: [nTx]
77 // FromBJJCompressed boolean encoded where each value is a *big.Int
78 FromBJJCompressed [][256]*big.Int // bool array, len: [nTx][256]
79
80 //
81 // Txs/L2Txs
82 //
83
84 // RqOffset relative transaction position to be linked. Used to perform atomic transactions.
85 RqOffset []*big.Int // uint8 (max 3 bits), len: [nTx]
86
87 // transaction L2 request data
88 // RqTxCompressedDataV2
89 RqTxCompressedDataV2 []*big.Int // big.Int (max 251 bits), len: [nTx]
90 // RqToEthAddr
91 RqToEthAddr []*big.Int // ethCommon.Address, len: [nTx]
92 // RqToBJJAy
93 RqToBJJAy []*big.Int // big.Int, len: [nTx]
94
95 // transaction L2 signature
96 // S
97 S []*big.Int // big.Int, len: [nTx]
98 // R8x
99 R8x []*big.Int // big.Int, len: [nTx]
100 // R8y
101 R8y []*big.Int // big.Int, len: [nTx]
102
103 //
104 // State MerkleTree Leafs transitions
105 //
106
107 // state 1, value of the sender (from) account leaf
108 TokenID1 []*big.Int // uint32, len: [nTx]
109 Nonce1 []*big.Int // uint64 (max 40 bits), len: [nTx]
110 Sign1 []*big.Int // bool, len: [nTx]
111 Ay1 []*big.Int // big.Int, len: [nTx]
112 Balance1 []*big.Int // big.Int (max 192 bits), len: [nTx]
113 EthAddr1 []*big.Int // ethCommon.Address, len: [nTx]
114 Siblings1 [][]*big.Int // big.Int, len: [nTx][nLevels + 1]
115 // Required for inserts and deletes, values of the CircomProcessorProof (smt insert proof)
116 IsOld0_1 []*big.Int // bool, len: [nTx]
117 OldKey1 []*big.Int // uint64 (max 40 bits), len: [nTx]
118 OldValue1 []*big.Int // Hash, len: [nTx]
119
120 // state 2, value of the receiver (to) account leaf
121 // if Tx is an Exit, state 2 is used for the Exit Merkle Proof
122 TokenID2 []*big.Int // uint32, len: [nTx]
123 Nonce2 []*big.Int // uint64 (max 40 bits), len: [nTx]
124 Sign2 []*big.Int // bool, len: [nTx]
125 Ay2 []*big.Int // big.Int, len: [nTx]
126 Balance2 []*big.Int // big.Int (max 192 bits), len: [nTx]
127 EthAddr2 []*big.Int // ethCommon.Address, len: [nTx]
128 Siblings2 [][]*big.Int // big.Int, len: [nTx][nLevels + 1]
129 // newExit determines if an exit transaction has to create a new leaf in the exit tree
130 NewExit []*big.Int // bool, len: [nTx]
131 // Required for inserts and deletes, values of the CircomProcessorProof (smt insert proof)
132 IsOld0_2 []*big.Int // bool, len: [nTx]
133 OldKey2 []*big.Int // uint64 (max 40 bits), len: [nTx]
134 OldValue2 []*big.Int // Hash, len: [nTx]
135
136 // state 3, value of the account leaf receiver of the Fees
137 // fee tx
138 // State fees
139 TokenID3 []*big.Int // uint32, len: [maxFeeTx]
140 Nonce3 []*big.Int // uint64 (max 40 bits), len: [maxFeeTx]
141 Sign3 []*big.Int // bool, len: [maxFeeTx]
142 Ay3 []*big.Int // big.Int, len: [maxFeeTx]
143 Balance3 []*big.Int // big.Int (max 192 bits), len: [maxFeeTx]
144 EthAddr3 []*big.Int // ethCommon.Address, len: [maxFeeTx]
145 Siblings3 [][]*big.Int // Hash, len: [maxFeeTx][nLevels + 1]
146
147 //
148 // Intermediate States
149 //
150
151 // Intermediate States to parallelize witness computation
152 // Note: the Intermediate States (IS) of the last transaction does not
153 // exist. Meaning that transaction 3 (4th) will fill the parameters
154 // FromIdx[3] and ISOnChain[3], but last transaction (nTx-1) will fill
155 // FromIdx[nTx-1] but will not fill ISOnChain. That's why IS have
156 // length of nTx-1, while the other parameters have length of nTx.
157 // Last transaction does not need intermediate state since its output
158 // will not be used.
159
160 // decode-tx
161 // ISOnChain indicates if tx is L1 (true) or L2 (false)
162 ISOnChain []*big.Int // bool, len: [nTx - 1]
163 // ISOutIdx current index account for each Tx
164 ISOutIdx []*big.Int // uint64 (max nLevels bits), len: [nTx - 1]
165 // rollup-tx
166 // ISStateRoot root at the moment of the Tx, the state root value once the Tx is processed into the state tree
167 ISStateRoot []*big.Int // Hash, len: [nTx - 1]
168 // ISExitTree root at the moment of the Tx the value once the Tx is processed into the exit tree
169 ISExitRoot []*big.Int // Hash, len: [nTx - 1]
170 // ISAccFeeOut accumulated fees once the Tx is processed
171 ISAccFeeOut [][]*big.Int // big.Int, len: [nTx - 1][maxFeeTx]
172 // fee-tx
173 // ISStateRootFee root at the moment of the Tx, the state root value once the Tx is processed into the state tree
174 ISStateRootFee []*big.Int // Hash, len: [maxFeeTx - 1]
175 // ISInitStateRootFee state root once all L1-L2 tx are processed (before computing the fees-tx)
176 ISInitStateRootFee *big.Int // Hash
177 // ISFinalAccFee final accumulated fees (before computing the fees-tx)
178 ISFinalAccFee []*big.Int // big.Int, len: [maxFeeTx - 1]
179}
180
181// NewZKInputs returns a pointer to an initialized struct of ZKInputs
182func NewZKInputs(nTx, maxFeeTx, nLevels int) *ZKInputs {
183 zki := &ZKInputs{}
184
185 // General
186 zki.OldLastIdx = big.NewInt(0)
187 zki.OldStateRoot = big.NewInt(0)
188 zki.GlobalChainID = big.NewInt(0)
189 zki.FeeIdxs = newSlice(maxFeeTx)
190 zki.FeePlanTokens = newSlice(maxFeeTx)
191
192 // Txs
193 zki.TxCompressedData = newSlice(nTx)
194 zki.TxCompressedDataV2 = newSlice(nTx)
195 zki.FromIdx = newSlice(nTx)
196 zki.AuxFromIdx = newSlice(nTx)
197 zki.ToIdx = newSlice(nTx)
198 zki.AuxToIdx = newSlice(nTx)
199 zki.ToBJJAy = newSlice(nTx)
200 zki.ToEthAddr = newSlice(nTx)
201 zki.OnChain = newSlice(nTx)
202 zki.NewAccount = newSlice(nTx)
203
204 // L1
205 zki.LoadAmountF = newSlice(nTx)
206 zki.FromEthAddr = newSlice(nTx)
207 zki.FromBJJCompressed = make([][256]*big.Int, nTx)
208 for i := 0; i < len(zki.FromBJJCompressed); i++ {
209 // zki.FromBJJCompressed[i] = newSlice(256)
210 for j := 0; j < 256; j++ {
211 zki.FromBJJCompressed[i][j] = big.NewInt(0)
212 }
213 }
214
215 // L2
216 zki.RqOffset = newSlice(nTx)
217 zki.RqTxCompressedDataV2 = newSlice(nTx)
218 zki.RqToEthAddr = newSlice(nTx)
219 zki.RqToBJJAy = newSlice(nTx)
220 zki.S = newSlice(nTx)
221 zki.R8x = newSlice(nTx)
222 zki.R8y = newSlice(nTx)
223
224 // State MerkleTree Leafs transitions
225 zki.TokenID1 = newSlice(nTx)
226 zki.Nonce1 = newSlice(nTx)
227 zki.Sign1 = newSlice(nTx)
228 zki.Ay1 = newSlice(nTx)
229 zki.Balance1 = newSlice(nTx)
230 zki.EthAddr1 = newSlice(nTx)
231 zki.Siblings1 = make([][]*big.Int, nTx)
232 for i := 0; i < len(zki.Siblings1); i++ {
233 zki.Siblings1[i] = newSlice(nLevels + 1)
234 }
235 zki.IsOld0_1 = newSlice(nTx)
236 zki.OldKey1 = newSlice(nTx)
237 zki.OldValue1 = newSlice(nTx)
238
239 zki.TokenID2 = newSlice(nTx)
240 zki.Nonce2 = newSlice(nTx)
241 zki.Sign2 = newSlice(nTx)
242 zki.Ay2 = newSlice(nTx)
243 zki.Balance2 = newSlice(nTx)
244 zki.EthAddr2 = newSlice(nTx)
245 zki.Siblings2 = make([][]*big.Int, nTx)
246 for i := 0; i < len(zki.Siblings2); i++ {
247 zki.Siblings2[i] = newSlice(nLevels + 1)
248 }
249 zki.NewExit = newSlice(nTx)
250 zki.IsOld0_2 = newSlice(nTx)
251 zki.OldKey2 = newSlice(nTx)
252 zki.OldValue2 = newSlice(nTx)
253
254 zki.TokenID3 = newSlice(maxFeeTx)
255 zki.Nonce3 = newSlice(maxFeeTx)
256 zki.Sign3 = newSlice(maxFeeTx)
257 zki.Ay3 = newSlice(maxFeeTx)
258 zki.Balance3 = newSlice(maxFeeTx)
259 zki.EthAddr3 = newSlice(maxFeeTx)
260 zki.Siblings3 = make([][]*big.Int, maxFeeTx)
261 for i := 0; i < len(zki.Siblings3); i++ {
262 zki.Siblings3[i] = newSlice(nLevels + 1)
263 }
264
265 // Intermediate States
266 zki.ISOnChain = newSlice(nTx - 1)
267 zki.ISOutIdx = newSlice(nTx - 1)
268 zki.ISStateRoot = newSlice(nTx - 1)
269 zki.ISExitRoot = newSlice(nTx - 1)
270 zki.ISAccFeeOut = make([][]*big.Int, nTx-1)
271 for i := 0; i < len(zki.ISAccFeeOut); i++ {
272 zki.ISAccFeeOut[i] = newSlice(maxFeeTx)
273 }
274 zki.ISStateRootFee = newSlice(maxFeeTx - 1)
275 zki.ISInitStateRootFee = big.NewInt(0)
276 zki.ISFinalAccFee = newSlice(maxFeeTx - 1)
277
278 return zki
279}
280
281// newSlice returns a []*big.Int slice of length n with values initialized at
282// 0.
283// Is used to initialize all *big.Ints of the ZKInputs data structure, so when
284// the transactions are processed and the ZKInputs filled, there is no need to
285// set all the elements, and if a transaction does not use a parameter, can be
286// leaved as it is in the ZKInputs, as will be 0, so later when using the
287// ZKInputs to generate the zkSnark proof there is no 'nil'/'null' values.
288func newSlice(n int) []*big.Int {
289 s := make([]*big.Int, n)
290 for i := 0; i < len(s); i++ {
291 s[i] = big.NewInt(0)
292 }
293 return s
294}