Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Sep 26, 2024
1 parent ffe70ec commit cbfca5d
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 110 deletions.
28 changes: 24 additions & 4 deletions noir-projects/aztec-nr/uint-note/src/uint_note.nr
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
use dep::aztec::{
prelude::{NullifiableNote, PrivateContext},
prelude::{NoteHeader, NullifiableNote, PrivateContext},
protocol_types::{constants::GENERATOR_INDEX__NOTE_NULLIFIER, hash::poseidon2_hash_with_separator},
note::utils::compute_note_hash_for_nullify, keys::getters::get_nsk_app, macros::notes::note
note::utils::compute_note_hash_for_nullify, oracle::unsafe_rand::unsafe_rand,
keys::getters::get_nsk_app, macros::notes::partial_note
};

#[note]
trait OwnedNote {
fn new(amount: U128, owner_npk_m_hash: Field) -> Self;
fn get_amount(self) -> U128;
}

// docs:start:TokenNote
#[partial_note(quote {amount})]
pub struct UintNote {
// The integer stored by the note
// The value of the note
value: U128,
// The nullifying public key hash is used with the nsk_app to ensure that the note can be privately spent.
npk_m_hash: Field,
// Randomness of the note to hide its contents
randomness: Field,
}
// docs:end:TokenNote

impl NullifiableNote for UintNote {
// docs:start:nullifier
fn compute_nullifier(self, context: &mut PrivateContext, note_hash_for_nullify: Field) -> Field {
let secret = context.request_nsk_app(self.npk_m_hash);
poseidon2_hash_with_separator(
Expand All @@ -25,6 +34,7 @@ impl NullifiableNote for UintNote {
GENERATOR_INDEX__NOTE_NULLIFIER as Field
)
}
// docs:end:nullifier

unconstrained fn compute_nullifier_without_context(self) -> Field {
let note_hash_for_nullify = compute_note_hash_for_nullify(self);
Expand All @@ -43,3 +53,13 @@ impl Eq for UintNote {
& (self.randomness == other.randomness)
}
}

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() }
}

