From ed35a2b3f882ae4192de3b08def934803c24a1cc Mon Sep 17 00:00:00 2001 From: sklppy88 Date: Thu, 22 Aug 2024 12:57:08 +0000 Subject: [PATCH] init --- .../contracts/token_contract/Nargo.toml | 1 + .../contracts/token_contract/src/main.nr | 20 ++- .../token_contract/src/test/refunds.nr | 7 +- .../token_contract/src/test/utils.nr | 2 +- .../contracts/token_contract/src/types.nr | 1 - .../token_contract/src/types/balance_set.nr | 2 +- .../token_contract/src/types/token_note.nr | 132 ------------------ .../src/e2e_fees/private_refunds.test.ts | 4 +- 8 files changed, 18 insertions(+), 151 deletions(-) delete mode 100644 noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr diff --git a/noir-projects/noir-contracts/contracts/token_contract/Nargo.toml b/noir-projects/noir-contracts/contracts/token_contract/Nargo.toml index 6814c6d8df14..f9618a81eaf7 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/Nargo.toml +++ b/noir-projects/noir-contracts/contracts/token_contract/Nargo.toml @@ -6,5 +6,6 @@ type = "contract" [dependencies] aztec = { path = "../../../aztec-nr/aztec" } +int_note = { path = "../../../aztec-nr/int-note" } compressed_string = { path = "../../../aztec-nr/compressed-string" } authwit = { path = "../../../aztec-nr/authwit" } 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 f958be19b189..1999e156289a 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr @@ -13,6 +13,7 @@ mod test; contract Token { // Libs + use dep::int_note::int_note::{IntNote, IntNoteHidingPoint}; use dep::compressed_string::FieldCompressedString; use dep::aztec::{ @@ -32,10 +33,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, TOKEN_NOTE_LEN, TokenNoteHidingPoint}, 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 @@ -64,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 @@ -536,7 +534,7 @@ contract Token { // 4. We create the partial notes for the fee payer and the user. // --> Called "partial" because they don't have the amount set yet (that will be done in `complete_refund(...)`). - let fee_payer_partial_note = TokenNote { + let fee_payer_partial_note = IntNote { header: NoteHeader { contract_address: AztecAddress::zero(), nonce: 0, @@ -547,7 +545,7 @@ contract Token { npk_m_hash: fee_payer_npk_m_hash, randomness: fee_payer_randomness }; - let user_partial_note = TokenNote { + let user_partial_note = IntNote { header: NoteHeader { contract_address: AztecAddress::zero(), nonce: 0, @@ -580,10 +578,10 @@ contract Token { #[aztec(internal)] fn complete_refund( // TODO(#7771): the following makes macros crash --> try getting it work once we migrate to metaprogramming - // mut fee_payer_point: TokenNoteHidingPoint, - // mut user_point: TokenNoteHidingPoint, - fee_payer_point_immutable: TokenNoteHidingPoint, - user_point_immutable: TokenNoteHidingPoint, + // mut fee_payer_point: IntNoteHidingPoint, + // mut user_point: IntNoteHidingPoint, + fee_payer_point_immutable: IntNoteHidingPoint, + user_point_immutable: IntNoteHidingPoint, funded_amount: Field ) { // TODO(#7771): nuke the following 2 lines once we have mutable args diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/refunds.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/refunds.nr index 8bd9b5763b81..f07a90b9b68a 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/refunds.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/refunds.nr @@ -1,4 +1,4 @@ -use crate::{test::utils, Token, types::token_note::TokenNote}; +use crate::{test::utils, Token}; use dep::aztec::{ test::helpers::cheatcodes, oracle::unsafe_rand::unsafe_rand, hash::compute_secret_hash, @@ -6,6 +6,7 @@ use dep::aztec::{ keys::getters::get_current_public_keys }; use dep::authwit::cheatcodes as authwit_cheatcodes; +use dep::int_note::int_note::IntNote; #[test] unconstrained fn setup_refund_success() { @@ -45,7 +46,7 @@ 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.store_note_in_cache( - &mut TokenNote { + &mut IntNote { amount: U128::from_integer(funded_amount - 1), npk_m_hash: user_npk_m_hash, randomness: user_randomness, @@ -55,7 +56,7 @@ unconstrained fn setup_refund_success() { token_contract_address ); env.store_note_in_cache( - &mut TokenNote { + &mut IntNote { amount: U128::from_integer(1), npk_m_hash: fee_payer_npk_m_hash, randomness: fee_payer_randomness, diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr index abbac9623032..397761962d68 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr @@ -6,7 +6,7 @@ use dep::aztec::{ oracle::{execution::{get_block_number, get_contract_address}, unsafe_rand::unsafe_rand, storage::storage_read} }; -use crate::{types::{token_note::TokenNote, transparent_note::TransparentNote}, Token}; +use crate::{types::{transparent_note::TransparentNote}, Token}; pub fn setup(with_account_contracts: bool) -> (&mut TestEnvironment, AztecAddress, AztecAddress, AztecAddress) { // Setup env, generate keys diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types.nr index 0c4b216e1891..59c9dc6f1c36 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types.nr @@ -1,3 +1,2 @@ mod transparent_note; mod balance_set; -mod token_note; 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 25d0407c6bb8..11d83d82d772 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 @@ -5,7 +5,7 @@ use dep::aztec::{ note::{note_getter::view_notes, note_emission::{NoteEmission, OuterNoteEmission}}, keys::{getters::get_current_public_keys, public_keys::NpkM} }; -use crate::types::token_note::OwnedNote; +use dep::int_note::int_note::OwnedNote; struct BalanceSet { set: PrivateSet, diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr deleted file mode 100644 index 6c557043be1c..000000000000 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr +++ /dev/null @@ -1,132 +0,0 @@ -use dep::aztec::{ - generators::{Ga1 as G_amt, Ga2 as G_npk, Ga3 as G_rnd, G_slot}, - prelude::{NoteHeader, NoteInterface, PrivateContext}, - protocol_types::{ - constants::GENERATOR_INDEX__NOTE_NULLIFIER, point::{Point, POINT_LENGTH}, scalar::Scalar, - hash::poseidon2_hash_with_separator, traits::Serialize -}, - note::utils::compute_note_hash_for_nullify, oracle::unsafe_rand::unsafe_rand, - keys::getters::get_nsk_app -}; -use dep::std::{embedded_curve_ops::multi_scalar_mul, hash::from_field_unsafe}; - -trait OwnedNote { - fn new(amount: U128, owner_npk_m_hash: Field) -> Self; - fn get_amount(self) -> U128; -} - -global TOKEN_NOTE_LEN: Field = 3; // 3 plus a header. -global TOKEN_NOTE_BYTES_LEN: Field = 3 * 32 + 64; - -#[aztec(note)] -struct TokenNote { - // The amount of tokens in the note - amount: 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, -} - -impl NoteInterface for TokenNote { - // 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([ - note_hash_for_nullify, - secret - ], - GENERATOR_INDEX__NOTE_NULLIFIER as Field, - ) - } - // docs:end:nullifier - - fn compute_nullifier_without_context(self) -> Field { - let note_hash_for_nullify = compute_note_hash_for_nullify(self); - let secret = get_nsk_app(self.npk_m_hash); - poseidon2_hash_with_separator([note_hash_for_nullify, secret],GENERATOR_INDEX__NOTE_NULLIFIER) - } - - fn compute_note_hiding_point(self) -> Point { - // We use the unsafe version because the multi_scalar_mul will constrain the scalars. - let amount_scalar = from_field_unsafe(self.amount.to_integer()); - let npk_m_hash_scalar = from_field_unsafe(self.npk_m_hash); - let randomness_scalar = from_field_unsafe(self.randomness); - let slot_scalar = from_field_unsafe(self.header.storage_slot); - // We compute the note hiding point as: - // `G_amt * amount + G_npk * npk_m_hash + G_rnd * randomness + G_slot * slot` - // instead of using pedersen or poseidon2 because it allows us to privately add and subtract from amount - // in public by leveraging homomorphism. - multi_scalar_mul( - [G_amt, G_npk, G_rnd, G_slot], - [amount_scalar, npk_m_hash_scalar, randomness_scalar, slot_scalar] - ) - } -} - -impl TokenNote { - // 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) -> TokenNoteHidingPoint { - TokenNoteHidingPoint::new(self.compute_note_hiding_point()) - } -} - -struct TokenNoteHidingPoint { - inner: Point -} - -impl TokenNoteHidingPoint { - fn new(point: Point) -> Self { - Self { inner: point } - } - - fn add_amount(&mut self, amount: U128) { - self.inner = multi_scalar_mul([G_amt], [from_field_unsafe(amount.to_integer())]) + self.inner; - } - - fn add_npk_m_hash(&mut self, npk_m_hash: Field) { - self.inner = multi_scalar_mul([G_npk], [from_field_unsafe(npk_m_hash)]) + self.inner; - } - - fn add_randomness(&mut self, randomness: Field) { - self.inner = multi_scalar_mul([G_rnd], [from_field_unsafe(randomness)]) + self.inner; - } - - fn add_slot(&mut self, slot: Field) { - self.inner = multi_scalar_mul([G_slot], [from_field_unsafe(slot)]) + self.inner; - } - - fn finalize(self) -> Field { - self.inner.x - } -} - -impl Serialize for TokenNoteHidingPoint { - fn serialize(self) -> [Field; POINT_LENGTH] { - self.inner.serialize() - } -} - -impl Eq for TokenNote { - fn eq(self, other: Self) -> bool { - (self.amount == other.amount) & - (self.npk_m_hash == other.npk_m_hash) & - (self.randomness == other.randomness) - } -} - -impl OwnedNote for TokenNote { - fn new(amount: U128, owner_npk_m_hash: Field) -> Self { - Self { - amount, - npk_m_hash: owner_npk_m_hash, - randomness: unsafe_rand(), - header: NoteHeader::empty(), - } - } - - fn get_amount(self) -> U128 { - self.amount - } -} 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 417f83c4773b..c7bf7a3e5f8a 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 @@ -99,7 +99,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, ), ); @@ -118,7 +118,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, ), );