From f36f5bf66db9e90abe5c4b1d4dcb7b1a61985ad4 Mon Sep 17 00:00:00 2001 From: dbanks12 Date: Wed, 25 Sep 2024 19:59:26 +0000 Subject: [PATCH] chore: change shared mutables to use poseidon --- noir-projects/aztec-nr/aztec/src/hash.nr | 8 ++++--- .../contracts/card_game_contract/src/main.nr | 6 ++--- .../easy_private_voting_contract/src/main.nr | 4 ++-- .../contracts/lending_contract/src/helpers.nr | 4 ++-- .../contracts/nft_contract/src/main.nr | 22 +++++++++---------- .../src/test/transfer_to_private.nr | 13 ++++++----- .../contracts/nft_contract/src/test/utils.nr | 8 +++---- .../contracts/test_contract/src/main.nr | 4 ++-- .../src/simulators/lending_simulator.ts | 4 ++-- 9 files changed, 38 insertions(+), 35 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/hash.nr b/noir-projects/aztec-nr/aztec/src/hash.nr index b96fec6e1a5..39e70b91771 100644 --- a/noir-projects/aztec-nr/aztec/src/hash.nr +++ b/noir-projects/aztec-nr/aztec/src/hash.nr @@ -1,12 +1,14 @@ use dep::protocol_types::{ address::{AztecAddress, EthAddress}, constants::{GENERATOR_INDEX__SECRET_HASH, GENERATOR_INDEX__MESSAGE_NULLIFIER, GENERATOR_INDEX__FUNCTION_ARGS}, - point::Point, traits::Hash, - hash::{sha256_to_field, poseidon2_hash_with_separator, poseidon2_hash_with_separator_slice} + point::Point, traits::Hash, hash::sha256_to_field }; use crate::utils::to_bytes::{arr_to_be_bytes_arr, str_to_be_bytes_arr}; -pub use dep::protocol_types::hash::{compute_siloed_nullifier, pedersen_hash}; +pub use dep::protocol_types::hash::{ + compute_siloed_nullifier, pedersen_hash, poseidon2_hash, poseidon2_hash_with_separator, + poseidon2_hash_with_separator_slice +}; pub fn pedersen_commitment(inputs: [Field; N], hash_index: u32) -> Point { std::hash::pedersen_commitment_with_separator(inputs, hash_index) diff --git a/noir-projects/noir-contracts/contracts/card_game_contract/src/main.nr b/noir-projects/noir-contracts/contracts/card_game_contract/src/main.nr index 095f870193b..73497221da4 100644 --- a/noir-projects/noir-contracts/contracts/card_game_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/card_game_contract/src/main.nr @@ -6,7 +6,7 @@ use dep::aztec::macros::aztec; #[aztec] contract CardGame { use dep::aztec::protocol_types::address::AztecAddress; - use dep::aztec::{hash::pedersen_hash, state_vars::{Map, PublicMutable}}; + use dep::aztec::{hash::poseidon2_hash, state_vars::{Map, PublicMutable}}; use dep::aztec::note::constants::MAX_NOTES_PER_PAGE; @@ -99,7 +99,7 @@ contract CardGame { let mut collection = storage.collections.at(player); let _inserted_cards = collection.add_cards(cards, player); - CardGame::at(context.this_address()).on_cards_claimed(game, player, pedersen_hash(cards_fields, 0)).enqueue(&mut context); + CardGame::at(context.this_address()).on_cards_claimed(game, player, poseidon2_hash(cards_fields)).enqueue(&mut context); } #[public] @@ -111,7 +111,7 @@ contract CardGame { assert(!game_data.claimed, "Already claimed"); game_data.claimed = true; - assert_eq(cards_hash, pedersen_hash(game_data.rounds_cards.map(|card: Card| card.to_field()), 0)); + assert_eq(cards_hash, poseidon2_hash(game_data.rounds_cards.map(|card: Card| card.to_field()))); let winner = game_data.winner(); assert(player.eq(winner.address), "Not the winner"); diff --git a/noir-projects/noir-contracts/contracts/easy_private_voting_contract/src/main.nr b/noir-projects/noir-contracts/contracts/easy_private_voting_contract/src/main.nr index 55539d9e4b5..1e641dd0c07 100644 --- a/noir-projects/noir-contracts/contracts/easy_private_voting_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/easy_private_voting_contract/src/main.nr @@ -5,7 +5,7 @@ contract EasyPrivateVoting { // docs:start:imports use dep::aztec::prelude::{AztecAddress, Map, PublicMutable, SharedImmutable}; use dep::aztec::{ - keys::getters::get_public_keys, + hash::poseidon2_hash, keys::getters::get_public_keys, macros::{storage::storage, functions::{public, initializer, private, internal}} }; // docs:end:imports @@ -35,7 +35,7 @@ contract EasyPrivateVoting { let msg_sender_npk_m_hash = get_public_keys(context.msg_sender()).npk_m.hash(); let secret = context.request_nsk_app(msg_sender_npk_m_hash); // get secret key of caller of function - let nullifier = std::hash::pedersen_hash([context.msg_sender().to_field(), secret]); // derive nullifier from sender and secret + let nullifier = poseidon2_hash([context.msg_sender().to_field(), secret]); // derive nullifier from sender and secret context.push_nullifier(nullifier); EasyPrivateVoting::at(context.this_address()).add_to_tally_public(candidate).enqueue(&mut context); } diff --git a/noir-projects/noir-contracts/contracts/lending_contract/src/helpers.nr b/noir-projects/noir-contracts/contracts/lending_contract/src/helpers.nr index c5e537e88ed..b7af91950be 100644 --- a/noir-projects/noir-contracts/contracts/lending_contract/src/helpers.nr +++ b/noir-projects/noir-contracts/contracts/lending_contract/src/helpers.nr @@ -1,4 +1,4 @@ -use dep::aztec::hash::pedersen_hash; +use dep::aztec::hash::poseidon2_hash; // Utility used to easily get a "id" for a private user that sits in the same // "space" as the public users. @@ -7,7 +7,7 @@ pub fn compute_identifier(secret: Field, on_behalf_of: Field, self: Field) -> Fi // EITHER secret OR on_behalf_of MUST be set. But not both assert(!((secret == 0) as bool & (on_behalf_of == 0) as bool)); if (secret != 0) { - pedersen_hash([self, secret], 0) + poseidon2_hash([self, secret]) } else { on_behalf_of } diff --git a/noir-projects/noir-contracts/contracts/nft_contract/src/main.nr b/noir-projects/noir-contracts/contracts/nft_contract/src/main.nr index cb72da92eec..93463b486cf 100644 --- a/noir-projects/noir-contracts/contracts/nft_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/nft_contract/src/main.nr @@ -10,17 +10,17 @@ contract NFT { use dep::compressed_string::FieldCompressedString; use dep::aztec::{ prelude::{NoteGetterOptions, NoteViewerOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress}, - encrypted_logs::{encrypted_note_emission::encode_and_encrypt_note}, hash::pedersen_hash, - keys::getters::get_public_keys, note::constants::MAX_NOTES_PER_PAGE, - protocol_types::traits::is_empty, utils::comparison::Comparator, - protocol_types::{point::Point, traits::Serialize}, + encrypted_logs::{encrypted_note_emission::encode_and_encrypt_note}, + hash::poseidon2_hash_with_separator, keys::getters::get_public_keys, + note::constants::MAX_NOTES_PER_PAGE, protocol_types::traits::is_empty, + utils::comparison::Comparator, protocol_types::{point::Point, traits::Serialize}, macros::{storage::storage, events::event, functions::{private, public, view, internal, initializer}} }; use dep::authwit::auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public, compute_authwit_nullifier}; use std::{embedded_curve_ops::EmbeddedCurvePoint, meta::derive}; use crate::types::nft_note::NFTNote; - global TRANSIENT_STORAGE_SLOT_PEDERSEN_INDEX = 3; + global TRANSIENT_STORAGE_SLOT_POSEIDON_INDEX: Field = 3; // TODO(#8467): Rename this to Transfer - calling this NFTTransfer to avoid export conflict with the Transfer event // in the Token contract. @@ -153,15 +153,15 @@ contract NFT { // We make the msg_sender/transfer_preparer part of the slot preimage to ensure he cannot interfere with // non-sender's slots - let transfer_preparer_storage_slot_commitment: Field = pedersen_hash( + let transfer_preparer_storage_slot_commitment: Field = poseidon2_hash_with_separator( [context.msg_sender().to_field(), transient_storage_slot_randomness], - TRANSIENT_STORAGE_SLOT_PEDERSEN_INDEX + TRANSIENT_STORAGE_SLOT_POSEIDON_INDEX ); // Then we hash the transfer preparer storage slot commitment with `from` and use that as the final slot // --> by hashing it with a `from` we ensure that `from` cannot interfere with slots not assigned to him. - let slot: Field = pedersen_hash( + let slot: Field = poseidon2_hash_with_separator( [from.to_field(), transfer_preparer_storage_slot_commitment], - TRANSIENT_STORAGE_SLOT_PEDERSEN_INDEX + TRANSIENT_STORAGE_SLOT_POSEIDON_INDEX ); NFT::at(context.this_address())._store_point_in_transient_storage(hiding_point.inner, slot).enqueue(&mut context); @@ -192,9 +192,9 @@ contract NFT { // Derive the slot from the transfer preparer storage slot commitment and the `from` address (declared // as `from` in this function) - let hiding_point_slot = pedersen_hash( + let hiding_point_slot = poseidon2_hash_with_separator( [from.to_field(), transfer_preparer_storage_slot_commitment], - TRANSIENT_STORAGE_SLOT_PEDERSEN_INDEX + TRANSIENT_STORAGE_SLOT_POSEIDON_INDEX ); // Read the hiding point from "transient" storage and check it's not empty to ensure the transfer was prepared diff --git a/noir-projects/noir-contracts/contracts/nft_contract/src/test/transfer_to_private.nr b/noir-projects/noir-contracts/contracts/nft_contract/src/test/transfer_to_private.nr index 51350ae3adf..b2cdae262ae 100644 --- a/noir-projects/noir-contracts/contracts/nft_contract/src/test/transfer_to_private.nr +++ b/noir-projects/noir-contracts/contracts/nft_contract/src/test/transfer_to_private.nr @@ -1,7 +1,8 @@ use crate::test::utils; use dep::aztec::{ - hash::pedersen_hash, keys::getters::get_public_keys, prelude::{AztecAddress, NoteHeader}, - oracle::unsafe_rand::unsafe_rand, protocol_types::storage::map::derive_storage_slot_in_map + hash::poseidon2_hash_with_separator, keys::getters::get_public_keys, + prelude::{AztecAddress, NoteHeader}, oracle::unsafe_rand::unsafe_rand, + protocol_types::storage::map::derive_storage_slot_in_map }; use crate::{types::nft_note::NFTNote, NFT}; @@ -28,9 +29,9 @@ unconstrained fn transfer_to_private_to_a_different_account() { let note_randomness = unsafe_rand(); let transient_storage_slot_randomness = unsafe_rand(); // Sender will be the msg_sender/transfer_preparer in prepare_transfer_to_private - let transfer_preparer_storage_slot_commitment = pedersen_hash( + let transfer_preparer_storage_slot_commitment = poseidon2_hash_with_separator( [sender.to_field(), transient_storage_slot_randomness], - NFT::TRANSIENT_STORAGE_SLOT_PEDERSEN_INDEX + NFT::TRANSIENT_STORAGE_SLOT_POSEIDON_INDEX ); // We prepare the transfer @@ -90,9 +91,9 @@ unconstrained fn transfer_to_private_finalizing_from_incorrect_sender() { let note_randomness = unsafe_rand(); let transient_storage_slot_randomness = unsafe_rand(); // Sender will be the msg_sender/transfer_preparer in prepare_transfer_to_private - let transfer_preparer_storage_slot_commitment = pedersen_hash( + let transfer_preparer_storage_slot_commitment = poseidon2_hash_with_separator( [correct_sender.to_field(), transient_storage_slot_randomness], - NFT::TRANSIENT_STORAGE_SLOT_PEDERSEN_INDEX + NFT::TRANSIENT_STORAGE_SLOT_POSEIDON_INDEX ); // We prepare the transfer diff --git a/noir-projects/noir-contracts/contracts/nft_contract/src/test/utils.nr b/noir-projects/noir-contracts/contracts/nft_contract/src/test/utils.nr index 7b95f3efda3..641a5e1753e 100644 --- a/noir-projects/noir-contracts/contracts/nft_contract/src/test/utils.nr +++ b/noir-projects/noir-contracts/contracts/nft_contract/src/test/utils.nr @@ -1,6 +1,6 @@ use dep::aztec::{ - hash::pedersen_hash, keys::getters::get_public_keys, prelude::{AztecAddress, NoteHeader}, - test::helpers::{cheatcodes, test_environment::TestEnvironment}, + hash::poseidon2_hash_with_separator, keys::getters::get_public_keys, + prelude::{AztecAddress, NoteHeader}, test::helpers::{cheatcodes, test_environment::TestEnvironment}, protocol_types::storage::map::derive_storage_slot_in_map, oracle::{execution::{get_block_number, get_contract_address}, unsafe_rand::unsafe_rand, storage::storage_read} }; @@ -52,9 +52,9 @@ unconstrained pub fn setup_mint_and_transfer_to_private(with_account_contracts: let note_randomness = unsafe_rand(); let transient_storage_slot_randomness = unsafe_rand(); - let transfer_preparer_storage_slot_commitment = pedersen_hash( + let transfer_preparer_storage_slot_commitment = poseidon2_hash_with_separator( [owner.to_field(), transient_storage_slot_randomness], - NFT::TRANSIENT_STORAGE_SLOT_PEDERSEN_INDEX + NFT::TRANSIENT_STORAGE_SLOT_POSEIDON_INDEX ); // We prepare the transfer with user being both the sender and the recipient (classical "shield" flow) diff --git a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr index e0df663feaa..023f91ad5b9 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr @@ -19,7 +19,7 @@ contract Test { use dep::aztec::keys::getters::get_public_keys; use dep::aztec::{ - hash::{pedersen_hash, compute_secret_hash, ArgsHasher}, keys::public_keys::IvpkM, + hash::{poseidon2_hash, compute_secret_hash, ArgsHasher}, keys::public_keys::IvpkM, note::{ lifecycle::{create_note, destroy_note_unsafe}, note_getter::{get_notes, view_notes}, note_getter_options::NoteStatus @@ -466,7 +466,7 @@ contract Test { } fn get_commitment(self) -> Field { - pedersen_hash([self.amount, self.secret_hash], 0) + poseidon2_hash([self.amount, self.secret_hash]) } } diff --git a/yarn-project/end-to-end/src/simulators/lending_simulator.ts b/yarn-project/end-to-end/src/simulators/lending_simulator.ts index 557677b5da4..baa694861a6 100644 --- a/yarn-project/end-to-end/src/simulators/lending_simulator.ts +++ b/yarn-project/end-to-end/src/simulators/lending_simulator.ts @@ -1,7 +1,7 @@ // Convenience struct to hold an account's address and secret that can easily be passed around. import { type AztecAddress, type CheatCodes, Fr } from '@aztec/aztec.js'; import { ETHEREUM_SLOT_DURATION } from '@aztec/circuits.js'; -import { pedersenHash } from '@aztec/foundation/crypto'; +import { poseidon2Hash } from '@aztec/foundation/crypto'; import { type RollupAbi } from '@aztec/l1-artifacts'; import { type LendingContract } from '@aztec/noir-contracts.js/Lending'; @@ -29,7 +29,7 @@ export class LendingAccount { * @returns Key in public space */ public key() { - return pedersenHash([this.address, this.secret]); + return poseidon2Hash([this.address, this.secret]); } }