From 2483e409864732f7b41d2ed9ddd546bf80f19f14 Mon Sep 17 00:00:00 2001 From: benesjan Date: Mon, 22 Apr 2024 08:10:55 +0000 Subject: [PATCH 1/3] refactor: introducing re-export for poseidon2 --- .../contracts/key_registry_contract/src/main.nr | 8 +++----- .../src/private_validation_request_processor.nr | 11 ++++++----- .../crates/types/src/address/aztec_address.nr | 5 ++--- .../noir-protocol-circuits/crates/types/src/hash.nr | 6 +++++- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/noir-projects/noir-contracts/contracts/key_registry_contract/src/main.nr b/noir-projects/noir-contracts/contracts/key_registry_contract/src/main.nr index 8f2810f3dbf..d4ed6addd03 100644 --- a/noir-projects/noir-contracts/contracts/key_registry_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/key_registry_contract/src/main.nr @@ -1,5 +1,4 @@ contract KeyRegistry { - use dep::std::hash::poseidon2::Poseidon2::hash as poseidon2_hash; use dep::authwit::auth::assert_current_call_valid_authwit_public; use dep::aztec::{ @@ -21,6 +20,7 @@ contract KeyRegistry { GENERATOR_INDEX__CONTRACT_ADDRESS_V1, GENERATOR_INDEX__PUBLIC_KEYS_HASH }, + hash::poseidon2_hash, traits::{ Serialize, Deserialize, @@ -86,8 +86,7 @@ contract KeyRegistry { outgoing_public_key, tagging_public_key, GENERATOR_INDEX__PUBLIC_KEYS_HASH, - ], - 5 + ] ); let computed_address = AztecAddress::from_field( @@ -95,8 +94,7 @@ contract KeyRegistry { partial_address.to_field(), public_keys_hash.to_field(), GENERATOR_INDEX__CONTRACT_ADDRESS_V1 as Field, - ], - 3 + ] ) ); diff --git a/noir-projects/noir-protocol-circuits/crates/reset-kernel-lib/src/private_validation_request_processor.nr b/noir-projects/noir-protocol-circuits/crates/reset-kernel-lib/src/private_validation_request_processor.nr index 0f82c12f557..7fc2e3929d7 100644 --- a/noir-projects/noir-protocol-circuits/crates/reset-kernel-lib/src/private_validation_request_processor.nr +++ b/noir-projects/noir-protocol-circuits/crates/reset-kernel-lib/src/private_validation_request_processor.nr @@ -1,4 +1,3 @@ -use dep::std::hash::poseidon2::Poseidon2::hash as poseidon2_hash; use crate::{nullifier_read_request_reset::NullifierReadRequestHints, reset::read_request::reset_read_requests}; use dep::types::{ abis::{side_effect::{SideEffect, SideEffectLinkedToNoteHash}, validation_requests::ValidationRequests}, @@ -7,7 +6,7 @@ use dep::types::{ MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_TX, GENERATOR_INDEX__NSK_M }, grumpkin_private_key::GrumpkinPrivateKey, keys::compute_siloed_nullifier_secret_key, - traits::is_empty + hash::poseidon2_hash, traits::is_empty }; struct PrivateValidationRequestProcessor { @@ -70,10 +69,12 @@ impl PrivateValidationRequestProcessor { ); // Then we check that siloing the master secret key with the contract address gives the app nullifier secret key - let app_nullifier_secret_key_preimage = [ + + let app_nullifier_secret_key = poseidon2_hash( + [ master_nullifier_secret_key.high, master_nullifier_secret_key.low, request.contract_address.to_field(), GENERATOR_INDEX__NSK_M - ]; - let app_nullifier_secret_key = poseidon2_hash(app_nullifier_secret_key_preimage, app_nullifier_secret_key_preimage.len()); + ] + ); assert( app_nullifier_secret_key.eq(request.app_nullifier_secret_key), "Failed to derive matching app nullifier secret key from the secret key." ); diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/address/aztec_address.nr b/noir-projects/noir-protocol-circuits/crates/types/src/address/aztec_address.nr index c96342721a6..fcbae4871d9 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/address/aztec_address.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/address/aztec_address.nr @@ -1,10 +1,9 @@ use crate::{ crate::address::{eth_address::EthAddress, partial_address::PartialAddress, public_keys_hash::PublicKeysHash}, constants::{AZTEC_ADDRESS_LENGTH, GENERATOR_INDEX__CONTRACT_ADDRESS_V1}, - contract_class_id::ContractClassId, hash::pedersen_hash, grumpkin_point::GrumpkinPoint, + contract_class_id::ContractClassId, hash::poseidon2_hash, grumpkin_point::GrumpkinPoint, traits::{Empty, FromField, ToField, Serialize, Deserialize}, utils }; -use dep::std::hash::poseidon2::Poseidon2::hash as poseidon2_hash; // Aztec address struct AztecAddress { @@ -57,7 +56,7 @@ impl AztecAddress { pub fn compute(pub_keys_hash: PublicKeysHash, partial_address: PartialAddress) -> AztecAddress { AztecAddress::from_field( poseidon2_hash( - [pub_keys_hash.to_field(), partial_address.to_field(), GENERATOR_INDEX__CONTRACT_ADDRESS_V1], 3 + [pub_keys_hash.to_field(), partial_address.to_field(), GENERATOR_INDEX__CONTRACT_ADDRESS_V1] ) ) } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr b/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr index f43ee8f9b5c..1adddd4934c 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr @@ -13,7 +13,7 @@ use crate::constants::{ use crate::traits::Hash; use crate::messaging::l2_to_l1_message::L2ToL1Message; use crate::merkle_tree::root::root_from_sibling_path; -use dep::std::hash::{pedersen_hash_with_separator, sha256}; +use dep::std::hash::{pedersen_hash_with_separator, poseidon2::Poseidon2::hash, sha256}; pub fn sha256_to_field(bytes_to_hash: [u8; N]) -> Field { let sha256_hashed = sha256(bytes_to_hash); @@ -171,6 +171,10 @@ pub fn pedersen_hash(inputs: [Field; N], hash_index: u32) -> Field { dep::std::hash::pedersen_hash_with_separator(inputs, hash_index) } +pub fn poseidon2_hash(inputs: [Field; N]) -> Field { + dep::std::hash::poseidon2::Poseidon2::hash(inputs, N) +} + #[test] fn smoke_sha256_to_field() { let full_buffer = [ From 10f08247e274cdde21460ce2c10fbe815332b00d Mon Sep 17 00:00:00 2001 From: benesjan Date: Mon, 22 Apr 2024 08:17:44 +0000 Subject: [PATCH 2/3] cleanup --- noir-projects/noir-protocol-circuits/crates/types/src/hash.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr b/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr index 1adddd4934c..ef7bc7f62bd 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr @@ -13,7 +13,7 @@ use crate::constants::{ use crate::traits::Hash; use crate::messaging::l2_to_l1_message::L2ToL1Message; use crate::merkle_tree::root::root_from_sibling_path; -use dep::std::hash::{pedersen_hash_with_separator, poseidon2::Poseidon2::hash, sha256}; +use dep::std::hash::{pedersen_hash_with_separator, sha256}; pub fn sha256_to_field(bytes_to_hash: [u8; N]) -> Field { let sha256_hashed = sha256(bytes_to_hash); From 45c4b437caff8d9af1f7a2f571f144ebbfcad5ae Mon Sep 17 00:00:00 2001 From: benesjan Date: Mon, 22 Apr 2024 10:48:21 +0000 Subject: [PATCH 3/3] adding re-export to aztec-nr --- noir-projects/aztec-nr/aztec/src/hash.nr | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/hash.nr b/noir-projects/aztec-nr/aztec/src/hash.nr index f6980f78c2c..8406cf8c298 100644 --- a/noir-projects/aztec-nr/aztec/src/hash.nr +++ b/noir-projects/aztec-nr/aztec/src/hash.nr @@ -4,11 +4,9 @@ use dep::protocol_types::{ GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, GENERATOR_INDEX__NULLIFIER, ARGS_HASH_CHUNK_COUNT, GENERATOR_INDEX__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH }, - traits::Hash, hash::{pedersen_hash, silo_nullifier} + traits::Hash, hash::{pedersen_hash, poseidon2_hash, silo_nullifier, sha256_to_field} }; -use dep::protocol_types::hash::sha256_to_field; - pub fn compute_secret_hash(secret: Field) -> Field { // TODO(#1205) This is probably not the right index to use pedersen_hash([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET)