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	// NewAccount boolean (0/1) flag set 'true' when L1 tx creates a new account (fromIdx==0)
 68	NewAccount []*big.Int // bool, len: [nTx]
 69
 70	//
 71	// Txs/L1Txs
 72	//
 73	// transaction L1
 74	// LoadAmountF encoded as float16
 75	LoadAmountF []*big.Int // uint16, len: [nTx]
 76	// FromEthAddr
 77	FromEthAddr []*big.Int // ethCommon.Address, len: [nTx]
 78	// FromBJJCompressed boolean encoded where each value is a *big.Int
 79	FromBJJCompressed [][256]*big.Int // bool array, len: [nTx][256]
 80
 81	//
 82	// Txs/L2Txs
 83	//
 84
 85	// RqOffset relative transaction position to be linked. Used to perform atomic transactions.
 86	RqOffset []*big.Int // uint8 (max 3 bits), len: [nTx]
 87
 88	// transaction L2 request data
 89	// RqTxCompressedDataV2
 90	RqTxCompressedDataV2 []*big.Int // big.Int (max 251 bits), len: [nTx]
 91	// RqToEthAddr
 92	RqToEthAddr []*big.Int // ethCommon.Address, len: [nTx]
 93	// RqToBJJAy
 94	RqToBJJAy []*big.Int // big.Int, len: [nTx]
 95
 96	// transaction L2 signature
 97	// S
 98	S []*big.Int // big.Int, len: [nTx]
 99	// R8x
