From cf15adb89dca172a4c4899050b3d17b6091cf1c5 Mon Sep 17 00:00:00 2001 From: Gregorio Juliana Date: Fri, 12 Jan 2024 11:44:32 +0100 Subject: [PATCH] chore!: define key type in maps (#3841) Closes https://github.com/AztecProtocol/aztec-packages/issues/2787 Unfortunately key type with a `ToField` trait binding is only defined in the `.at` method, due to this blocker: https://github.com/noir-lang/noir/issues/3954 --- boxes/token/src/contracts/src/main.nr | 40 +++++++++---------- .../src/contracts/src/types/balance_set.nr | 19 ++++----- .../src/contracts/src/types/balances_map.nr | 21 ++++++---- docs/docs/dev_docs/testing/cheat_codes.md | 4 +- docs/docs/misc/migration_notes.md | 22 ++++++++++ .../nargo_fmt/tests/expected/contract.nr | 4 +- .../tooling/nargo_fmt/tests/input/contract.nr | 17 ++++++-- yarn-project/aztec-nr/authwit/src/account.nr | 2 +- .../aztec-nr/aztec/src/state_vars/map.nr | 15 ++++--- .../benchmarking_contract/src/main.nr | 14 +++---- .../contracts/card_game_contract/src/main.nr | 20 +++++----- .../contracts/counter_contract/src/main.nr | 8 ++-- .../docs_example_contract/src/actions.nr | 8 ++-- .../docs_example_contract/src/main.nr | 2 +- .../easy_private_token_contract/src/main.nr | 12 +++--- .../easy_private_voting_contract/src/main.nr | 2 +- .../inclusion_proofs_contract/src/main.nr | 12 +++--- .../contracts/lending_contract/src/main.nr | 26 ++++++------ .../pending_commitments_contract/src/main.nr | 12 +++--- .../contracts/price_feed_contract/src/main.nr | 2 +- .../contracts/slow_tree_contract/src/main.nr | 2 +- .../stateful_test_contract/src/main.nr | 14 +++---- .../token_blacklist_contract/src/main.nr | 28 ++++++------- .../contracts/token_contract/src/main.nr | 40 +++++++++---------- .../contracts/uniswap_contract/src/main.nr | 2 +- .../src/crates/types/src/address.nr | 28 ++++++++----- .../src/crates/types/src/lib.nr | 2 +- .../src/crates/types/src/traits.nr | 10 +++++ 28 files changed, 222 insertions(+), 166 deletions(-) diff --git a/boxes/token/src/contracts/src/main.nr b/boxes/token/src/contracts/src/main.nr index 686871f1661..e8ee1a18dbf 100644 --- a/boxes/token/src/contracts/src/main.nr +++ b/boxes/token/src/contracts/src/main.nr @@ -58,7 +58,7 @@ contract Token { admin: PublicState, // docs:end:storage_admin // docs:start:storage_minters - minters: Map>, + minters: Map>, // docs:end:storage_minters // docs:start:storage_balances balances: BalancesMap, @@ -67,7 +67,7 @@ contract Token { // docs:start:storage_pending_shields pending_shields: Set, // docs:end:storage_pending_shields - public_balances: Map>, + public_balances: Map>, } // docs:end:storage_struct @@ -147,7 +147,7 @@ contract Token { assert(storage.admin.read().eq(context.msg_sender()), "caller is not admin"); // docs:end:read_admin // docs:start:write_minter - storage.minters.at(minter.to_field()).write(approve); + storage.minters.at(minter).write(approve); // docs:end:write_minter } // docs:end:set_minter @@ -156,13 +156,13 @@ contract Token { #[aztec(public)] fn mint_public(to: AztecAddress, amount: Field) { // docs:start:read_minter - assert(storage.minters.at(context.msg_sender().to_field()).read(), "caller is not minter"); + assert(storage.minters.at(context.msg_sender()).read(), "caller is not minter"); // docs:end:read_minter let amount = SafeU120::new(amount); - let new_balance = storage.public_balances.at(to.to_field()).read().add(amount); + let new_balance = storage.public_balances.at(to).read().add(amount); let supply = storage.total_supply.read().add(amount); - storage.public_balances.at(to.to_field()).write(new_balance); + storage.public_balances.at(to).write(new_balance); storage.total_supply.write(supply); } // docs:end:mint_public @@ -170,7 +170,7 @@ contract Token { // docs:start:mint_private #[aztec(public)] fn mint_private(amount: Field, secret_hash: Field) { - assert(storage.minters.at(context.msg_sender().to_field()).read(), "caller is not minter"); + assert(storage.minters.at(context.msg_sender()).read(), "caller is not minter"); let pending_shields = storage.pending_shields; let mut note = TransparentNote::new(amount, secret_hash); let supply = storage.total_supply.read().add(SafeU120::new(amount)); @@ -193,12 +193,12 @@ contract Token { } let amount = SafeU120::new(amount); - let from_balance = storage.public_balances.at(from.to_field()).read().sub(amount); + let from_balance = storage.public_balances.at(from).read().sub(amount); let pending_shields = storage.pending_shields; let mut note = TransparentNote::new(amount.value as Field, secret_hash); - storage.public_balances.at(from.to_field()).write(from_balance); + storage.public_balances.at(from).write(from_balance); pending_shields.insert_from_public(&mut note); } // docs:end:shield @@ -213,11 +213,11 @@ contract Token { } let amount = SafeU120::new(amount); - let from_balance = storage.public_balances.at(from.to_field()).read().sub(amount); - storage.public_balances.at(from.to_field()).write(from_balance); + let from_balance = storage.public_balances.at(from).read().sub(amount); + storage.public_balances.at(from).write(from_balance); - let to_balance = storage.public_balances.at(to.to_field()).read().add(amount); - storage.public_balances.at(to.to_field()).write(to_balance); + let to_balance = storage.public_balances.at(to).read().add(amount); + storage.public_balances.at(to).write(to_balance); } // docs:end:transfer_public @@ -233,8 +233,8 @@ contract Token { // docs:end:assert_current_call_valid_authwit_public let amount = SafeU120::new(amount); - let from_balance = storage.public_balances.at(from.to_field()).read().sub(amount); - storage.public_balances.at(from.to_field()).write(from_balance); + let from_balance = storage.public_balances.at(from).read().sub(amount); + storage.public_balances.at(from).write(from_balance); let new_supply = storage.total_supply.read().sub(amount); storage.total_supply.write(new_supply); @@ -315,7 +315,7 @@ contract Token { internal fn _initialize(new_admin: AztecAddress) { assert(!new_admin.is_zero(), "invalid admin"); storage.admin.write(new_admin); - storage.minters.at(new_admin.to_field()).write(true); + storage.minters.at(new_admin).write(true); } // docs:end:initialize @@ -324,8 +324,8 @@ contract Token { // docs:start:increase_public_balance #[aztec(public)] internal fn _increase_public_balance(to: AztecAddress, amount: Field) { - let new_balance = storage.public_balances.at(to.to_field()).read().add(SafeU120::new(amount)); - storage.public_balances.at(to.to_field()).write(new_balance); + let new_balance = storage.public_balances.at(to).read().add(SafeU120::new(amount)); + storage.public_balances.at(to).write(new_balance); } // docs:end:increase_public_balance @@ -348,7 +348,7 @@ contract Token { // docs:start:is_minter unconstrained fn is_minter(minter: AztecAddress) -> pub bool { - storage.minters.at(minter.to_field()).read() + storage.minters.at(minter).read() } // docs:end:is_minter @@ -366,7 +366,7 @@ contract Token { // docs:start:balance_of_public unconstrained fn balance_of_public(owner: AztecAddress) -> pub u120 { - storage.public_balances.at(owner.to_field()).read().value + storage.public_balances.at(owner).read().value } // docs:end:balance_of_public diff --git a/boxes/token/src/contracts/src/types/balance_set.nr b/boxes/token/src/contracts/src/types/balance_set.nr index a5c00d81967..66aade5b9dd 100644 --- a/boxes/token/src/contracts/src/types/balance_set.nr +++ b/boxes/token/src/contracts/src/types/balance_set.nr @@ -30,21 +30,13 @@ use crate::types::token_note::{TokenNote, TOKEN_NOTE_LEN, TokenNoteMethods}; // Does not require spending key, but only knowledge. // Spending key requirement should be enforced by the contract using this. struct BalanceSet { - context: Context, - owner: AztecAddress, - set: Set + owner: AztecAddress, + set: Set } impl BalanceSet { - pub fn new(context: Context, owner: AztecAddress, storage_slot: Field) -> Self { - assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1."); - let set = Set { - context, - storage_slot, - note_interface: TokenNoteMethods, - }; + pub fn new(set: Set, owner: AztecAddress) -> Self { Self { - context, owner, set, } @@ -114,7 +106,10 @@ impl BalanceSet { } } -pub fn filter_notes_min_sum(notes: [Option; MAX_READ_REQUESTS_PER_CALL], min_sum: SafeU120) -> [Option; MAX_READ_REQUESTS_PER_CALL] { +pub fn filter_notes_min_sum( + notes: [Option; MAX_READ_REQUESTS_PER_CALL], + min_sum: SafeU120 +) -> [Option; MAX_READ_REQUESTS_PER_CALL] { let mut selected = [Option::none(); MAX_READ_REQUESTS_PER_CALL]; let mut sum = SafeU120::min(); for i in 0..notes.len() { diff --git a/boxes/token/src/contracts/src/types/balances_map.nr b/boxes/token/src/contracts/src/types/balances_map.nr index 04a595e9a8f..111d3ec50ed 100644 --- a/boxes/token/src/contracts/src/types/balances_map.nr +++ b/boxes/token/src/contracts/src/types/balances_map.nr @@ -4,9 +4,11 @@ use crate::types::balance_set::BalanceSet; use dep::aztec::hash::pedersen_hash; use dep::protocol_types::address::AztecAddress; +use crate::types::token_note::{TokenNote, TOKEN_NOTE_LEN, TokenNoteMethods}; +use dep::aztec::state_vars::{map::Map, set::Set}; + struct BalancesMap { - context: Context, - storage_slot: Field, + store: Map>, } impl BalancesMap { @@ -14,15 +16,20 @@ impl BalancesMap { context: Context, storage_slot: Field, ) -> Self { - assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1."); + let store = Map::new(context, storage_slot, |context, storage_slot| { + Set { + context, + storage_slot, + note_interface: TokenNoteMethods, + } + }); Self { - context, - storage_slot, + store, } } pub fn at(self, owner: AztecAddress) -> BalanceSet { - let derived_storage_slot = pedersen_hash([self.storage_slot, owner.to_field()], 0); - BalanceSet::new(self.context, owner, derived_storage_slot) + let set = self.store.at(owner); + BalanceSet::new(set, owner) } } diff --git a/docs/docs/dev_docs/testing/cheat_codes.md b/docs/docs/dev_docs/testing/cheat_codes.md index aaa68ad85a5..d789872625e 100644 --- a/docs/docs/dev_docs/testing/cheat_codes.md +++ b/docs/docs/dev_docs/testing/cheat_codes.md @@ -462,7 +462,7 @@ The baseSlot is specified in the Aztec.nr contract. ```rust struct Storage { - balances: Map>, + balances: Map>, } impl Storage { @@ -500,7 +500,7 @@ Note: One Field element occupies a storage slot. Hence, structs with multiple fi ```rust struct Storage { - balances: Map>, + balances: Map>, } impl Storage { diff --git a/docs/docs/misc/migration_notes.md b/docs/docs/misc/migration_notes.md index 6c20da38de8..17f0785e50b 100644 --- a/docs/docs/misc/migration_notes.md +++ b/docs/docs/misc/migration_notes.md @@ -113,3 +113,25 @@ Now, just remove the `src` folder,: easy_private_token_contract = {git = "https://github.com/AztecProtocol/aztec-packages/", tag ="v0.17.0", directory = "yarn-project/noir-contracts/contracts/easy_private_token_contract"} ``` +### [Aztec.nr] key type definition in Map + +The `Map` class now requires defining the key type in its declaration which *must* implement the `ToField` trait. + +Before: +```rust +struct Storage { + balances: Map> +} + +let user_balance = balances.at(owner.to_field()) +``` + +Now: +```rust +struct Storage { + balances: Map> +} + +let user_balance = balances.at(owner) +``` + diff --git a/noir/tooling/nargo_fmt/tests/expected/contract.nr b/noir/tooling/nargo_fmt/tests/expected/contract.nr index 0313da832a8..cd53add10a0 100644 --- a/noir/tooling/nargo_fmt/tests/expected/contract.nr +++ b/noir/tooling/nargo_fmt/tests/expected/contract.nr @@ -20,8 +20,8 @@ contract Benchmarking { }; struct Storage { - notes: Map>, - balances: Map>, + notes: Map>, + balances: Map>, } impl Storage { diff --git a/noir/tooling/nargo_fmt/tests/input/contract.nr b/noir/tooling/nargo_fmt/tests/input/contract.nr index 58ae0e909a1..7aa688fef68 100644 --- a/noir/tooling/nargo_fmt/tests/input/contract.nr +++ b/noir/tooling/nargo_fmt/tests/input/contract.nr @@ -20,8 +20,8 @@ contract Benchmarking { }; struct Storage { - notes: Map>, - balances: Map>, + notes: Map>, + balances: Map>, } impl Storage { @@ -58,7 +58,11 @@ contract Benchmarking { fn increment_balance(owner: Field, value: Field) { let current = storage.balances.at(owner).read(); storage.balances.at(owner).write(current + value); - let _callStackItem1 = context.call_public_function(context.this_address(), FunctionSelector::from_signature("broadcast(Field)"), [owner]); + let _callStackItem1 = context.call_public_function( + context.this_address(), + FunctionSelector::from_signature("broadcast(Field)"), + [owner] + ); } // Est ultricies integer quis auctor elit sed. In nibh mauris cursus mattis molestie a iaculis. @@ -67,7 +71,12 @@ contract Benchmarking { emit_unencrypted_log(&mut context, storage.balances.at(owner).read()); } - unconstrained fn compute_note_hash_and_nullifier(contract_address: AztecAddress, nonce: Field, storage_slot: Field, preimage: [Field; VALUE_NOTE_LEN]) -> [Field; 4] { + unconstrained fn compute_note_hash_and_nullifier( + contract_address: AztecAddress, + nonce: Field, + storage_slot: Field, + preimage: [Field; VALUE_NOTE_LEN] + ) -> [Field; 4] { let note_header = NoteHeader::new(contract_address, nonce, storage_slot); note_utils::compute_note_hash_and_nullifier(ValueNoteMethods, note_header, preimage) } diff --git a/yarn-project/aztec-nr/authwit/src/account.nr b/yarn-project/aztec-nr/authwit/src/account.nr index 33b92954c77..21c13e3c8ef 100644 --- a/yarn-project/aztec-nr/authwit/src/account.nr +++ b/yarn-project/aztec-nr/authwit/src/account.nr @@ -8,7 +8,7 @@ use crate::auth::IS_VALID_SELECTOR; struct AccountActions { context: Context, is_valid_impl: fn(&mut PrivateContext, Field) -> bool, - approved_action: Map>, + approved_action: Map>, } impl AccountActions { diff --git a/yarn-project/aztec-nr/aztec/src/state_vars/map.nr b/yarn-project/aztec-nr/aztec/src/state_vars/map.nr index bca3872b9a9..fefa84ea00e 100644 --- a/yarn-project/aztec-nr/aztec/src/state_vars/map.nr +++ b/yarn-project/aztec-nr/aztec/src/state_vars/map.nr @@ -1,22 +1,25 @@ use crate::context::{PrivateContext, PublicContext, Context}; use dep::std::option::Option; -use dep::protocol_types::hash::pedersen_hash; +use dep::protocol_types::{ + hash::pedersen_hash, + traits::{ToField} +}; // docs:start:map -struct Map { +struct Map { context: Context, storage_slot: Field, state_var_constructor: fn(Context, Field) -> V, } // docs:end:map -impl Map { +impl Map { // docs:start:new pub fn new( context: Context, storage_slot: Field, state_var_constructor: fn(Context, Field) -> V, - ) -> Map { + ) -> Self { assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1."); Map { context, @@ -27,9 +30,9 @@ impl Map { // docs:end:new // docs:start:at - pub fn at(self, key: Field) -> V { + pub fn at(self, key: K) -> V where K: ToField { // TODO(#1204): use a generator index for the storage slot - let derived_storage_slot = pedersen_hash([self.storage_slot, key],0); + let derived_storage_slot = pedersen_hash([self.storage_slot, key.to_field()],0); let state_var_constructor = self.state_var_constructor; state_var_constructor(self.context, derived_storage_slot) diff --git a/yarn-project/noir-contracts/contracts/benchmarking_contract/src/main.nr b/yarn-project/noir-contracts/contracts/benchmarking_contract/src/main.nr index e27e1dfb7bb..2ff63e20bba 100644 --- a/yarn-project/noir-contracts/contracts/benchmarking_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/benchmarking_contract/src/main.nr @@ -24,8 +24,8 @@ contract Benchmarking { }; struct Storage { - notes: Map>, - balances: Map>, + notes: Map>, + balances: Map>, } impl Storage { @@ -43,7 +43,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(storage.notes.at(owner.to_field()), 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. @@ -53,7 +53,7 @@ contract Benchmarking { // by @rahul-kothari for a full explanation on why this is needed. #[aztec(private)] fn recreate_note(owner: AztecAddress, index: u32) { - let owner_notes = storage.notes.at(owner.to_field()); + let owner_notes = storage.notes.at(owner); let getter_options = NoteGetterOptions::new().set_limit(1).set_offset(index); let notes = owner_notes.get_notes(getter_options); let note = notes[0].unwrap_unchecked(); @@ -64,8 +64,8 @@ contract Benchmarking { // Reads and writes to public storage and enqueues a call to another public function. #[aztec(public)] fn increment_balance(owner: AztecAddress, value: Field) { - let current = storage.balances.at(owner.to_field()).read(); - storage.balances.at(owner.to_field()).write(current + value); + let current = storage.balances.at(owner).read(); + storage.balances.at(owner).write(current + value); let _callStackItem1 = context.call_public_function( context.this_address(), FunctionSelector::from_signature("broadcast((Field))"), @@ -76,7 +76,7 @@ contract Benchmarking { // Emits a public log. #[aztec(public)] fn broadcast(owner: AztecAddress) { - emit_unencrypted_log(&mut context, storage.balances.at(owner.to_field()).read()); + emit_unencrypted_log(&mut context, storage.balances.at(owner).read()); } unconstrained fn compute_note_hash_and_nullifier( diff --git a/yarn-project/noir-contracts/contracts/card_game_contract/src/main.nr b/yarn-project/noir-contracts/contracts/card_game_contract/src/main.nr index b28d27c625b..35271a8a13b 100644 --- a/yarn-project/noir-contracts/contracts/card_game_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/card_game_contract/src/main.nr @@ -54,9 +54,9 @@ contract CardGame { }; struct Storage { - collections: Map, - game_decks: Map>, - games: Map>, + collections: Map, + game_decks: Map>, + games: Map>, } impl Storage { @@ -114,7 +114,7 @@ contract CardGame { let buyer = context.msg_sender(); let mut cards = get_pack_cards(seed, buyer); - let mut collection = storage.collections.at(buyer.to_field()); + let mut collection = storage.collections.at(buyer); let _inserted_cards = collection.add_cards(cards, buyer); } @@ -124,9 +124,9 @@ contract CardGame { let player = context.msg_sender(); - let mut collection = storage.collections.at(player.to_field()); + let mut collection = storage.collections.at(player); collection.remove_cards(cards, player); - let mut game_deck = storage.game_decks.at(game as Field).at(player.to_field()); + let mut game_deck = storage.game_decks.at(game as Field).at(player); let _added_to_game_deck = game_deck.add_cards(cards, player); let selector = FunctionSelector::from_signature("on_game_joined(u32,(Field),u32)"); let strength = compute_deck_strength(cards); @@ -160,7 +160,7 @@ contract CardGame { fn play_card(game: u32, card: Card) { let player = context.msg_sender(); - let mut game_deck = storage.game_decks.at(game as Field).at(player.to_field()); + let mut game_deck = storage.game_decks.at(game as Field).at(player); game_deck.remove_cards([card], player); let selector = FunctionSelector::from_signature("on_card_played(u32,(Field),Field)"); @@ -192,7 +192,7 @@ contract CardGame { let player = context.msg_sender(); let cards = cards_fields.map(|card_field| Card::from_field(card_field)); - let mut collection = storage.collections.at(player.to_field()); + let mut collection = storage.collections.at(player); let _inserted_cards = collection.add_cards(cards, player); let selector = FunctionSelector::from_signature("on_cards_claimed(u32,(Field),Field)"); @@ -220,13 +220,13 @@ contract CardGame { } unconstrained fn view_collection_cards(owner: AztecAddress, offset: u32) -> pub [Option; MAX_NOTES_PER_PAGE] { - let collection = storage.collections.at(owner.to_field()); + let collection = storage.collections.at(owner); collection.view_cards(offset) } unconstrained fn view_game_cards(game: u32, player: AztecAddress, offset: u32) -> pub [Option; MAX_NOTES_PER_PAGE] { - let game_deck = storage.game_decks.at(game as Field).at(player.to_field()); + let game_deck = storage.game_decks.at(game as Field).at(player); game_deck.view_cards(offset) } diff --git a/yarn-project/noir-contracts/contracts/counter_contract/src/main.nr b/yarn-project/noir-contracts/contracts/counter_contract/src/main.nr index ec4367bfa10..4b4f4bfd8ca 100644 --- a/yarn-project/noir-contracts/contracts/counter_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/counter_contract/src/main.nr @@ -22,7 +22,7 @@ contract Counter { // docs:start:storage_struct struct Storage { - counters: Map, + counters: Map, } // docs:end:storage_struct @@ -47,7 +47,7 @@ contract Counter { #[aztec(private)] fn constructor(headstart: u120, owner: AztecAddress) { let counters = storage.counters; - counters.at(owner.to_field()).add(headstart, owner); + counters.at(owner).add(headstart, owner); } // docs:end:constructor @@ -55,14 +55,14 @@ contract Counter { #[aztec(private)] fn increment(owner: AztecAddress) { let counters = storage.counters; - counters.at(owner.to_field()).add(1, owner); + counters.at(owner).add(1, owner); } // docs:end:increment // docs:start:get_counter unconstrained fn get_counter(owner: AztecAddress) -> pub Field { let counters = storage.counters; - balance_utils::get_balance(counters.at(owner.to_field()).set) + balance_utils::get_balance(counters.at(owner).set) } // docs:end:get_counter diff --git a/yarn-project/noir-contracts/contracts/docs_example_contract/src/actions.nr b/yarn-project/noir-contracts/contracts/docs_example_contract/src/actions.nr index 88f7a9422a1..9feed4b03cb 100644 --- a/yarn-project/noir-contracts/contracts/docs_example_contract/src/actions.nr +++ b/yarn-project/noir-contracts/contracts/docs_example_contract/src/actions.nr @@ -138,19 +138,19 @@ unconstrained pub fn get_total_points(state_var: Set, a // docs:start:state_vars-MapAtSingletonInit pub fn add_new_profile( - state_var: Map>, + state_var: Map>, account: AztecAddress, profile: &mut ProfileNote ) { - state_var.at(account.to_field()).initialize(profile, Option::some(account), true); + state_var.at(account).initialize(profile, Option::some(account), true); } // docs:end:state_vars-MapAtSingletonInit // docs:start:state_vars-MapAtSingletonGet pub fn get_profile( - state_var: Map>, + state_var: Map>, account: AztecAddress ) -> ProfileNote { - state_var.at(account.to_field()).get_note(true) + state_var.at(account).get_note(true) } // docs:end:state_vars-MapAtSingletonGet diff --git a/yarn-project/noir-contracts/contracts/docs_example_contract/src/main.nr b/yarn-project/noir-contracts/contracts/docs_example_contract/src/main.nr index 6c4f8a65a74..4a8ba985a5c 100644 --- a/yarn-project/noir-contracts/contracts/docs_example_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/docs_example_contract/src/main.nr @@ -41,7 +41,7 @@ contract DocsExample { // docs:end:storage-singleton-declaration cards: Set, // docs:start:storage-map-singleton-declaration - profiles: Map>, + profiles: Map>, // docs:end:storage-map-singleton-declaration } // docs:end:storage-struct-declaration diff --git a/yarn-project/noir-contracts/contracts/easy_private_token_contract/src/main.nr b/yarn-project/noir-contracts/contracts/easy_private_token_contract/src/main.nr index c1541833c5f..c8e4e7590ce 100644 --- a/yarn-project/noir-contracts/contracts/easy_private_token_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/easy_private_token_contract/src/main.nr @@ -20,7 +20,7 @@ contract EasyPrivateToken { use dep::easy_private_state::easy_private_state::EasyPrivateUint; struct Storage { - balances: Map, + balances: Map, } impl Storage { @@ -44,7 +44,7 @@ contract EasyPrivateToken { fn constructor(initial_supply: u120, owner: AztecAddress) { let balances = storage.balances; - balances.at(owner.to_field()).add(initial_supply, owner); + balances.at(owner).add(initial_supply, owner); } // Mints `amount` of tokens to `owner`. @@ -52,7 +52,7 @@ contract EasyPrivateToken { fn mint(amount: u120, owner: AztecAddress) { let balances = storage.balances; - balances.at(owner.to_field()).add(amount, owner); + balances.at(owner).add(amount, owner); } // Transfers `amount` of tokens from `sender` to a `recipient`. @@ -60,8 +60,8 @@ contract EasyPrivateToken { fn transfer(amount: u120, sender: AztecAddress, recipient: AztecAddress) { let balances = storage.balances; - balances.at(sender.to_field()).sub(amount, sender); - balances.at(recipient.to_field()).add(amount, recipient); + balances.at(sender).sub(amount, sender); + balances.at(recipient).add(amount, recipient); } // Helper function to get the balance of a user ("unconstrained" is a Noir alternative of Solidity's "view" function). @@ -69,7 +69,7 @@ contract EasyPrivateToken { let balances = storage.balances; // Return the sum of all notes in the set. - balance_utils::get_balance(balances.at(owner.to_field()).set) + balance_utils::get_balance(balances.at(owner).set) } // Computes note hash and nullifier. diff --git a/yarn-project/noir-contracts/contracts/easy_private_voting_contract/src/main.nr b/yarn-project/noir-contracts/contracts/easy_private_voting_contract/src/main.nr index c3200a928aa..561068535c2 100644 --- a/yarn-project/noir-contracts/contracts/easy_private_voting_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/easy_private_voting_contract/src/main.nr @@ -18,7 +18,7 @@ contract EasyPrivateVoting { // docs:start:storage_struct struct Storage { admin: PublicState, // admin can end vote - tally: Map>, // we will store candidate as key and number of votes as value + tally: Map>, // we will store candidate as key and number of votes as value voteEnded: PublicState, // voteEnded is boolean } // docs:end:storage_struct diff --git a/yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr b/yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr index 6b5f635b245..b31e402acd5 100644 --- a/yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr @@ -50,7 +50,7 @@ contract InclusionProofs { use dep::value_note::value_note::{ValueNote, ValueNoteMethods, VALUE_NOTE_LEN}; struct Storage { - private_values: Map>, + private_values: Map>, public_value: PublicState, } @@ -87,7 +87,7 @@ contract InclusionProofs { // Creates a value note owned by `owner`. #[aztec(private)] fn create_note(owner: AztecAddress, value: Field) { - let owner_private_values = storage.private_values.at(owner.to_field()); + let owner_private_values = storage.private_values.at(owner); let mut note = ValueNote::new(value, owner); owner_private_values.insert(&mut note, true); } @@ -103,7 +103,7 @@ contract InclusionProofs { spare_commitment: Field ) { // 1) Get the note from PXE. - let private_values = storage.private_values.at(owner.to_field()); + let private_values = storage.private_values.at(owner); let options = NoteGetterOptions::new().select(1, owner.to_field()).set_limit(1); let notes = private_values.get_notes(options); let maybe_note = notes[0]; @@ -133,7 +133,7 @@ contract InclusionProofs { spare_nullifier: Field ) { // 2) Get the note from PXE - let private_values = storage.private_values.at(owner.to_field()); + let private_values = storage.private_values.at(owner); let options = NoteGetterOptions::new().select(1, owner.to_field()).set_limit(1); let notes = private_values.get_notes(options); let maybe_note = notes[0]; @@ -158,7 +158,7 @@ contract InclusionProofs { block_number: u32 // The block at which we'll prove that the note exists and is not nullified ) { // 1) Get the note from PXE. - let private_values = storage.private_values.at(owner.to_field()); + let private_values = storage.private_values.at(owner); let options = NoteGetterOptions::new().select(1, owner.to_field()).set_limit(1); let notes = private_values.get_notes(options); let note = notes[0].unwrap(); @@ -169,7 +169,7 @@ contract InclusionProofs { #[aztec(private)] fn nullify_note(owner: AztecAddress) { - let private_values = storage.private_values.at(owner.to_field()); + let private_values = storage.private_values.at(owner); let options = NoteGetterOptions::new().select(1, owner.to_field()).set_limit(1); let notes = private_values.get_notes(options); let note = notes[0].unwrap(); diff --git a/yarn-project/noir-contracts/contracts/lending_contract/src/main.nr b/yarn-project/noir-contracts/contracts/lending_contract/src/main.nr index f81555ae990..636585900ad 100644 --- a/yarn-project/noir-contracts/contracts/lending_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/lending_contract/src/main.nr @@ -38,9 +38,9 @@ contract Lending { struct Storage { collateral_asset: PublicState, stable_coin: PublicState, - assets: Map>, - collateral: Map>, - static_debt: Map>, // abusing keys very heavily + assets: Map>, + collateral: Map>, + static_debt: Map>, // abusing keys very heavily } impl Storage { @@ -200,7 +200,7 @@ contract Lending { let coll_asset = storage.collateral_asset.read(); assert(coll_asset.eq(collateral_asset)); - let coll_loc = storage.collateral.at(owner.to_field()); + let coll_loc = storage.collateral.at(owner); let collateral = coll_loc.read(); coll_loc.write(collateral + amount); } @@ -231,10 +231,10 @@ contract Lending { let asset = Lending::at(context.this_address()).update_accumulator(context); let price = PriceFeed::at(asset.oracle).get_price(context); - let coll_loc = storage.collateral.at(owner.to_field()); + let coll_loc = storage.collateral.at(owner); let collateral: Field = coll_loc.read(); - let debt_loc = storage.static_debt.at(owner.to_field()); + let debt_loc = storage.static_debt.at(owner); let static_debt: Field = debt_loc.read(); // debt_covered will revert if decrease would leave insufficient collateral to cover debt. @@ -284,15 +284,15 @@ contract Lending { let price = PriceFeed::at(asset.oracle).get_price(context); // Fetch collateral and static_debt, compute health of current position - let collateral = storage.collateral.at(owner.to_field()).read() as u120; - let static_debt = storage.static_debt.at(owner.to_field()).read() as u120; + let collateral = storage.collateral.at(owner).read() as u120; + let static_debt = storage.static_debt.at(owner).read() as u120; let debt_covered = covered_by_collateral(price, asset.loan_to_value, collateral, 0, 0); let debt_returns = debt_updates(asset.interest_accumulator, static_debt, amount as u120, 0); assert(debt_returns.debt_value < debt_covered); - storage.static_debt.at(owner.to_field()).write(debt_returns.static_debt as Field); + storage.static_debt.at(owner).write(debt_returns.static_debt as Field); // @todo @LHerskind Need to support both private and public minting. let stable_coin = storage.stable_coin.read(); @@ -336,10 +336,10 @@ contract Lending { // To ensure that private is using the correct token. assert(stable_coin.eq(storage.stable_coin.read())); - let static_debt = storage.static_debt.at(owner.to_field()).read() as u120; + let static_debt = storage.static_debt.at(owner).read() as u120; let debt_returns = debt_updates(asset.interest_accumulator, static_debt, 0, amount as u120); - storage.static_debt.at(owner.to_field()).write(debt_returns.static_debt as Field); + storage.static_debt.at(owner).write(debt_returns.static_debt as Field); } unconstrained fn get_asset(assetId: Field) -> pub Asset { @@ -347,8 +347,8 @@ contract Lending { } unconstrained fn get_position(owner: AztecAddress) -> pub Position { - let collateral = storage.collateral.at(owner.to_field()).read(); - let static_debt = storage.static_debt.at(owner.to_field()).read(); + let collateral = storage.collateral.at(owner).read(); + let static_debt = storage.static_debt.at(owner).read(); let asset = storage.assets.at(0).read(); let debt = debt_value(static_debt as u120, asset.interest_accumulator as u120) as Field; Position { collateral, static_debt, debt } diff --git a/yarn-project/noir-contracts/contracts/pending_commitments_contract/src/main.nr b/yarn-project/noir-contracts/contracts/pending_commitments_contract/src/main.nr index f274a545dc7..3fec0052803 100644 --- a/yarn-project/noir-contracts/contracts/pending_commitments_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/pending_commitments_contract/src/main.nr @@ -26,7 +26,7 @@ contract PendingCommitments { }; struct Storage { - balances: Map>, + balances: Map>, } impl Storage { @@ -54,7 +54,7 @@ contract PendingCommitments { // Realistic way to describe this test is "Mint note A, then burn note A in the same transaction" #[aztec(private)] fn test_insert_then_get_then_nullify_flat(amount: Field, owner: AztecAddress) -> Field { - let owner_balance = storage.balances.at(owner.to_field()); + let owner_balance = storage.balances.at(owner); let mut note = ValueNote::new(amount, owner); // Insert note @@ -76,7 +76,7 @@ contract PendingCommitments { // Confirm cannot access commitments inserted later in same function #[aztec(private)] fn test_bad_get_then_insert_flat(amount: Field, owner: AztecAddress) -> Field { - let owner_balance = storage.balances.at(owner.to_field()); + let owner_balance = storage.balances.at(owner); let options = NoteGetterOptions::with_filter(filter_notes_min_sum, amount); // get note (note inserted at bottom of function shouldn't exist yet) @@ -99,7 +99,7 @@ contract PendingCommitments { // Nested/inner function to create and insert a note #[aztec(private)] fn insert_note(amount: Field, owner: AztecAddress) { - let owner_balance = storage.balances.at(owner.to_field()); + let owner_balance = storage.balances.at(owner); let mut note = ValueNote::new(amount, owner); // Insert note @@ -109,7 +109,7 @@ contract PendingCommitments { // Nested/inner function to get a note and confirm it matches the expected value #[aztec(private)] fn get_then_nullify_note(expected_value: Field, owner: AztecAddress) -> Field { - let owner_balance = storage.balances.at(owner.to_field()); + let owner_balance = storage.balances.at(owner); let options = NoteGetterOptions::new().set_limit(1); let note = owner_balance.get_notes(options)[0].unwrap(); @@ -124,7 +124,7 @@ contract PendingCommitments { // Nested/inner function to get a note and confirms that none is returned #[aztec(private)] fn get_note_zero_balance(owner: AztecAddress) { - let owner_balance = storage.balances.at(owner.to_field()); + let owner_balance = storage.balances.at(owner); let options = NoteGetterOptions::new(); let maybe_notes = owner_balance.get_notes(options); diff --git a/yarn-project/noir-contracts/contracts/price_feed_contract/src/main.nr b/yarn-project/noir-contracts/contracts/price_feed_contract/src/main.nr index 298f82f829c..ccb6633f999 100644 --- a/yarn-project/noir-contracts/contracts/price_feed_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/price_feed_contract/src/main.nr @@ -14,7 +14,7 @@ contract PriceFeed { // Storage structure, containing all storage, and specifying what slots they use. struct Storage { - assets: Map>, + assets: Map>, } impl Storage { diff --git a/yarn-project/noir-contracts/contracts/slow_tree_contract/src/main.nr b/yarn-project/noir-contracts/contracts/slow_tree_contract/src/main.nr index a95cb1218df..3ae10094a71 100644 --- a/yarn-project/noir-contracts/contracts/slow_tree_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/slow_tree_contract/src/main.nr @@ -42,7 +42,7 @@ contract SlowTree { global EMPTY_ROOT: Field = 5785871043333994658400733180052743689641713274194136017445890613179954325976; struct Storage { - trees: Map>, + trees: Map>, } // docs:end:constants_and_storage diff --git a/yarn-project/noir-contracts/contracts/stateful_test_contract/src/main.nr b/yarn-project/noir-contracts/contracts/stateful_test_contract/src/main.nr index 6bd292271ea..ad8afd37c97 100644 --- a/yarn-project/noir-contracts/contracts/stateful_test_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/stateful_test_contract/src/main.nr @@ -20,8 +20,8 @@ contract StatefulTest { }; struct Storage { - notes: Map>, - public_values: Map>, + notes: Map>, + public_values: Map>, } impl Storage { @@ -51,14 +51,14 @@ contract StatefulTest { #[aztec(private)] fn constructor(owner: AztecAddress, value: Field) { - let loc = storage.notes.at(owner.to_field()); + let loc = storage.notes.at(owner); increment(loc, value, owner); } #[aztec(private)] fn create_note(owner: AztecAddress, value: Field) { if (value != 0) { - let loc = storage.notes.at(owner.to_field()); + let loc = storage.notes.at(owner); increment(loc, value, owner); } } @@ -67,15 +67,15 @@ contract StatefulTest { fn destroy_and_create(recipient: AztecAddress, amount: Field) { let sender = context.msg_sender(); - let sender_notes = storage.notes.at(sender.to_field()); + let sender_notes = storage.notes.at(sender); decrement(sender_notes, amount, sender); - let recipient_notes = storage.notes.at(recipient.to_field()); + let recipient_notes = storage.notes.at(recipient); increment(recipient_notes, amount, recipient); } unconstrained fn summed_values(owner: AztecAddress) -> pub Field { - let owner_balance = storage.notes.at(owner.to_field()); + let owner_balance = storage.notes.at(owner); // Return the sum of all notes in the set. balance_utils::get_balance(owner_balance) diff --git a/yarn-project/noir-contracts/contracts/token_blacklist_contract/src/main.nr b/yarn-project/noir-contracts/contracts/token_blacklist_contract/src/main.nr index aa64ec956fc..529299a8a0f 100644 --- a/yarn-project/noir-contracts/contracts/token_blacklist_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/token_blacklist_contract/src/main.nr @@ -62,7 +62,7 @@ contract TokenBlacklist { balances: BalancesMap, total_supply: PublicState, pending_shields: Set, - public_balances: Map>, + public_balances: Map>, slow_update: ImmutableSingleton, public_slow_update: PublicState, } @@ -183,10 +183,10 @@ contract TokenBlacklist { assert(caller_roles.is_minter, "caller is not minter"); let amount = SafeU120::new(amount); - let new_balance = storage.public_balances.at(to.to_field()).read().add(amount); + let new_balance = storage.public_balances.at(to).read().add(amount); let supply = storage.total_supply.read().add(amount); - storage.public_balances.at(to.to_field()).write(new_balance); + storage.public_balances.at(to).write(new_balance); storage.total_supply.write(supply); } @@ -218,12 +218,12 @@ contract TokenBlacklist { } let amount = SafeU120::new(amount); - let from_balance = storage.public_balances.at(from.to_field()).read().sub(amount); + let from_balance = storage.public_balances.at(from).read().sub(amount); let pending_shields = storage.pending_shields; let mut note = TransparentNote::new(amount.value as Field, secret_hash); - storage.public_balances.at(from.to_field()).write(from_balance); + storage.public_balances.at(from).write(from_balance); pending_shields.insert_from_public(&mut note); } @@ -242,11 +242,11 @@ contract TokenBlacklist { } let amount = SafeU120::new(amount); - let from_balance = storage.public_balances.at(from.to_field()).read().sub(amount); - storage.public_balances.at(from.to_field()).write(from_balance); + let from_balance = storage.public_balances.at(from).read().sub(amount); + storage.public_balances.at(from).write(from_balance); - let to_balance = storage.public_balances.at(to.to_field()).read().add(amount); - storage.public_balances.at(to.to_field()).write(to_balance); + let to_balance = storage.public_balances.at(to).read().add(amount); + storage.public_balances.at(to).write(to_balance); } #[aztec(public)] @@ -262,8 +262,8 @@ contract TokenBlacklist { } let amount = SafeU120::new(amount); - let from_balance = storage.public_balances.at(from.to_field()).read().sub(amount); - storage.public_balances.at(from.to_field()).write(from_balance); + let from_balance = storage.public_balances.at(from).read().sub(amount); + storage.public_balances.at(from).write(from_balance); let new_supply = storage.total_supply.read().sub(amount); storage.total_supply.write(new_supply); @@ -354,8 +354,8 @@ contract TokenBlacklist { #[aztec(public)] internal fn _increase_public_balance(to: AztecAddress, amount: Field) { - let new_balance = storage.public_balances.at(to.to_field()).read().add(SafeU120::new(amount)); - storage.public_balances.at(to.to_field()).write(new_balance); + let new_balance = storage.public_balances.at(to).read().add(SafeU120::new(amount)); + storage.public_balances.at(to).write(new_balance); } #[aztec(public)] @@ -376,7 +376,7 @@ contract TokenBlacklist { } unconstrained fn balance_of_public(owner: AztecAddress) -> pub u120 { - storage.public_balances.at(owner.to_field()).read().value + storage.public_balances.at(owner).read().value } // Below this point is the stuff of nightmares. diff --git a/yarn-project/noir-contracts/contracts/token_contract/src/main.nr b/yarn-project/noir-contracts/contracts/token_contract/src/main.nr index be8d77bdde6..fbc21f54b08 100644 --- a/yarn-project/noir-contracts/contracts/token_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/token_contract/src/main.nr @@ -60,7 +60,7 @@ contract Token { admin: PublicState, // docs:end:storage_admin // docs:start:storage_minters - minters: Map>, + minters: Map>, // docs:end:storage_minters // docs:start:storage_balances balances: BalancesMap, @@ -69,7 +69,7 @@ contract Token { // docs:start:storage_pending_shields pending_shields: Set, // docs:end:storage_pending_shields - public_balances: Map>, + public_balances: Map>, symbol: PublicState, name: PublicState, decimals: PublicState, @@ -200,7 +200,7 @@ contract Token { assert(storage.admin.read().eq(context.msg_sender()), "caller is not admin"); // docs:end:read_admin // docs:start:write_minter - storage.minters.at(minter.to_field()).write(approve); + storage.minters.at(minter).write(approve); // docs:end:write_minter } // docs:end:set_minter @@ -209,13 +209,13 @@ contract Token { #[aztec(public)] fn mint_public(to: AztecAddress, amount: Field) { // docs:start:read_minter - assert(storage.minters.at(context.msg_sender().to_field()).read(), "caller is not minter"); + assert(storage.minters.at(context.msg_sender()).read(), "caller is not minter"); // docs:end:read_minter let amount = SafeU120::new(amount); - let new_balance = storage.public_balances.at(to.to_field()).read().add(amount); + let new_balance = storage.public_balances.at(to).read().add(amount); let supply = storage.total_supply.read().add(amount); - storage.public_balances.at(to.to_field()).write(new_balance); + storage.public_balances.at(to).write(new_balance); storage.total_supply.write(supply); } // docs:end:mint_public @@ -223,7 +223,7 @@ contract Token { // docs:start:mint_private #[aztec(public)] fn mint_private(amount: Field, secret_hash: Field) { - assert(storage.minters.at(context.msg_sender().to_field()).read(), "caller is not minter"); + assert(storage.minters.at(context.msg_sender()).read(), "caller is not minter"); let pending_shields = storage.pending_shields; let mut note = TransparentNote::new(amount, secret_hash); let supply = storage.total_supply.read().add(SafeU120::new(amount)); @@ -246,12 +246,12 @@ contract Token { } let amount = SafeU120::new(amount); - let from_balance = storage.public_balances.at(from.to_field()).read().sub(amount); + let from_balance = storage.public_balances.at(from).read().sub(amount); let pending_shields = storage.pending_shields; let mut note = TransparentNote::new(amount.value as Field, secret_hash); - storage.public_balances.at(from.to_field()).write(from_balance); + storage.public_balances.at(from).write(from_balance); pending_shields.insert_from_public(&mut note); } // docs:end:shield @@ -266,11 +266,11 @@ contract Token { } let amount = SafeU120::new(amount); - let from_balance = storage.public_balances.at(from.to_field()).read().sub(amount); - storage.public_balances.at(from.to_field()).write(from_balance); + let from_balance = storage.public_balances.at(from).read().sub(amount); + storage.public_balances.at(from).write(from_balance); - let to_balance = storage.public_balances.at(to.to_field()).read().add(amount); - storage.public_balances.at(to.to_field()).write(to_balance); + let to_balance = storage.public_balances.at(to).read().add(amount); + storage.public_balances.at(to).write(to_balance); } // docs:end:transfer_public @@ -286,8 +286,8 @@ contract Token { // docs:end:assert_current_call_valid_authwit_public let amount = SafeU120::new(amount); - let from_balance = storage.public_balances.at(from.to_field()).read().sub(amount); - storage.public_balances.at(from.to_field()).write(from_balance); + let from_balance = storage.public_balances.at(from).read().sub(amount); + storage.public_balances.at(from).write(from_balance); let new_supply = storage.total_supply.read().sub(amount); storage.total_supply.write(new_supply); @@ -373,7 +373,7 @@ contract Token { ) { assert(!new_admin.is_zero(), "invalid admin"); storage.admin.write(new_admin); - storage.minters.at(new_admin.to_field()).write(true); + storage.minters.at(new_admin).write(true); storage.name.write(name); storage.symbol.write(symbol); storage.decimals.write(decimals); @@ -385,8 +385,8 @@ contract Token { // docs:start:increase_public_balance #[aztec(public)] internal fn _increase_public_balance(to: AztecAddress, amount: Field) { - let new_balance = storage.public_balances.at(to.to_field()).read().add(SafeU120::new(amount)); - storage.public_balances.at(to.to_field()).write(new_balance); + let new_balance = storage.public_balances.at(to).read().add(SafeU120::new(amount)); + storage.public_balances.at(to).write(new_balance); } // docs:end:increase_public_balance @@ -409,7 +409,7 @@ contract Token { // docs:start:is_minter unconstrained fn is_minter(minter: AztecAddress) -> pub bool { - storage.minters.at(minter.to_field()).read() + storage.minters.at(minter).read() } // docs:end:is_minter @@ -427,7 +427,7 @@ contract Token { // docs:start:balance_of_public unconstrained fn balance_of_public(owner: AztecAddress) -> pub u120 { - storage.public_balances.at(owner.to_field()).read().value + storage.public_balances.at(owner).read().value } // docs:end:balance_of_public diff --git a/yarn-project/noir-contracts/contracts/uniswap_contract/src/main.nr b/yarn-project/noir-contracts/contracts/uniswap_contract/src/main.nr index 7e9e230bbc9..68cec00fcb4 100644 --- a/yarn-project/noir-contracts/contracts/uniswap_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/uniswap_contract/src/main.nr @@ -32,7 +32,7 @@ contract Uniswap { struct Storage { // like with account contracts, stores the approval message on a slot and tracks if they are active - approved_action: Map>, + approved_action: Map>, // tracks the nonce used to create the approval message for burning funds // gets incremented each time after use to prevent replay attacks nonce_for_burn_approval: PublicState, diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/address.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/address.nr index 7c5cb9959c2..14706b5bd14 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/address.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/address.nr @@ -8,7 +8,7 @@ use crate::{ grumpkin_point::GrumpkinPoint, }; use dep::std::cmp::Eq; -use crate::traits::Empty; +use crate::traits::{Empty, ToField}; // Aztec address struct AztecAddress { @@ -29,6 +29,12 @@ impl Empty for AztecAddress { } } +impl ToField for AztecAddress { + fn to_field(self) -> Field { + self.inner + } +} + impl AztecAddress { pub fn zero() -> Self { Self { @@ -51,10 +57,6 @@ impl AztecAddress { ) } - pub fn to_field(self) -> Field { - self.inner - } - pub fn is_zero(self) -> bool { self.inner == 0 } @@ -99,6 +101,12 @@ impl Empty for EthAddress { } } +impl ToField for EthAddress { + fn to_field(self) -> Field { + self.inner + } +} + impl EthAddress{ pub fn zero() -> Self { Self { @@ -112,10 +120,6 @@ impl EthAddress{ } } - pub fn to_field(self) -> Field{ - self.inner - } - pub fn is_zero(self) -> bool { self.inner == 0 } @@ -147,6 +151,12 @@ struct PartialAddress { inner : Field } +impl ToField for PartialAddress { + fn to_field(self) -> Field { + self.inner + } +} + impl PartialAddress { pub fn from_field(field : Field) -> Self { Self { diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/lib.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/lib.nr index 61d39603638..1c3c82c15f1 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/lib.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/lib.nr @@ -11,13 +11,13 @@ mod constants; mod mocked; mod hash; +mod traits; mod header; mod interop_testing; mod tests; -mod traits; mod state_reference; mod partial_state_reference; diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/traits.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/traits.nr index cc428de504a..68ad8e56ff6 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/traits.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/traits.nr @@ -34,3 +34,13 @@ pub fn is_empty_array(array: [T; N]) -> bool where T: Empty + Eq { trait Hash { fn hash(self) -> Field; } + +trait ToField { + fn to_field(self) -> Field; +} + +impl ToField for Field { + fn to_field(self) -> Field { + self + } +}