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
  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	// decode-tx
153	// ISOnChain indicates if tx is L1 (true) or L2 (false)
154	ISOnChain []*big.Int // bool, len: [nTx - 1]
155	// ISOutIdx current index account for each Tx
156	ISOutIdx []*big.Int // uint64 (max nLevels bits), len: [nTx - 1]
157	// rollup-tx
158	// ISStateRoot root at the moment of the Tx, the state root value once the Tx is processed into the state tree
159	ISStateRoot []*big.Int // Hash, len: [nTx - 1]
160	// ISExitTree root at the moment of the Tx the value once the Tx is processed into the exit tree
161	ISExitRoot []*big.Int // Hash, len: [nTx - 1]
162	// ISAccFeeOut accumulated fees once the Tx is processed
163	ISAccFeeOut [][]*big.Int // big.Int, len: [nTx - 1][maxFeeTx]
164	// fee-tx
165	// ISStateRootFee root at the moment of the Tx, the state root value once the Tx is processed into the state tree
166	ISStateRootFee []*big.Int // Hash, len: [maxFeeTx - 1]
167	// ISInitStateRootFee state root once all L1-L2 tx are processed (before computing the fees-tx)
168	ISInitStateRootFee *big.Int // Hash
169	// ISFinalAccFee final accumulated fees (before computing the fees-tx)
170	ISFinalAccFee []*big.Int // big.Int, len: [maxFeeTx - 1]
171}
172
173// NewZKInputs returns a pointer to an initialized struct of ZKInputs
174func NewZKInputs(nTx, maxFeeTx, nLevels int) *ZKInputs {
175	zki := &ZKInputs{}
176
177	// General
178	zki.OldLastIdx = big.NewInt(0)
179	zki.OldStateRoot = big.NewInt(0)
180	zki.GlobalChainID = big.NewInt(0)
181	zki.FeeIdxs = newSlice(maxFeeTx)
182	zki.FeePlanTokens = newSlice(maxFeeTx)
183
184	// Txs
185	zki.TxCompressedData = newSlice(nTx)
186	zki.TxCompressedDataV2 = newSlice(nTx)
187	zki.FromIdx = newSlice(nTx)
188	zki.AuxFromIdx = newSlice(nTx)
189	zki.ToIdx = newSlice(nTx)
190	zki.AuxToIdx = newSlice(nTx)
191	zki.ToBJJAy = newSlice(nTx)
192	zki.ToEthAddr = newSlice(nTx)
193	zki.OnChain = newSlice(nTx)
194	zki.NewAccount = newSlice(nTx)
195
196	// L1
197	zki.LoadAmountF = newSlice(nTx)
198	zki.FromEthAddr = newSlice(nTx)
199	zki.FromBJJCompressed = make([][256]*big.Int, nTx)
200	for i := 0; i < len(zki.FromBJJCompressed); i++ {
201		// zki.FromBJJCompressed[i] = newSlice(256)
202		for j := 0; j < 256; j++ {
203			zki.FromBJJCompressed[i][j] = big.NewInt(0)
204		}
205	}
206
207	// L2
208	zki.RqOffset = newSlice(nTx)
209	zki.RqTxCompressedDataV2 = newSlice(nTx)
210	zki.RqToEthAddr = newSlice(nTx)
211	zki.RqToBJJAy = newSlice(nTx)
212	zki.S = newSlice(nTx)
213	zki.R8x = newSlice(nTx)
214	zki.R8y = newSlice(nTx)
215
216	// State MerkleTree Leafs transitions
217	zki.TokenID1 = newSlice(nTx)
218	zki.Nonce1 = newSlice(nTx)
219	zki.Sign1 = newSlice(nTx)
220	zki.Ay1 = newSlice(nTx)
221	zki.Balance1 = newSlice(nTx)
222	zki.EthAddr1 = newSlice(nTx)
223	zki.Siblings1 = make([][]*big.Int, nTx)
224	for i := 0; i < len(zki.Siblings1); i++ {
225		zki.Siblings1[i] = newSlice(nLevels + 1)
226	}
227	zki.IsOld0_1 = newSlice(nTx)
228	zki.OldKey1 = newSlice(nTx)
229	zki.OldValue1 = newSlice(nTx)
230
231	zki.TokenID2 = newSlice(nTx)
232	zki.Nonce2 = newSlice(nTx)
233	zki.Sign2 = newSlice(nTx)
234	zki.Ay2 = newSlice(nTx)
235	zki.Balance2 = newSlice(nTx)
236	zki.EthAddr2 = newSlice(nTx)
237	zki.Siblings2 = make([][]*big.Int, nTx)
238	for i := 0; i < len(zki.Siblings2); i++ {
239		zki.Siblings2[i] = newSlice(nLevels + 1)
240	}
241	zki.NewExit = newSlice(nTx)
242	zki.IsOld0_2 = newSlice(nTx)
243	zki.OldKey2 = newSlice(nTx)
244	zki.OldValue2 = newSlice(nTx)
245
246	zki.TokenID3 = newSlice(maxFeeTx)
247	zki.Nonce3 = newSlice(maxFeeTx)
248	zki.Sign3 = newSlice(maxFeeTx)
249	zki.Ay3 = newSlice(maxFeeTx)
250	zki.Balance3 = newSlice(maxFeeTx)
251	zki.EthAddr3 = newSlice(maxFeeTx)
252	zki.Siblings3 = make([][]*big.Int, maxFeeTx)
253	for i := 0; i < len(zki.Siblings3); i++ {
254		zki.Siblings3[i] = newSlice(nLevels + 1)
255	}
256
257	// Intermediate States
258	zki.ISOnChain = newSlice(nTx - 1)
259	zki.ISOutIdx = newSlice(nTx - 1)
260	zki.ISStateRoot = newSlice(nTx - 1)
261	zki.ISExitRoot = newSlice(nTx - 1)
262	zki.ISAccFeeOut = make([][]*big.Int, nTx-1)
263	for i := 0; i < len(zki.ISAccFeeOut); i++ {
264		zki.ISAccFeeOut[i] = newSlice(maxFeeTx)
265	}
266	zki.ISStateRootFee = newSlice(maxFeeTx - 1)
267	zki.ISInitStateRootFee = big.NewInt(0)
268	zki.ISFinalAccFee = newSlice(maxFeeTx - 1)
269
270	return zki
271}
272
273// newSlice returns a []*big.Int slice of length n with values initialized at
274// 0.
275// Is used to initialize all *big.Ints of the ZKInputs data structure, so when
276// the transactions are processed and the ZKInputs filled, there is no need to
277// set all the elements, and if a transaction does not use a parameter, can be
278// leaved as it is in the ZKInputs, as will be 0, so later when using the
279// ZKInputs to generate the zkSnark proof there is no 'nil'/'null' values.
280func newSlice(n int) []*big.Int {
281	s := make([]*big.Int, n)
282	for i := 0; i < len(s); i++ {
283		s[i] = big.NewInt(0)
284	}
285	return s
286}