From f13c6f9c84bb72f9a66797cce7b37142a466abba Mon Sep 17 00:00:00 2001 From: sirasistant Date: Mon, 1 Apr 2024 09:42:49 +0000 Subject: [PATCH 1/3] wip: remove some pedersens --- .../aztec-nr/aztec/src/note/utils.nr | 13 +++--- .../crates/types/src/constants.nr | 5 --- .../crates/types/src/hash.nr | 43 +++++-------------- yarn-project/circuits.js/src/constants.gen.ts | 5 --- yarn-project/circuits.js/src/hash/hash.ts | 10 ++--- .../src/e2e_deploy_contract.test.ts | 2 +- .../foundation/src/crypto/poseidon/index.ts | 11 ++++- yellow-paper/docs/state/note-hash-tree.md | 6 +-- 8 files changed, 34 insertions(+), 61 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/note/utils.nr b/noir-projects/aztec-nr/aztec/src/note/utils.nr index 8732ca6be61f..4a5884c1e269 100644 --- a/noir-projects/aztec-nr/aztec/src/note/utils.nr +++ b/noir-projects/aztec-nr/aztec/src/note/utils.nr @@ -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: Note) -> Field where Note: NoteInterface { @@ -47,8 +45,7 @@ pub fn compute_siloed_nullifier( 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: Note) -> Field where Note: NoteInterface { 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 2fec9bce466f..0ca96d004f53 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -206,13 +206,8 @@ 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; 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 6badc53d6595..7aeacb807ae0 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr @@ -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(bytes_to_hash: [u8; N]) -> Field { let sha256_hashed = sha256(bytes_to_hash); @@ -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 { @@ -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( @@ -200,6 +175,10 @@ pub fn pedersen_hash(inputs: [Field; N], hash_index: u32) -> Field { dep::std::hash::pedersen_hash_with_separator(inputs, hash_index) } +pub fn poseidon_hash(inputs: [Field; N]) -> Field { + Poseidon2::hash(inputs, inputs.len()) +} + #[test] fn smoke_sha256_to_field() { let full_buffer = [ diff --git a/yarn-project/circuits.js/src/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index e2144817194e..b59787e3ee2f 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -118,13 +118,8 @@ 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, diff --git a/yarn-project/circuits.js/src/hash/hash.ts b/yarn-project/circuits.js/src/hash/hash.ts index 60440f4205e5..6b934f6170bb 100644 --- a/yarn-project/circuits.js/src/hash/hash.ts +++ b/yarn-project/circuits.js/src/hash/hash.ts @@ -1,6 +1,6 @@ import { AztecAddress } from '@aztec/foundation/aztec-address'; import { padArrayEnd } from '@aztec/foundation/collection'; -import { pedersenHash, pedersenHashBuffer } from '@aztec/foundation/crypto'; +import { pedersenHash, pedersenHashBuffer, poseidonHash } from '@aztec/foundation/crypto'; import { Fr } from '@aztec/foundation/fields'; import { createDebugLogger } from '@aztec/foundation/log'; import { numToUInt8, numToUInt16BE, numToUInt32BE } from '@aztec/foundation/serialize'; @@ -61,7 +61,7 @@ export function hashVK(vkBuf: Buffer) { * @returns A commitment nonce. */ export function computeCommitmentNonce(nullifierZero: Fr, commitmentIndex: number): Fr { - return pedersenHash([nullifierZero, numToUInt32BE(commitmentIndex, 32)], GeneratorIndex.NOTE_HASH_NONCE); + return poseidonHash([nullifierZero, numToUInt32BE(commitmentIndex, 32)]); } /** @@ -72,7 +72,7 @@ export function computeCommitmentNonce(nullifierZero: Fr, commitmentIndex: numbe * @returns A siloed commitment. */ export function siloNoteHash(contract: AztecAddress, innerNoteHash: Fr): Fr { - return pedersenHash([contract, innerNoteHash], GeneratorIndex.SILOED_NOTE_HASH); + return poseidonHash([contract, innerNoteHash]); } /** @@ -82,7 +82,7 @@ export function siloNoteHash(contract: AztecAddress, innerNoteHash: Fr): Fr { * @returns A unique commitment. */ export function computeUniqueCommitment(nonce: Fr, siloedCommitment: Fr): Fr { - return pedersenHash([nonce, siloedCommitment], GeneratorIndex.UNIQUE_NOTE_HASH); + return poseidonHash([nonce, siloedCommitment]); } /** @@ -93,7 +93,7 @@ export function computeUniqueCommitment(nonce: Fr, siloedCommitment: Fr): Fr { * @returns A siloed nullifier. */ export function siloNullifier(contract: AztecAddress, innerNullifier: Fr): Fr { - return pedersenHash([contract, innerNullifier], GeneratorIndex.OUTER_NULLIFIER); + return poseidonHash([contract, innerNullifier]); } /** diff --git a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts index 7638c537499c..151a7b7fd978 100644 --- a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts @@ -55,7 +55,7 @@ describe('e2e_deploy_contract', () => { * Milestone 1.1. * https://hackmd.io/ouVCnacHQRq2o1oRc5ksNA#Interfaces-and-Responsibilities */ - it('should deploy a test contract', async () => { + it.only('should deploy a test contract', async () => { const salt = Fr.random(); const publicKey = accounts[0].publicKey; const deploymentData = getContractInstanceFromDeployParams(TestContractArtifact, { diff --git a/yarn-project/foundation/src/crypto/poseidon/index.ts b/yarn-project/foundation/src/crypto/poseidon/index.ts index 8f77b5802115..e70e3878df50 100644 --- a/yarn-project/foundation/src/crypto/poseidon/index.ts +++ b/yarn-project/foundation/src/crypto/poseidon/index.ts @@ -1,16 +1,23 @@ import { BarretenbergSync, Fr as FrBarretenberg } from '@aztec/bb.js'; import { Fr } from '../../fields/fields.js'; +import { Bufferable, serializeToBufferArray } from '../../serialize/serialize.js'; /** * Create a poseidon hash (field) from an array of input fields. * Left pads any inputs less than 32 bytes. */ -export function poseidonHash(input: Buffer[]): Fr { +export function poseidonHash(input: Bufferable[]): Fr { + let bufferredInput = serializeToBufferArray(input); + if (!bufferredInput.every(i => i.length <= 32)) { + throw new Error('All Pedersen Hash input buffers must be <= 32 bytes.'); + } + bufferredInput = bufferredInput.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i)); + return Fr.fromBuffer( Buffer.from( BarretenbergSync.getSingleton() - .poseidonHash(input.map(i => new FrBarretenberg(i))) + .poseidonHash(bufferredInput.map(fr => new FrBarretenberg(fr))) .toBuffer(), ), ); diff --git a/yellow-paper/docs/state/note-hash-tree.md b/yellow-paper/docs/state/note-hash-tree.md index 788b02363ae3..f48258e66e5f 100644 --- a/yellow-paper/docs/state/note-hash-tree.md +++ b/yellow-paper/docs/state/note-hash-tree.md @@ -12,10 +12,10 @@ The pseudocode for siloing and making a commitment unique is the following, wher ``` fn compute_unique_siloed_note_hash(commitment, contract, transaction): - let siloed_note_hash = hash([contract, commitment], SILOED_NOTE_HASH) + let siloed_note_hash = hash([contract, commitment]) let index = index_of(commitment, transaction.commitments) - let nonce = hash([transaction.tx_hash, index], NOTE_HASH_NONCE) - return hash([nonce, siloed_note_hash], UNIQUE_NOTE_HASH) + let nonce = hash([transaction.tx_hash, index]) + return hash([nonce, siloed_note_hash]) ``` The unique siloed commitment of a note is included in the [transaction `data`](../transactions/tx-object.md), and then inserted into the Note Hash tree by the sequencer as the transaction is included in a block. From 6f1d59a55c6d493203cb0bdb18a048bc27900ffd Mon Sep 17 00:00:00 2001 From: sirasistant Date: Mon, 1 Apr 2024 10:31:35 +0000 Subject: [PATCH 2/3] feat: convert more hashes to poseidon, fix test --- noir-projects/aztec-nr/aztec/src/hash.nr | 10 +++------- .../aztec/src/state_vars/private_immutable.nr | 7 ++----- .../aztec-nr/aztec/src/state_vars/private_mutable.nr | 7 ++----- ...lifier_non_existent_read_request_hints_builder.nr | 6 ++++-- .../crates/types/src/abis/public_data_read.nr | 5 ++--- .../crates/types/src/constants.nr | 3 --- .../crates/types/src/contrakt/storage_read.nr | 4 ++-- .../crates/types/src/utils/arrays.nr | 12 ++++++------ yarn-project/circuits.js/src/constants.gen.ts | 2 -- yarn-project/circuits.js/src/hash/hash.ts | 2 +- .../end-to-end/src/e2e_deploy_contract.test.ts | 2 +- 11 files changed, 23 insertions(+), 37 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/hash.nr b/noir-projects/aztec-nr/aztec/src/hash.nr index 8beff1afb1d1..b7f0e63ff0df 100644 --- a/noir-projects/aztec-nr/aztec/src/hash.nr +++ b/noir-projects/aztec-nr/aztec/src/hash.nr @@ -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}; @@ -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 { diff --git a/noir-projects/aztec-nr/aztec/src/state_vars/private_immutable.nr b/noir-projects/aztec-nr/aztec/src/state_vars/private_immutable.nr index 7824ae28a58b..56bdf9290c75 100644 --- a/noir-projects/aztec-nr/aztec/src/state_vars/private_immutable.nr +++ b/noir-projects/aztec-nr/aztec/src/state_vars/private_immutable.nr @@ -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::{ @@ -32,10 +32,7 @@ impl PrivateImmutable { // This is especially dangerous for initial assignment to elements of a `Map` 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 diff --git a/noir-projects/aztec-nr/aztec/src/state_vars/private_mutable.nr b/noir-projects/aztec-nr/aztec/src/state_vars/private_mutable.nr index 5d0f8b11fec8..f246d7cc59e0 100644 --- a/noir-projects/aztec-nr/aztec/src/state_vars/private_mutable.nr +++ b/noir-projects/aztec-nr/aztec/src/state_vars/private_mutable.nr @@ -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::{ @@ -34,10 +34,7 @@ impl PrivateMutable { // 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 diff --git a/noir-projects/noir-protocol-circuits/crates/reset-kernel-lib/src/tests/nullifier_non_existent_read_request_hints_builder.nr b/noir-projects/noir-protocol-circuits/crates/reset-kernel-lib/src/tests/nullifier_non_existent_read_request_hints_builder.nr index d4de21463a74..e3f3fba6b23e 100644 --- a/noir-projects/noir-protocol-circuits/crates/reset-kernel-lib/src/tests/nullifier_non_existent_read_request_hints_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/reset-kernel-lib/src/tests/nullifier_non_existent_read_request_hints_builder.nr @@ -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; @@ -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()); } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_data_read.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_data_read.nr index f790fe142c82..2f4461348b25 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_data_read.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_data_read.nr @@ -1,4 +1,3 @@ -use crate::constants::GENERATOR_INDEX__PUBLIC_DATA_READ; use dep::std::cmp::Eq; use crate::traits::{Empty, Hash}; @@ -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) + ]) } } 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 0ca96d004f53..f6d0170aa9f8 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -206,9 +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__NULLIFIER = 5; -global GENERATOR_INDEX__INITIALIZATION_NULLIFIER = 6; -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; diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/contrakt/storage_read.nr b/noir-projects/noir-protocol-circuits/crates/types/src/contrakt/storage_read.nr index 04ee782bd3d0..df89e6237f95 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/contrakt/storage_read.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/contrakt/storage_read.nr @@ -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} }; @@ -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()) } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/utils/arrays.nr b/noir-projects/noir-protocol-circuits/crates/types/src/utils/arrays.nr index 73af12d2969c..cb05f7dd13bc 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/utils/arrays.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/utils/arrays.nr @@ -57,11 +57,11 @@ pub fn array_eq(array: [T; N], expected: [T; S]) -> bool where T: Empty eq } -pub fn find_index(array: [T; N], find: fn[Env](T) -> bool) -> u64 { - let mut index = N; +pub fn find_index(array: [T; N], find: fn[Env](T) -> bool) -> Option { + 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 @@ -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); } @@ -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] diff --git a/yarn-project/circuits.js/src/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index b59787e3ee2f..b00eeb84edb8 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -118,8 +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 { - NULLIFIER = 5, - INITIALIZATION_NULLIFIER = 6, PUBLIC_DATA_READ = 8, PUBLIC_DATA_UPDATE_REQUEST = 9, FUNCTION_DATA = 10, diff --git a/yarn-project/circuits.js/src/hash/hash.ts b/yarn-project/circuits.js/src/hash/hash.ts index 6b934f6170bb..7169b9edab09 100644 --- a/yarn-project/circuits.js/src/hash/hash.ts +++ b/yarn-project/circuits.js/src/hash/hash.ts @@ -171,6 +171,6 @@ export function computeL1ToL2MessageNullifier( secret: Fr, messageIndex: bigint, ) { - const innerMessageNullifier = pedersenHash([messageHash, secret, messageIndex], GeneratorIndex.NULLIFIER); + const innerMessageNullifier = poseidonHash([messageHash, secret, messageIndex]); return siloNullifier(contract, innerMessageNullifier); } diff --git a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts index 151a7b7fd978..7638c537499c 100644 --- a/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts +++ b/yarn-project/end-to-end/src/e2e_deploy_contract.test.ts @@ -55,7 +55,7 @@ describe('e2e_deploy_contract', () => { * Milestone 1.1. * https://hackmd.io/ouVCnacHQRq2o1oRc5ksNA#Interfaces-and-Responsibilities */ - it.only('should deploy a test contract', async () => { + it('should deploy a test contract', async () => { const salt = Fr.random(); const publicKey = accounts[0].publicKey; const deploymentData = getContractInstanceFromDeployParams(TestContractArtifact, { From 281519e190c50fe5f210cf40121a435837a476e7 Mon Sep 17 00:00:00 2001 From: sirasistant Date: Mon, 1 Apr 2024 10:38:18 +0000 Subject: [PATCH 3/3] wip: more removal of pedersen --- .../crates/types/src/abis/function_data.nr | 4 ++-- .../crates/types/src/abis/public_data_update_request.nr | 5 ++--- .../noir-protocol-circuits/crates/types/src/constants.nr | 2 -- .../crates/types/src/contrakt/storage_update_request.nr | 6 +++--- yarn-project/circuits.js/src/structs/function_data.ts | 6 +++--- 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_data.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_data.nr index cf4f8985cac5..86e683de8ce5 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_data.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_data.nr @@ -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} }; @@ -44,7 +44,7 @@ impl Deserialize for FunctionData { impl Hash for FunctionData { fn hash(self) -> Field { - pedersen_hash(self.serialize(), GENERATOR_INDEX__FUNCTION_DATA) + poseidon_hash(self.serialize()) } } diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_data_update_request.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_data_update_request.nr index 8177f389f185..21b363b71683 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_data_update_request.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis/public_data_update_request.nr @@ -1,4 +1,3 @@ -use crate::constants::GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST; use dep::std::cmp::Eq; use crate::traits::{Empty, Hash}; @@ -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) + ]) } } 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 f6d0170aa9f8..6bf8db5c2257 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/constants.nr @@ -206,8 +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__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; diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/contrakt/storage_update_request.nr b/noir-projects/noir-protocol-circuits/crates/types/src/contrakt/storage_update_request.nr index 01176175f606..702505d63d3c 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/contrakt/storage_update_request.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/contrakt/storage_update_request.nr @@ -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; @@ -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()) } } diff --git a/yarn-project/circuits.js/src/structs/function_data.ts b/yarn-project/circuits.js/src/structs/function_data.ts index 08c803270b5a..2c0f918b7d01 100644 --- a/yarn-project/circuits.js/src/structs/function_data.ts +++ b/yarn-project/circuits.js/src/structs/function_data.ts @@ -1,9 +1,9 @@ import { FunctionAbi, FunctionSelector, FunctionType } from '@aztec/foundation/abi'; -import { pedersenHash } from '@aztec/foundation/crypto'; +import { poseidonHash } from '@aztec/foundation/crypto'; import { Fr } from '@aztec/foundation/fields'; import { BufferReader, FieldReader, serializeToBuffer } from '@aztec/foundation/serialize'; -import { FUNCTION_DATA_LENGTH, GeneratorIndex } from '../constants.gen.js'; +import { FUNCTION_DATA_LENGTH } from '../constants.gen.js'; import { ContractFunctionDao } from '../types/contract_function_dao.js'; /** Function description for circuit. */ @@ -87,6 +87,6 @@ export class FunctionData { } hash(): Fr { - return pedersenHash(this.toFields(), GeneratorIndex.FUNCTION_DATA); + return poseidonHash(this.toFields()); } }