100	R8x []*big.Int // big.Int, len: [nTx]
101	// R8y
102	R8y []*big.Int // big.Int, len: [nTx]
103
104	//
105	// State MerkleTree Leafs transitions
106	//
107
108	// state 1, value of the sender (from) account leaf
109	TokenID1  []*big.Int   // uint32, len: [nTx]
110	Nonce1    []*big.Int   // uint64 (max 40 bits), len: [nTx]
111	Sign1     []*big.Int   // bool, len: [nTx]
112	Balance1  []*big.Int   // big.Int (max 192 bits), len: [nTx]
113	Ay1       []*big.Int   // big.Int, len: [nTx]
114	EthAddr1  []*big.Int   // ethCommon.Address, len: [nTx]
115	Siblings1 [][]*big.Int // big.Int, len: [nTx][nLevels + 1]
116	// Required for inserts and deletes, values of the CircomProcessorProof (smt insert proof)
117	IsOld0_1  []*big.Int // bool, len: [nTx]
118	OldKey1   []*big.Int // uint64 (max 40 bits), len: [nTx]
119	OldValue1 []*big.Int // Hash, len: [nTx]
120
121	// state 2, value of the receiver (to) account leaf
122	// if Tx is an Exit, state 2 is used for the Exit Merkle Proof
123	TokenID2  []*big.Int   // uint32, len: [nTx]
124	Nonce2    []*big.Int   // uint64 (max 40 bits), len: [nTx]
125	Sign2     []*big.Int   // bool, len: [nTx]
126	Balance2  []*big.Int   // big.Int (max 192 bits), len: [nTx]
127	Ay2       []*big.Int   // big.Int, len: [nTx]
128	EthAddr2  []*big.Int   // ethCommon.Address, len: [nTx]
129	Siblings2 [][]*big.Int // big.Int, len: [nTx][nLevels + 1]
130	// newExit determines if an exit transaction has to create a new leaf in the exit tree
131	NewExit []*big.Int // bool, len: [nTx]
132	// Required for inserts and deletes, values of the CircomProcessorProof (smt insert proof)
133	IsOld0_2  []*big.Int // bool, len: [nTx]
134	OldKey2   []*big.Int // uint64 (max 40 bits), len: [nTx]
135	OldValue2 []*big.Int // Hash, len: [nTx]
136
137	// state 3, value of the account leaf receiver of the Fees
138	// fee tx
139	// State fees
140	TokenID3  []*big.Int   // uint32, len: [maxFeeTx]
141	Nonce3    []*big.Int   // uint64 (max 40 bits), len: [maxFeeTx]
142	Sign3     []*big.Int   // bool, len: [maxFeeTx]
143	Balance3  []*big.Int   // big.Int (max 192 bits), len: [maxFeeTx]
144	Ay3       []*big.Int   // big.Int, len: [maxFeeTx]
145	EthAddr3  []*big.Int   // ethCommon.Address, len: [maxFeeTx]
146	Siblings3 [][]*big.Int // Hash, len: [maxFeeTx][nLevels + 1]
147
148	//
149	// Intermediate States
150	//
151
152	// Intermediate States to parallelize witness computation
153	// decode-tx
154	// ISOnChain indicates if tx is L1 (true) or L2 (false)
155	ISOnChain []*big.Int // bool, len: [nTx - 1]
156	// ISOutIdx current index account for each Tx
157	ISOutIdx []*big.Int // uint64 (max nLevels bits), len: [nTx - 1]
158	// rollup-tx
159	// ISStateRoot root at the moment of the Tx, the state root value once the Tx is processed into the state tree
160	ISStateRoot []*big.Int // Hash, len: [nTx - 1]
161	// ISExitTree root at the moment of the Tx the value once the Tx is processed into the exit tree
162	ISExitRoot []*big.Int // Hash, len: [nTx - 1]
163	// ISAccFeeOut accumulated fees once the Tx is processed
164	ISAccFeeOut [][]*big.Int // big.Int, len: [nTx - 1][maxFeeTx]
165	// fee-tx
166	// ISStateRootFee root at the moment of the Tx, the state root value once the Tx is processed into the state tree
167	ISStateRootFee []*big.Int // Hash, len: [maxFeeTx - 1]
168	// ISInitStateRootFee state root once all L1-L2 tx are processed (before computing the fees-tx)
169	ISInitStateRootFee *big.Int // Hash
170	// ISFinalAccFee final accumulated fees (before computing the fees-tx)
171	ISFinalAccFee []*big.Int // big.Int, len: [maxFeeTx - 1]
172}
173
174// NewZKInputs returns a pointer to an initialized struct of ZKInputs
175func NewZKInputs(nTx, maxFeeTx, nLevels int) *ZKInputs {
176	zki := &ZKInputs{}
177
178	// General
179	zki.OldLastIdx = big.NewInt(0)
180	zki.OldStateRoot = big.NewInt(0)
181	zki.GlobalChainID = big.NewInt(0)
182	zki.FeeIdxs = newSlice(maxFeeTx)
183	zki.FeePlanTokens = newSlice(maxFeeTx)
184
185	// Txs
186	zki.TxCompressedData = newSlice(nTx)
187	zki.TxCompressedDataV2 = newSlice(nTx)
188	zki.FromIdx = newSlice(nTx)
189	zki.AuxFromIdx = newSlice(nTx)
190	zki.ToIdx = newSlice(nTx)
191	zki.AuxToIdx = newSlice(nTx)
192	zki.ToBJJAy = newSlice(nTx)
193	zki.ToEthAddr = newSlice(nTx)
194	zki.OnChain = newSlice(nTx)
195	zki.NewAccount = newSlice(nTx)
196
197	// L1
198	zki.LoadAmountF = newSlice(nTx)
199	zki.FromEthAddr = newSlice(nTx)
200	zki.FromBJJCompressed = make([][256]*big.Int, nTx)
201	for i := 0; i < len(zki.FromBJJCompressed); i++ {
202		// zki.FromBJJCompressed[i] = newSlice(256)
203		for j := 0; j < 256; j++ {
204			zki.FromBJJCompressed[i][j] = big.NewInt(0)
205		}
206	}
207
208	// L2
209	zki.RqOffset = newSlice(nTx)
210	zki.RqTxCompressedDataV2 = newSlice(nTx)
211	zki.RqToEthAddr = newSlice(nTx)
212	zki.RqToBJJAy = newSlice(nTx)
213	zki.S = newSlice(nTx)
214	zki.R8x = newSlice(nTx)
215	zki.R8y = newSlice(nTx)
216
217	// State MerkleTree Leafs transitions
218	zki.TokenID1 = newSlice(nTx)
219	zki.Nonce1 = newSlice(nTx)
220	zki.Sign1 = newSlice(nTx)
221	zki.Balance1 = newSlice(nTx)
222	zki.Ay1 = newSlice(nTx)
223	zki.EthAddr1 = newSlice(nTx)
224	zki.Siblings1 = make([][]*big.Int, nTx)
225	for i := 0; i < len(zki.Siblings1); i++ {
226		zki.Siblings1[i] = newSlice(nLevels + 1)
227	}
228	zki.IsOld0_1 = newSlice(nTx)
229	zki.OldKey1 = newSlice(nTx)
230	zki.OldValue1 = newSlice(nTx)
231
232	zki.TokenID2 = newSlice(nTx)
233	zki.Nonce2 = newSlice(nTx)
234	zki.Sign2 = newSlice(nTx)
235	zki.Balance2 = newSlice(nTx)
236	zki.Ay2 = newSlice(nTx)
237	zki.EthAddr2 = newSlice(nTx)
238	zki.Siblings2 = make([][]*big.Int, nTx)
239	for i := 0; i < len(zki.Siblings2); i++ {
240		zki.Siblings2[i] = newSlice(nLevels + 1)
241	}
242	zki.NewExit = newSlice(nTx)
243	zki.IsOld0_2 = newSlice(nTx)
244	zki.OldKey2 = newSlice(nTx)
245	zki.OldValue2 = newSlice(nTx)
246
247	zki.TokenID3 = newSlice(maxFeeTx)
248	zki.Nonce3 = newSlice(maxFeeTx)
249	zki.Sign3 = newSlice(maxFeeTx)
250	zki.Balance3 = newSlice(maxFeeTx)
251	zki.Ay3 = newSlice(maxFeeTx)
252	zki.EthAddr3 = newSlice(maxFeeTx)
253	zki.Siblings3 = make([][]*big.Int, maxFeeTx)
254	for i := 0; i < len(zki.Siblings3); i++ {
255		zki.Siblings3[i] = newSlice(nLevels + 1)
256	}
257
258	// Intermediate States
259	zki.ISOnChain = newSlice(nTx - 1)
260	zki.ISOutIdx = newSlice(nTx - 1)
261	zki.ISStateRoot = newSlice(nTx - 1)
262	zki.ISExitRoot = newSlice(nTx - 1)
263	zki.ISAccFeeOut = make([][]*big.Int, nTx-1)
264	for i := 0; i < len(zki.ISAccFeeOut); i++ {
265		zki.ISAccFeeOut[i] = newSlice(maxFeeTx)
266	}
267	zki.ISStateRootFee = newSlice(maxFeeTx - 1)
268	zki.ISInitStateRootFee = big.NewInt(0)
269	zki.ISFinalAccFee = newSlice(maxFeeTx - 1)
270
271	return zki
272}
273
274// newSlice returns a []*big.Int slice of length n with values initialized at
275// 0.
276// Is used to initialize all *big.Ints of the ZKInputs data structure, so when
277// the transactions are processed and the ZKInputs filled, there is no need to
278// set all the elements, and if a transaction does not use a parameter, can be
279// leaved as it is in the ZKInputs, as will be 0, so later when using the
280// ZKInputs to generate the zkSnark proof there is no 'nil'/'null' values.
281func newSlice(n int) []*big.Int {
282	s := make([]*big.Int, n)
283	for i := 0; i < len(s); i++ {
284		s[i] = big.NewInt(0)
285	}
286	return s
287}