diff --git a/docs/docs/aztec/concepts/storage/partial_notes.md b/docs/docs/aztec/concepts/storage/partial_notes.md index 2ea605e50df4..f5fe8b156478 100644 --- a/docs/docs/aztec/concepts/storage/partial_notes.md +++ b/docs/docs/aztec/concepts/storage/partial_notes.md @@ -122,7 +122,7 @@ Then we just emit `P_a.x` and `P_b.x` as a note hashes, and we're done! This is implemented in the example token contract: -#include_code compute_note_hiding_point noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr rust +#include_code compute_note_hiding_point noir-projects/aztec-nr/uint-note/src/uint_note.nr rust Those `G_x` are generators that generated [here](https://github.com/AztecProtocol/aztec-packages/blob/#include_aztec_version/noir-projects/noir-projects/aztec-nr/aztec/src/generators.nr). Anyone can use them for separating different fields in a "partial note". diff --git a/noir-projects/aztec-nr/uint-note/src/uint_note.nr b/noir-projects/aztec-nr/uint-note/src/uint_note.nr index f8a20947e74f..d27f658b5992 100644 --- a/noir-projects/aztec-nr/uint-note/src/uint_note.nr +++ b/noir-projects/aztec-nr/uint-note/src/uint_note.nr @@ -60,6 +60,9 @@ impl NoteInterface 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 { diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr index 4e79fc684fc6..d6754e0afe0f 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr @@ -62,7 +62,7 @@ contract Token { minters: Map>, // docs:end:storage_minters // docs:start:storage_balances - balances: Map>, + balances: Map, // docs:end:storage_balances total_supply: PublicMutable, // docs:start:storage_pending_shields diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/balance_set.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/balance_set.nr index 5b30c1a77d9f..0330d8773c8a 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/balance_set.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/balance_set.nr @@ -1,32 +1,31 @@ -use dep::aztec::prelude::{NoteGetterOptions, NoteViewerOptions, NoteInterface, PrivateSet, Point}; +use dep::aztec::prelude::{NoteGetterOptions, NoteViewerOptions, PrivateSet}; use dep::aztec::{ context::{PrivateContext, UnconstrainedContext}, protocol_types::constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, - note::{note_getter::view_notes, note_emission::{NoteEmission, OuterNoteEmission}}, - keys::{getters::get_current_public_keys, public_keys::NpkM} + note::note_emission::OuterNoteEmission, keys::public_keys::NpkM }; -use dep::uint_note::uint_note::OwnedNote; +use dep::uint_note::uint_note::{UintNote}; -struct BalanceSet { - set: PrivateSet, +struct BalanceSet { + set: PrivateSet, } -impl BalanceSet { +impl BalanceSet { 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 BalanceSet { - unconstrained pub fn balance_of(self: Self) -> U128 where T: NoteInterface + OwnedNote { +impl BalanceSet { + unconstrained pub fn balance_of(self: Self) -> U128 { self.balance_of_with_offset(0) } - unconstrained pub fn balance_of_with_offset( + unconstrained pub fn balance_of_with_offset( self: Self, offset: u32 - ) -> U128 where T: NoteInterface + OwnedNote { + ) -> U128 { let mut balance = U128::from_integer(0); // docs:start:view_notes let mut options = NoteViewerOptions::new(); @@ -34,7 +33,7 @@ impl BalanceSet { // 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) { @@ -45,17 +44,17 @@ impl BalanceSet { } } -impl BalanceSet { - pub fn add( +impl BalanceSet<&mut PrivateContext> { + pub fn add( self: Self, owner_npk_m: NpkM, addend: U128 - ) -> OuterNoteEmission where T: NoteInterface + OwnedNote + Eq { + ) -> OuterNoteEmission { 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))) @@ -63,11 +62,11 @@ impl BalanceSet { } } - pub fn sub( + pub fn sub( self: Self, owner_npk_m: NpkM, amount: U128 - ) -> OuterNoteEmission where T: NoteInterface + OwnedNote + Eq { + ) -> OuterNoteEmission { 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 @@ -85,11 +84,11 @@ impl BalanceSet { // 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( + pub fn try_sub( self: Self, target_amount: U128, max_notes: u32 - ) -> U128 where T: NoteInterface + 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 @@ -102,7 +101,7 @@ impl BalanceSet { 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; } } @@ -115,10 +114,10 @@ impl BalanceSet { // 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( - notes: [Option; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL], +pub fn preprocess_notes_min_sum( + notes: [Option; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL], min_sum: U128 -) -> [Option; MAX_NOTE_HASH_READ_REQUESTS_PER_CALL] where T: NoteInterface + OwnedNote { +) -> [Option; 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() { @@ -129,7 +128,7 @@ pub fn preprocess_notes_min_sum( 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 diff --git a/yarn-project/end-to-end/src/e2e_fees/private_refunds.test.ts b/yarn-project/end-to-end/src/e2e_fees/private_refunds.test.ts index 0691c48c1869..7e85851e36e3 100644 --- a/yarn-project/end-to-end/src/e2e_fees/private_refunds.test.ts +++ b/yarn-project/end-to-end/src/e2e_fees/private_refunds.test.ts @@ -100,7 +100,7 @@ describe('e2e_fees/private_refunds', () => { t.aliceAddress, token.address, deriveStorageSlotInMap(TokenContract.storage.balances.slot, t.aliceAddress), - TokenContract.notes.IntNote.id, + TokenContract.notes.UintNote.id, txHash, ), ); @@ -120,7 +120,7 @@ describe('e2e_fees/private_refunds', () => { t.bobAddress, token.address, deriveStorageSlotInMap(TokenContract.storage.balances.slot, t.bobAddress), - TokenContract.notes.IntNote.id, + TokenContract.notes.UintNote.id, txHash, ), );