-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathprivate_context.nr
602 lines (537 loc) · 23 KB
/
private_context.nr
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
use dep::protocol_types::debug_log::debug_log_format;
use crate::{
context::{inputs::PrivateContextInputs, packed_returns::PackedReturns},
hash::{ArgsHasher, hash_args_array},
keys::constants::{NULLIFIER_INDEX, NUM_KEY_TYPES, OUTGOING_INDEX, sk_generators},
messaging::process_l1_to_l2_message,
oracle::{
arguments,
block_header::get_block_header_at,
call_private_function::call_private_function_internal,
enqueue_public_function_call::{
enqueue_public_function_call_internal, notify_set_min_revertible_side_effect_counter,
set_public_teardown_function_call_internal,
},
key_validation_request::get_key_validation_request,
returns::pack_returns,
},
};
use dep::protocol_types::{
abis::{
call_context::CallContext,
function_selector::FunctionSelector,
log::Log,
log_hash::LogHash,
max_block_number::MaxBlockNumber,
note_hash::NoteHash,
nullifier::Nullifier,
private_call_request::PrivateCallRequest,
private_circuit_public_inputs::PrivateCircuitPublicInputs,
private_log::PrivateLogData,
public_call_request::PublicCallRequest,
read_request::ReadRequest,
side_effect::Counted,
validation_requests::{KeyValidationRequest, KeyValidationRequestAndGenerator},
},
address::{AztecAddress, EthAddress},
block_header::BlockHeader,
constants::{
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_HASH_READ_REQUESTS_PER_CALL, MAX_NOTE_HASHES_PER_CALL,
MAX_NULLIFIER_READ_REQUESTS_PER_CALL, MAX_NULLIFIERS_PER_CALL,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL, MAX_PRIVATE_LOGS_PER_CALL,
PRIVATE_LOG_SIZE_IN_FIELDS, PUBLIC_DISPATCH_SELECTOR,
},
messaging::l2_to_l1_message::L2ToL1Message,
traits::Empty,
};
// When finished, one can call .finish() to convert back to the abi
pub struct PrivateContext {
// docs:start:private-context
pub inputs: PrivateContextInputs,
pub side_effect_counter: u32,
pub min_revertible_side_effect_counter: u32,
pub is_fee_payer: bool,
pub args_hash: Field,
pub return_hash: Field,
pub max_block_number: MaxBlockNumber,
pub note_hash_read_requests: BoundedVec<ReadRequest, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL>,
pub nullifier_read_requests: BoundedVec<ReadRequest, MAX_NULLIFIER_READ_REQUESTS_PER_CALL>,
key_validation_requests_and_generators: BoundedVec<KeyValidationRequestAndGenerator, MAX_KEY_VALIDATION_REQUESTS_PER_CALL>,
pub note_hashes: BoundedVec<NoteHash, MAX_NOTE_HASHES_PER_CALL>,
pub nullifiers: BoundedVec<Nullifier, MAX_NULLIFIERS_PER_CALL>,
pub private_call_requests: BoundedVec<PrivateCallRequest, MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL>,
pub public_call_requests: BoundedVec<Counted<PublicCallRequest>, MAX_ENQUEUED_CALLS_PER_CALL>,
pub public_teardown_call_request: PublicCallRequest,
pub l2_to_l1_msgs: BoundedVec<L2ToL1Message, MAX_L2_TO_L1_MSGS_PER_CALL>,
// docs:end:private-context
// Header of a block whose state is used during private execution (not the block the transaction is included in).
pub historical_header: BlockHeader,
pub private_logs: BoundedVec<PrivateLogData, MAX_PRIVATE_LOGS_PER_CALL>,
pub contract_class_logs_hashes: BoundedVec<LogHash, MAX_CONTRACT_CLASS_LOGS_PER_CALL>,
// Contains the last key validation request for each key type. This is used to cache the last request and avoid
// fetching the same request multiple times.
// The index of the array corresponds to the key type (0 nullifier, 1 incoming, 2 outgoing, 3 tagging).
pub last_key_validation_requests: [Option<KeyValidationRequest>; NUM_KEY_TYPES],
}
impl PrivateContext {
pub fn new(inputs: PrivateContextInputs, args_hash: Field) -> PrivateContext {
PrivateContext {
inputs,
side_effect_counter: inputs.start_side_effect_counter + 1,
min_revertible_side_effect_counter: 0,
is_fee_payer: false,
args_hash,
return_hash: 0,
max_block_number: MaxBlockNumber::empty(),
note_hash_read_requests: BoundedVec::new(),
nullifier_read_requests: BoundedVec::new(),
key_validation_requests_and_generators: BoundedVec::new(),
note_hashes: BoundedVec::new(),
nullifiers: BoundedVec::new(),
historical_header: inputs.historical_header,
private_call_requests: BoundedVec::new(),
public_call_requests: BoundedVec::new(),
public_teardown_call_request: PublicCallRequest::empty(),
l2_to_l1_msgs: BoundedVec::new(),
private_logs: BoundedVec::new(),
contract_class_logs_hashes: BoundedVec::new(),
last_key_validation_requests: [Option::none(); NUM_KEY_TYPES],
}
}
pub fn msg_sender(self) -> AztecAddress {
self.inputs.call_context.msg_sender
}
pub fn this_address(self) -> AztecAddress {
self.inputs.call_context.contract_address
}
pub fn chain_id(self) -> Field {
self.inputs.tx_context.chain_id
}
pub fn version(self) -> Field {
self.inputs.tx_context.version
}
pub fn selector(self) -> FunctionSelector {
self.inputs.call_context.function_selector
}
pub fn get_args_hash(self) -> Field {
self.args_hash
}
pub fn push_note_hash(&mut self, note_hash: Field) {
self.note_hashes.push(NoteHash { value: note_hash, counter: self.next_counter() });
// WARNING(https://github.com/AztecProtocol/aztec-packages/issues/10558): if you delete this debug_log_format line, some tests fail.
debug_log_format(
"Context.note_hashes, after pushing new note hash: {0}",
self.note_hashes.storage().map(|nh: NoteHash| nh.value),
);
}
pub fn push_nullifier(&mut self, nullifier: Field) {
self.nullifiers.push(
Nullifier { value: nullifier, note_hash: 0, counter: self.next_counter() },
);
}
pub fn push_nullifier_for_note_hash(&mut self, nullifier: Field, nullified_note_hash: Field) {
self.nullifiers.push(
Nullifier {
value: nullifier,
note_hash: nullified_note_hash,
counter: self.next_counter(),
},
);
}
// Returns the header of a block whose state is used during private execution (not the block the transaction is
// included in).
pub fn get_block_header(self) -> BlockHeader {
self.historical_header
}
// Returns the header of an arbitrary block whose block number is less than or equal to the block number
// of historical header.
pub fn get_block_header_at(self, block_number: u32) -> BlockHeader {
get_block_header_at(block_number, self)
}
pub fn set_return_hash(&mut self, returns_hasher: ArgsHasher) {
pack_returns(returns_hasher.fields);
self.return_hash = returns_hasher.hash();
}
pub fn finish(self) -> PrivateCircuitPublicInputs {
PrivateCircuitPublicInputs {
call_context: self.inputs.call_context,
args_hash: self.args_hash,
returns_hash: self.return_hash,
min_revertible_side_effect_counter: self.min_revertible_side_effect_counter,
is_fee_payer: self.is_fee_payer,
max_block_number: self.max_block_number,
note_hash_read_requests: self.note_hash_read_requests.storage(),
nullifier_read_requests: self.nullifier_read_requests.storage(),
key_validation_requests_and_generators: self
.key_validation_requests_and_generators
.storage(),
note_hashes: self.note_hashes.storage(),
nullifiers: self.nullifiers.storage(),
private_call_requests: self.private_call_requests.storage(),
public_call_requests: self.public_call_requests.storage(),
public_teardown_call_request: self.public_teardown_call_request,
l2_to_l1_msgs: self.l2_to_l1_msgs.storage(),
start_side_effect_counter: self.inputs.start_side_effect_counter,
end_side_effect_counter: self.side_effect_counter,
private_logs: self.private_logs.storage(),
contract_class_logs_hashes: self.contract_class_logs_hashes.storage(),
historical_header: self.historical_header,
tx_context: self.inputs.tx_context,
}
}
pub fn set_as_fee_payer(&mut self) {
dep::protocol_types::debug_log::debug_log_format(
"Setting {0} as fee payer",
[self.this_address().to_field()],
);
self.is_fee_payer = true;
}
pub fn end_setup(&mut self) {
// dep::protocol_types::debug_log::debug_log_format(
// "Ending setup at counter {0}",
// [self.side_effect_counter as Field]
// );
self.min_revertible_side_effect_counter = self.side_effect_counter;
notify_set_min_revertible_side_effect_counter(self.min_revertible_side_effect_counter);
}
// docs:start:max-block-number
pub fn set_tx_max_block_number(&mut self, max_block_number: u32) {
// docs:end:max-block-number
self.max_block_number =
MaxBlockNumber::min_with_u32(self.max_block_number, max_block_number);
}
pub fn push_note_hash_read_request(&mut self, note_hash: Field) {
let side_effect = ReadRequest { value: note_hash, counter: self.next_counter() };
self.note_hash_read_requests.push(side_effect);
}
pub fn push_nullifier_read_request(&mut self, nullifier: Field) {
let request = ReadRequest { value: nullifier, counter: self.next_counter() };
self.nullifier_read_requests.push(request);
}
pub fn request_nsk_app(&mut self, npk_m_hash: Field) -> Field {
self.request_sk_app(npk_m_hash, NULLIFIER_INDEX)
}
pub fn request_ovsk_app(&mut self, ovpk_m_hash: Field) -> Field {
self.request_sk_app(ovpk_m_hash, OUTGOING_INDEX)
}
fn request_sk_app(&mut self, pk_m_hash: Field, key_index: Field) -> Field {
let cached_request =
self.last_key_validation_requests[key_index].unwrap_or(KeyValidationRequest::empty());
if cached_request.pk_m.hash() == pk_m_hash {
// We get a match so the cached request is the latest one
cached_request.sk_app
} else {
// We didn't get a match meaning the cached result is stale
// Typically we'd validate keys by showing that they are the preimage of `pk_m_hash`, but that'd require
// the oracle returning the master secret keys, which could cause malicious contracts to leak it or learn
// about secrets from other contracts. We therefore silo secret keys, and rely on the private kernel to
// validate that we siloed secret key corresponds to correct siloing of the master secret key that hashes
// to `pk_m_hash`.
let request = unsafe { get_key_validation_request(pk_m_hash, key_index) };
assert(request.pk_m.hash() == pk_m_hash);
self.key_validation_requests_and_generators.push(
KeyValidationRequestAndGenerator {
request,
sk_app_generator: sk_generators[key_index],
},
);
self.last_key_validation_requests[key_index] = Option::some(request);
request.sk_app
}
}
// docs:start:context_message_portal
pub fn message_portal(&mut self, recipient: EthAddress, content: Field) {
// docs:end:context_message_portal
let message = L2ToL1Message { recipient, content, counter: self.next_counter() };
self.l2_to_l1_msgs.push(message);
}
// docs:start:context_consume_l1_to_l2_message
// docs:start:consume_l1_to_l2_message
pub fn consume_l1_to_l2_message(
&mut self,
content: Field,
secret: Field,
sender: EthAddress,
leaf_index: Field,
) {
// docs:end:context_consume_l1_to_l2_message
let nullifier = process_l1_to_l2_message(
self.historical_header.state.l1_to_l2_message_tree.root,
self.this_address(),
sender,
self.chain_id(),
self.version(),
content,
secret,
leaf_index,
);
// Push nullifier (and the "commitment" corresponding to this can be "empty")
self.push_nullifier(nullifier)
}
// docs:end:consume_l1_to_l2_message
pub fn emit_private_log(&mut self, log: [Field; PRIVATE_LOG_SIZE_IN_FIELDS]) {
let counter = self.next_counter();
let private_log = PrivateLogData { log: Log::new(log), note_hash_counter: 0, counter };
self.private_logs.push(private_log);
}
pub fn emit_raw_note_log(
&mut self,
log: [Field; PRIVATE_LOG_SIZE_IN_FIELDS],
note_hash_counter: u32,
) {
let counter = self.next_counter();
let private_log = PrivateLogData { log: Log::new(log), note_hash_counter, counter };
self.private_logs.push(private_log);
}
pub fn call_private_function<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args: [Field; ARGS_COUNT],
) -> PackedReturns {
let args_hash = hash_args_array(args);
arguments::pack_arguments_array(args);
self.call_private_function_with_packed_args(
contract_address,
function_selector,
args_hash,
false,
)
}
pub fn static_call_private_function<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args: [Field; ARGS_COUNT],
) -> PackedReturns {
let args_hash = hash_args_array(args);
arguments::pack_arguments_array(args);
self.call_private_function_with_packed_args(
contract_address,
function_selector,
args_hash,
true,
)
}
pub fn call_private_function_no_args(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
) -> PackedReturns {
self.call_private_function_with_packed_args(contract_address, function_selector, 0, false)
}
pub fn static_call_private_function_no_args(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
) -> PackedReturns {
self.call_private_function_with_packed_args(contract_address, function_selector, 0, true)
}
pub fn call_private_function_with_packed_args(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args_hash: Field,
is_static_call: bool,
) -> PackedReturns {
let mut is_static_call = is_static_call | self.inputs.call_context.is_static_call;
let start_side_effect_counter = self.side_effect_counter;
// The oracle simulates the private call and returns the value of the side effects counter after execution of
// the call (which means that end_side_effect_counter - start_side_effect_counter is the number of side effects
// that took place), along with the hash of the return values. We validate these by requesting a private kernel
// iteration in which the return values are constrained to hash to `returns_hash` and the side effects counter
// to increment from start to end.
let (end_side_effect_counter, returns_hash) = unsafe {
call_private_function_internal(
contract_address,
function_selector,
args_hash,
start_side_effect_counter,
is_static_call,
)
};
self.private_call_requests.push(
PrivateCallRequest {
call_context: CallContext {
msg_sender: self.this_address(),
contract_address,
function_selector,
is_static_call,
},
args_hash,
returns_hash,
start_side_effect_counter,
end_side_effect_counter,
},
);
// TODO (fees) figure out why this crashes the prover and enable it
// we need this in order to pay fees inside child call contexts
// assert(
// (item.public_inputs.min_revertible_side_effect_counter == 0 as u32)
// | (item.public_inputs.min_revertible_side_effect_counter
// > self.min_revertible_side_effect_counter)
// );
// if item.public_inputs.min_revertible_side_effect_counter
// > self.min_revertible_side_effect_counter {
// self.min_revertible_side_effect_counter = item.public_inputs.min_revertible_side_effect_counter;
// }
self.side_effect_counter = end_side_effect_counter + 1;
PackedReturns::new(returns_hash)
}
pub fn call_public_function<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args: [Field; ARGS_COUNT],
) {
let args_hash = hash_args_array(args);
arguments::pack_arguments_array(args);
self.call_public_function_with_packed_args(
contract_address,
function_selector,
args_hash,
false,
)
}
pub fn static_call_public_function<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args: [Field; ARGS_COUNT],
) {
let args_hash = hash_args_array(args);
arguments::pack_arguments_array(args);
self.call_public_function_with_packed_args(
contract_address,
function_selector,
args_hash,
true,
)
}
pub fn call_public_function_no_args(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
) {
self.call_public_function_with_packed_args(contract_address, function_selector, 0, false)
}
pub fn static_call_public_function_no_args(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
) {
self.call_public_function_with_packed_args(contract_address, function_selector, 0, true)
}
pub fn call_public_function_with_packed_args(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args_hash: Field,
is_static_call: bool,
) {
let counter = self.next_counter();
let mut is_static_call = is_static_call | self.inputs.call_context.is_static_call;
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/8985): Fix this.
// WARNING: This is insecure and should be temporary!
// The oracle repacks the arguments and returns a new args_hash.
// new_args = [selector, ...old_args], so as to make it suitable to call the public dispatch function.
// We don't validate or compute it in the circuit because a) it's harder to do with slices, and
// b) this is only temporary.
let args_hash = enqueue_public_function_call_internal(
contract_address,
function_selector,
args_hash,
counter,
is_static_call,
);
// Public calls are rerouted through the dispatch function.
let function_selector = comptime { FunctionSelector::from_field(PUBLIC_DISPATCH_SELECTOR) };
let call_request = PublicCallRequest {
msg_sender: self.this_address(),
contract_address,
function_selector,
is_static_call,
args_hash,
};
self.public_call_requests.push(Counted::new(call_request, counter));
}
pub fn set_public_teardown_function<let ARGS_COUNT: u32>(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args: [Field; ARGS_COUNT],
) {
let args_hash = hash_args_array(args);
arguments::pack_arguments_array(args);
self.set_public_teardown_function_with_packed_args(
contract_address,
function_selector,
args_hash,
false,
)
}
pub fn set_public_teardown_function_with_packed_args(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args_hash: Field,
is_static_call: bool,
) {
let counter = self.next_counter();
let mut is_static_call = is_static_call | self.inputs.call_context.is_static_call;
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/8985): Fix this.
// WARNING: This is insecure and should be temporary!
// The oracle repacks the arguments and returns a new args_hash.
// new_args = [selector, ...old_args], so as to make it suitable to call the public dispatch function.
// We don't validate or compute it in the circuit because a) it's harder to do with slices, and
// b) this is only temporary.
let args_hash = set_public_teardown_function_call_internal(
contract_address,
function_selector,
args_hash,
counter,
is_static_call,
);
let function_selector = comptime { FunctionSelector::from_field(PUBLIC_DISPATCH_SELECTOR) };
self.public_teardown_call_request = PublicCallRequest {
msg_sender: self.this_address(),
contract_address,
function_selector,
is_static_call,
args_hash,
};
}
fn next_counter(&mut self) -> u32 {
let counter = self.side_effect_counter;
self.side_effect_counter += 1;
counter
}
}
impl Empty for PrivateContext {
fn empty() -> Self {
PrivateContext {
inputs: PrivateContextInputs::empty(),
side_effect_counter: 0 as u32,
min_revertible_side_effect_counter: 0 as u32,
is_fee_payer: false,
args_hash: 0,
return_hash: 0,
max_block_number: MaxBlockNumber::empty(),
note_hash_read_requests: BoundedVec::new(),
nullifier_read_requests: BoundedVec::new(),
key_validation_requests_and_generators: BoundedVec::new(),
note_hashes: BoundedVec::new(),
nullifiers: BoundedVec::new(),
private_call_requests: BoundedVec::new(),
public_call_requests: BoundedVec::new(),
public_teardown_call_request: PublicCallRequest::empty(),
l2_to_l1_msgs: BoundedVec::new(),
historical_header: BlockHeader::empty(),
private_logs: BoundedVec::new(),
contract_class_logs_hashes: BoundedVec::new(),
last_key_validation_requests: [Option::none(); NUM_KEY_TYPES],
}
}
}