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

chore: fix a bunch of generics issues in aztec-nr #8295

Merged
merged 3 commits into from
Aug 30, 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
12 changes: 6 additions & 6 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl PrivateContext {
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
// 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
Expand Down Expand Up @@ -323,31 +323,31 @@ impl PrivateContext {
self.call_private_function_with_packed_args(contract_address, function_selector, args_hash, false, true)
}

pub fn call_private_function_no_args<RETURNS_COUNT>(
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<RETURNS_COUNT>(
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<let ARGS_COUNT: u32>(
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<RETURNS_COUNT>(
pub fn call_private_function_with_packed_args(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
Expand Down Expand Up @@ -511,7 +511,7 @@ impl PrivateContext {
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<let ARGS_COUNT: u32>(
pub fn set_public_teardown_function_with_packed_args(
&mut self,
contract_address: AztecAddress,
function_selector: FunctionSelector,
Expand Down
10 changes: 6 additions & 4 deletions noir-projects/aztec-nr/aztec/src/context/public_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,6 @@ impl<let N: u32> FunctionReturns<N> {
FunctionReturns { values }
}

pub fn assert_empty(returns: FunctionReturns<0>) {
assert(returns.values.len() == 0);
}

pub fn raw(self) -> [Field; N] {
self.values
}
Expand All @@ -403,3 +399,9 @@ impl<let N: u32> FunctionReturns<N> {
Deserialize::deserialize(self.raw())
}
}

impl FunctionReturns<0> {
pub fn assert_empty(self) {
assert(self.values.len() == 0);
}
}
Comment on lines +403 to +407
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function seems very odd as we're just testing that a zero length array has zero length...

6 changes: 3 additions & 3 deletions noir-projects/aztec-nr/aztec/src/note/note_getter/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn extract_property_value_from_selector<let N: u32>(
serialized_note: [Field; N],
selector: PropertySelector
) -> Field {
// Selectors use PropertySelectors in order to locate note properties inside the serialized note.
// Selectors use PropertySelectors in order to locate note properties inside the serialized note.
// This allows easier packing and custom (de)serialization schemas. A note property is located
// inside the serialized note using the index inside the array, a byte offset and a length.
let value = serialized_note[selector.index].to_be_bytes(32);
Expand Down Expand Up @@ -113,7 +113,7 @@ pub fn get_notes<Note, let N: u32, let M: u32, PREPROCESSOR_ARGS, FILTER_ARGS>(
constrain_get_notes_internal(context, storage_slot, opt_notes, options)
}

unconstrained fn apply_preprocessor<Note, let N: u32, let M: u32, PREPROCESSOR_ARGS>(
unconstrained fn apply_preprocessor<Note, PREPROCESSOR_ARGS>(
notes: [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
preprocessor: fn([Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL], PREPROCESSOR_ARGS) -> [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
preprocessor_args: PREPROCESSOR_ARGS
Expand Down Expand Up @@ -264,7 +264,7 @@ unconstrained pub fn view_notes<Note, let N: u32, let M: u32>(
notes
}

unconstrained fn flatten_options<Note, let N: u32>(
unconstrained fn flatten_options<let N: u32>(
selects: BoundedVec<Option<Select>, N>,
sorts: BoundedVec<Option<Sort>, N>
) -> (u8, [u8; N], [u8; N], [u8; N], [Field; N], [u8; N], [u8; N], [u8; N], [u8; N], [u8; N]) {
Expand Down
122 changes: 64 additions & 58 deletions noir-projects/aztec-nr/aztec/src/note/note_getter_options.nr
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ global NoteStatus = NoteStatusEnum {
// TODO 4217: add 'NULLIFIED'
};

fn return_all_notes<Note, let N: u32>(
fn return_all_notes<Note>(
notes: [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
_p: Field
) -> [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL] {
Expand Down Expand Up @@ -100,64 +100,9 @@ struct NoteGetterOptions<Note, let N: u32, let M: u32, PREPROCESSOR_ARGS, FILTER
// the result size.
// And finally, a custom preprocessor and filter to refine the outcome further.
impl<Note, let N: u32, let M: u32, PREPROCESSOR_ARGS, FILTER_ARGS> NoteGetterOptions<Note, N, M, PREPROCESSOR_ARGS, FILTER_ARGS> {
// This function initializes a NoteGetterOptions that simply returns the maximum number of notes allowed in a call.
pub fn new() -> NoteGetterOptions<Note, N, M, Field, Field> where Note: NoteInterface<N, M> {
NoteGetterOptions {
selects: BoundedVec::new(),
sorts: BoundedVec::new(),
limit: MAX_NOTE_HASH_READ_REQUESTS_PER_CALL as u32,
offset: 0,
preprocessor: return_all_notes,
preprocessor_args: 0,
filter: return_all_notes,
filter_args: 0,
status: NoteStatus.ACTIVE
}
}

// This function initializes a NoteGetterOptions with a preprocessor, which takes the notes returned from
// the database and preprocessor_args as its parameters.
// `preprocessor_args` allows you to provide additional data or context to the custom preprocessor.
pub fn with_preprocessor(
preprocessor: fn([Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL], PREPROCESSOR_ARGS) -> [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
preprocessor_args: PREPROCESSOR_ARGS
) -> NoteGetterOptions<Note, N, M, PREPROCESSOR_ARGS, Field> where Note: NoteInterface<N, M> {
NoteGetterOptions {
selects: BoundedVec::new(),
sorts: BoundedVec::new(),
limit: MAX_NOTE_HASH_READ_REQUESTS_PER_CALL as u32,
offset: 0,
preprocessor,
preprocessor_args,
filter: return_all_notes,
filter_args: 0,
status: NoteStatus.ACTIVE
}
}

// This function initializes a NoteGetterOptions with a filter, which takes
// the notes returned from the database and filter_args as its parameters.
// `filter_args` allows you to provide additional data or context to the custom filter.
pub fn with_filter(
filter: fn([Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL], FILTER_ARGS) -> [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
filter_args: FILTER_ARGS
) -> NoteGetterOptions<Note, N, M, Field, FILTER_ARGS> where Note: NoteInterface<N, M> {
NoteGetterOptions {
selects: BoundedVec::new(),
sorts: BoundedVec::new(),
limit: MAX_NOTE_HASH_READ_REQUESTS_PER_CALL as u32,
offset: 0,
preprocessor: return_all_notes,
preprocessor_args: 0,
filter,
filter_args,
status: NoteStatus.ACTIVE
}
}

// This method adds a `Select` criterion to the options.
// It takes a property_selector indicating which field to select,
// a value representing the specific value to match in that field, and
// It takes a property_selector indicating which field to select,
// a value representing the specific value to match in that field, and
// a comparator (For possible values of comparators, please see the Comparator enum above)
pub fn select<T>(
&mut self,
Expand Down Expand Up @@ -207,3 +152,64 @@ impl<Note, let N: u32, let M: u32, PREPROCESSOR_ARGS, FILTER_ARGS> NoteGetterOpt
*self
}
}

impl<Note, let N: u32, let M: u32> NoteGetterOptions<Note, N, M, Field, Field> where Note: NoteInterface<N, M> {
// This function initializes a NoteGetterOptions that simply returns the maximum number of notes allowed in a call.
pub fn new() -> Self {
Self {
selects: BoundedVec::new(),
sorts: BoundedVec::new(),
limit: MAX_NOTE_HASH_READ_REQUESTS_PER_CALL as u32,
offset: 0,
preprocessor: return_all_notes,
preprocessor_args: 0,
filter: return_all_notes,
filter_args: 0,
status: NoteStatus.ACTIVE
}
}
}

impl<Note, let N: u32, let M: u32, PREPROCESSOR_ARGS> NoteGetterOptions<Note, N, M, PREPROCESSOR_ARGS, Field> where Note: NoteInterface<N, M> {
// This function initializes a NoteGetterOptions with a preprocessor, which takes the notes returned from
// the database and preprocessor_args as its parameters.
// `preprocessor_args` allows you to provide additional data or context to the custom preprocessor.
pub fn with_preprocessor(
preprocessor: fn([Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL], PREPROCESSOR_ARGS) -> [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
preprocessor_args: PREPROCESSOR_ARGS
) -> Self {
Self {
selects: BoundedVec::new(),
sorts: BoundedVec::new(),
limit: MAX_NOTE_HASH_READ_REQUESTS_PER_CALL as u32,
offset: 0,
preprocessor,
preprocessor_args,
filter: return_all_notes,
filter_args: 0,
status: NoteStatus.ACTIVE
}
}
}

impl<Note, let N: u32, let M: u32, FILTER_ARGS> NoteGetterOptions<Note, N, M, Field, FILTER_ARGS> where Note: NoteInterface<N, M> {
// This function initializes a NoteGetterOptions with a filter, which takes
// the notes returned from the database and filter_args as its parameters.
// `filter_args` allows you to provide additional data or context to the custom filter.
pub fn with_filter(
filter: fn([Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL], FILTER_ARGS) -> [Option<Note>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
filter_args: FILTER_ARGS
) -> Self {
Self {
selects: BoundedVec::new(),
sorts: BoundedVec::new(),
limit: MAX_NOTE_HASH_READ_REQUESTS_PER_CALL as u32,
offset: 0,
preprocessor: return_all_notes,
preprocessor_args: 0,
filter,
filter_args,
status: NoteStatus.ACTIVE
}
}
}
8 changes: 4 additions & 4 deletions noir-projects/aztec-nr/aztec/src/oracle/arguments.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ unconstrained fn pack_arguments_array_oracle<let N: u32>(_args: [Field; N]) -> F
unconstrained fn pack_arguments_oracle(_args: [Field]) -> Field {}

/// - Pack arguments (array version) will notify the simulator that these arguments will be used later at
/// some point in the call.
/// some point in the call.
/// - When the external call is made later, the simulator will know what the values unpack to.
/// - This oracle will not be required in public vm functions, as the vm will keep track of arguments
/// - This oracle will not be required in public vm functions, as the vm will keep track of arguments
/// itself.
unconstrained pub fn pack_arguments_array<let N: u32>(args: [Field; N]) -> Field {
pack_arguments_array_oracle(args)
}

/// - Pack arguments (slice version) will notify the simulator that these arguments will be used later at
/// some point in the call.
/// some point in the call.
/// - When the external call is made later, the simulator will know what the values unpack to.
/// - This oracle will not be required in public vm functions, as the vm will keep track of arguments
/// - This oracle will not be required in public vm functions, as the vm will keep track of arguments
/// itself.
unconstrained pub fn pack_arguments(args: [Field]) -> Field {
pack_arguments_oracle(args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ unconstrained pub fn get_membership_witness<let N: u32, let M: u32>(

// Note: get_nullifier_membership_witness function is implemented in get_nullifier_membership_witness.nr

unconstrained pub fn get_note_hash_membership_witness<let N: u32, let M: u32>(
unconstrained pub fn get_note_hash_membership_witness(
block_number: u32,
leaf_value: Field
) -> MembershipWitness<NOTE_HASH_TREE_HEIGHT, NOTE_HASH_TREE_HEIGHT + 1> {
Expand Down
8 changes: 2 additions & 6 deletions noir-projects/aztec-nr/aztec/src/oracle/notes.nr
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ unconstrained pub fn notify_created_note<let N: u32>(
}

#[oracle(notifyNullifiedNote)]
unconstrained fn notify_nullified_note_oracle<let N: u32>(_nullifier: Field, _note_hash: Field, _counter: u32) -> Field {}
unconstrained fn notify_nullified_note_oracle(_nullifier: Field, _note_hash: Field, _counter: u32) -> Field {}

unconstrained pub fn notify_nullified_note<let N: u32>(
nullifier: Field,
note_hash: Field,
counter: u32
) -> Field {
unconstrained pub fn notify_nullified_note(nullifier: Field, note_hash: Field, counter: u32) -> Field {
notify_nullified_note_oracle(nullifier, note_hash, counter)
}

Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/oracle/returns.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[oracle(packReturns)]
unconstrained fn pack_returns_oracle<let N: u32>(_returns: [Field]) -> Field {}
unconstrained fn pack_returns_oracle(_returns: [Field]) -> Field {}

unconstrained pub fn pack_returns(returns: [Field]) {
let _unused = pack_returns_oracle(returns);
Expand Down
1 change: 1 addition & 0 deletions noir-projects/aztec-nr/aztec/src/state_vars/map.nr
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ impl<K, V, Context> Map<K, V, Context> {
}
// docs:end:at
}

Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ impl<T, let INITIAL_DELAY: u32, Context> SharedMutable<T, INITIAL_DELAY, Context
Self { context, storage_slot }
}

fn hash_scheduled_data(
value_change: ScheduledValueChange<T>,
delay_change: ScheduledDelayChange<INITIAL_DELAY>
) -> Field {
// TODO(#5491 and https://github.com/noir-lang/noir/issues/4784): update this so that we don't need to rely on
// ScheduledValueChange serializing to 3 and ScheduledDelayChange serializing to 1
let concatenated: [Field; 4] = concat_arrays(value_change.serialize(), delay_change.serialize());
poseidon2_hash(concatenated)
}

// Since we can't rely on the native storage allocation scheme, we hash the storage slot to get a unique location in
// which we can safely store as much data as we need.
// See https://github.com/AztecProtocol/aztec-packages/issues/5492 and
Expand Down Expand Up @@ -228,6 +218,16 @@ impl<T, let INITIAL_DELAY: u32> SharedMutable<T, INITIAL_DELAY, &mut PrivateCont
self.context.set_tx_max_block_number(block_horizon);
value_change.get_current_at(historical_block_number)
}

fn hash_scheduled_data(
value_change: ScheduledValueChange<T>,
delay_change: ScheduledDelayChange<INITIAL_DELAY>
) -> Field {
// TODO(#5491 and https://github.com/noir-lang/noir/issues/4784): update this so that we don't need to rely on
// ScheduledValueChange serializing to 3 and ScheduledDelayChange serializing to 1
let concatenated: [Field; 4] = concat_arrays(value_change.serialize(), delay_change.serialize());
poseidon2_hash(concatenated)
}
Comment on lines +222 to +230
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved this to just be implemented for the PrivateContext as this is the only situation which requires this behaviour afaik.

}

impl<T, let INITIAL_DELAY: u32> SharedMutable<T, INITIAL_DELAY, UnconstrainedContext> where T: ToField + FromField + Eq {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<Context> EasyPrivateUint<Context> {
}
}

impl<Context> EasyPrivateUint<&mut PrivateContext> {
impl EasyPrivateUint<&mut PrivateContext> {
// Very similar to `value_note::utils::increment`.
pub fn add(self, addend: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
let owner_keys = get_current_public_keys(self.context, owner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ contract Counter {
let initial_value: Field = 5;
env.impersonate(owner);

// Deploy contract and initialize
// Deploy contract and initialize
let initializer = Counter::interface().initialize(initial_value as u64, owner, outgoing_viewer);
let counter_contract = env.deploy_self("Counter").with_private_initializer(initializer);
let contract_address = counter_contract.to_address();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// docs:start:easy_private_token_contract
contract EasyPrivateToken {
use dep::aztec::prelude::{AztecAddress, NoteHeader, Map};
use dep::aztec::prelude::{AztecAddress, Map};
use dep::value_note::balance_utils;
use dep::easy_private_state::EasyPrivateUint;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

// A contract used along with `StaticChild` contract to test static calls.
contract StaticParent {
use dep::aztec::prelude::{AztecAddress, FunctionSelector, Deserialize};
use dep::aztec::prelude::{AztecAddress, FunctionSelector};
use dep::aztec::context::gas::GasOpts;
use dep::static_child_contract::StaticChild;

Expand Down
Loading