Skip to content

Commit bbaebf4

Browse files
authored
feat!: moving compute_selector to FunctionSelector (AztecProtocol#3806)
Fixes AztecProtocol#3681
1 parent df581f0 commit bbaebf4

File tree

31 files changed

+204
-163
lines changed

31 files changed

+204
-163
lines changed

boxes/token/src/contracts/src/main.nr

+7-5
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ contract Token {
2929
bool_serialization::{BoolSerializationMethods, BOOL_SERIALIZED_LEN},
3030
address_serialization::{AddressSerializationMethods, AZTEC_ADDRESS_SERIALIZED_LEN},
3131
},
32-
selector::compute_selector,
3332
};
34-
use dep::protocol_types::address::AztecAddress;
33+
use dep::protocol_types::{
34+
abis::function_selector::FunctionSelector,
35+
address::AztecAddress,
36+
};
3537

3638
// docs:start:import_authwit
3739
use dep::authwit::{
@@ -123,7 +125,7 @@ contract Token {
123125
// docs:start:constructor
124126
#[aztec(private)]
125127
fn constructor(admin: AztecAddress) {
126-
let selector = compute_selector("_initialize((Field))");
128+
let selector = FunctionSelector::from_signature("_initialize((Field))");
127129
context.call_public_function(context.this_address(), selector, [admin.to_field()]);
128130
}
129131
// docs:end:constructor
@@ -268,7 +270,7 @@ contract Token {
268270

269271
storage.balances.at(from).sub(SafeU120::new(amount));
270272

271-
let selector = compute_selector("_increase_public_balance((Field),Field)");
273+
let selector = FunctionSelector::from_signature("_increase_public_balance((Field),Field)");
272274
let _void = context.call_public_function(context.this_address(), selector, [to.to_field(), amount]);
273275
}
274276
// docs:end:unshield
@@ -303,7 +305,7 @@ contract Token {
303305

304306
storage.balances.at(from).sub(SafeU120::new(amount));
305307

306-
let selector = compute_selector("_reduce_total_supply(Field)");
308+
let selector = FunctionSelector::from_signature("_reduce_total_supply(Field)");
307309
let _void = context.call_public_function(context.this_address(), selector, [amount]);
308310
}
309311
// docs:end:burn

docs/docs/dev_docs/contracts/syntax/functions.md

-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ Oracles introduce **non-determinism** into a circuit, and thus are `unconstraine
111111

112112
### A few useful inbuilt oracles
113113

114-
- [`compute_selector`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec-nr/aztec/src/selector.nr) - Computes the selector of a function. This is useful for when you want to call a function from within a circuit, but don't have an interface at hand and don't want to hardcode the selector in hex.
115114
- [`debug_log`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec-nr/aztec/src/oracle/debug_log.nr) - Provides a couple of debug functions that can be used to log information to the console.
116115
- [`auth_witness`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec-nr/authwit/src/auth_witness.nr) - Provides a way to fetch the authentication witness for a given address. This is useful when building account contracts to support approve-like functionality.
117116
- [`get_l1_to_l2_message`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec-nr/aztec/src/oracle/get_l1_to_l2_message.nr) - Useful for application that receive messages from L1 to be consumed on L2, such as token bridges or other cross-chain applications.

docs/docs/dev_docs/tutorials/writing_private_voting_contract.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ We are using various utils within the Aztec library:
7171

7272
* `context` - exposes things such as the contract address, msg_sender, etc
7373
* `oracle::get_secret_key` - get your secret key to help us create a randomized nullifier
74-
* `selector::compute_selector` - compute a function selector so we can call functions from other functions
74+
* `FunctionSelector::from_signature` - compute a function selector from signature so we can call functions from other functions
7575
* `state_vars::{ map::Map, public_state::PublicState, }` - we will use a Map to store the votes (key = voteId, value = number of votes), and PublicState to hold our public values that we mentioned earlier
7676
* `types::type_serialization::{..}` - various serialization methods for defining how to use these types
7777
* `types::address::{AztecAddress},` - our admin will be held as an address
@@ -113,7 +113,7 @@ Therefore our constructor must call a public function by using `context.call_pub
113113

114114
`context.call_public_function()` takes three arguments:
115115
1. The contract address whose method we want to call
116-
2. The selector of the function to call (we can use `compute_selector()` for this)
116+
2. The selector of the function to call (we can use `FunctionSelector::from_signature(...)` for this)
117117
3. The arguments of the function (we pass the `admin`)
118118

119119
We now need to write the `_initialize()` function:

noir/aztec_macros/src/lib.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -483,11 +483,12 @@ const SIGNATURE_PLACEHOLDER: &str = "SIGNATURE_PLACEHOLDER";
483483

484484
/// Generates the impl for an event selector
485485
///
486+
/// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3590): Make this point to aztec-nr once the issue is fixed.
486487
/// Inserts the following code:
487488
/// ```noir
488489
/// impl SomeStruct {
489490
/// fn selector() -> FunctionSelector {
490-
/// aztec::selector::compute_selector("SIGNATURE_PLACEHOLDER")
491+
/// protocol_types::abis::function_selector::FunctionSelector::from_signature("SIGNATURE_PLACEHOLDER")
491492
/// }
492493
/// }
493494
/// ```
@@ -498,15 +499,18 @@ const SIGNATURE_PLACEHOLDER: &str = "SIGNATURE_PLACEHOLDER";
498499
fn generate_selector_impl(structure: &NoirStruct) -> TypeImpl {
499500
let struct_type = make_type(UnresolvedTypeData::Named(path(structure.name.clone()), vec![]));
500501

502+
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3590): Make this point to aztec-nr once the issue is fixed.
503+
let selector_path = chained_path!("protocol_types", "abis", "function_selector", "FunctionSelector");
504+
let mut from_signature_path = selector_path.clone();
505+
from_signature_path.segments.push(ident("from_signature"));
506+
501507
let selector_fun_body = BlockExpression(vec![make_statement(StatementKind::Expression(call(
502-
variable_path(chained_path!("aztec", "selector", "compute_selector")),
508+
variable_path(from_signature_path),
503509
vec![expression(ExpressionKind::Literal(Literal::Str(SIGNATURE_PLACEHOLDER.to_string())))],
504510
)))]);
505511

506512
// Define `FunctionSelector` return type
507-
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/3590): Make this point to aztec-nr once the issue is fixed.
508-
let return_type_path = chained_path!("protocol_types", "abis", "function_selector", "FunctionSelector");
509-
let return_type = FunctionReturnType::Ty(make_type(UnresolvedTypeData::Named(return_type_path, vec![])));
513+
let return_type = FunctionReturnType::Ty(make_type(UnresolvedTypeData::Named(selector_path, vec![])));
510514

511515
let mut selector_fn_def = FunctionDefinition::normal(
512516
&ident("selector"),

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// 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 {
6+
use dep::protocol_types::abis::function_selector::FunctionSelector;
7+
68
use dep::value_note::{
79
utils::{increment, decrement},
810
value_note::{VALUE_NOTE_LEN, ValueNote, ValueNoteMethods},
@@ -11,7 +13,6 @@ contract Benchmarking {
1113
use dep::aztec::{
1214
context::{Context},
1315
note::{utils as note_utils, note_getter_options::NoteGetterOptions, note_header::NoteHeader},
14-
selector::compute_selector,
1516
log::emit_unencrypted_log,
1617
state_vars::{map::Map, public_state::PublicState, set::Set},
1718
types::type_serialization::field_serialization::{FieldSerializationMethods, FIELD_SERIALIZED_LEN},
@@ -59,7 +60,7 @@ contract Benchmarking {
5960
storage.balances.at(owner).write(current + value);
6061
let _callStackItem1 = context.call_public_function(
6162
context.this_address(),
62-
compute_selector("broadcast(Field)"),
63+
FunctionSelector::from_signature("broadcast(Field)"),
6364
[owner]
6465
);
6566
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// 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 {
6+
use dep::protocol_types::abis::function_selector::FunctionSelector;
7+
68
use dep::value_note::{
79
utils::{increment, decrement},
810
value_note::{VALUE_NOTE_LEN, ValueNote, ValueNoteMethods},
@@ -11,7 +13,6 @@ contract Benchmarking {
1113
use dep::aztec::{
1214
context::{Context},
1315
note::{utils as note_utils, note_getter_options::NoteGetterOptions, note_header::NoteHeader},
14-
selector::compute_selector,
1516
log::emit_unencrypted_log,
1617
state_vars::{map::Map, public_state::PublicState, set::Set},
1718
types::type_serialization::field_serialization::{FieldSerializationMethods, FIELD_SERIALIZED_LEN},
@@ -57,7 +58,7 @@ contract Benchmarking {
5758
fn increment_balance(owner: Field, value: Field) {
5859
let current = storage.balances.at(owner).read();
5960
storage.balances.at(owner).write(current + value);
60-
let _callStackItem1 = context.call_public_function(context.this_address(), compute_selector("broadcast(Field)"), [owner]);
61+
let _callStackItem1 = context.call_public_function(context.this_address(), FunctionSelector::from_signature("broadcast(Field)"), [owner]);
6162
}
6263

6364
// Est ultricies integer quis auctor elit sed. In nibh mauris cursus mattis molestie a iaculis.

yarn-project/aztec-nr/authwit/src/account.nr

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use dep::aztec::context::{PrivateContext, PublicContext, Context};
2-
use dep::aztec::selector::compute_selector;
32
use dep::aztec::state_vars::{map::Map, public_state::PublicState};
43
use dep::aztec::types::type_serialization::bool_serialization::{BoolSerializationMethods,BOOL_SERIALIZED_LEN};
54

yarn-project/aztec-nr/aztec/src/lib.nr

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ mod log;
66
mod messaging;
77
mod note;
88
mod oracle;
9-
mod selector;
109
mod state_vars;
1110
mod types;
1211
mod utils;

yarn-project/aztec-nr/aztec/src/selector.nr

-17
This file was deleted.

yarn-project/aztec-nr/aztec/src/utils.nr

-16
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@ pub fn arr_copy_slice<T, N, M>(src: [T; N], mut dst: [T; M], offset: Field) -> [
55
dst
66
}
77

8-
pub fn field_from_bytes<N>(bytes: [u8; N], big_endian: bool) -> Field {
9-
assert(bytes.len() as u32 < 32, "field_from_bytes: N must be less than 32");
10-
let mut as_field = 0;
11-
let mut offset = 1;
12-
for i in 0..N {
13-
let mut index = i;
14-
if big_endian {
15-
index = N - i - 1;
16-
}
17-
as_field += (bytes[index] as Field) * offset;
18-
offset *= 256;
19-
}
20-
21-
as_field
22-
}
23-
248
// TODO(#3470): Copied over from https://github.com/AztecProtocol/aztec-packages/blob/a07c4bd47313be6aa604a63f37857eb0136b41ba/yarn-project/noir-protocol-circuits/src/crates/rollup-lib/src/base/base_rollup_inputs.nr#L599
259
// move to a shared place?
2610

yarn-project/noir-compiler/src/cli/add_noir_compiler_commander_actions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ export function addNoirCompilerCommanderActions(program: Command, log: LogFn = (
4040
.option('--artifacts <path>', 'Folder containing the compiled artifacts, relative to the project path', 'target')
4141
.option(
4242
'-o, --outdir <path>',
43-
'Output folder for the generated noir interfaces, relative to the project path',
43+
'Output folder for the generated typescript interfaces, relative to the project path',
4444
'interfaces',
4545
)
46-
.description('Generates Noir interfaces from the artifacts in the given project')
46+
.description('Generates typescript interfaces from the artifacts in the given project')
4747

4848
.action(async (projectPath: string, options) => {
4949
const { generateTypescriptInterface } = await import('./generate_typescript_interface.js');

yarn-project/noir-contracts/src/contracts/benchmarking_contract/src/main.nr

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ contract Benchmarking {
1313
use dep::aztec::{
1414
context::{Context},
1515
note::{utils as note_utils, note_getter_options::NoteGetterOptions, note_header::NoteHeader},
16-
selector::compute_selector,
1716
log::emit_unencrypted_log,
1817
state_vars::{map::Map, public_state::PublicState, set::Set},
1918
types::type_serialization::field_serialization::{FieldSerializationMethods, FIELD_SERIALIZED_LEN},
2019
};
2120

22-
use dep::protocol_types::address::AztecAddress;
21+
use dep::protocol_types::{
22+
abis::function_selector::FunctionSelector,
23+
address::AztecAddress,
24+
};
2325

2426
struct Storage {
2527
notes: Map<Set<ValueNote, VALUE_NOTE_LEN>>,
@@ -66,7 +68,7 @@ contract Benchmarking {
6668
storage.balances.at(owner.to_field()).write(current + value);
6769
let _callStackItem1 = context.call_public_function(
6870
context.this_address(),
69-
compute_selector("broadcast((Field))"),
71+
FunctionSelector::from_signature("broadcast((Field))"),
7072
[owner.to_field()]
7173
);
7274
}

yarn-project/noir-contracts/src/contracts/card_game_contract/src/main.nr

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod game;
33

44
contract CardGame {
55
use dep::protocol_types::{
6+
abis::function_selector::FunctionSelector,
67
address::AztecAddress,
78
constants::MAX_NOTES_PER_PAGE,
89
};
@@ -33,7 +34,6 @@ contract CardGame {
3334
note_header::NoteHeader,
3435
utils as note_utils,
3536
},
36-
selector::compute_selector
3737
};
3838

3939
use crate::cards::{
@@ -128,7 +128,7 @@ contract CardGame {
128128
collection.remove_cards(cards, player);
129129
let mut game_deck = storage.game_decks.at(game as Field).at(player.to_field());
130130
let _added_to_game_deck = game_deck.add_cards(cards, player);
131-
let selector = compute_selector("on_game_joined(u32,(Field),u32)");
131+
let selector = FunctionSelector::from_signature("on_game_joined(u32,(Field),u32)");
132132
let strength = compute_deck_strength(cards);
133133
context.call_public_function(
134134
context.this_address(),
@@ -163,7 +163,7 @@ contract CardGame {
163163
let mut game_deck = storage.game_decks.at(game as Field).at(player.to_field());
164164
game_deck.remove_cards([card], player);
165165

166-
let selector = compute_selector("on_card_played(u32,(Field),Field)");
166+
let selector = FunctionSelector::from_signature("on_card_played(u32,(Field),Field)");
167167
// docs:start:call_public_function
168168
context.call_public_function(
169169
context.this_address(),
@@ -195,7 +195,7 @@ contract CardGame {
195195
let mut collection = storage.collections.at(player.to_field());
196196
let _inserted_cards = collection.add_cards(cards, player);
197197

198-
let selector = compute_selector("on_cards_claimed(u32,(Field),Field)");
198+
let selector = FunctionSelector::from_signature("on_cards_claimed(u32,(Field),Field)");
199199
context.call_public_function(
200200
context.this_address(),
201201
selector,

yarn-project/noir-contracts/src/contracts/child_contract/src/main.nr

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ contract Child {
55
use dep::aztec::{
66
abi::CallContext,
77
context::{PrivateContext, PublicContext, Context},
8-
selector::compute_selector,
98
log::emit_unencrypted_log,
109
state_vars::public_state::PublicState,
1110
types::type_serialization::field_serialization::{FieldSerializationMethods, FIELD_SERIALIZED_LEN},
1211
};
13-
use dep::protocol_types::address::AztecAddress;
12+
use dep::protocol_types::{
13+
abis::function_selector::FunctionSelector,
14+
address::AztecAddress,
15+
};
1416

1517
struct Storage {
1618
current_value: PublicState<Field, FIELD_SERIALIZED_LEN>,
@@ -93,7 +95,7 @@ contract Child {
9395

9496
#[aztec(public)]
9597
fn setValueTwiceWithNestedFirst() {
96-
let pubSetValueSelector = compute_selector("pubSetValue(Field)");
98+
let pubSetValueSelector = FunctionSelector::from_signature("pubSetValue(Field)");
9799
let _ret = context.call_public_function(context.this_address(), pubSetValueSelector, [10]);
98100

99101
storage.current_value.write(20);
@@ -105,7 +107,7 @@ contract Child {
105107
storage.current_value.write(20);
106108
emit_unencrypted_log(&mut context, 20);
107109

108-
let pubSetValueSelector = compute_selector("pubSetValue(Field)");
110+
let pubSetValueSelector = FunctionSelector::from_signature("pubSetValue(Field)");
109111
let _ret = context.call_public_function(context.this_address(), pubSetValueSelector, [10]);
110112
}
111113

yarn-project/noir-contracts/src/contracts/easy_private_voting_contract/src/main.nr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
contract EasyPrivateVoting {
22
// docs:start:imports
33
use dep::protocol_types::{
4+
abis::function_selector::FunctionSelector,
45
address::AztecAddress,
56
constants::EMPTY_NULLIFIED_COMMITMENT,
67
};
78
use dep::aztec::{
89
context::{PrivateContext, Context},
910
oracle::get_secret_key::get_secret_key, // used to compute nullifier
10-
selector::compute_selector, // used to compute function selector for calling a function
1111
state_vars::{ map::Map, public_state::PublicState,},
1212
types::type_serialization::{ // serialization methods for using booleans and aztec addresses
1313
bool_serialization::{BoolSerializationMethods, BOOL_SERIALIZED_LEN},
@@ -57,7 +57,7 @@ contract EasyPrivateVoting {
5757
context.call_public_function(
5858
// we cannot update public state directly from private function but we can call public function (which queues it)
5959
context.this_address(),// contract address whose method we want to call
60-
compute_selector("_initialize((Field))"), // function selector
60+
FunctionSelector::from_signature("_initialize((Field))"), // function selector
6161
[admin.to_field()] // parameters
6262
);
6363
}
@@ -77,7 +77,7 @@ contract EasyPrivateVoting {
7777
context.push_new_nullifier(nullifier, EMPTY_NULLIFIED_COMMITMENT); // push nullifier
7878
context.call_public_function(
7979
context.this_address(),
80-
compute_selector("add_to_tally_public(Field)"),
80+
FunctionSelector::from_signature("add_to_tally_public(Field)"),
8181
[candidate]
8282
);
8383
}

yarn-project/noir-contracts/src/contracts/escrow_contract/src/main.nr

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
contract Escrow {
33
use dep::std::option::Option;
44

5-
use dep::protocol_types::address::AztecAddress;
5+
use dep::protocol_types::{
6+
abis::function_selector::FunctionSelector,
7+
address::AztecAddress,
8+
};
69

710
use dep::aztec::{
811
context::{PrivateContext, PublicContext, Context},
@@ -12,7 +15,6 @@ contract Escrow {
1215
utils as note_utils,
1316
},
1417
oracle::get_public_key::get_public_key,
15-
selector::compute_selector,
1618
state_vars::set::Set,
1719
};
1820

@@ -59,7 +61,7 @@ contract Escrow {
5961
let notes = storage.owners.get_notes(options);
6062
assert(notes[0].is_some(), "Sender is not an owner.");
6163

62-
let selector = compute_selector("transfer((Field),(Field),Field,Field)");
64+
let selector = FunctionSelector::from_signature("transfer((Field),(Field),Field,Field)");
6365
let _callStackItem = context.call_private_function(
6466
token,
6567
selector,

0 commit comments

Comments
 (0)