-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathprivate_circuit_public_inputs.ts
323 lines (311 loc) · 11.7 KB
/
private_circuit_public_inputs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { makeTuple } from '@aztec/foundation/array';
import { Fr } from '@aztec/foundation/fields';
import { bufferSchemaFor } from '@aztec/foundation/schemas';
import {
BufferReader,
FieldReader,
type Tuple,
serializeToBuffer,
serializeToFields,
} from '@aztec/foundation/serialize';
import { type FieldsOf } from '@aztec/foundation/types';
import {
MAX_CONTRACT_CLASS_LOGS_PER_CALL,
MAX_ENQUEUED_CALLS_PER_CALL,
MAX_KEY_VALIDATION_REQUESTS_PER_CALL,
MAX_L2_TO_L1_MSGS_PER_CALL,
MAX_NOTE_HASHES_PER_CALL,
MAX_NOTE_HASH_READ_REQUESTS_PER_CALL,
MAX_NULLIFIERS_PER_CALL,
MAX_NULLIFIER_READ_REQUESTS_PER_CALL,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL,
MAX_PRIVATE_LOGS_PER_CALL,
PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH,
} from '../constants.gen.js';
import { BlockHeader } from '../structs/block_header.js';
import { isEmptyArray } from '../utils/index.js';
import { CallContext } from './call_context.js';
import { KeyValidationRequestAndGenerator } from './key_validation_request_and_generator.js';
import { L2ToL1Message } from './l2_to_l1_message.js';
import { LogHash } from './log_hash.js';
import { MaxBlockNumber } from './max_block_number.js';
import { NoteHash } from './note_hash.js';
import { Nullifier } from './nullifier.js';
import { PrivateCallRequest } from './private_call_request.js';
import { PrivateLogData } from './private_log_data.js';
import { CountedPublicCallRequest, PublicCallRequest } from './public_call_request.js';
import { ReadRequest } from './read_request.js';
import { TxContext } from './tx_context.js';
/**
* Public inputs to a private circuit.
*/
export class PrivateCircuitPublicInputs {
constructor(
/**
* Context of the call corresponding to this private circuit execution.
*/
public callContext: CallContext,
/**
* Pedersen hash of function arguments.
*/
public argsHash: Fr,
/**
* Pedersen hash of the return values of the corresponding function call.
*/
public returnsHash: Fr,
/**
* The side-effect counter under which all side effects are non-revertible.
*/
public minRevertibleSideEffectCounter: Fr,
/**
* Whether the caller of the function is the fee payer.
*/
public isFeePayer: boolean,
/**
* The maximum block number in which this transaction can be included and be valid.
*/
public maxBlockNumber: MaxBlockNumber,
/**
* Read requests created by the corresponding function call.
*/
public noteHashReadRequests: Tuple<ReadRequest, typeof MAX_NOTE_HASH_READ_REQUESTS_PER_CALL>,
/**
* Nullifier read requests created by the corresponding function call.
*/
public nullifierReadRequests: Tuple<ReadRequest, typeof MAX_NULLIFIER_READ_REQUESTS_PER_CALL>,
/**
* Key validation requests and generators created by the corresponding function call.
*/
public keyValidationRequestsAndGenerators: Tuple<
KeyValidationRequestAndGenerator,
typeof MAX_KEY_VALIDATION_REQUESTS_PER_CALL
>,
/**
* New note hashes created by the corresponding function call.
*/
public noteHashes: Tuple<NoteHash, typeof MAX_NOTE_HASHES_PER_CALL>,
/**
* New nullifiers created by the corresponding function call.
*/
public nullifiers: Tuple<Nullifier, typeof MAX_NULLIFIERS_PER_CALL>,
/**
* Private call requests made within the current kernel iteration.
*/
public privateCallRequests: Tuple<PrivateCallRequest, typeof MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL>,
/**
* Public call stack at the current kernel iteration.
*/
public publicCallRequests: Tuple<CountedPublicCallRequest, typeof MAX_ENQUEUED_CALLS_PER_CALL>,
/**
* Hash of the public teardown function.
*/
public publicTeardownCallRequest: PublicCallRequest,
/**
* New L2 to L1 messages created by the corresponding function call.
*/
public l2ToL1Msgs: Tuple<L2ToL1Message, typeof MAX_L2_TO_L1_MSGS_PER_CALL>,
/**
* Logs emitted in this function call.
*/
public privateLogs: Tuple<PrivateLogData, typeof MAX_PRIVATE_LOGS_PER_CALL>,
/**
* Hash of the contract class logs emitted in this function call.
* Note: Truncated to 31 bytes to fit in Fr.
*/
public contractClassLogsHashes: Tuple<LogHash, typeof MAX_CONTRACT_CLASS_LOGS_PER_CALL>,
/**
* The side effect counter at the start of this call.
*/
public startSideEffectCounter: Fr,
/**
* The end side effect counter for this call.
*/
public endSideEffectCounter: Fr,
/**
* Header of a block whose state is used during private execution (not the block the transaction is included in).
*/
public historicalHeader: BlockHeader,
/**
* Transaction context.
*
* Note: The chainId and version in the txContext are not redundant to the values in self.historical_header.global_variables because
* they can be different in case of a protocol upgrade. In such a situation we could be using header from a block
* before the upgrade took place but be using the updated protocol to execute and prove the transaction.
*/
public txContext: TxContext,
) {}
/**
* Create PrivateCircuitPublicInputs from a fields dictionary.
* @param fields - The dictionary.
* @returns A PrivateCircuitPublicInputs object.
*/
static from(fields: FieldsOf<PrivateCircuitPublicInputs>): PrivateCircuitPublicInputs {
return new PrivateCircuitPublicInputs(...PrivateCircuitPublicInputs.getFields(fields));
}
/**
* Deserializes from a buffer or reader.
* @param buffer - Buffer or reader to read from.
* @returns The deserialized instance.
*/
static fromBuffer(buffer: Buffer | BufferReader): PrivateCircuitPublicInputs {
const reader = BufferReader.asReader(buffer);
return new PrivateCircuitPublicInputs(
reader.readObject(CallContext),
reader.readObject(Fr),
reader.readObject(Fr),
reader.readObject(Fr),
reader.readBoolean(),
reader.readObject(MaxBlockNumber),
reader.readArray(MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, ReadRequest),
reader.readArray(MAX_NULLIFIER_READ_REQUESTS_PER_CALL, ReadRequest),
reader.readArray(MAX_KEY_VALIDATION_REQUESTS_PER_CALL, KeyValidationRequestAndGenerator),
reader.readArray(MAX_NOTE_HASHES_PER_CALL, NoteHash),
reader.readArray(MAX_NULLIFIERS_PER_CALL, Nullifier),
reader.readArray(MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL, PrivateCallRequest),
reader.readArray(MAX_ENQUEUED_CALLS_PER_CALL, CountedPublicCallRequest),
reader.readObject(PublicCallRequest),
reader.readArray(MAX_L2_TO_L1_MSGS_PER_CALL, L2ToL1Message),
reader.readArray(MAX_PRIVATE_LOGS_PER_CALL, PrivateLogData),
reader.readArray(MAX_CONTRACT_CLASS_LOGS_PER_CALL, LogHash),
reader.readObject(Fr),
reader.readObject(Fr),
reader.readObject(BlockHeader),
reader.readObject(TxContext),
);
}
static fromFields(fields: Fr[] | FieldReader): PrivateCircuitPublicInputs {
const reader = FieldReader.asReader(fields);
return new PrivateCircuitPublicInputs(
reader.readObject(CallContext),
reader.readField(),
reader.readField(),
reader.readField(),
reader.readBoolean(),
reader.readObject(MaxBlockNumber),
reader.readArray(MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, ReadRequest),
reader.readArray(MAX_NULLIFIER_READ_REQUESTS_PER_CALL, ReadRequest),
reader.readArray(MAX_KEY_VALIDATION_REQUESTS_PER_CALL, KeyValidationRequestAndGenerator),
reader.readArray(MAX_NOTE_HASHES_PER_CALL, NoteHash),
reader.readArray(MAX_NULLIFIERS_PER_CALL, Nullifier),
reader.readArray(MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL, PrivateCallRequest),
reader.readArray(MAX_ENQUEUED_CALLS_PER_CALL, CountedPublicCallRequest),
reader.readObject(PublicCallRequest),
reader.readArray(MAX_L2_TO_L1_MSGS_PER_CALL, L2ToL1Message),
reader.readArray(MAX_PRIVATE_LOGS_PER_CALL, PrivateLogData),
reader.readArray(MAX_CONTRACT_CLASS_LOGS_PER_CALL, LogHash),
reader.readField(),
reader.readField(),
reader.readObject(BlockHeader),
reader.readObject(TxContext),
);
}
/**
* Create an empty PrivateCircuitPublicInputs.
* @returns An empty PrivateCircuitPublicInputs object.
*/
public static empty(): PrivateCircuitPublicInputs {
return new PrivateCircuitPublicInputs(
CallContext.empty(),
Fr.ZERO,
Fr.ZERO,
Fr.ZERO,
false,
MaxBlockNumber.empty(),
makeTuple(MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, ReadRequest.empty),
makeTuple(MAX_NULLIFIER_READ_REQUESTS_PER_CALL, ReadRequest.empty),
makeTuple(MAX_KEY_VALIDATION_REQUESTS_PER_CALL, KeyValidationRequestAndGenerator.empty),
makeTuple(MAX_NOTE_HASHES_PER_CALL, NoteHash.empty),
makeTuple(MAX_NULLIFIERS_PER_CALL, Nullifier.empty),
makeTuple(MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL, PrivateCallRequest.empty),
makeTuple(MAX_ENQUEUED_CALLS_PER_CALL, CountedPublicCallRequest.empty),
PublicCallRequest.empty(),
makeTuple(MAX_L2_TO_L1_MSGS_PER_CALL, L2ToL1Message.empty),
makeTuple(MAX_PRIVATE_LOGS_PER_CALL, PrivateLogData.empty),
makeTuple(MAX_CONTRACT_CLASS_LOGS_PER_CALL, LogHash.empty),
Fr.ZERO,
Fr.ZERO,
BlockHeader.empty(),
TxContext.empty(),
);
}
isEmpty() {
return (
this.callContext.isEmpty() &&
this.argsHash.isZero() &&
this.returnsHash.isZero() &&
this.minRevertibleSideEffectCounter.isZero() &&
!this.isFeePayer &&
this.maxBlockNumber.isEmpty() &&
isEmptyArray(this.noteHashReadRequests) &&
isEmptyArray(this.nullifierReadRequests) &&
isEmptyArray(this.keyValidationRequestsAndGenerators) &&
isEmptyArray(this.noteHashes) &&
isEmptyArray(this.nullifiers) &&
isEmptyArray(this.privateCallRequests) &&
isEmptyArray(this.publicCallRequests) &&
this.publicTeardownCallRequest.isEmpty() &&
isEmptyArray(this.l2ToL1Msgs) &&
isEmptyArray(this.privateLogs) &&
isEmptyArray(this.contractClassLogsHashes) &&
this.startSideEffectCounter.isZero() &&
this.endSideEffectCounter.isZero() &&
this.historicalHeader.isEmpty() &&
this.txContext.isEmpty()
);
}
/**
* Serialize into a field array. Low-level utility.
* @param fields - Object with fields.
* @returns The array.
*/
static getFields(fields: FieldsOf<PrivateCircuitPublicInputs>) {
return [
fields.callContext,
fields.argsHash,
fields.returnsHash,
fields.minRevertibleSideEffectCounter,
fields.isFeePayer,
fields.maxBlockNumber,
fields.noteHashReadRequests,
fields.nullifierReadRequests,
fields.keyValidationRequestsAndGenerators,
fields.noteHashes,
fields.nullifiers,
fields.privateCallRequests,
fields.publicCallRequests,
fields.publicTeardownCallRequest,
fields.l2ToL1Msgs,
fields.privateLogs,
fields.contractClassLogsHashes,
fields.startSideEffectCounter,
fields.endSideEffectCounter,
fields.historicalHeader,
fields.txContext,
] as const;
}
/**
* Serialize this as a buffer.
* @returns The buffer.
*/
toBuffer(): Buffer {
return serializeToBuffer(...PrivateCircuitPublicInputs.getFields(this));
}
/**
* Serialize this as a field array.
*/
toFields(): Fr[] {
const fields = serializeToFields(...PrivateCircuitPublicInputs.getFields(this));
if (fields.length !== PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH) {
throw new Error(
`Invalid number of fields for PrivateCircuitPublicInputs. Expected ${PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH}, got ${fields.length}`,
);
}
return fields;
}
public toJSON() {
return this.toBuffer();
}
static get schema() {
return bufferSchemaFor(PrivateCircuitPublicInputs);
}
}