Skip to content

Commit

Permalink
addressing feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Aug 28, 2024
1 parent 713ff7c commit c92bc4d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
3 changes: 3 additions & 0 deletions noir-projects/aztec-nr/uint-note/src/uint_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ impl NoteInterface<UINT_NOTE_LEN, UINT_NOTE_BYTES_LEN> for UintNote {
}

impl UintNote {
fn new(value: U128, owner_npk_m_hash: Field) -> Self {
Self { value, npk_m_hash: owner_npk_m_hash, randomness: unsafe_rand(), header: NoteHeader::empty() }
}
// TODO: Merge this func with `compute_note_hiding_point`. I (benesjan) didn't do it in the initial PR to not have
// to modify macros and all the related funcs in it.
fn to_note_hiding_point(self) -> UintNoteHidingPoint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@ use dep::aztec::{
note::{note_getter::view_notes, note_emission::{NoteEmission, OuterNoteEmission}},
keys::{getters::get_current_public_keys, public_keys::NpkM}
};
use dep::uint_note::uint_note::OwnedNote;
use dep::uint_note::uint_note::{UintNote, UINT_NOTE_LEN, UINT_NOTE_BYTES_LEN};

struct BalanceSet<T, Context> {
set: PrivateSet<T, Context>,
struct BalanceSet<Context> {
set: PrivateSet<UintNote, Context>,
}

impl<T, Context> BalanceSet<T, Context> {
impl<Context> BalanceSet<Context> {
pub fn new(context: Context, storage_slot: Field) -> Self {
assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1.");
Self { set: PrivateSet::new(context, storage_slot) }
}
}

impl<T> BalanceSet<T, UnconstrainedContext> {
unconstrained pub fn balance_of<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN>(self: Self) -> U128 where T: NoteInterface<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN> + OwnedNote {
impl BalanceSet <UnconstrainedContext> {
unconstrained pub fn balance_of<UINT_NOTE_LEN, UINT_NOTE_BYTE_LEN>(self: Self) -> U128 {
self.balance_of_with_offset(0)
}

unconstrained pub fn balance_of_with_offset<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN>(
unconstrained pub fn balance_of_with_offset<UINT_NOTE_LEN, UINT_NOTE_BYTE_LEN>(
self: Self,
offset: u32
) -> U128 where T: NoteInterface<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN> + OwnedNote {
) -> U128 {
let mut balance = U128::from_integer(0);
// docs:start:view_notes
let mut options = NoteViewerOptions::new();
let notes = self.set.view_notes(options.set_offset(offset));
// docs:end:view_notes
for i in 0..options.limit {
if i < notes.len() {
balance = balance + notes.get_unchecked(i).get_amount();
balance = balance + notes.get_unchecked(i).value;
}
}
if (notes.len() == options.limit) {
Expand All @@ -45,29 +45,29 @@ impl<T> BalanceSet<T, UnconstrainedContext> {
}
}

impl<T> BalanceSet<T, &mut PrivateContext> {
pub fn add<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN>(
impl BalanceSet<&mut PrivateContext> {
pub fn add<UINT_NOTE_LEN, UINT_NOTE_BYTE_LEN>(
self: Self,
owner_npk_m: NpkM,
addend: U128
) -> OuterNoteEmission<T> where T: NoteInterface<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN> + OwnedNote + Eq {
) -> OuterNoteEmission<UintNote> {
if addend == U128::from_integer(0) {
OuterNoteEmission::new(Option::none())
} else {
// We fetch the nullifier public key hash from the registry / from our PXE
let mut addend_note = T::new(addend, owner_npk_m.hash());
let mut addend_note = UintNote::new(addend, owner_npk_m.hash());

// docs:start:insert
OuterNoteEmission::new(Option::some(self.set.insert(&mut addend_note)))
// docs:end:insert
}
}

pub fn sub<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN>(
pub fn sub<UINT_NOTE_LEN, UINT_NOTE_BYTE_LEN>(
self: Self,
owner_npk_m: NpkM,
amount: U128
) -> OuterNoteEmission<T> where T: NoteInterface<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN> + OwnedNote + Eq {
) -> OuterNoteEmission<UintNote> {
let subtracted = self.try_sub(amount, MAX_NOTE_HASH_READ_REQUESTS_PER_CALL);

// try_sub may have substracted more or less than amount. We must ensure that we subtracted at least as much as
Expand All @@ -85,11 +85,11 @@ impl<T> BalanceSet<T, &mut PrivateContext> {
// The `max_notes` parameter is used to fine-tune the number of constraints created by this function. The gate count
// scales relatively linearly with `max_notes`, but a lower `max_notes` parameter increases the likelihood of
// `try_sub` subtracting an amount smaller than `target_amount`.
pub fn try_sub<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN>(
pub fn try_sub<UINT_NOTE_LEN, UINT_NOTE_BYTE_LEN>(
self: Self,
target_amount: U128,
max_notes: u32
) -> U128 where T: NoteInterface<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN> + OwnedNote + Eq {
) -> U128 {
// We are using a preprocessor here (filter applied in an unconstrained context) instead of a filter because
// we do not need to prove correct execution of the preprocessor.
// Because the `min_sum` notes is not constrained, users could choose to e.g. not call it. However, all this
Expand All @@ -102,7 +102,7 @@ impl<T> BalanceSet<T, &mut PrivateContext> {
for i in 0..options.limit {
if i < notes.len() {
let note = notes.get_unchecked(i);
subtracted = subtracted + note.get_amount();
subtracted = subtracted + note.value;
}
}

Expand All @@ -115,10 +115,10 @@ impl<T> BalanceSet<T, &mut PrivateContext> {
// The preprocessor (a filter applied in an unconstrained context) does not check if total sum is larger or equal to
// 'min_sum' - all it does is remove extra notes if it does reach that value.
// Note that proper usage of this preprocessor requires for notes to be sorted in descending order.
pub fn preprocess_notes_min_sum<T, T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN>(
notes: [Option<T>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
pub fn preprocess_notes_min_sum<UINT_NOTE_LEN, UINT_NOTE_BYTE_LEN>(
notes: [Option<UintNote>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL],
min_sum: U128
) -> [Option<T>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL] where T: NoteInterface<T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN> + OwnedNote {
) -> [Option<UintNote>; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL] {
let mut selected = [Option::none(); MAX_NOTE_HASH_READ_REQUESTS_PER_CALL];
let mut sum = U128::from_integer(0);
for i in 0..notes.len() {
Expand All @@ -129,7 +129,7 @@ pub fn preprocess_notes_min_sum<T, T_SERIALIZED_LEN, T_SERIALIZED_BYTES_LEN>(
if notes[i].is_some() & sum < min_sum {
let note = notes[i].unwrap_unchecked();
selected[i] = Option::some(note);
sum = sum.add(note.get_amount());
sum = sum.add(note.value);
}
}
selected
Expand Down

0 comments on commit c92bc4d

Please sign in to comment.