Skip to content

Commit

Permalink
fix: address a bunch of issues with generics (#8625)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored and AztecBot committed Sep 19, 2024
1 parent 8af5eb1 commit 82283f5
Show file tree
Hide file tree
Showing 24 changed files with 65 additions and 62 deletions.
4 changes: 2 additions & 2 deletions address-note/src/address_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use dep::aztec::{
oracle::unsafe_rand::unsafe_rand, keys::getters::get_nsk_app, context::PrivateContext
};

global ADDRESS_NOTE_LEN: Field = 3;
global ADDRESS_NOTE_LEN: u32 = 3;
// ADDRESS_NOTE_LEN * 32 + 32(storage_slot as bytes) + 32(note_type_id as bytes)
global ADDRESS_NOTE_BYTES_LEN: Field = 3 * 32 + 64;
global ADDRESS_NOTE_BYTES_LEN: u32 = 3 * 32 + 64;

// docs:start:address_note_def
// docs:start:address_note_struct
Expand Down
4 changes: 2 additions & 2 deletions authwit/src/cheatcodes.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use dep::aztec::{

use crate::auth::{compute_inner_authwit_hash, compute_authwit_message_hash, set_authorized};

pub fn add_private_authwit_from_call_interface<C, M, T, P, Env>(
pub fn add_private_authwit_from_call_interface<C, let M: u32, T, P, Env>(
on_behalf_of: AztecAddress,
caller: AztecAddress,
call_interface: C
Expand All @@ -22,7 +22,7 @@ pub fn add_private_authwit_from_call_interface<C, M, T, P, Env>(
cheatcodes::add_authwit(on_behalf_of, message_hash);
}

pub fn add_public_authwit_from_call_interface<C, M, T, P, Env>(
pub fn add_public_authwit_from_call_interface<C, let M: u32, T, P, Env>(
on_behalf_of: AztecAddress,
caller: AztecAddress,
call_interface: C
Expand Down
6 changes: 3 additions & 3 deletions authwit/src/entrypoint/app.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use dep::aztec::protocol_types::{
use crate::entrypoint::function_call::{FunctionCall, FUNCTION_CALL_SIZE_IN_BYTES};

// FUNCTION_CALL_SIZE * ACCOUNT_MAX_CALLS + 1
global APP_PAYLOAD_SIZE: u64 = 21;
global APP_PAYLOAD_SIZE: u32 = 21;
// FUNCTION_CALL_SIZE_IN_BYTES * ACCOUNT_MAX_CALLS + 32
global APP_PAYLOAD_SIZE_IN_BYTES: u64 = 424;
global APP_PAYLOAD_SIZE_IN_BYTES: u32 = 424;

global ACCOUNT_MAX_CALLS: u64 = 4;
global ACCOUNT_MAX_CALLS: u32 = 4;

// Note: If you change the following struct you have to update default_entrypoint.ts
// docs:start:app-payload-struct
Expand Down
6 changes: 3 additions & 3 deletions authwit/src/entrypoint/fee.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use dep::aztec::protocol_types::{
use crate::entrypoint::function_call::FunctionCall;

// 2 * 5 (FUNCTION_CALL_SIZE) + 2
global FEE_PAYLOAD_SIZE: Field = 12;
global FEE_PAYLOAD_SIZE: u32 = 12;

// 2 * 98 (FUNCTION_CALL_SIZE_IN_BYTES) + 32
global FEE_PAYLOAD_SIZE_IN_BYTES: Field = 228;
global FEE_PAYLOAD_SIZE_IN_BYTES: u32 = 228;

global MAX_FEE_FUNCTION_CALLS = 2;
global MAX_FEE_FUNCTION_CALLS: u32 = 2;

// docs:start:fee-payload-struct
struct FeePayload {
Expand Down
4 changes: 2 additions & 2 deletions authwit/src/entrypoint/function_call.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use dep::aztec::protocol_types::{abis::function_selector::FunctionSelector, address::AztecAddress, traits::Serialize};

// 1 (ARGS_HASH) + 1 (FUNCTION_SELECTOR) + 1 (TARGET_ADDRESS) + 1 (IS_PUBLIC) + 1 (IS_STATIC)
global FUNCTION_CALL_SIZE: Field = 5;
global FUNCTION_CALL_SIZE: u32 = 5;
// 3 * 32 + 2
global FUNCTION_CALL_SIZE_IN_BYTES: Field = 98;
global FUNCTION_CALL_SIZE_IN_BYTES: u32 = 98;

struct FunctionCall {
args_hash: Field,
Expand Down
10 changes: 5 additions & 5 deletions aztec/src/encrypted_logs/incoming_body.nr
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ mod test {
header: NoteHeader,
}

global ADDRESS_NOTE_LEN: Field = 3;
global ADDRESS_NOTE_BYTES_LEN = 32 * 3 + 64;
global ADDRESS_NOTE_LEN: u32 = 3;
global ADDRESS_NOTE_BYTES_LEN: u32 = 32 * 3 + 64;

impl NoteInterface<ADDRESS_NOTE_LEN, ADDRESS_NOTE_BYTES_LEN> for AddressNote {
fn compute_note_hiding_point(_self: Self) -> Point {
Expand Down Expand Up @@ -186,9 +186,9 @@ mod test {
}
}

global TEST_EVENT_LEN: Field = 3;
global TEST_EVENT_BYTES_LEN = 32 * 3 + 64;
global TEST_EVENT_BYTES_LEN_WITHOUT_RANDOMNESS = 32 * 3 + 32;
global TEST_EVENT_LEN: u32 = 3;
global TEST_EVENT_BYTES_LEN: u32 = 32 * 3 + 64;
global TEST_EVENT_BYTES_LEN_WITHOUT_RANDOMNESS: u32 = 32 * 3 + 32;

impl EventInterface<TEST_EVENT_BYTES_LEN, TEST_EVENT_BYTES_LEN_WITHOUT_RANDOMNESS> for TestEvent {
fn get_event_type_id() -> EventSelector {
Expand Down
7 changes: 5 additions & 2 deletions aztec/src/history/note_inclusion.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ use crate::{
};

trait ProveNoteInclusion {
fn prove_note_inclusion<Note, N, M>(header: Header, note: Note) where Note: NoteInterface<N, M>;
fn prove_note_inclusion<Note, let N: u32, let M: u32>(header: Header, note: Note) where Note: NoteInterface<N, M>;
}

impl ProveNoteInclusion for Header {
fn prove_note_inclusion<Note, N, M>(self, note: Note) where Note: NoteInterface<N, M> {
fn prove_note_inclusion<Note, let N: u32, let M: u32>(
self,
note: Note
) where Note: NoteInterface<N, M> {
let note_hash = compute_note_hash_for_nullify(note);

let witness = unsafe {
Expand Down
4 changes: 2 additions & 2 deletions aztec/src/history/note_validity.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::{context::PrivateContext, note::note_interface::NoteInterface};
use dep::protocol_types::header::Header;

trait ProveNoteValidity {
fn prove_note_validity<Note, N, M>(header: Header, note: Note, context: &mut PrivateContext) where Note: NoteInterface<N, M>;
fn prove_note_validity<Note, let N: u32, let M: u32>(header: Header, note: Note, context: &mut PrivateContext) where Note: NoteInterface<N, M>;
}

impl ProveNoteValidity for Header {
fn prove_note_validity<Note, N, M>(
fn prove_note_validity<Note, let N: u32, let M: u32>(
self,
note: Note,
context: &mut PrivateContext
Expand Down
4 changes: 2 additions & 2 deletions aztec/src/history/nullifier_inclusion.nr
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ impl ProveNullifierInclusion for Header {
}

trait ProveNoteIsNullified {
fn prove_note_is_nullified<Note, N, M>(header: Header, note: Note, context: &mut PrivateContext) where Note: NoteInterface<N, M>;
fn prove_note_is_nullified<Note, let N: u32, let M: u32>(header: Header, note: Note, context: &mut PrivateContext) where Note: NoteInterface<N, M>;
}

impl ProveNoteIsNullified for Header {
// docs:start:prove_note_is_nullified
fn prove_note_is_nullified<Note, N, M>(
fn prove_note_is_nullified<Note, let N: u32, let M: u32>(
self,
note: Note,
context: &mut PrivateContext
Expand Down
4 changes: 2 additions & 2 deletions aztec/src/history/nullifier_non_inclusion.nr
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ impl ProveNullifierNonInclusion for Header {
}

trait ProveNoteNotNullified {
fn prove_note_not_nullified<Note, N, M>(header: Header, note: Note, context: &mut PrivateContext) where Note: NoteInterface<N, M>;
fn prove_note_not_nullified<Note, let N: u32, let M: u32>(header: Header, note: Note, context: &mut PrivateContext) where Note: NoteInterface<N, M>;
}

impl ProveNoteNotNullified for Header {
// docs:start:prove_note_not_nullified
fn prove_note_not_nullified<Note, N, M>(
fn prove_note_not_nullified<Note, let N: u32, let M: u32>(
self,
note: Note,
context: &mut PrivateContext
Expand Down
8 changes: 4 additions & 4 deletions aztec/src/note/lifecycle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::note::{
};
use crate::oracle::notes::{notify_created_note, notify_nullified_note};

pub fn create_note<Note, N, M>(
pub fn create_note<Note, let N: u32, let M: u32>(
context: &mut PrivateContext,
storage_slot: Field,
note: &mut Note
Expand Down Expand Up @@ -36,7 +36,7 @@ pub fn create_note<Note, N, M>(
NoteEmission::new(*note)
}

pub fn create_note_hash_from_public<Note, N, M>(
pub fn create_note_hash_from_public<Note, let N: u32, let M: u32>(
context: &mut PublicContext,
storage_slot: Field,
note: &mut Note
Expand All @@ -52,7 +52,7 @@ pub fn create_note_hash_from_public<Note, N, M>(
}

// Note: This function is currently totally unused.
pub fn destroy_note<Note, N, M>(
pub fn destroy_note<Note, let N: u32, let M: u32>(
context: &mut PrivateContext,
note: Note
) where Note: NoteInterface<N, M> {
Expand All @@ -61,7 +61,7 @@ pub fn destroy_note<Note, N, M>(
destroy_note_unsafe(context, note, note_hash_for_read_request)
}

pub fn destroy_note_unsafe<Note, N, M>(
pub fn destroy_note_unsafe<Note, let N: u32, let M: u32>(
context: &mut PrivateContext,
note: Note,
note_hash_for_read_request: Field
Expand Down
2 changes: 1 addition & 1 deletion aztec/src/note/note_header.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use dep::protocol_types::{address::AztecAddress, traits::{Empty, Serialize}};

global NOTE_HEADER_LENGTH: Field = 4;
global NOTE_HEADER_LENGTH: u32 = 4;

struct NoteHeader {
contract_address: AztecAddress,
Expand Down
6 changes: 3 additions & 3 deletions aztec/src/state_vars/private_immutable.nr
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<Note, Context> PrivateImmutable<Note, Context> {

impl<Note> PrivateImmutable<Note, &mut PrivateContext> {
// docs:start:initialize
pub fn initialize<N, M>(
pub fn initialize<let N: u32, let M: u32>(
self,
note: &mut Note
) -> NoteEmission<Note> where Note: NoteInterface<N, M> {
Expand All @@ -54,7 +54,7 @@ impl<Note> PrivateImmutable<Note, &mut PrivateContext> {
// docs:end:initialize

// docs:start:get_note
pub fn get_note<N, M>(self) -> Note where Note: NoteInterface<N, M> {
pub fn get_note<let N: u32, let M: u32>(self) -> Note where Note: NoteInterface<N, M> {
let storage_slot = self.storage_slot;
get_note(self.context, storage_slot).0
}
Expand All @@ -71,7 +71,7 @@ impl<Note> PrivateImmutable<Note, UnconstrainedContext> {

// view_note does not actually use the context, but it calls oracles that are only available in private
// docs:start:view_note
unconstrained pub fn view_note<N, M>(self) -> Note where Note: NoteInterface<N, M> {
unconstrained pub fn view_note<let N: u32, let M: u32>(self) -> Note where Note: NoteInterface<N, M> {
let mut options = NoteViewerOptions::new();
view_notes(self.storage_slot, options.set_limit(1)).get(0)
}
Expand Down
4 changes: 2 additions & 2 deletions aztec/src/state_vars/private_mutable.nr
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<Note, Context> PrivateMutable<Note, Context> {
}
}

impl<Note, N, M> PrivateMutable<Note, &mut PrivateContext> where Note: NoteInterface<N, M> {
impl<Note, let N: u32, let M: u32> PrivateMutable<Note, &mut PrivateContext> where Note: NoteInterface<N, M> {
// docs:start:initialize
pub fn initialize(self, note: &mut Note) -> NoteEmission<Note> {
// Nullify the storage slot.
Expand Down Expand Up @@ -102,7 +102,7 @@ impl<Note, N, M> PrivateMutable<Note, &mut PrivateContext> where Note: NoteInter
// docs:end:get_note
}

impl<Note, N, M> PrivateMutable<Note, UnconstrainedContext> where Note: NoteInterface<N, M> {
impl<Note, let N: u32, let M: u32> PrivateMutable<Note, UnconstrainedContext> where Note: NoteInterface<N, M> {
unconstrained pub fn is_initialized(self) -> bool {
let nullifier = self.compute_initialization_nullifier();
check_nullifier_exists(nullifier)
Expand Down
4 changes: 2 additions & 2 deletions aztec/src/state_vars/public_immutable.nr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<T, Context> PublicImmutable<T, Context> {
// docs:end:public_immutable_struct_new
}

impl <T, T_SERIALIZED_LEN> PublicImmutable<T, &mut PublicContext> where T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN> {
impl<T, let T_SERIALIZED_LEN: u32> PublicImmutable<T, &mut PublicContext> where T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN> {
// docs:start:public_immutable_struct_write
pub fn initialize(self, value: T) {
// We check that the struct is not yet initialized by checking if the initialization slot is 0
Expand All @@ -46,7 +46,7 @@ impl <T, T_SERIALIZED_LEN> PublicImmutable<T, &mut PublicContext> where T: Seria
// docs:end:public_immutable_struct_read
}

impl<T, T_SERIALIZED_LEN> PublicImmutable<T, UnconstrainedContext>where T: Deserialize<T_SERIALIZED_LEN> {
impl<T, let T_SERIALIZED_LEN: u32> PublicImmutable<T, UnconstrainedContext> where T: Deserialize<T_SERIALIZED_LEN> {
unconstrained pub fn read(self) -> T {
self.context.storage_read(self.storage_slot)
}
Expand Down
4 changes: 2 additions & 2 deletions aztec/src/state_vars/public_mutable.nr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl<T, Context> PublicMutable<T, Context> {
// docs:end:public_mutable_struct_new
}

impl<T, T_SERIALIZED_LEN> PublicMutable<T, &mut PublicContext> where T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN> {
impl<T, let T_SERIALIZED_LEN: u32> PublicMutable<T, &mut PublicContext> where T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN> {
// docs:start:public_mutable_struct_read
pub fn read(self) -> T {
self.context.storage_read(self.storage_slot)
Expand All @@ -38,7 +38,7 @@ impl<T, T_SERIALIZED_LEN> PublicMutable<T, &mut PublicContext> where T: Serializ
// docs:end:public_mutable_struct_write
}

impl<T, T_SERIALIZED_LEN> PublicMutable<T, UnconstrainedContext> where T: Deserialize<T_SERIALIZED_LEN> {
impl<T, let T_SERIALIZED_LEN: u32> PublicMutable<T, UnconstrainedContext> where T: Deserialize<T_SERIALIZED_LEN> {
unconstrained pub fn read(self) -> T {
self.context.storage_read(self.storage_slot)
}
Expand Down
6 changes: 3 additions & 3 deletions aztec/src/state_vars/shared_immutable.nr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<T, Context> SharedImmutable<T, Context> {
}
}

impl<T, T_SERIALIZED_LEN> SharedImmutable<T, &mut PublicContext> where T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN> {
impl<T, let T_SERIALIZED_LEN: u32> SharedImmutable<T, &mut PublicContext> where T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN> {
// Intended to be only called once.
pub fn initialize(self, value: T) {
// We check that the struct is not yet initialized by checking if the initialization slot is 0
Expand All @@ -38,13 +38,13 @@ impl<T, T_SERIALIZED_LEN> SharedImmutable<T, &mut PublicContext> where T: Serial
}
}

impl<T, T_SERIALIZED_LEN> SharedImmutable<T, UnconstrainedContext> where T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN> {
impl<T, let T_SERIALIZED_LEN: u32> SharedImmutable<T, UnconstrainedContext> where T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN> {
unconstrained pub fn read_public(self) -> T {
self.context.storage_read(self.storage_slot)
}
}

impl<T, T_SERIALIZED_LEN> SharedImmutable<T, &mut PrivateContext> where T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN> {
impl<T, let T_SERIALIZED_LEN: u32> SharedImmutable<T, &mut PrivateContext> where T: Serialize<T_SERIALIZED_LEN> + Deserialize<T_SERIALIZED_LEN> {
pub fn read_private(self) -> T {
let header = self.context.get_header();
let mut fields = [0; T_SERIALIZED_LEN];
Expand Down
6 changes: 3 additions & 3 deletions aztec/src/state_vars/shared_mutable/scheduled_delay_change.nr
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl<let INITIAL_DELAY: u32> ScheduledDelayChange<INITIAL_DELAY> {
}
}

impl<INITIAL_DELAY> Serialize<1> for ScheduledDelayChange<INITIAL_DELAY> {
impl<let INITIAL_DELAY: u32> Serialize<1> for ScheduledDelayChange<INITIAL_DELAY> {
fn serialize(self) -> [Field; 1] {
// We pack all three u32 values into a single U128, which is made up of two u64 limbs.
// Low limb: [ pre_inner: u32 | post_inner: u32 ]
Expand All @@ -145,7 +145,7 @@ impl<INITIAL_DELAY> Serialize<1> for ScheduledDelayChange<INITIAL_DELAY> {
}
}

impl<INITIAL_DELAY> Deserialize<1> for ScheduledDelayChange<INITIAL_DELAY> {
impl<let INITIAL_DELAY: u32> Deserialize<1> for ScheduledDelayChange<INITIAL_DELAY> {
fn deserialize(input: [Field; 1]) -> Self {
let packed = U128::from_integer(input[0]);

Expand Down Expand Up @@ -175,7 +175,7 @@ impl<INITIAL_DELAY> Deserialize<1> for ScheduledDelayChange<INITIAL_DELAY> {
}
}

impl<INITIAL_DELAY> Eq for ScheduledDelayChange<INITIAL_DELAY> {
impl<let INITIAL_DELAY: u32> Eq for ScheduledDelayChange<INITIAL_DELAY> {
fn eq(self, other: Self) -> bool {
(self.pre == other.pre)
& (self.post == other.post)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::state_vars::shared_mutable::scheduled_delay_change::ScheduledDelayChange;

global TEST_INITIAL_DELAY = 13;
global TEST_INITIAL_DELAY: u32 = 13;

fn assert_equal_after_conversion(original: ScheduledDelayChange<TEST_INITIAL_DELAY>) {
// We have to do explicit type annotations because Noir lacks turbofish support.
Expand Down Expand Up @@ -190,15 +190,15 @@ fn test_schedule_change_to_longer_delay_from_initial() {
let mut delay_change = get_initial_delay_change();
delay_change.schedule_change(new, current_block_number);

// Like in the after change scenario, change is effective immediately because the new delay is longer than the
// Like in the after change scenario, change is effective immediately because the new delay is longer than the
// current one.
assert_eq(delay_change.pre.unwrap(), TEST_INITIAL_DELAY);
assert_eq(delay_change.post.unwrap(), new);
assert_eq(delay_change.block_of_change, current_block_number);
assert_eq(delay_change.get_current(current_block_number), new);
}

fn assert_effective_minimum_delay_invariants<INITIAL_DELAY>(
fn assert_effective_minimum_delay_invariants<let INITIAL_DELAY: u32>(
delay_change: &mut ScheduledDelayChange<INITIAL_DELAY>,
historical_block_number: u32,
effective_minimum_delay: u32
Expand All @@ -222,8 +222,8 @@ fn assert_effective_minimum_delay_invariants<INITIAL_DELAY>(
let value_change_block = change_schedule_block + delay_change.get_current(change_schedule_block);
assert(expected_earliest_value_change_block <= value_change_block);

// Finally, a delay reduction could be scheduled immediately after the historical block. We reduce the delay to
// zero, which means that at the delay block of change there'll be no delay and a value change could be
// Finally, a delay reduction could be scheduled immediately after the historical block. We reduce the delay to
// zero, which means that at the delay block of change there'll be no delay and a value change could be
// performed immediately then.
delay_change.schedule_change(0, historical_block_number + 1);
assert(expected_earliest_value_change_block <= delay_change.block_of_change);
Expand Down Expand Up @@ -326,7 +326,7 @@ fn test_get_effective_delay_at_initial() {

let historical_block_number = 200;

// Like in the after change scenario, no delay change is scheduled, so the effective delay is simply the current
// Like in the after change scenario, no delay change is scheduled, so the effective delay is simply the current
// one (initial).
let effective_minimum_delay = delay_change.get_effective_minimum_delay_at(historical_block_number);
assert_eq(effective_minimum_delay, TEST_INITIAL_DELAY);
Expand Down
Loading

0 comments on commit 82283f5

Please sign in to comment.