Skip to content

Commit

Permalink
refactor: introducing re-export for poseidon2 (#5898)
Browse files Browse the repository at this point in the history
Fixes #5863
  • Loading branch information
benesjan authored Apr 22, 2024
1 parent 2b51fee commit 03a87b8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
4 changes: 1 addition & 3 deletions noir-projects/aztec-nr/aztec/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -21,6 +20,7 @@ contract KeyRegistry {
GENERATOR_INDEX__CONTRACT_ADDRESS_V1,
GENERATOR_INDEX__PUBLIC_KEYS_HASH
},
hash::poseidon2_hash,
traits::{
Serialize,
Deserialize,
Expand Down Expand Up @@ -86,17 +86,15 @@ contract KeyRegistry {
outgoing_public_key,
tagging_public_key,
GENERATOR_INDEX__PUBLIC_KEYS_HASH,
],
5
]
);

let computed_address = AztecAddress::from_field(
poseidon2_hash([
partial_address.to_field(),
public_keys_hash.to_field(),
GENERATOR_INDEX__CONTRACT_ADDRESS_V1 as Field,
],
3
]
)
);

Expand Down
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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 {
Expand Down Expand Up @@ -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."
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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]
)
)
}
Expand Down
4 changes: 4 additions & 0 deletions noir-projects/noir-protocol-circuits/crates/types/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ pub fn pedersen_hash<N>(inputs: [Field; N], hash_index: u32) -> Field {
dep::std::hash::pedersen_hash_with_separator(inputs, hash_index)
}

pub fn poseidon2_hash<N>(inputs: [Field; N]) -> Field {
dep::std::hash::poseidon2::Poseidon2::hash(inputs, N)
}

#[test]
fn smoke_sha256_to_field() {
let full_buffer = [
Expand Down

0 comments on commit 03a87b8

Please sign in to comment.