Skip to content

Commit

Permalink
Merge 281519e into a0720ff
Browse files Browse the repository at this point in the history
  • Loading branch information
sirasistant authored Apr 1, 2024
2 parents a0720ff + 281519e commit b24266c
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 109 deletions.
10 changes: 3 additions & 7 deletions noir-projects/aztec-nr/aztec/src/hash.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use dep::protocol_types::{
address::{AztecAddress, EthAddress},
constants::{GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, GENERATOR_INDEX__NULLIFIER},
hash::{pedersen_hash, silo_nullifier}
address::{AztecAddress, EthAddress}, constants::GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET,
hash::{pedersen_hash, poseidon_hash, silo_nullifier}
};

use dep::protocol_types::hash::{hash_args, hash_args_array, sha256_to_field};
Expand Down Expand Up @@ -42,10 +41,7 @@ pub fn compute_message_hash(
// The nullifier of a l1 to l2 message is the hash of the message salted with the secret and index of the message hash
// in the L1 to L2 message tree
pub fn compute_message_nullifier(message_hash: Field, secret: Field, leaf_index: Field) -> Field {
pedersen_hash(
[message_hash, secret, leaf_index],
GENERATOR_INDEX__NULLIFIER
)
poseidon_hash([message_hash, secret, leaf_index])
}

pub fn compute_siloed_nullifier(address: AztecAddress, nullifier: Field) -> Field {
Expand Down
13 changes: 5 additions & 8 deletions noir-projects/aztec-nr/aztec/src/note/utils.nr
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use crate::{context::PrivateContext, note::{note_header::NoteHeader, note_interface::NoteInterface}};

use dep::protocol_types::{
address::AztecAddress,
constants::{GENERATOR_INDEX__OUTER_NULLIFIER, GENERATOR_INDEX__UNIQUE_NOTE_HASH, GENERATOR_INDEX__SILOED_NOTE_HASH},
hash::pedersen_hash, utils::arr_copy_slice
address::AztecAddress, hash::{pedersen_hash, poseidon_hash, silo_note_hash, silo_nullifier},
utils::arr_copy_slice
};

fn compute_siloed_hash(contract_address: AztecAddress, inner_note_hash: Field) -> Field {
let inputs = [contract_address.to_field(), inner_note_hash];
pedersen_hash(inputs, GENERATOR_INDEX__SILOED_NOTE_HASH)
silo_note_hash(contract_address, inner_note_hash)
}

fn compute_unique_hash(nonce: Field, siloed_note_hash: Field) -> Field {
let inputs = [nonce, siloed_note_hash];
pedersen_hash(inputs, GENERATOR_INDEX__UNIQUE_NOTE_HASH)
poseidon_hash(inputs)
}

fn compute_inner_note_hash<Note, N>(note: Note) -> Field where Note: NoteInterface<N> {
Expand Down Expand Up @@ -47,8 +45,7 @@ pub fn compute_siloed_nullifier<Note, N>(
let header = note_with_header.get_header();
let inner_nullifier = note_with_header.compute_nullifier(context);

let input = [header.contract_address.to_field(), inner_nullifier];
pedersen_hash(input, GENERATOR_INDEX__OUTER_NULLIFIER)
silo_nullifier(header.contract_address, inner_nullifier)
}

pub fn compute_note_hash_for_insertion<Note, N>(note: Note) -> Field where Note: NoteInterface<N> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::protocol_types::{address::AztecAddress, constants::GENERATOR_INDEX__INITIALIZATION_NULLIFIER, hash::pedersen_hash};
use dep::protocol_types::{address::AztecAddress, hash::poseidon_hash};

use crate::context::{PrivateContext, Context};
use crate::note::{
Expand Down Expand Up @@ -32,10 +32,7 @@ impl<Note> PrivateImmutable<Note> {
// This is especially dangerous for initial assignment to elements of a `Map<AztecAddress, PrivateImmutable>` type (for example), because the storage slot often also identifies an actor.
// e.g. the initial assignment to `my_map.at(msg.sender)` will leak: `msg.sender`, the fact that an element of `my_map` was assigned-to for the first time, and the contract_address.
pub fn compute_initialization_nullifier(self) -> Field {
pedersen_hash(
[self.storage_slot],
GENERATOR_INDEX__INITIALIZATION_NULLIFIER
)
poseidon_hash([self.storage_slot])
}

// docs:start:is_initialized
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dep::protocol_types::{address::AztecAddress, constants::GENERATOR_INDEX__INITIALIZATION_NULLIFIER, hash::pedersen_hash};
use dep::protocol_types::{address::AztecAddress, hash::poseidon_hash};

use crate::context::{PrivateContext, PublicContext, Context};
use crate::note::{
Expand Down Expand Up @@ -34,10 +34,7 @@ impl<Note> PrivateMutable<Note> {
// Note: subsequent nullification of this state variable, via the `replace` method will not be leaky, if the `compute_nullifier()` method of the underlying note is designed to ensure privacy.
// For example, if the `compute_nullifier()` method injects the secret key of a note owner into the computed nullifier's preimage.
pub fn compute_initialization_nullifier(self) -> Field {
pedersen_hash(
[self.storage_slot],
GENERATOR_INDEX__INITIALIZATION_NULLIFIER
)
poseidon_hash([self.storage_slot])
}

// docs:start:is_initialized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use dep::types::{
},
merkle_tree::MembershipWitness,
tests::{merkle_tree_utils::NonEmptyMerkleTree, sort::sort_get_sorted_hints},
utils::{arrays::find_index, field::full_field_greater_than}
utils::{arrays::{find_index, array_to_bounded_vec}, field::full_field_greater_than}
};
use dep::std::unsafe;

Expand Down Expand Up @@ -62,11 +62,13 @@ impl NullifierNonExistentReadRequestHintsBuilder {
let sorted_pending_values = sorted_result.sorted_array;
let sorted_pending_value_index_hints = sorted_result.sorted_index_hints;

let nullifiers_bounded_vec = array_to_bounded_vec(self.pending_nullifiers);

let mut next_pending_value_indices = [0; MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_TX];
for i in 0..MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_TX {
if i < self.read_values.len() {
let value = self.read_values.get_unchecked(i);
next_pending_value_indices[i] = find_index(sorted_pending_values, |v: SideEffectLinkedToNoteHash| !v.value.lt(value));
next_pending_value_indices[i] = find_index(sorted_pending_values, |v: SideEffectLinkedToNoteHash| !v.value.lt(value)).unwrap_or(nullifiers_bounded_vec.len());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
abis::function_selector::FunctionSelector,
constants::{GENERATOR_INDEX__FUNCTION_DATA, FUNCTION_DATA_LENGTH}, hash::pedersen_hash,
constants::{FUNCTION_DATA_LENGTH}, hash::poseidon_hash,
traits::{Serialize, Hash, Deserialize}
};

Expand Down Expand Up @@ -44,7 +44,7 @@ impl Deserialize<FUNCTION_DATA_LENGTH> for FunctionData {

impl Hash for FunctionData {
fn hash(self) -> Field {
pedersen_hash(self.serialize(), GENERATOR_INDEX__FUNCTION_DATA)
poseidon_hash(self.serialize())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::constants::GENERATOR_INDEX__PUBLIC_DATA_READ;
use dep::std::cmp::Eq;
use crate::traits::{Empty, Hash};

Expand All @@ -24,10 +23,10 @@ impl Empty for PublicDataRead {

impl Hash for PublicDataRead {
fn hash(self) -> Field {
dep::std::hash::pedersen_hash_with_separator([
crate::hash::poseidon_hash([
self.leaf_slot,
self.value,
], GENERATOR_INDEX__PUBLIC_DATA_READ)
])
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::constants::GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST;
use dep::std::cmp::Eq;
use crate::traits::{Empty, Hash};

Expand All @@ -25,10 +24,10 @@ impl Empty for PublicDataUpdateRequest {

impl Hash for PublicDataUpdateRequest {
fn hash(self) -> Field {
dep::std::hash::pedersen_hash_with_separator([
crate::hash::poseidon_hash([
self.leaf_slot,
self.new_value
], GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST)
])
}
}

Expand Down
10 changes: 0 additions & 10 deletions noir-projects/noir-protocol-circuits/crates/types/src/constants.nr
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,6 @@ global NUM_BASE_PARITY_PER_ROOT_PARITY: u64 = 4;
* Note: When modifying, modify `GeneratorIndexPacker` in packer.hpp accordingly.
*/
// Indices with size ≤ 8
global GENERATOR_INDEX__NOTE_HASH = 1;
global GENERATOR_INDEX__NOTE_HASH_NONCE = 2;
global GENERATOR_INDEX__UNIQUE_NOTE_HASH = 3;
global GENERATOR_INDEX__SILOED_NOTE_HASH = 4;
global GENERATOR_INDEX__NULLIFIER = 5;
global GENERATOR_INDEX__INITIALIZATION_NULLIFIER = 6;
global GENERATOR_INDEX__OUTER_NULLIFIER = 7;
global GENERATOR_INDEX__PUBLIC_DATA_READ = 8;
global GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST = 9;
global GENERATOR_INDEX__FUNCTION_DATA = 10;
global GENERATOR_INDEX__FUNCTION_LEAF = 11;
global GENERATOR_INDEX__CONTRACT_DEPLOYMENT_DATA = 12;
global GENERATOR_INDEX__CONSTRUCTOR = 13;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
constants::{CONTRACT_STORAGE_READ_LENGTH, GENERATOR_INDEX__PUBLIC_DATA_READ}, hash::pedersen_hash,
constants::CONTRACT_STORAGE_READ_LENGTH, hash::poseidon_hash,
traits::{Deserialize, Hash, Empty, Serialize}
};

Expand All @@ -25,7 +25,7 @@ impl Empty for StorageRead {

impl Hash for StorageRead {
fn hash(self) -> Field {
pedersen_hash(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_READ)
poseidon_hash(self.serialize())
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
constants::{CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH, GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST},
hash::pedersen_hash, traits::{Deserialize, Hash, Empty, Serialize}
constants::CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH, hash::poseidon_hash,
traits::{Deserialize, Hash, Empty, Serialize}
};
use dep::std::cmp::Eq;

Expand All @@ -27,7 +27,7 @@ impl Empty for StorageUpdateRequest {

impl Hash for StorageUpdateRequest {
fn hash(self) -> Field {
pedersen_hash(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST)
poseidon_hash(self.serialize())
}
}

Expand Down
43 changes: 11 additions & 32 deletions noir-projects/noir-protocol-circuits/crates/types/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ use crate::abis::side_effect::SideEffect;
use crate::utils::{uint256::U256, field::field_from_bytes_32_trunc};
use crate::constants::{
ARGS_HASH_CHUNK_COUNT, ARGS_HASH_CHUNK_LENGTH, MAX_ARGS_LENGTH, FUNCTION_TREE_HEIGHT,
GENERATOR_INDEX__SILOED_NOTE_HASH, GENERATOR_INDEX__OUTER_NULLIFIER, GENERATOR_INDEX__VK,
GENERATOR_INDEX__CONSTRUCTOR, GENERATOR_INDEX__PARTIAL_ADDRESS, GENERATOR_INDEX__CONTRACT_ADDRESS,
GENERATOR_INDEX__NOTE_HASH_NONCE, GENERATOR_INDEX__UNIQUE_NOTE_HASH, GENERATOR_INDEX__FUNCTION_ARGS
GENERATOR_INDEX__VK, GENERATOR_INDEX__CONSTRUCTOR, GENERATOR_INDEX__PARTIAL_ADDRESS,
GENERATOR_INDEX__CONTRACT_ADDRESS, GENERATOR_INDEX__FUNCTION_ARGS
};
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, sha256, poseidon2::{Poseidon2, Poseidon2Hasher}};

pub fn sha256_to_field<N>(bytes_to_hash: [u8; N]) -> Field {
let sha256_hashed = sha256(bytes_to_hash);
Expand Down Expand Up @@ -65,23 +64,11 @@ pub fn private_functions_root_from_siblings(
}

pub fn silo_note_hash(address: AztecAddress, inner_commitment: Field) -> Field {
pedersen_hash(
[
address.to_field(),
inner_commitment
],
GENERATOR_INDEX__SILOED_NOTE_HASH
)
poseidon_hash([address.to_field(), inner_commitment])
}

pub fn silo_nullifier(address: AztecAddress, nullifier: Field) -> Field {
pedersen_hash(
[
address.to_field(),
nullifier
],
GENERATOR_INDEX__OUTER_NULLIFIER
)
poseidon_hash([address.to_field(), nullifier])
}

pub fn merkle_hash(left: Field, right: Field) -> Field {
Expand Down Expand Up @@ -159,23 +146,11 @@ pub fn compute_logs_hash(previous_log_hash: Field, current_log_hash: Field) -> F
}

pub fn compute_note_hash_nonce(first_nullifier: Field, commitment_index: u64) -> Field {
pedersen_hash(
[
first_nullifier,
commitment_index as Field
],
GENERATOR_INDEX__NOTE_HASH_NONCE
)
poseidon_hash([first_nullifier, commitment_index as Field])
}

pub fn compute_unique_siloed_note_hash(nonce: Field, siloed_note_hash: Field) -> Field {
pedersen_hash(
[
nonce,
siloed_note_hash
],
GENERATOR_INDEX__UNIQUE_NOTE_HASH
)
poseidon_hash([nonce, siloed_note_hash])
}

pub fn compute_unique_siloed_note_hashes<N>(
Expand All @@ -200,6 +175,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 poseidon_hash<N>(inputs: [Field; N]) -> Field {
Poseidon2::hash(inputs, inputs.len())
}

#[test]
fn smoke_sha256_to_field() {
let full_buffer = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ pub fn array_eq<T, N, S>(array: [T; N], expected: [T; S]) -> bool where T: Empty
eq
}

pub fn find_index<T, N, Env>(array: [T; N], find: fn[Env](T) -> bool) -> u64 {
let mut index = N;
pub fn find_index<T, N, Env>(array: [T; N], find: fn[Env](T) -> bool) -> Option<u64> {
let mut index = Option::none();
for i in 0..N {
if (index == N) & find(array[i]) {
index = i;
if index.is_none() & find(array[i]) {
index = Option::some(i);
}
}
index
Expand Down Expand Up @@ -201,7 +201,7 @@ fn test_array_length() {
fn find_index_greater_than_min() {
let values = [10, 20, 30, 40];
let min = 22;
let index = find_index(values, |v: Field| min.lt(v));
let index = find_index(values, |v: Field| min.lt(v)).unwrap();
assert_eq(index, 2);
}

Expand All @@ -210,7 +210,7 @@ fn find_index_not_found() {
let values = [10, 20, 30, 40];
let min = 100;
let index = find_index(values, |v: Field| min.lt(v));
assert_eq(index, 4);
assert(index.is_none());
}

#[test]
Expand Down
7 changes: 0 additions & 7 deletions yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,6 @@ export const LOGS_HASHES_NUM_BYTES_PER_BASE_ROLLUP = 64;
export const NUM_MSGS_PER_BASE_PARITY = 4;
export const NUM_BASE_PARITY_PER_ROOT_PARITY = 4;
export enum GeneratorIndex {
NOTE_HASH = 1,
NOTE_HASH_NONCE = 2,
UNIQUE_NOTE_HASH = 3,
SILOED_NOTE_HASH = 4,
NULLIFIER = 5,
INITIALIZATION_NULLIFIER = 6,
OUTER_NULLIFIER = 7,
PUBLIC_DATA_READ = 8,
PUBLIC_DATA_UPDATE_REQUEST = 9,
FUNCTION_DATA = 10,
Expand Down
Loading

0 comments on commit b24266c

Please sign in to comment.