Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: calculate FunctionSelectors and EventSelectors during comptime #8354

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions noir-projects/aztec-nr/authwit/src/auth.nr
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ pub fn assert_inner_hash_valid_authwit(context: &mut PrivateContext, on_behalf_o
// We perform a static call here and not a standard one to ensure that the account contract cannot re-enter.
let result: Field = context.static_call_private_function(
on_behalf_of,
FunctionSelector::from_signature("verify_private_authwit(Field)"),
comptime {
FunctionSelector::from_signature("verify_private_authwit(Field)")
},
[inner_hash]
).unpack_into();
assert(result == IS_VALID_SELECTOR, "Message not authorized by account");
Expand Down Expand Up @@ -263,7 +265,9 @@ pub fn assert_current_call_valid_authwit_public(context: &mut PublicContext, on_
pub fn assert_inner_hash_valid_authwit_public(context: &mut PublicContext, on_behalf_of: AztecAddress, inner_hash: Field) {
let result: Field = context.call_public_function(
CANONICAL_AUTH_REGISTRY_ADDRESS,
FunctionSelector::from_signature("consume((Field),Field)"),
comptime {
FunctionSelector::from_signature("consume((Field),Field)")
},
[on_behalf_of.to_field(), inner_hash].as_slice(),
GasOpts::default()
).deserialize_into();
Expand Down Expand Up @@ -357,7 +361,9 @@ pub fn compute_authwit_message_hash(consumer: AztecAddress, chain_id: Field, ver
pub fn set_authorized(context: &mut PublicContext, message_hash: Field, authorize: bool) {
context.call_public_function(
CANONICAL_AUTH_REGISTRY_ADDRESS,
FunctionSelector::from_signature("set_authorized(Field,bool)"),
comptime {
FunctionSelector::from_signature("set_authorized(Field,bool)")
},
[message_hash, authorize as Field].as_slice(),
GasOpts::default()
).assert_empty();
Expand All @@ -373,7 +379,9 @@ pub fn set_authorized(context: &mut PublicContext, message_hash: Field, authoriz
pub fn set_reject_all(context: &mut PublicContext, reject: bool) {
context.call_public_function(
CANONICAL_AUTH_REGISTRY_ADDRESS,
FunctionSelector::from_signature("set_reject_all(bool)"),
comptime {
FunctionSelector::from_signature("set_reject_all(bool)")
},
[context.this_address().to_field(), reject as Field].as_slice(),
GasOpts::default()
).assert_empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mod test {

impl EventInterface<TEST_EVENT_BYTES_LEN, TEST_EVENT_BYTES_LEN_WITHOUT_RANDOMNESS> for TestEvent {
fn get_event_type_id() -> EventSelector {
EventSelector::from_signature("TestEvent(Field,Field,Field)")
comptime { EventSelector::from_signature("TestEvent(Field,Field,Field)") }
}

fn private_to_be_bytes(self, randomness: Field) -> [u8; TEST_EVENT_BYTES_LEN] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ contract AvmTest {
#[aztec(public)]
fn check_selector() {
assert(
context.selector() == FunctionSelector::from_signature("check_selector()"), "Unexpected selector!"
context.selector() == comptime {
FunctionSelector::from_signature("check_selector()")
}, "Unexpected selector!"
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ contract FPC {
// FPC::at(context.this_address()).pay_refund_with_shielded_rebate(amount, asset, secret_hash).set_public_teardown_function(&mut context);
context.set_public_teardown_function(
context.this_address(),
FunctionSelector::from_signature("pay_refund_with_shielded_rebate(Field,(Field),Field)"),
comptime {
FunctionSelector::from_signature("pay_refund_with_shielded_rebate(Field,(Field),Field)")
},
[amount, asset.to_field(), secret_hash]
);
}
Expand All @@ -41,7 +43,9 @@ contract FPC {
// FPC::at(context.this_address()).pay_refund(context.msg_sender(), amount, asset).set_public_teardown_function(&mut context);
context.set_public_teardown_function(
context.this_address(),
FunctionSelector::from_signature("pay_refund((Field),Field,(Field))"),
comptime {
FunctionSelector::from_signature("pay_refund((Field),Field,(Field))")
},
[context.msg_sender().to_field(), amount, asset.to_field()]
);
}
Expand Down
28 changes: 20 additions & 8 deletions noir-projects/noir-contracts/contracts/parent_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ contract Parent {
context.call_public_function(target_contract, target_selector, [target_value]);
}

// Private function that enqueues two calls to a child contract:
// Private function that enqueues two calls to a child contract:
// - one through a nested call to enqueue_call_to_child with value 10,
// - followed by one issued directly from this function with value 20.
#[aztec(private)]
fn enqueue_calls_to_child_with_nested_first(
target_contract: AztecAddress,
target_selector: FunctionSelector
) {
let enqueue_call_to_child_selector = FunctionSelector::from_signature("enqueue_call_to_child((Field),(u32),Field)");
let enqueue_call_to_child_selector = comptime {
FunctionSelector::from_signature("enqueue_call_to_child((Field),(u32),Field)")
};
let _ret = context.call_private_function(
context.this_address(),
enqueue_call_to_child_selector,
Expand All @@ -73,7 +75,7 @@ contract Parent {
context.call_public_function(target_contract, target_selector, [20]);
}

// Private function that enqueues two calls to a child contract:
// Private function that enqueues two calls to a child contract:
// - one issued directly from this function with value 20,
// - followed by one through a nested call to enqueue_call_to_child with value 10.
#[aztec(private)]
Expand All @@ -82,7 +84,9 @@ contract Parent {
target_selector: FunctionSelector
) {
context.call_public_function(target_contract, target_selector, [20]);
let enqueue_call_to_child_selector = FunctionSelector::from_signature("enqueue_call_to_child((Field),(u32),Field)");
let enqueue_call_to_child_selector = comptime {
FunctionSelector::from_signature("enqueue_call_to_child((Field),(u32),Field)")
};
let _ret = context.call_private_function(
context.this_address(),
enqueue_call_to_child_selector,
Expand Down Expand Up @@ -110,7 +114,9 @@ contract Parent {
target_selector: FunctionSelector,
target_value: Field
) {
let pub_entry_point_selector = FunctionSelector::from_signature("pub_entry_point((Field),(u32),Field)");
let pub_entry_point_selector = comptime {
FunctionSelector::from_signature("pub_entry_point((Field),(u32),Field)")
};
let this_address = context.this_address();
let _void = context.call_public_function(
this_address,
Expand All @@ -126,7 +132,9 @@ contract Parent {
target_selector: FunctionSelector,
target_value: Field
) {
let pub_entry_point_selector = FunctionSelector::from_signature("pub_entry_point((Field),(u32),Field)");
let pub_entry_point_selector = comptime {
FunctionSelector::from_signature("pub_entry_point((Field),(u32),Field)")
};
let this_address = context.this_address();

context.call_public_function(
Expand Down Expand Up @@ -265,7 +273,9 @@ contract Parent {
let value_to_set = 7;
let parent_private_set_call_interface = Parent::interface().private_call(
child_contract_address,
FunctionSelector::from_signature("private_set_value(Field,(Field))"),
comptime {
FunctionSelector::from_signature("private_set_value(Field,(Field))")
},
[value_to_set, owner.to_field()]
);
let result: Field = env.call_private(parent_private_set_call_interface);
Expand All @@ -282,7 +292,9 @@ contract Parent {
// Get value from child through parent
let parent_private_get_call_interface = Parent::interface().private_call(
child_contract_address,
FunctionSelector::from_signature("private_get_value(Field,(Field))"),
comptime {
FunctionSelector::from_signature("private_get_value(Field,(Field))")
},
[7, owner.to_field()]
);
let read_result: Field = env.call_private(parent_private_get_call_interface);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ contract Test {
fn test_setting_teardown() {
context.set_public_teardown_function(
context.this_address(),
FunctionSelector::from_signature("dummy_public_call()"),
comptime {
FunctionSelector::from_signature("dummy_public_call()")
},
[]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,9 @@ contract Token {
// function has access to the final transaction fee, which is needed to compute the actual refund amount.
context.set_public_teardown_function(
context.this_address(),
FunctionSelector::from_signature("complete_refund(((Field,Field,bool)),((Field,Field,bool)),Field)"),
comptime {
FunctionSelector::from_signature("complete_refund(((Field,Field,bool)),((Field,Field,bool)),Field)")
},
[
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
]
Expand Down
2 changes: 1 addition & 1 deletion noir/noir-repo/aztec_macros/src/transforms/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ fn generate_fn_get_event_type_id(
let function_source = format!(
"
fn get_event_type_id() -> dep::aztec::protocol_types::abis::event_selector::EventSelector {{
dep::aztec::protocol_types::abis::event_selector::EventSelector::from_signature(\"{event_type}({from_signature_input})\")
comptime {{ dep::aztec::protocol_types::abis::event_selector::EventSelector::from_signature(\"{event_type}({from_signature_input})\") }}
}}
",
)
Expand Down
12 changes: 7 additions & 5 deletions noir/noir-repo/tooling/nargo_fmt/tests/expected/contract.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
// Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
// Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
// Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
contract Benchmarking {
use aztec::protocol_types::abis::function_selector::FunctionSelector;
Expand Down Expand Up @@ -63,7 +63,9 @@ contract Benchmarking {
storage.balances.at(owner).write(current + value);
let _callStackItem1 = context.call_public_function(
context.this_address(),
FunctionSelector::from_signature("broadcast(Field)"),
comptime {
FunctionSelector::from_signature("broadcast(Field)")
},
[owner]
);
}
Expand All @@ -75,5 +77,5 @@ contract Benchmarking {
}
}

// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1
// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1
contract Uniswap {}
12 changes: 7 additions & 5 deletions noir/noir-repo/tooling/nargo_fmt/tests/input/contract.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
// Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
// Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
// Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
// Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
contract Benchmarking {
use aztec::protocol_types::abis::function_selector::FunctionSelector;
Expand Down Expand Up @@ -63,7 +63,9 @@ contract Benchmarking {
storage.balances.at(owner).write(current + value);
let _callStackItem1 = context.call_public_function(
context.this_address(),
FunctionSelector::from_signature("broadcast(Field)"),
comptime {
FunctionSelector::from_signature("broadcast(Field)")
},
[owner]
);
}
Expand All @@ -75,5 +77,5 @@ contract Benchmarking {
}
}

// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1
// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1
contract Uniswap {}
Loading