fn get_amount(self) -> U128 {
self.value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ type = "contract"

[dependencies]
aztec = { path = "../../../aztec-nr/aztec" }
uint_note = { path = "../../../aztec-nr/uint-note" }
compressed_string = { path = "../../../aztec-nr/compressed-string" }
authwit = { path = "../../../aztec-nr/authwit" }
31 changes: 7 additions & 24 deletions noir-projects/noir-contracts/contracts/token_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ contract Token {
// Libs
use std::meta::derive;

use dep::uint_note::uint_note::{UintNote, UintNoteHidingPoint};
use dep::compressed_string::FieldCompressedString;

use dep::aztec::{
Expand All @@ -34,8 +35,7 @@ contract Token {
use dep::authwit::auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public, compute_authwit_nullifier};
// docs:end:import_authwit

use crate::types::{transparent_note::TransparentNote, token_note::TokenNote, balance_set::BalanceSet};

use crate::types::{transparent_note::TransparentNote, balance_set::BalanceSet};
// docs:end::imports

// In the first transfer iteration we are computing a lot of additional information (validating inputs, retrieving
Expand Down Expand Up @@ -65,7 +65,7 @@ contract Token {
minters: Map<AztecAddress, PublicMutable<bool, Context>, Context>,
// docs:end:storage_minters
// docs:start:storage_balances
balances: Map<AztecAddress, BalanceSet<TokenNote, Context>, Context>,
balances: Map<AztecAddress, BalanceSet<UintNote, Context>, Context>,
// docs:end:storage_balances
total_supply: PublicMutable<U128, Context>,
// docs:start:storage_pending_shields
Expand Down Expand Up @@ -488,12 +488,12 @@ contract Token {
encode_and_encrypt_note_with_keys_unconstrained(&mut context, user_keys.ovpk_m, user_keys.ivpk_m, user)
);
// 4. Now we get the note hiding points.
let mut fee_payer_point = TokenNote::hiding_point().new(
let mut fee_payer_point = UintNote::hiding_point().new(
fee_payer_npk_m_hash,
fee_payer_randomness,
storage.balances.at(fee_payer).set.storage_slot
);
let mut user_point = TokenNote::hiding_point().new(
let mut user_point = UintNote::hiding_point().new(
user_npk_m_hash,
user_randomness,
storage.balances.at(user).set.storage_slot
Expand All @@ -517,8 +517,8 @@ contract Token {
#[public]
#[internal]
fn complete_refund(fee_payer_point: Point, user_point: Point, funded_amount: Field) {
let mut fee_payer_hiding_point = TokenNote::hiding_point().from_point(fee_payer_point);
let mut user_hiding_point = TokenNote::hiding_point().from_point(user_point);
let mut fee_payer_hiding_point = UintNote::hiding_point().from_point(fee_payer_point);
let mut user_hiding_point = UintNote::hiding_point().from_point(user_point);
// TODO(#7728): Remove the next line
let funded_amount = U128::from_integer(funded_amount);
let tx_fee = U128::from_integer(context.transaction_fee());
Expand All @@ -531,28 +531,11 @@ contract Token {
let fee_payer_note_hash = fee_payer_hiding_point.finalize(tx_fee);
let user_note_hash = user_hiding_point.finalize(refund_amount);
// 5. At last we emit the note hashes.
context.push_note_hash(fee_payer_note_hash);
context.push_note_hash(user_note_hash);
// --> Once the tx is settled user and fee recipient can add the notes to their pixies.
}
// docs:end:complete_refund
/// Internal ///
// docs:start:increase_public_balance
#[public]
#[internal]
fn _increase_public_balance(to: AztecAddress, amount: Field) {
let new_balance = storage.public_balances.at(to).read().add(U128::from_integer(amount));
storage.public_balances.at(to).write(new_balance);
}
// docs:end:increase_public_balance
// docs:start:reduce_total_supply
#[public]
#[internal]
fn _reduce_total_supply(amount: Field) {
// Only to be called from burn.
let new_supply = storage.total_supply.read().sub(U128::from_integer(amount));
storage.total_supply.write(new_supply);
}
// docs:end:reduce_total_supply
/// Unconstrained ///
// docs:start:balance_of_private
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::{test::utils, Token, types::token_note::TokenNote};
use crate::{test::utils, Token};

use dep::aztec::{
prelude::NoteHeader, protocol_types::storage::map::derive_storage_slot_in_map,
keys::getters::get_public_keys
};
use dep::authwit::cheatcodes as authwit_cheatcodes;
use dep::uint_note::uint_note::UintNote;

#[test]
unconstrained fn setup_refund_success() {
Expand Down Expand Up @@ -44,22 +45,22 @@ unconstrained fn setup_refund_success() {
// worth `funded_amount - transaction_fee`. We "know" the transaction fee was 1 (it is hardcoded in
// `executePublicFunction` TXE oracle) but we need to notify TXE of the note (preimage).
env.add_note(
&mut TokenNote {
amount: U128::from_integer(funded_amount - 1),
npk_m_hash: user_npk_m_hash,
randomness: user_randomness,
header: NoteHeader::empty()
},
&mut UintNote {
value: U128::from_integer(funded_amount - 1),
npk_m_hash: user_npk_m_hash,
randomness: user_randomness,
header: NoteHeader::empty()
},
user_balances_slot,
token_contract_address
);
env.add_note(
&mut TokenNote {
amount: U128::from_integer(1),
npk_m_hash: fee_payer_npk_m_hash,
randomness: fee_payer_randomness,
header: NoteHeader::empty()
},
&mut UintNote {
value: U128::from_integer(1),
npk_m_hash: fee_payer_npk_m_hash,
randomness: fee_payer_randomness,
header: NoteHeader::empty()
},
fee_payer_balances_slot,
token_contract_address
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
mod transparent_note;
mod balance_set;
mod token_note;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use dep::aztec::{
note::{note_interface::NullifiableNote, note_getter::view_notes, note_emission::OuterNoteEmission},
keys::{getters::get_public_keys, public_keys::NpkM}
};
use crate::types::token_note::OwnedNote;
use dep::uint_note::uint_note::OwnedNote;

pub struct BalanceSet<T, Context> {
set: PrivateSet<T, Context>,
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions yarn-project/end-to-end/src/e2e_fees/private_refunds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('e2e_fees/private_refunds', () => {
t.aliceAddress,
token.address,
deriveStorageSlotInMap(TokenContract.storage.balances.slot, t.aliceAddress),
TokenContract.notes.TokenNote.id,
TokenContract.notes.IntNote.id,
txHash,
),
);
Expand All @@ -122,7 +122,7 @@ describe('e2e_fees/private_refunds', () => {
t.bobAddress,
token.address,
deriveStorageSlotInMap(TokenContract.storage.balances.slot, t.bobAddress),
TokenContract.notes.TokenNote.id,
TokenContract.notes.IntNote.id,
txHash,
),
);
Expand Down

0 comments on commit cbfca5d

Please sign in to comment.