From 369f4850c5c9e52ed944b82ef5dab95acd1acc24 Mon Sep 17 00:00:00 2001 From: benesjan Date: Mon, 22 Apr 2024 15:25:47 +0000 Subject: [PATCH] secret_hash funcs cleanup --- noir-projects/aztec-nr/aztec/src/context/avm_context.nr | 4 ++-- noir-projects/aztec-nr/aztec/src/hash.nr | 9 ++++++--- noir-projects/aztec-nr/aztec/src/messaging.nr | 4 ++-- .../contracts/gas_token_contract/src/main.nr | 2 +- .../noir-contracts/contracts/test_contract/src/main.nr | 4 ++-- .../contracts/token_blacklist_contract/src/main.nr | 4 ++-- .../contracts/token_bridge_contract/src/main.nr | 2 -- .../noir-contracts/contracts/token_contract/src/main.nr | 4 ++-- .../noir-protocol-circuits/crates/types/src/constants.nr | 1 + yarn-project/circuits.js/src/constants.gen.ts | 1 + 10 files changed, 19 insertions(+), 16 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/context/avm_context.nr b/noir-projects/aztec-nr/aztec/src/context/avm_context.nr index 1df9863af656..a4e3e8fc08e8 100644 --- a/noir-projects/aztec-nr/aztec/src/context/avm_context.nr +++ b/noir-projects/aztec-nr/aztec/src/context/avm_context.nr @@ -1,4 +1,4 @@ -use crate::hash::{compute_secret_hash, compute_message_hash, compute_message_nullifier}; +use crate::hash::{compute_message_secret_hash, compute_message_hash, compute_message_nullifier}; use dep::protocol_types::{ address::{AztecAddress, EthAddress}, constants::{L1_TO_L2_MESSAGE_LENGTH, NESTED_CALL_L2_GAS_BUFFER}, header::Header @@ -96,7 +96,7 @@ impl PublicContextInterface for AvmContext { } fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress, leaf_index: Field) { - let secret_hash = compute_secret_hash(secret); + let secret_hash = compute_message_secret_hash(secret); let message_hash = compute_message_hash( sender, self.chain_id(), diff --git a/noir-projects/aztec-nr/aztec/src/hash.nr b/noir-projects/aztec-nr/aztec/src/hash.nr index 4b12c21ea756..ecd818327640 100644 --- a/noir-projects/aztec-nr/aztec/src/hash.nr +++ b/noir-projects/aztec-nr/aztec/src/hash.nr @@ -2,16 +2,19 @@ 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__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH, GENERATOR_INDEX__NOTE_SECRET_HASH }, traits::Hash, hash::{pedersen_hash, poseidon2_hash, silo_nullifier, sha256_to_field} }; -pub fn compute_secret_hash(secret: Field) -> Field { - // TODO(#1205) This is probably not the right index to use +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/aztec-nr/aztec/src/messaging.nr b/noir-projects/aztec-nr/aztec/src/messaging.nr index 77087758f824..c4b197060eb5 100644 --- a/noir-projects/aztec-nr/aztec/src/messaging.nr +++ b/noir-projects/aztec-nr/aztec/src/messaging.nr @@ -1,5 +1,5 @@ use crate::{ - hash::{compute_secret_hash, compute_message_hash, compute_message_nullifier}, + hash::{compute_message_secret_hash, compute_message_hash, compute_message_nullifier}, oracle::get_l1_to_l2_membership_witness::get_l1_to_l2_membership_witness }; @@ -15,7 +15,7 @@ pub fn process_l1_to_l2_message( content: Field, secret: Field ) -> Field { - let secret_hash = compute_secret_hash(secret); + let secret_hash = compute_message_secret_hash(secret); let message_hash = compute_message_hash( portal_contract_address, chain_id, diff --git a/noir-projects/noir-contracts/contracts/gas_token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/gas_token_contract/src/main.nr index ec1adf321160..ec393e026cd7 100644 --- a/noir-projects/noir-contracts/contracts/gas_token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/gas_token_contract/src/main.nr @@ -2,7 +2,7 @@ mod lib; contract GasToken { use dep::aztec::protocol_types::{abis::function_selector::FunctionSelector, address::{AztecAddress, EthAddress}}; - use dep::aztec::{hash::compute_secret_hash, state_vars::{PublicMutable, Map}}; + use dep::aztec::state_vars::{PublicMutable, Map}; use crate::lib::{calculate_fee, get_bridge_gas_msg_hash}; 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 43a0d80c5289..a5042373102f 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_secret_hash, ArgsHasher}, + hash::{pedersen_hash, compute_note_secret_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_secret_hash(secret); + let secret_hash = compute_note_secret_hash(secret); 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 ddb115c721ef..c2816411e317 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 @@ -16,7 +16,7 @@ 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_secret_hash, state_vars::{Map, PublicMutable, PrivateSet, SharedImmutable} + hash::compute_note_secret_hash, state_vars::{Map, PublicMutable, PrivateSet, SharedImmutable} }; use dep::field_note::field_note::FieldNote; @@ -193,7 +193,7 @@ contract TokenBlacklist { assert(!to_roles.is_blacklisted, "Blacklisted: Recipient"); let pending_shields = storage.pending_shields; - let secret_hash = compute_secret_hash(secret); + let secret_hash = compute_note_secret_hash(secret); // 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_bridge_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_bridge_contract/src/main.nr index 28226b5b10b1..9149b722de60 100644 --- a/noir-projects/noir-contracts/contracts/token_bridge_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_bridge_contract/src/main.nr @@ -8,8 +8,6 @@ contract TokenBridge { use dep::aztec::prelude::{FunctionSelector, AztecAddress, EthAddress, PublicMutable}; - use dep::aztec::{context::Context, hash::compute_secret_hash}; - use dep::token_portal_content_hash_lib::{get_mint_public_content_hash, get_mint_private_content_hash, get_withdraw_content_hash}; use dep::token::Token; 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 cc892421ece9..b98b96bec8cf 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr @@ -15,7 +15,7 @@ contract Token { use dep::compressed_string::FieldCompressedString; use dep::aztec::prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress}; - use dep::aztec::hash::compute_secret_hash; + use dep::aztec::hash::compute_note_secret_hash; // docs:start:import_authwit use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}}; @@ -239,7 +239,7 @@ contract Token { #[aztec(private)] fn redeem_shield(to: AztecAddress, amount: Field, secret: Field) { let pending_shields = storage.pending_shields; - let secret_hash = compute_secret_hash(secret); + let secret_hash = compute_note_secret_hash(secret); // 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-protocol-circuits/crates/types/src/constants.nr b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr index 4e8887b4de35..0c767b63c6f8 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -243,3 +243,4 @@ 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/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index f36019aff279..1bfb7b509d99 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -203,4 +203,5 @@ export enum GeneratorIndex { TSK_M = 50, PUBLIC_KEYS_HASH = 51, NOTE_NULLIFIER = 52, + NOTE_SECRET_HASH = 53, }