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

fix: quick fix of #6405 by removing context from value note utils #6509

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 7 additions & 10 deletions noir-projects/aztec-nr/value-note/src/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ pub fn create_note_getter_options_for_decreasing_balance(amount: Field) -> NoteG
// Creates a new note for the recipient.
// Inserts it to the recipient's set of notes.
pub fn increment(
mut context: PrivateContext,
balance: PrivateSet<ValueNote>,
amount: Field,
recipient: AztecAddress
) {
let recipient_npk_m_hash = get_npk_m_hash(&mut context, recipient);
let recipient_ivpk_m = get_ivpk_m(&mut context, recipient);
let context = balance.context.private.unwrap();
let recipient_npk_m_hash = get_npk_m_hash(context, recipient);
let recipient_ivpk_m = get_ivpk_m(context, recipient);

let mut note = ValueNote::new(amount, recipient_npk_m_hash);
// Insert the new note to the owner's set of notes and emit the log if value is non-zero.
Expand All @@ -31,12 +31,11 @@ pub fn increment(
// If the value of the removed notes exceeds the requested `amount`, create a new note containing the excess value, so that exactly `amount` is removed.
// Fail if the sum of the selected notes is less than the amount.
pub fn decrement(
mut context: PrivateContext,
balance: PrivateSet<ValueNote>,
amount: Field,
owner: AztecAddress
) {
let sum = decrement_by_at_most(context, balance, amount, owner);
let sum = decrement_by_at_most(balance, amount, owner);
assert(sum == amount, "Balance too low");
}

Expand All @@ -49,7 +48,6 @@ pub fn decrement(
//
// It returns the decremented amount, which should be less than or equal to max_amount.
pub fn decrement_by_at_most(
mut context: PrivateContext,
balance: PrivateSet<ValueNote>,
max_amount: Field,
owner: AztecAddress
Expand All @@ -60,7 +58,7 @@ pub fn decrement_by_at_most(
let mut decremented = 0;
for i in 0..opt_notes.len() {
if opt_notes[i].is_some() {
decremented += destroy_note(context, balance, owner, opt_notes[i].unwrap_unchecked());
decremented += destroy_note(balance, owner, opt_notes[i].unwrap_unchecked());
}
}

Expand All @@ -70,22 +68,21 @@ pub fn decrement_by_at_most(
change_value = decremented - max_amount;
decremented -= change_value;
}
increment(context, balance, change_value, owner);
increment(balance, change_value, owner);

decremented
}

// Removes the note from the owner's set of notes.
// Returns the value of the destroyed note.
pub fn destroy_note(
mut context: PrivateContext,
balance: PrivateSet<ValueNote>,
owner: AztecAddress,
note: ValueNote
) -> Field {
// Ensure the note is actually owned by the owner (to prevent user from generating a valid proof while
// spending someone else's notes).
let owner_npk_m_hash = get_npk_m_hash(&mut context, owner);
let owner_npk_m_hash = get_npk_m_hash(balance.context.private.unwrap(), owner);

// TODO (#6312): This will break with key rotation. Fix this. Will not be able to pass this after rotating keys.
assert(note.npk_m_hash.eq(owner_npk_m_hash));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract Benchmarking {
// Creates a new value note for the target owner. Use this method to seed an initial set of notes.
#[aztec(private)]
fn create_note(owner: AztecAddress, value: Field) {
increment(context, storage.notes.at(owner), value, owner);
increment(storage.notes.at(owner), value, owner);
}

// Deletes a note at a specific index in the set and creates a new one with the same value.
Expand All @@ -33,7 +33,7 @@ contract Benchmarking {
let notes = owner_notes.get_notes(getter_options.set_limit(1).set_offset(index));
let note = notes[0].unwrap_unchecked();
owner_notes.remove(note);
increment(context, owner_notes, note.value, owner);
increment(owner_notes, note.value, owner);
}

// Reads and writes to public storage and enqueues a call to another public function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ contract StatefulTest {
fn create_note(owner: AztecAddress, value: Field) {
if (value != 0) {
let loc = storage.notes.at(owner);
increment(context, loc, value, owner);
increment(loc, value, owner);
}
}

Expand All @@ -47,7 +47,7 @@ contract StatefulTest {
if (value != 0) {
let loc = storage.notes.at(owner);
// TODO (#6312): This will break with key rotation. Fix this. Will not be able to spend / increment any notes after rotating key.
increment(context, loc, value, owner);
increment(loc, value, owner);
}
}

Expand All @@ -57,10 +57,10 @@ contract StatefulTest {
let sender = context.msg_sender();

let sender_notes = storage.notes.at(sender);
decrement(context, sender_notes, amount, sender);
decrement(sender_notes, amount, sender);

let recipient_notes = storage.notes.at(recipient);
increment(context, recipient_notes, amount, recipient);
increment(recipient_notes, amount, recipient);
}

#[aztec(private)]
Expand All @@ -69,10 +69,10 @@ contract StatefulTest {
let sender = context.msg_sender();

let sender_notes = storage.notes.at(sender);
decrement(context, sender_notes, amount, sender);
decrement(sender_notes, amount, sender);

let recipient_notes = storage.notes.at(recipient);
increment(context, recipient_notes, amount, recipient);
increment(recipient_notes, amount, recipient);
}

#[aztec(public)]
Expand Down
Loading