-
Notifications
You must be signed in to change notification settings - Fork 601
Expand file tree
/
Copy pathavm_context.nr
More file actions
282 lines (238 loc) · 8.55 KB
/
avm_context.nr
File metadata and controls
282 lines (238 loc) · 8.55 KB
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
use dep::protocol_types::{address::{AztecAddress, EthAddress}, constants::L1_TO_L2_MESSAGE_LENGTH, header::Header};
use dep::protocol_types::traits::Serialize;
use dep::protocol_types::abis::function_selector::FunctionSelector;
use dep::protocol_types::abis::public_circuit_public_inputs::PublicCircuitPublicInputs;
use dep::protocol_types::constants::RETURN_VALUES_LENGTH;
use crate::context::inputs::avm_context_inputs::AvmContextInputs;
use crate::context::interface::ContextInterface;
use crate::context::interface::PublicContextInterface;
struct AVMContext {
inputs: AvmContextInputs,
}
impl AVMContext {
pub fn new(inputs: AvmContextInputs) -> Self {
AVMContext { inputs }
}
pub fn origin(self) -> AztecAddress {
origin()
}
pub fn storage_address(self) -> AztecAddress {
storage_address()
}
pub fn fee_per_l1_gas(self) -> Field {
fee_per_l1_gas()
}
pub fn fee_per_l2_gas(self) -> Field {
fee_per_l2_gas()
}
pub fn fee_per_da_gas(self) -> Field {
fee_per_da_gas()
}
/**
* Emit a log with the given event selector and message.
*
* @param event_selector The event selector for the log.
* @param message The message to emit in the log.
* Should be automatically convertible to [Field; N]. For example str<N> works with
* one char per field. Otherwise you can use CompressedString.
*/
pub fn accumulate_unencrypted_logs<T>(&mut self, event_selector: Field, log: T) {
emit_unencrypted_log(event_selector, log);
}
pub fn note_hash_exists(self, note_hash: Field, leaf_index: Field) -> bool {
note_hash_exists(note_hash, leaf_index) == 1
}
pub fn l1_to_l2_msg_exists(self, msg_hash: Field, msg_leaf_index: Field) -> bool {
l1_to_l2_msg_exists(msg_hash, msg_leaf_index) == 1
}
pub fn nullifier_exists(self, nullifier: Field) -> bool {
nullifier_exists(nullifier) == 1
}
fn call_public_function_raw<ARGS_COUNT, RET_COUNT>(
self: &mut Self,
gas: [Field; 3],
contract_address: AztecAddress,
temporary_function_selector: Field,
args: [Field; ARGS_COUNT]
) -> ([Field; RET_COUNT], u8) {
call(gas, contract_address, args, temporary_function_selector)
}
fn static_call_public_function_raw<ARGS_COUNT, RET_COUNT>(
self: &mut Self,
gas: [Field; 3],
contract_address: AztecAddress,
temporary_function_selector: Field,
args: [Field; ARGS_COUNT]
) -> ([Field; RET_COUNT], u8) {
call_static(gas, contract_address, args, temporary_function_selector)
}
}
impl PublicContextInterface for AVMContext {
fn block_number(self) -> Field {
block_number()
}
fn timestamp(self) -> Field {
timestamp()
}
fn coinbase(self) -> EthAddress {
assert(false, "'coinbase' not implemented!");
EthAddress::zero()
}
fn fee_recipient(self) -> AztecAddress {
assert(false, "'fee_recipient' not implemented!");
AztecAddress::zero()
}
fn push_nullifier_read_request(&mut self, nullifier: Field) {
assert(false, "'push_nullifier_read_request' not implemented!");
}
fn push_nullifier_non_existent_read_request(&mut self, nullifier: Field) {
assert(false, "'push_nullifier_non_existent_read_request' not implemented!");
}
fn accumulate_encrypted_logs<N>(&mut self, log: [Field; N]) {
assert(false, "'accumulate_encrypted_logs' not implemented!");
}
fn accumulate_unencrypted_logs<T>(&mut self, log: T) {
let event_selector = 0;
self.accumulate_unencrypted_logs(event_selector, log);
}
fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress) {
assert(false, "'consume_l1_to_l2_message' not implemented!");
}
fn message_portal(&mut self, recipient: EthAddress, content: Field) {
send_l2_to_l1_msg(recipient, content);
}
fn call_public_function<ARGS_COUNT>(
self: &mut Self,
contract_address: AztecAddress,
temporary_function_selector: FunctionSelector,
args: [Field; ARGS_COUNT]
) -> [Field; RETURN_VALUES_LENGTH] {
let gas = [/*l1_gas*/42, /*l2_gas*/24, /*da_gas*/420];
let results = call(
gas,
contract_address,
args,
temporary_function_selector.to_field()
);
let data_to_return: [Field; RETURN_VALUES_LENGTH] = results.0;
let success: u8 = results.1;
assert(success == 1, "Nested call failed!");
data_to_return
}
fn static_call_public_function<ARGS_COUNT>(
self: &mut Self,
contract_address: AztecAddress,
temporary_function_selector: FunctionSelector,
args: [Field; ARGS_COUNT]
) -> [Field; RETURN_VALUES_LENGTH] {
let gas = [/*l1_gas*/42, /*l2_gas*/24, /*da_gas*/420];
let (data_to_return, success): ([Field; RETURN_VALUES_LENGTH], u8) = call_static(
gas,
contract_address,
args,
temporary_function_selector.to_field()
);
assert(success == 1, "Nested static call failed!");
data_to_return
}
fn delegate_call_public_function<ARGS_COUNT>(
self: &mut Self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
args: [Field; ARGS_COUNT]
) -> [Field; RETURN_VALUES_LENGTH] {
assert(false, "'delegate_call_public_function' not implemented!");
[0; RETURN_VALUES_LENGTH]
}
}
impl ContextInterface for AVMContext {
fn push_new_note_hash(&mut self, note_hash: Field) {
emit_note_hash(note_hash);
}
fn push_new_nullifier(&mut self, nullifier: Field, _nullified_commitment: Field) {
// Cannot nullify pending commitments in AVM, so `nullified_commitment` is not used
emit_nullifier(nullifier);
}
fn msg_sender(self) -> AztecAddress {
sender()
}
fn this_address(self) -> AztecAddress {
address()
}
fn this_portal_address(self) -> EthAddress {
portal()
}
fn chain_id(self) -> Field {
chain_id()
}
fn version(self) -> Field {
version()
}
fn selector(self) -> FunctionSelector {
FunctionSelector::from_field(self.inputs.selector)
}
fn get_header(self) -> Header {
assert(false, "'get_header' not implemented!");
Header::empty()
}
fn get_args_hash(self) -> Field {
self.inputs.args_hash
}
}
// AVM oracles (opcodes) follow, do not use directly.
#[oracle(avmOpcodeAddress)]
fn address() -> AztecAddress {}
#[oracle(avmOpcodeStorageAddress)]
fn storage_address() -> AztecAddress {}
#[oracle(avmOpcodeOrigin)]
fn origin() -> AztecAddress {}
#[oracle(avmOpcodeSender)]
fn sender() -> AztecAddress {}
#[oracle(avmOpcodePortal)]
fn portal() -> EthAddress {}
#[oracle(avmOpcodeFeePerL1Gas)]
fn fee_per_l1_gas() -> Field {}
#[oracle(avmOpcodeFeePerL2Gas)]
fn fee_per_l2_gas() -> Field {}
#[oracle(avmOpcodeFeePerDaGas)]
fn fee_per_da_gas() -> Field {}
#[oracle(avmOpcodeChainId)]
fn chain_id() -> Field {}
#[oracle(avmOpcodeVersion)]
fn version() -> Field {}
#[oracle(avmOpcodeBlockNumber)]
fn block_number() -> Field {}
#[oracle(avmOpcodeTimestamp)]
fn timestamp() -> Field {}
#[oracle(avmOpcodeNoteHashExists)]
fn note_hash_exists(note_hash: Field, leaf_index: Field) -> u8 {}
#[oracle(avmOpcodeEmitNoteHash)]
fn emit_note_hash(note_hash: Field) {}
#[oracle(avmOpcodeNullifierExists)]
fn nullifier_exists(nullifier: Field) -> u8 {}
#[oracle(avmOpcodeEmitNullifier)]
fn emit_nullifier(nullifier: Field) {}
#[oracle(amvOpcodeEmitUnencryptedLog)]
fn emit_unencrypted_log<T>(event_selector: Field, message: T) {}
#[oracle(avmOpcodeL1ToL2MsgExists)]
fn l1_to_l2_msg_exists(msg_hash: Field, msg_leaf_index: Field) -> u8 {}
#[oracle(avmOpcodeSendL2ToL1Msg)]
fn send_l2_to_l1_msg(recipient: EthAddress, content: Field) {}
#[oracle(avmOpcodeCall)]
fn call<ARGS_COUNT, RET_SIZE>(
gas: [Field; 3], // gas allocation: [l1_gas, l2_gas, da_gas]
address: AztecAddress,
args: [Field; ARGS_COUNT],
// TODO(5110): consider passing in calldata directly
temporary_function_selector: Field
) -> ([Field; RET_SIZE], u8) {}
// ^ return data ^ success
#[oracle(avmOpcodeStaticCall)]
fn call_static<ARGS_COUNT, RET_SIZE>(
gas: [Field; 3], // gas allocation: [l1_gas, l2_gas, da_gas]
address: AztecAddress,
args: [Field; ARGS_COUNT],
// TODO(5110): consider passing in calldata directly
temporary_function_selector: Field
) -> ([Field; RET_SIZE], u8) {}
// ^ return data ^ success