Skip to content

Commit

Permalink
chore: change shared mutables to use poseidon
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanks12 committed Oct 1, 2024
1 parent 36431d7 commit 171cda0
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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]
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
}
Expand Down
22 changes: 11 additions & 11 deletions noir-projects/noir-contracts/contracts/nft_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 3;

// TODO(#8467): Rename this to Transfer - calling this NFTTransfer to avoid export conflict with the Transfer event
// in the Token contract.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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}
};
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
}
}

Expand Down

0 comments on commit 171cda0

Please sign in to comment.