Skip to content

Commit

Permalink
chore: fix imports
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Feb 2, 2024
1 parent 6895f52 commit e6eb57d
Show file tree
Hide file tree
Showing 43 changed files with 185 additions and 254 deletions.
4 changes: 2 additions & 2 deletions boxes/token/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ contract Token {
) -> pub [Field; 4] {
let note_header = NoteHeader::new(contract_address, nonce, storage_slot);
if (storage_slot == storage.pending_shields.get_storage_slot()) {
note_utils::compute_note_hash_and_nullifier(TransparentNote::deserialize, note_header, serialized_note)
note_utils::compute_note_hash_and_nullifier(TransparentNote::deserialize_content, note_header, serialized_note)
} else {
note_utils::compute_note_hash_and_nullifier(TokenNote::deserialize, note_header, serialized_note)
note_utils::compute_note_hash_and_nullifier(TokenNote::deserialize_content, note_header, serialized_note)
}
}
// docs:end:compute_note_hash_and_nullifier
Expand Down
11 changes: 5 additions & 6 deletions boxes/token/src/contracts/src/types/balances_map.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use dep::aztec::{
protocol_types::{
address::AztecAddress,
constants::MAX_READ_REQUESTS_PER_CALL,
traits::{Serialize, Deserialize}
},
state_vars::{
set::Set,
Expand Down Expand Up @@ -37,11 +36,11 @@ impl<T> BalancesMap<T> {
}
}

unconstrained pub fn balance_of<T_SERIALIZED_LEN>(self: Self, owner: AztecAddress) -> SafeU120 where T: Deserialize<T_SERIALIZED_LEN> + Serialize<T_SERIALIZED_LEN> + NoteInterface + OwnedNote {
unconstrained pub fn balance_of<T_SERIALIZED_LEN>(self: Self, owner: AztecAddress) -> SafeU120 where T: NoteInterface<T_SERIALIZED_LEN> + OwnedNote {
self.balance_of_with_offset(owner, 0)
}

unconstrained pub fn balance_of_with_offset<T_SERIALIZED_LEN>(self: Self, owner: AztecAddress, offset: u32) -> SafeU120 where T: Deserialize<T_SERIALIZED_LEN> + Serialize<T_SERIALIZED_LEN> + NoteInterface + OwnedNote {
unconstrained pub fn balance_of_with_offset<T_SERIALIZED_LEN>(self: Self, owner: AztecAddress, offset: u32) -> SafeU120 where T: NoteInterface<T_SERIALIZED_LEN> + OwnedNote {
// Same as SafeU120::new(0), but fewer constraints because no check.
let mut balance = SafeU120::min();
// docs:start:view_notes
Expand All @@ -61,15 +60,15 @@ impl<T> BalancesMap<T> {
balance
}

pub fn add<T_SERIALIZED_LEN>(self: Self, owner: AztecAddress, addend: SafeU120) where T: Deserialize<T_SERIALIZED_LEN> + Serialize<T_SERIALIZED_LEN> + NoteInterface + OwnedNote {
pub fn add<T_SERIALIZED_LEN>(self: Self, owner: AztecAddress, addend: SafeU120) where T: NoteInterface<T_SERIALIZED_LEN> + OwnedNote {
let mut addend_note = T::new(addend, owner);

// docs:start:insert
self.map.at(owner).insert(&mut addend_note, true);
// docs:end:insert
}

pub fn sub<T_SERIALIZED_LEN>(self: Self, owner: AztecAddress, subtrahend: SafeU120) where T: Deserialize<T_SERIALIZED_LEN> + Serialize<T_SERIALIZED_LEN> + NoteInterface + OwnedNote{
pub fn sub<T_SERIALIZED_LEN>(self: Self, owner: AztecAddress, subtrahend: SafeU120) where T: NoteInterface<T_SERIALIZED_LEN> + OwnedNote{
// docs:start:get_notes
let options = NoteGetterOptions::with_filter(filter_notes_min_sum, subtrahend);
let maybe_notes = self.map.at(owner).get_notes(options);
Expand Down Expand Up @@ -105,7 +104,7 @@ impl<T> BalancesMap<T> {
pub fn filter_notes_min_sum<T, T_SERIALIZED_LEN>(
notes: [Option<T>; MAX_READ_REQUESTS_PER_CALL],
min_sum: SafeU120
) -> [Option<T>; MAX_READ_REQUESTS_PER_CALL] where T: Deserialize<T_SERIALIZED_LEN> + Serialize<T_SERIALIZED_LEN> + NoteInterface + OwnedNote {
) -> [Option<T>; MAX_READ_REQUESTS_PER_CALL] where T: NoteInterface<T_SERIALIZED_LEN> + OwnedNote {
let mut selected = [Option::none(); MAX_READ_REQUESTS_PER_CALL];
let mut sum = SafeU120::min();
for i in 0..notes.len() {
Expand Down
18 changes: 5 additions & 13 deletions boxes/token/src/contracts/src/types/token_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ use dep::aztec::{
state_vars::set::Set,
log::emit_encrypted_log,
hash::pedersen_hash,
protocol_types::traits::{
Serialize,
Deserialize
},
};
use dep::aztec::oracle::{
rand::rand,
Expand Down Expand Up @@ -49,27 +45,23 @@ struct TokenNote {
header: NoteHeader,
}

impl Serialize<TOKEN_NOTE_LEN> for TokenNote {
fn serialize(self) -> [Field; TOKEN_NOTE_LEN] {
impl NoteInterface>TOKEN_NOTE_LEN> for TokenNote {
fn serialize_content(self) -> [Field; TOKEN_NOTE_LEN] {
[self.amount.value as Field, self.owner.to_field(), self.randomness]
}
}

impl Deserialize<TOKEN_NOTE_LEN> for TokenNote {
fn deserialize(serialized_note: [Field; TOKEN_NOTE_LEN]) -> Self {
fn deserialize_content(serialized_note: [Field; TOKEN_NOTE_LEN]) -> Self {
Self {
amount: SafeU120::new(serialized_note[0]),
owner: AztecAddress::from_field(serialized_note[1]),
randomness: serialized_note[2],
header: NoteHeader::empty(),
}
}
}

impl NoteInterface for TokenNote {
fn compute_note_content_hash(self) -> Field {
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash(self.serialize(), 0)
pedersen_hash(self.serialize_content(), 0)
}

// docs:start:nullifier
Expand Down Expand Up @@ -114,7 +106,7 @@ impl NoteInterface for TokenNote {
(*context).this_address(),
slot,
encryption_pub_key,
self.serialize(),
self.serialize_content(),
);
}
}
Expand Down
28 changes: 12 additions & 16 deletions boxes/token/src/contracts/src/types/transparent_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use dep::aztec::{
},
hash::{compute_secret_hash, pedersen_hash},
context::PrivateContext,
protocol_types::traits::{Serialize, Deserialize, Empty}
};

global TRANSPARENT_NOTE_LEN: Field = 2;
Expand All @@ -23,34 +22,31 @@ struct TransparentNote {
header: NoteHeader,
}

impl Serialize<TRANSPARENT_NOTE_LEN> for TransparentNote {
fn serialize(self) -> [Field; TRANSPARENT_NOTE_LEN] {
[self.amount, self.secret_hash]

impl Empty for TransparentNote {
fn empty() -> Self {
TransparentNote::new(0, 0)
}
}

impl Deserialize<TRANSPARENT_NOTE_LEN> for TransparentNote {
fn deserialize(serialized_note: [Field; TRANSPARENT_NOTE_LEN]) -> Self {

impl NoteInterface<TRANSPARENT_NOTE_LEN> for TransparentNote {
fn serialize_content(self) -> [Field; TRANSPARENT_NOTE_LEN] {
[self.amount, self.secret_hash]
}

fn deserialize_content(serialized_note: [Field; TRANSPARENT_NOTE_LEN]) -> Self {
TransparentNote {
amount: serialized_note[0],
secret_hash: serialized_note[1],
secret: 0,
header: NoteHeader::empty(),
}
}
}

impl Empty for TransparentNote {
fn empty() -> Self {
TransparentNote::new(0, 0)
}
}

impl NoteInterface for TransparentNote {

fn compute_note_content_hash(self) -> Field {
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash(self.serialize(), 0)
pedersen_hash(self.serialize_content(), 0)
}

fn compute_nullifier(self, _context: &mut PrivateContext) -> Field {
Expand Down
16 changes: 6 additions & 10 deletions yarn-project/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dep::aztec::log::emit_encrypted_log;
use dep::aztec::{
protocol_types::{
address::AztecAddress,
traits::{Serialize, Deserialize, Empty}
traits::Empty
},
note::{
note_header::NoteHeader,
Expand All @@ -31,27 +31,23 @@ struct AddressNote {
header: NoteHeader,
}

impl Serialize<ADDRESS_NOTE_LEN> for AddressNote {
fn serialize(self) -> [Field; ADDRESS_NOTE_LEN]{
impl NoteInterface<ADDRESS_NOTE_LEN> for AddressNote {
fn serialize_content(self) -> [Field; ADDRESS_NOTE_LEN]{
[self.address.to_field(), self.owner.to_field(), self.randomness]
}
}

impl Deserialize<ADDRESS_NOTE_LEN> for AddressNote {
fn deserialize(serialized_note: [Field; ADDRESS_NOTE_LEN]) -> Self {
fn deserialize_content(serialized_note: [Field; ADDRESS_NOTE_LEN]) -> Self {
AddressNote {
address: AztecAddress::from_field(serialized_note[0]),
owner: AztecAddress::from_field(serialized_note[1]),
randomness: serialized_note[2],
header: NoteHeader::empty(),
}
}
}

impl NoteInterface for AddressNote {
fn compute_note_content_hash(self) -> Field {
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash(self.serialize(), 0)
pedersen_hash(self.serialize_content(), 0)
}

fn compute_nullifier(self, context: &mut PrivateContext) -> Field {
Expand Down Expand Up @@ -93,7 +89,7 @@ impl NoteInterface for AddressNote {
(*context).this_address(),
slot,
encryption_pub_key,
self.serialize(),
self.serialize_content(),
);
// docs:end:encrypted
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/aztec/src/history/note_inclusion.nr
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn prove_note_inclusion<Note, N>(
note_with_header: Note,
block_number: u32, // The block at which we'll prove that the note exists
context: PrivateContext
) where Note: NoteInterface {
) where Note: NoteInterface<N> {
let note_commitment = compute_note_hash_for_consumption(note_with_header);

prove_note_commitment_inclusion(note_commitment, block_number, context);
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/aztec/src/history/note_validity.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn prove_note_validity<Note, N>(
note_with_header: Note,
block_number: u32, // The block at which we'll prove that the note exists
context: &mut PrivateContext
) where Note: NoteInterface {
) where Note: NoteInterface<N> {
prove_note_inclusion(note_with_header, block_number, *context);
prove_note_not_nullified(note_with_header, block_number, context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn prove_note_not_nullified<Note, N>(
note_with_header: Note,
block_number: u32, // The block at which we'll prove that the note was not nullified
context: &mut PrivateContext
) where Note: NoteInterface {
) where Note: NoteInterface<N> {
let nullifier = compute_siloed_nullifier(note_with_header, context);

prove_nullifier_non_inclusion(nullifier, block_number, *context);
Expand Down
13 changes: 8 additions & 5 deletions yarn-project/aztec-nr/aztec/src/note/lifecycle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ use crate::note::{
utils::{compute_note_hash_for_insertion, compute_note_hash_for_consumption},
};
use crate::oracle::notes::{notify_created_note, notify_nullified_note};
use dep::protocol_types::traits::{Serialize, Deserialize};

pub fn create_note<Note, N>(
context: &mut PrivateContext,
storage_slot: Field,
note: &mut Note,
broadcast: bool
) where Note: NoteInterface + Serialize<N> + Deserialize<N> {
) where Note: NoteInterface<N> {
let contract_address = (*context).this_address();

let header = NoteHeader { contract_address, storage_slot, nonce: 0, is_transient: true };
Expand All @@ -26,7 +25,7 @@ pub fn create_note<Note, N>(
let inner_note_hash = compute_note_hash_for_insertion(*note);

// TODO: Strong typing required because of https://github.com/noir-lang/noir/issues/4088
let serialized_note: [Field; N] = Note::serialize(*note);
let serialized_note: [Field; N] = Note::serialize_content(*note);
assert(notify_created_note(storage_slot, serialized_note, inner_note_hash) == 0);

context.push_new_note_hash(inner_note_hash);
Expand All @@ -36,7 +35,11 @@ pub fn create_note<Note, N>(
}
}

pub fn create_note_hash_from_public<Note>(context: &mut PublicContext, storage_slot: Field, note: &mut Note) where Note: NoteInterface {
pub fn create_note_hash_from_public<Note, N>(
context: &mut PublicContext,
storage_slot: Field,
note: &mut Note
) where Note: NoteInterface<N> {
let contract_address = (*context).this_address();

let header = NoteHeader { contract_address, storage_slot, nonce: 0, is_transient: true };
Expand All @@ -47,7 +50,7 @@ pub fn create_note_hash_from_public<Note>(context: &mut PublicContext, storage_s
context.push_new_note_hash(inner_note_hash);
}

pub fn destroy_note<Note>(context: &mut PrivateContext, note: Note) where Note: NoteInterface {
pub fn destroy_note<Note, N>(context: &mut PrivateContext, note: Note) where Note: NoteInterface<N> {
let mut nullifier = 0;
let mut consumed_note_hash: Field = 0;
nullifier = note.compute_nullifier(context);
Expand Down
15 changes: 7 additions & 8 deletions yarn-project/aztec-nr/aztec/src/note/note_getter.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use dep::protocol_types::{
MAX_NOTES_PER_PAGE,
VIEW_NOTE_ORACLE_RETURN_LENGTH,
},
traits::{Deserialize, Serialize}
};
use crate::context::PrivateContext;
use crate::note::{
Expand All @@ -22,7 +21,7 @@ fn check_note_header<Note, N>(
context: PrivateContext,
storage_slot: Field,
note: Note
) where Note: NoteInterface {
) where Note: NoteInterface<N> {
let header = note.get_header();
let contract_address = context.this_address();
assert(header.contract_address.eq(contract_address));
Expand Down Expand Up @@ -73,7 +72,7 @@ fn check_notes_order<N>(
pub fn get_note<Note, N>(
context: &mut PrivateContext,
storage_slot: Field
) -> Note where Note: NoteInterface + Deserialize<N> {
) -> Note where Note: NoteInterface<N> {
let note = get_note_internal(storage_slot);

check_note_header(*context, storage_slot, note);
Expand All @@ -88,15 +87,15 @@ pub fn get_notes<Note, N, FILTER_ARGS>(
context: &mut PrivateContext,
storage_slot: Field,
options: NoteGetterOptions<Note, N, FILTER_ARGS>
) -> [Option<Note>; MAX_READ_REQUESTS_PER_CALL] where Note: NoteInterface + Deserialize<N> + Serialize<N> {
) -> [Option<Note>; MAX_READ_REQUESTS_PER_CALL] where Note: NoteInterface<N> {
let opt_notes = get_notes_internal(storage_slot, options);
let mut num_notes = 0;
let mut prev_fields = [0; N];
for i in 0..opt_notes.len() {
let opt_note = opt_notes[i];
if opt_note.is_some() {
let note = opt_note.unwrap_unchecked();
let fields = note.serialize();
let fields = note.serialize_content();
check_note_header(*context, storage_slot, note);
check_note_fields(fields, options.selects);
if i != 0 {
Expand All @@ -118,7 +117,7 @@ pub fn get_notes<Note, N, FILTER_ARGS>(
opt_notes
}

unconstrained fn get_note_internal<Note, N>(storage_slot: Field) -> Note where Note: NoteInterface + Deserialize<N> {
unconstrained fn get_note_internal<Note, N>(storage_slot: Field) -> Note where Note: NoteInterface<N> {
let placeholder_note = [Option::none()];
let placeholder_fields = [0; GET_NOTE_ORACLE_RETURN_LENGTH];
let placeholder_note_length = [0; N];
Expand All @@ -142,7 +141,7 @@ unconstrained fn get_note_internal<Note, N>(storage_slot: Field) -> Note where N
unconstrained fn get_notes_internal<Note, N, FILTER_ARGS>(
storage_slot: Field,
options: NoteGetterOptions<Note, N, FILTER_ARGS>
) -> [Option<Note>; MAX_READ_REQUESTS_PER_CALL] where Note: NoteInterface + Deserialize<N> {
) -> [Option<Note>; MAX_READ_REQUESTS_PER_CALL] where Note: NoteInterface<N> {
let (num_selects, select_by, select_values, select_comparators, sort_by, sort_order) = flatten_options(options.selects, options.sorts);
let placeholder_opt_notes = [Option::none(); MAX_READ_REQUESTS_PER_CALL];
let placeholder_fields = [0; GET_NOTES_ORACLE_RETURN_LENGTH];
Expand Down Expand Up @@ -171,7 +170,7 @@ unconstrained fn get_notes_internal<Note, N, FILTER_ARGS>(
unconstrained pub fn view_notes<Note, N>(
storage_slot: Field,
options: NoteViewerOptions<Note, N>
) -> [Option<Note>; MAX_NOTES_PER_PAGE] where Note: NoteInterface + Deserialize<N> {
) -> [Option<Note>; MAX_NOTES_PER_PAGE] where Note: NoteInterface<N> {
let (num_selects, select_by, select_values, select_comparators, sort_by, sort_order) = flatten_options(options.selects, options.sorts);
let placeholder_opt_notes = [Option::none(); MAX_NOTES_PER_PAGE];
let placeholder_fields = [0; VIEW_NOTE_ORACLE_RETURN_LENGTH];
Expand Down
5 changes: 2 additions & 3 deletions yarn-project/aztec-nr/aztec/src/note/note_getter_options.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use dep::std::option::Option;
use dep::protocol_types::{
constants::MAX_READ_REQUESTS_PER_CALL,
traits::Deserialize,
};
use crate::note::note_interface::NoteInterface;

Expand Down Expand Up @@ -92,7 +91,7 @@ struct NoteGetterOptions<Note, N, FILTER_ARGS> {
// And finally, a custom filter to refine the outcome further.
impl<Note, N, FILTER_ARGS> NoteGetterOptions<Note, N, 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, Field> where Note: NoteInterface + Deserialize<N> {
pub fn new() -> NoteGetterOptions<Note, N, Field> where Note: NoteInterface<N> {
NoteGetterOptions {
selects: BoundedVec::new(Option::none()),
sorts: BoundedVec::new(Option::none()),
Expand All @@ -109,7 +108,7 @@ impl<Note, N, FILTER_ARGS> NoteGetterOptions<Note, N, FILTER_ARGS> {
pub fn with_filter(
filter: fn ([Option<Note>; MAX_READ_REQUESTS_PER_CALL], FILTER_ARGS) -> [Option<Note>; MAX_READ_REQUESTS_PER_CALL],
filter_args: FILTER_ARGS,
) -> Self where Note: NoteInterface + Deserialize<N> {
) -> Self where Note: NoteInterface<N> {
NoteGetterOptions {
selects: BoundedVec::new(Option::none()),
sorts: BoundedVec::new(Option::none()),
Expand Down
Loading

0 comments on commit e6eb57d

Please sign in to comment.