diff --git a/noir-projects/aztec-nr/aztec/src/hash.nr b/noir-projects/aztec-nr/aztec/src/hash.nr index ecd818327640..bfeb3ff53928 100644 --- a/noir-projects/aztec-nr/aztec/src/hash.nr +++ b/noir-projects/aztec-nr/aztec/src/hash.nr @@ -2,7 +2,7 @@ use dep::protocol_types::{ address::{AztecAddress, EthAddress}, constants::{ GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, GENERATOR_INDEX__MESSAGE_NULLIFIER, ARGS_HASH_CHUNK_COUNT, - GENERATOR_INDEX__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH, GENERATOR_INDEX__NOTE_SECRET_HASH + GENERATOR_INDEX__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH }, traits::Hash, hash::{pedersen_hash, poseidon2_hash, silo_nullifier, sha256_to_field} }; @@ -11,10 +11,6 @@ pub fn compute_message_secret_hash(secret: Field) -> Field { pedersen_hash([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET) } -pub fn compute_note_secret_hash(secret: Field) -> Field { - pedersen_hash([secret], GENERATOR_INDEX__NOTE_SECRET_HASH) -} - pub fn compute_message_hash( sender: EthAddress, chain_id: Field, 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 a5042373102f..67db3324c38e 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr @@ -16,7 +16,7 @@ contract Test { use dep::aztec::{ context::{Context, inputs::private_context_inputs::PrivateContextInputs}, - hash::{pedersen_hash, compute_note_secret_hash, ArgsHasher}, + hash::{pedersen_hash, poseidon2_hash, ArgsHasher}, note::{ lifecycle::{create_note, destroy_note}, note_getter::{get_notes, view_notes}, note_getter_options::NoteStatus @@ -364,7 +364,7 @@ contract Test { // Adapted from TokenContract#redeem_shield but without an initcheck so it can be run in simulator/src/client/private_execution.test.ts fn consume_note_from_secret(secret: Field) { let notes_set = storage.example_set; - let secret_hash = compute_note_secret_hash(secret); + let secret_hash = poseidon2_hash([secret, 92543]); // global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543; let mut options = NoteGetterOptions::new(); options = options.select(FieldNote::properties().value, secret_hash, Option::none()).set_limit(1); let notes = notes_set.get_notes(options); diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr index c2816411e317..b9b0ec788d71 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr @@ -15,15 +15,18 @@ contract TokenBlacklist { use dep::aztec::protocol_types::{abis::function_selector::FunctionSelector, address::AztecAddress}; use dep::aztec::{ - note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader}, - hash::compute_note_secret_hash, state_vars::{Map, PublicMutable, PrivateSet, SharedImmutable} + note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader}, hash::poseidon2_hash, + state_vars::{Map, PublicMutable, PrivateSet, SharedImmutable} }; use dep::field_note::field_note::FieldNote; use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}}; - use crate::types::{transparent_note::TransparentNote, token_note::TokenNote, balances_map::BalancesMap, roles::UserFlags}; + use crate::types::{ + transparent_note::{TransparentNote, GENERATOR_INDEX__TRANSPARENT_NOTE}, token_note::TokenNote, + balances_map::BalancesMap, roles::UserFlags + }; // docs:start:interface use dep::slow_tree::SlowTree; // docs:end:interface @@ -193,7 +196,7 @@ contract TokenBlacklist { assert(!to_roles.is_blacklisted, "Blacklisted: Recipient"); let pending_shields = storage.pending_shields; - let secret_hash = compute_note_secret_hash(secret); + let secret_hash = poseidon2_hash([secret, GENERATOR_INDEX__TRANSPARENT_NOTE]); // Get 1 note (set_limit(1)) which has amount stored in field with index 0 (select(0, amount)) and secret_hash // stored in field with index 1 (select(1, secret_hash)). let mut options = NoteGetterOptions::new(); diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr index d5cf7197cef6..6f06d084a514 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr @@ -6,6 +6,8 @@ use dep::aztec::{ }; global TRANSPARENT_NOTE_LEN: Field = 2; +// Defined here as it's not a protocol constant. Copied over to private execution test. +global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543; // Transparent note represents a note that is created in the clear (public execution), but can only be spent by those // that know the preimage of the "secret_hash" (the secret). This is typically used when shielding a token balance. 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 b98b96bec8cf..7c4087a69863 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr @@ -14,14 +14,19 @@ contract Token { use dep::compressed_string::FieldCompressedString; - use dep::aztec::prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress}; - use dep::aztec::hash::compute_note_secret_hash; + use dep::aztec::{ + hash::poseidon2_hash, + prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress} + }; // docs:start:import_authwit use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}}; // docs:end:import_authwit - use crate::types::{transparent_note::TransparentNote, token_note::{TokenNote, TOKEN_NOTE_LEN}, balances_map::BalancesMap}; + use crate::types::{ + transparent_note::{TransparentNote, GENERATOR_INDEX__TRANSPARENT_NOTE}, + token_note::{TokenNote, TOKEN_NOTE_LEN}, balances_map::BalancesMap + }; // docs:end::imports // docs:start:storage_struct @@ -239,7 +244,7 @@ contract Token { #[aztec(private)] fn redeem_shield(to: AztecAddress, amount: Field, secret: Field) { let pending_shields = storage.pending_shields; - let secret_hash = compute_note_secret_hash(secret); + let secret_hash = poseidon2_hash([secret, GENERATOR_INDEX__TRANSPARENT_NOTE]); // Get 1 note (set_limit(1)) which has amount stored in field with index 0 (select(0, amount)) and secret_hash // stored in field with index 1 (select(1, secret_hash)). let mut options = NoteGetterOptions::new(); diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr index d5cf7197cef6..19b5025eacde 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr @@ -6,6 +6,8 @@ use dep::aztec::{ }; global TRANSPARENT_NOTE_LEN: Field = 2; +// Defined here as it's not a protocol constant. Copied over to private execution test and test contract. +global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543; // Transparent note represents a note that is created in the clear (public execution), but can only be spent by those // that know the preimage of the "secret_hash" (the secret). This is typically used when shielding a token balance. diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr index 0c767b63c6f8..4e8887b4de35 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -243,4 +243,3 @@ global GENERATOR_INDEX__OVSK_M = 49; global GENERATOR_INDEX__TSK_M = 50; global GENERATOR_INDEX__PUBLIC_KEYS_HASH = 51; global GENERATOR_INDEX__NOTE_NULLIFIER = 52; -global GENERATOR_INDEX__NOTE_SECRET_HASH = 53; diff --git a/yarn-project/circuits.js/src/hash/hash.ts b/yarn-project/circuits.js/src/hash/hash.ts index f06cbdbec561..a011300861e6 100644 --- a/yarn-project/circuits.js/src/hash/hash.ts +++ b/yarn-project/circuits.js/src/hash/hash.ts @@ -157,12 +157,12 @@ export function computeNullifierHash(input: SideEffectLinkedToNoteHash) { } /** - * Given a secret, it computes its pedersen hash - used to send l1 to l2 messages - * @param secret - the secret to hash - secret could be generated however you want e.g. `Fr.random()` - * @returns the hash + * Computes a hash of a secret as is done in the L1 to L2 message flow. + * @param secret - The secret to hash (could be generated however you want e.g. `Fr.random()`) + * @returns The hash */ -export function computeMessageSecretHash(secretMessage: Fr) { - return pedersenHash([secretMessage], GeneratorIndex.L1_TO_L2_MESSAGE_SECRET); +export function computeMessageSecretHash(secret: Fr) { + return pedersenHash([secret], GeneratorIndex.L1_TO_L2_MESSAGE_SECRET); } export function computeL1ToL2MessageNullifier( diff --git a/yarn-project/simulator/src/client/private_execution.test.ts b/yarn-project/simulator/src/client/private_execution.test.ts index 370416c79c80..82d471f65406 100644 --- a/yarn-project/simulator/src/client/private_execution.test.ts +++ b/yarn-project/simulator/src/client/private_execution.test.ts @@ -21,7 +21,7 @@ import { nonEmptySideEffects, sideEffectArrayToValueArray, } from '@aztec/circuits.js'; -import { computeCommitmentNonce, computeMessageSecretHash, computeVarArgsHash } from '@aztec/circuits.js/hash'; +import { computeCommitmentNonce, computeVarArgsHash } from '@aztec/circuits.js/hash'; import { makeHeader } from '@aztec/circuits.js/testing'; import { type FunctionArtifact, @@ -62,6 +62,9 @@ import { AcirSimulator } from './simulator.js'; jest.setTimeout(60_000); +// Copied over from `transparent_note.nr` - not placed in constants.nr as it's not a protocol constant +const GENERATOR_INDEX__TRANSPARENT_NOTE = 92543; + describe('Private Execution test suite', () => { let oracle: MockProxy; let node: MockProxy; @@ -738,7 +741,7 @@ describe('Private Execution test suite', () => { it('Should be able to consume a dummy public to private message', async () => { const artifact = getFunctionArtifact(TestContractArtifact, 'consume_note_from_secret'); const secret = new Fr(1n); - const secretHash = computeMessageSecretHash(secret); + const secretHash = poseidon2Hash([secret, GENERATOR_INDEX__TRANSPARENT_NOTE]); const note = new Note([secretHash]); const storageSlot = new Fr(5); oracle.getNotes.mockResolvedValue([