-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathprivate_context.nr
610 lines (537 loc) · 23.7 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
603
604
605
606
607
608
609
610
use crate::{
context::{inputs::PrivateContextInputs, packed_returns::PackedReturns},
messaging::process_l1_to_l2_message, hash::{hash_args_array, ArgsHasher},
keys::constants::{NULLIFIER_INDEX, OUTGOING_INDEX, NUM_KEY_TYPES, sk_generators},
oracle::{
key_validation_request::get_key_validation_request, arguments, returns::pack_returns,
call_private_function::call_private_function_internal, header::get_header_at,
logs::{emit_encrypted_note_log, emit_encrypted_event_log},
enqueue_public_function_call::{
enqueue_public_function_call_internal, notify_set_min_revertible_side_effect_counter,
set_public_teardown_function_call_internal
}
}
};
use dep::protocol_types::{
abis::{
call_context::CallContext, function_selector::FunctionSelector, gas::Gas,
max_block_number::MaxBlockNumber,
validation_requests::{KeyValidationRequest, KeyValidationRequestAndGenerator},
private_call_request::PrivateCallRequest, private_circuit_public_inputs::PrivateCircuitPublicInputs,
public_call_request::PublicCallRequest,
public_call_stack_item_compressed::PublicCallStackItemCompressed, read_request::ReadRequest,
note_hash::NoteHash, nullifier::Nullifier, log_hash::{LogHash, NoteLogHash, EncryptedLogHash}
},
address::{AztecAddress, EthAddress},
constants::{
MAX_NOTE_HASHES_PER_CALL, MAX_L2_TO_L1_MSGS_PER_CALL, MAX_NULLIFIERS_PER_CALL,
MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL,
MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, MAX_NULLIFIER_READ_REQUESTS_PER_CALL,
MAX_KEY_VALIDATION_REQUESTS_PER_CALL, MAX_ENCRYPTED_LOGS_PER_CALL, MAX_UNENCRYPTED_LOGS_PER_CALL,
MAX_NOTE_ENCRYPTED_LOGS_PER_CALL
},
header::Header, messaging::l2_to_l1_message::L2ToL1Message, utils::reader::Reader, traits::Empty
};
// When finished, one can call .finish() to convert back to the abi
pub struct PrivateContext {
// docs:start:private-context
inputs: PrivateContextInputs,
side_effect_counter: u32,
min_revertible_side_effect_counter: u32,
is_fee_payer: bool,
args_hash: Field,
return_hash: Field,
max_block_number: MaxBlockNumber,
note_hash_read_requests: BoundedVec<ReadRequest, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL>,
nullifier_read_requests: BoundedVec<ReadRequest, MAX_NULLIFIER_READ_REQUESTS_PER_CALL>,
key_validation_requests_and_generators: BoundedVec<KeyValidationRequestAndGenerator, MAX_KEY_VALIDATION_REQUESTS_PER_CALL>,
note_hashes: BoundedVec<NoteHash, MAX_NOTE_HASHES_PER_CALL>,
nullifiers: BoundedVec<Nullifier, MAX_NULLIFIERS_PER_CALL>,
private_call_requests : BoundedVec<PrivateCallRequest, MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL>,
public_call_requests : BoundedVec<PublicCallRequest, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL>,
public_teardown_call_request: PublicCallRequest,
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).
historical_header: Header,
note_encrypted_logs_hashes: BoundedVec<NoteLogHash, MAX_NOTE_ENCRYPTED_LOGS_PER_CALL>,
encrypted_logs_hashes: BoundedVec<EncryptedLogHash, MAX_ENCRYPTED_LOGS_PER_CALL>,
unencrypted_logs_hashes: BoundedVec<LogHash, MAX_UNENCRYPTED_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).
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(),
note_encrypted_logs_hashes: BoundedVec::new(),
encrypted_logs_hashes: BoundedVec::new(),
unencrypted_logs_hashes: BoundedVec::new(),
last_key_validation_requests: [Option::none(); NUM_KEY_TYPES]
}
}
fn msg_sender(self) -> AztecAddress {
self.inputs.call_context.msg_sender
}
fn this_address(self) -> AztecAddress {
self.inputs.call_context.storage_contract_address
}
fn chain_id(self) -> Field {
self.inputs.tx_context.chain_id
}
fn version(self) -> Field {
self.inputs.tx_context.version
}
fn selector(self) -> FunctionSelector {
self.inputs.call_context.function_selector
}
fn get_args_hash(self) -> Field {
self.args_hash
}
fn push_note_hash(&mut self, note_hash: Field) {
self.note_hashes.push(NoteHash { value: note_hash, counter: self.next_counter() });
}
fn push_nullifier(&mut self, nullifier: Field) {
self.nullifiers.push(Nullifier { value: nullifier, note_hash: 0, counter: self.next_counter() });
}
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).
fn get_header(self) -> Header {
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_header_at(self, block_number: u32) -> Header {
get_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,
note_encrypted_logs_hashes: self.note_encrypted_logs_hashes.storage,
encrypted_logs_hashes: self.encrypted_logs_hashes.storage,
unencrypted_logs_hashes: self.unencrypted_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. We fetch new values from oracle and instruct
// protocol circuits to validate them by storing the validation request in context.
let request = get_key_validation_request(pk_m_hash, key_index);
let request_and_generator = KeyValidationRequestAndGenerator { request, sk_app_generator: sk_generators[key_index] };
// We constrain that the pk_m_hash matches the one in the request (otherwise we could get an arbitrary
// valid key request and not the one corresponding to pk_m_hash).
assert(request.pk_m.hash() == pk_m_hash);
self.key_validation_requests_and_generators.push(request_and_generator);
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) {
// 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
);
// Push nullifier (and the "commitment" corresponding to this can be "empty")
self.push_nullifier(nullifier)
}
// docs:end:consume_l1_to_l2_message
// NB: A randomness value of 0 signals that the kernels should not mask the contract address
// used in siloing later on e.g. 'handshaking' contract w/ known address.
pub fn emit_raw_event_log_with_masked_address<let M: u32>(
&mut self,
randomness: Field,
log: [u8; M],
log_hash: Field
) {
let counter = self.next_counter();
let contract_address = self.this_address();
let len = log.len() as Field + 4;
let side_effect = EncryptedLogHash { value: log_hash, counter, length: len, randomness };
self.encrypted_logs_hashes.push(side_effect);
emit_encrypted_event_log(contract_address, randomness, log, counter);
}
pub fn emit_raw_note_log<let M: u32>(&mut self, note_hash_counter: u32, log: [u8; M], log_hash: Field) {
let counter = self.next_counter();
let len = log.len() as Field + 4;
let side_effect = NoteLogHash { value: log_hash, counter, length: len, note_hash_counter };
self.note_encrypted_logs_hashes.push(side_effect);
emit_encrypted_note_log(note_hash_counter, log, counter);
}
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);
assert(args_hash == arguments::pack_arguments_array(args));
self.call_private_function_with_packed_args(contract_address, function_selector, args_hash, false, 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);
assert(args_hash == arguments::pack_arguments_array(args));
self.call_private_function_with_packed_args(contract_address, function_selector, args_hash, true, false)
}
pub fn delegate_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);
assert(args_hash == arguments::pack_arguments_array(args));
self.call_private_function_with_packed_args(contract_address, function_selector, args_hash, false, 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, 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, false)
}
pub fn delegate_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, true)
}
pub fn call_private_function_with_packed_args(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args_hash: Field,
is_static_call: bool,
is_delegate_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;
let (end_side_effect_counter, returns_hash) = call_private_function_internal(
contract_address,
function_selector,
args_hash,
start_side_effect_counter,
is_static_call,
is_delegate_call
);
self.side_effect_counter = end_side_effect_counter + 1;
// 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;
// }
let call_context = self.generate_call_context(
contract_address,
function_selector,
is_static_call,
is_delegate_call
);
self.private_call_requests.push(
PrivateCallRequest {
contract_address,
call_context,
args_hash,
returns_hash,
start_side_effect_counter,
end_side_effect_counter
}
);
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);
assert(args_hash == arguments::pack_arguments_array(args));
self.call_public_function_with_packed_args(contract_address, function_selector, args_hash, false, 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);
assert(args_hash == arguments::pack_arguments_array(args));
self.call_public_function_with_packed_args(contract_address, function_selector, args_hash, true, false)
}
pub fn delegate_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);
assert(args_hash == arguments::pack_arguments_array(args));
self.call_public_function_with_packed_args(contract_address, function_selector, args_hash, false, 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, 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, false)
}
pub fn delegate_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, true)
}
pub fn call_public_function_with_packed_args(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args_hash: Field,
is_static_call: bool,
is_delegate_call: bool
) {
let counter = self.next_counter();
let mut is_static_call = is_static_call | self.inputs.call_context.is_static_call;
enqueue_public_function_call_internal(
contract_address,
function_selector,
args_hash,
counter,
is_static_call,
is_delegate_call
);
let call_context = self.generate_call_context(
contract_address,
function_selector,
is_static_call,
is_delegate_call
);
let item = PublicCallStackItemCompressed {
contract_address,
call_context,
args_hash,
returns_hash: 0,
revert_code: 0,
start_gas_left: Gas::empty(),
end_gas_left: Gas::empty()
};
let call_request = PublicCallRequest { item, counter };
self.public_call_requests.push(call_request);
}
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);
assert(args_hash == arguments::pack_arguments_array(args));
self.set_public_teardown_function_with_packed_args(contract_address, function_selector, args_hash, false, 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,
is_delegate_call: bool
) {
let counter = self.next_counter();
let mut is_static_call = is_static_call | self.inputs.call_context.is_static_call;
set_public_teardown_function_call_internal(
contract_address,
function_selector,
args_hash,
counter,
is_static_call,
is_delegate_call
);
let call_context = self.generate_call_context(
contract_address,
function_selector,
is_static_call,
is_delegate_call
);
let item = PublicCallStackItemCompressed {
contract_address,
call_context,
args_hash,
returns_hash: 0,
revert_code: 0,
start_gas_left: Gas::empty(),
end_gas_left: Gas::empty()
};
self.public_teardown_call_request = PublicCallRequest {
item,
counter,
};
}
fn generate_call_context(
self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
is_static_call: bool,
is_delegate_call: bool
) -> CallContext {
let msg_sender = if is_delegate_call {
self.msg_sender()
} else {
self.this_address()
};
let storage_contract_address = if is_delegate_call {
self.this_address()
} else {
contract_address
};
CallContext { msg_sender, storage_contract_address, function_selector, is_static_call, is_delegate_call }
}
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: Header::empty(),
note_encrypted_logs_hashes: BoundedVec::new(),
encrypted_logs_hashes: BoundedVec::new(),
unencrypted_logs_hashes: BoundedVec::new(),
last_key_validation_requests: [Option::none(); NUM_KEY_TYPES]
}
}
}