Skip to content

Commit 0fda73f

Browse files
committed
feat: calculate FunctionSelectors and EventSelectors during comptime
1 parent f8f4709 commit 0fda73f

File tree

10 files changed

+57
-32
lines changed

10 files changed

+57
-32
lines changed

noir-projects/aztec-nr/authwit/src/auth.nr

+6-5
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,9 @@ pub fn assert_inner_hash_valid_authwit(context: &mut PrivateContext, on_behalf_o
219219
// We perform a static call here and not a standard one to ensure that the account contract cannot re-enter.
220220
let result: Field = context.static_call_private_function(
221221
on_behalf_of,
222-
FunctionSelector::from_signature("verify_private_authwit(Field)"),
223-
[inner_hash]
222+
comptime {
223+
FunctionSelector::from_signature("verify_private_authwit(Field)")
224+
}, [inner_hash]
224225
).unpack_into();
225226
assert(result == IS_VALID_SELECTOR, "Message not authorized by account");
226227
// Compute the nullifier, similar computation to the outer hash, but without the chain_id and version.
@@ -263,7 +264,7 @@ pub fn assert_current_call_valid_authwit_public(context: &mut PublicContext, on_
263264
pub fn assert_inner_hash_valid_authwit_public(context: &mut PublicContext, on_behalf_of: AztecAddress, inner_hash: Field) {
264265
let result: Field = context.call_public_function(
265266
CANONICAL_AUTH_REGISTRY_ADDRESS,
266-
FunctionSelector::from_signature("consume((Field),Field)"),
267+
comptime { FunctionSelector::from_signature("consume((Field),Field)") },
267268
[on_behalf_of.to_field(), inner_hash].as_slice(),
268269
GasOpts::default()
269270
).deserialize_into();
@@ -357,7 +358,7 @@ pub fn compute_authwit_message_hash(consumer: AztecAddress, chain_id: Field, ver
357358
pub fn set_authorized(context: &mut PublicContext, message_hash: Field, authorize: bool) {
358359
context.call_public_function(
359360
CANONICAL_AUTH_REGISTRY_ADDRESS,
360-
FunctionSelector::from_signature("set_authorized(Field,bool)"),
361+
comptime { FunctionSelector::from_signature("set_authorized(Field,bool)") },
361362
[message_hash, authorize as Field].as_slice(),
362363
GasOpts::default()
363364
).assert_empty();
@@ -373,7 +374,7 @@ pub fn set_authorized(context: &mut PublicContext, message_hash: Field, authoriz
373374
pub fn set_reject_all(context: &mut PublicContext, reject: bool) {
374375
context.call_public_function(
375376
CANONICAL_AUTH_REGISTRY_ADDRESS,
376-
FunctionSelector::from_signature("set_reject_all(bool)"),
377+
comptime { FunctionSelector::from_signature("set_reject_all(bool)") },
377378
[context.this_address().to_field(), reject as Field].as_slice(),
378379
GasOpts::default()
379380
).assert_empty();

noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ mod test {
177177

178178
impl EventInterface<TEST_EVENT_BYTES_LEN, TEST_EVENT_BYTES_LEN_WITHOUT_RANDOMNESS> for TestEvent {
179179
fn get_event_type_id() -> EventSelector {
180-
EventSelector::from_signature("TestEvent(Field,Field,Field)")
180+
comptime { EventSelector::from_signature("TestEvent(Field,Field,Field)") }
181181
}
182182

183183
fn private_to_be_bytes(self, randomness: Field) -> [u8; TEST_EVENT_BYTES_LEN] {

noir-projects/noir-contracts/contracts/avm_test_contract/src/main.nr

+3-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ contract AvmTest {
341341
#[aztec(public)]
342342
fn check_selector() {
343343
assert(
344-
context.selector() == FunctionSelector::from_signature("check_selector()"), "Unexpected selector!"
344+
context.selector() == comptime {
345+
FunctionSelector::from_signature("check_selector()")
346+
}, "Unexpected selector!"
345347
);
346348
}
347349

noir-projects/noir-contracts/contracts/fpc_contract/src/main.nr

+6-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ contract FPC {
2828
// FPC::at(context.this_address()).pay_refund_with_shielded_rebate(amount, asset, secret_hash).set_public_teardown_function(&mut context);
2929
context.set_public_teardown_function(
3030
context.this_address(),
31-
FunctionSelector::from_signature("pay_refund_with_shielded_rebate(Field,(Field),Field)"),
31+
comptime {
32+
FunctionSelector::from_signature("pay_refund_with_shielded_rebate(Field,(Field),Field)")
33+
},
3234
[amount, asset.to_field(), secret_hash]
3335
);
3436
}
@@ -41,7 +43,9 @@ contract FPC {
4143
// FPC::at(context.this_address()).pay_refund(context.msg_sender(), amount, asset).set_public_teardown_function(&mut context);
4244
context.set_public_teardown_function(
4345
context.this_address(),
44-
FunctionSelector::from_signature("pay_refund((Field),Field,(Field))"),
46+
comptime {
47+
FunctionSelector::from_signature("pay_refund((Field),Field,(Field))")
48+
},
4549
[context.msg_sender().to_field(), amount, asset.to_field()]
4650
);
4751
}

noir-projects/noir-contracts/contracts/parent_contract/src/main.nr

+20-8
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,17 @@ contract Parent {
5656
context.call_public_function(target_contract, target_selector, [target_value]);
5757
}
5858

59-
// Private function that enqueues two calls to a child contract:
59+
// Private function that enqueues two calls to a child contract:
6060
// - one through a nested call to enqueue_call_to_child with value 10,
6161
// - followed by one issued directly from this function with value 20.
6262
#[aztec(private)]
6363
fn enqueue_calls_to_child_with_nested_first(
6464
target_contract: AztecAddress,
6565
target_selector: FunctionSelector
6666
) {
67-
let enqueue_call_to_child_selector = FunctionSelector::from_signature("enqueue_call_to_child((Field),(u32),Field)");
67+
let enqueue_call_to_child_selector = comptime {
68+
FunctionSelector::from_signature("enqueue_call_to_child((Field),(u32),Field)")
69+
};
6870
let _ret = context.call_private_function(
6971
context.this_address(),
7072
enqueue_call_to_child_selector,
@@ -73,7 +75,7 @@ contract Parent {
7375
context.call_public_function(target_contract, target_selector, [20]);
7476
}
7577

76-
// Private function that enqueues two calls to a child contract:
78+
// Private function that enqueues two calls to a child contract:
7779
// - one issued directly from this function with value 20,
7880
// - followed by one through a nested call to enqueue_call_to_child with value 10.
7981
#[aztec(private)]
@@ -82,7 +84,9 @@ contract Parent {
8284
target_selector: FunctionSelector
8385
) {
8486
context.call_public_function(target_contract, target_selector, [20]);
85-
let enqueue_call_to_child_selector = FunctionSelector::from_signature("enqueue_call_to_child((Field),(u32),Field)");
87+
let enqueue_call_to_child_selector = comptime {
88+
FunctionSelector::from_signature("enqueue_call_to_child((Field),(u32),Field)")
89+
};
8690
let _ret = context.call_private_function(
8791
context.this_address(),
8892
enqueue_call_to_child_selector,
@@ -110,7 +114,9 @@ contract Parent {
110114
target_selector: FunctionSelector,
111115
target_value: Field
112116
) {
113-
let pub_entry_point_selector = FunctionSelector::from_signature("pub_entry_point((Field),(u32),Field)");
117+
let pub_entry_point_selector = comptime {
118+
FunctionSelector::from_signature("pub_entry_point((Field),(u32),Field)")
119+
};
114120
let this_address = context.this_address();
115121
let _void = context.call_public_function(
116122
this_address,
@@ -126,7 +132,9 @@ contract Parent {
126132
target_selector: FunctionSelector,
127133
target_value: Field
128134
) {
129-
let pub_entry_point_selector = FunctionSelector::from_signature("pub_entry_point((Field),(u32),Field)");
135+
let pub_entry_point_selector = comptime {
136+
FunctionSelector::from_signature("pub_entry_point((Field),(u32),Field)")
137+
};
130138
let this_address = context.this_address();
131139

132140
context.call_public_function(
@@ -265,7 +273,9 @@ contract Parent {
265273
let value_to_set = 7;
266274
let parent_private_set_call_interface = Parent::interface().private_call(
267275
child_contract_address,
268-
FunctionSelector::from_signature("private_set_value(Field,(Field))"),
276+
comptime {
277+
FunctionSelector::from_signature("private_set_value(Field,(Field))")
278+
},
269279
[value_to_set, owner.to_field()]
270280
);
271281
let result: Field = env.call_private(parent_private_set_call_interface);
@@ -282,7 +292,9 @@ contract Parent {
282292
// Get value from child through parent
283293
let parent_private_get_call_interface = Parent::interface().private_call(
284294
child_contract_address,
285-
FunctionSelector::from_signature("private_get_value(Field,(Field))"),
295+
comptime {
296+
FunctionSelector::from_signature("private_get_value(Field,(Field))")
297+
},
286298
[7, owner.to_field()]
287299
);
288300
let read_result: Field = env.call_private(parent_private_get_call_interface);

noir-projects/noir-contracts/contracts/test_contract/src/main.nr

+3-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ contract Test {
213213
fn test_setting_teardown() {
214214
context.set_public_teardown_function(
215215
context.this_address(),
216-
FunctionSelector::from_signature("dummy_public_call()"),
216+
comptime {
217+
FunctionSelector::from_signature("dummy_public_call()")
218+
},
217219
[]
218220
);
219221
}

noir-projects/noir-contracts/contracts/token_contract/src/main.nr

+3-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,9 @@ contract Token {
572572
// function has access to the final transaction fee, which is needed to compute the actual refund amount.
573573
context.set_public_teardown_function(
574574
context.this_address(),
575-
FunctionSelector::from_signature("complete_refund(((Field,Field,bool)),((Field,Field,bool)),Field)"),
575+
comptime {
576+
FunctionSelector::from_signature("complete_refund(((Field,Field,bool)),((Field,Field,bool)),Field)")
577+
},
576578
[
577579
fee_payer_point.inner.x, fee_payer_point.inner.y, fee_payer_point.inner.is_infinite as Field, user_point.inner.x, user_point.inner.y, user_point.inner.is_infinite as Field, funded_amount
578580
]

noir/noir-repo/aztec_macros/src/transforms/events.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ fn generate_fn_get_event_type_id(
230230
let function_source = format!(
231231
"
232232
fn get_event_type_id() -> dep::aztec::protocol_types::abis::event_selector::EventSelector {{
233-
dep::aztec::protocol_types::abis::event_selector::EventSelector::from_signature(\"{event_type}({from_signature_input})\")
233+
comptime {{ dep::aztec::protocol_types::abis::event_selector::EventSelector::from_signature(\"{event_type}({from_signature_input})\") }}
234234
}}
235235
",
236236
)

noir/noir-repo/tooling/nargo_fmt/tests/expected/contract.nr

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
2-
// Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
3-
// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
1+
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
2+
// Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
3+
// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
44
// Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
55
contract Benchmarking {
66
use aztec::protocol_types::abis::function_selector::FunctionSelector;
@@ -63,8 +63,9 @@ contract Benchmarking {
6363
storage.balances.at(owner).write(current + value);
6464
let _callStackItem1 = context.call_public_function(
6565
context.this_address(),
66-
FunctionSelector::from_signature("broadcast(Field)"),
67-
[owner]
66+
comptime {
67+
FunctionSelector::from_signature("broadcast(Field)")
68+
}, [owner]
6869
);
6970
}
7071

@@ -75,5 +76,5 @@ contract Benchmarking {
7576
}
7677
}
7778

78-
// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1
79+
// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1
7980
contract Uniswap {}

noir/noir-repo/tooling/nargo_fmt/tests/input/contract.nr

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
2-
// Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
3-
// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
1+
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
2+
// Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
3+
// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
44
// Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
55
contract Benchmarking {
66
use aztec::protocol_types::abis::function_selector::FunctionSelector;
@@ -63,8 +63,9 @@ contract Benchmarking {
6363
storage.balances.at(owner).write(current + value);
6464
let _callStackItem1 = context.call_public_function(
6565
context.this_address(),
66-
FunctionSelector::from_signature("broadcast(Field)"),
67-
[owner]
66+
comptime {
67+
FunctionSelector::from_signature("broadcast(Field)")
68+
}, [owner]
6869
);
6970
}
7071

@@ -75,5 +76,5 @@ contract Benchmarking {
7576
}
7677
}
7778

78-
// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1
79+
// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1
7980
contract Uniswap {}

0 commit comments

Comments
 (0)