diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/common.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/common.nr index 43c012dc9ba..6e21f11973e 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/common.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/common.nr @@ -18,10 +18,8 @@ use dep::types::{ }, grumpkin_private_key::GrumpkinPrivateKey, hash::{ - compute_constructor_hash, compute_l2_to_l1_hash, compute_logs_hash, - compute_new_contract_address_hash, function_tree_root_from_siblings, pedersen_hash, - private_functions_root_from_siblings, silo_note_hash, silo_nullifier, - stdlib_recursion_verification_key_compress_native_vk + compute_l2_to_l1_hash, compute_logs_hash, pedersen_hash, private_functions_root_from_siblings, + silo_note_hash, silo_nullifier, stdlib_recursion_verification_key_compress_native_vk }, merkle_tree::check_membership, utils::{arrays::{array_length, array_to_bounded_vec, validate_array}}, diff --git a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_init.nr b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_init.nr index b94c09f3000..bff33efae84 100644 --- a/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_init.nr +++ b/noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/private_kernel_init.nr @@ -124,7 +124,7 @@ mod tests { address::{AztecAddress, EthAddress, compute_initialization_hash}, constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, grumpkin_point::GrumpkinPoint, grumpkin_private_key::GrumpkinPrivateKey, - hash::{compute_constructor_hash, compute_logs_hash, stdlib_recursion_verification_key_compress_native_vk}, + hash::{compute_logs_hash, stdlib_recursion_verification_key_compress_native_vk}, messaging::l2_to_l1_message::L2ToL1Message, tests::private_call_data_builder::PrivateCallDataBuilder, transaction::tx_request::TxRequest, utils::arrays::array_length diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis.nr index a4aac38d69b..12741ec2d76 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/abis.nr @@ -4,7 +4,6 @@ mod contract_class_function_leaf_preimage; mod function_selector; mod function_data; -mod function_leaf_preimage; mod global_variables; diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_leaf_preimage.nr b/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_leaf_preimage.nr deleted file mode 100644 index ded25c0740e..00000000000 --- a/noir-projects/noir-protocol-circuits/crates/types/src/abis/function_leaf_preimage.nr +++ /dev/null @@ -1,77 +0,0 @@ -use crate::{ - abis::function_selector::FunctionSelector, - constants::{GENERATOR_INDEX__FUNCTION_LEAF, FUNCTION_LEAF_PREIMAGE_LENGTH}, hash::pedersen_hash, - traits::{Serialize, Hash, Deserialize} -}; - -struct FunctionLeafPreimage { - selector : FunctionSelector, - is_internal : bool, - is_private : bool, - vk_hash : Field, - acir_hash : Field -} - -impl Eq for FunctionLeafPreimage { - fn eq(self, other: Self) -> bool { - self.selector.eq(other.selector) & - (self.is_internal == other.is_internal) & - (self.is_private == other.is_private) & - (self.vk_hash == other.vk_hash) & - (self.acir_hash == other.acir_hash) - } -} - -impl Serialize for FunctionLeafPreimage { - fn serialize(self) -> [Field; FUNCTION_LEAF_PREIMAGE_LENGTH] { - [ - self.selector.to_field(), - self.is_internal as Field, - self.is_private as Field, - self.vk_hash, - self.acir_hash, - ] - } -} - -impl Deserialize for FunctionLeafPreimage { - fn deserialize(serialized: [Field; FUNCTION_LEAF_PREIMAGE_LENGTH]) -> Self { - Self { - selector: FunctionSelector::from_field(serialized[0]), - is_internal: serialized[1] as bool, - is_private: serialized[2] as bool, - vk_hash: serialized[3], - acir_hash: serialized[4], - } - } -} - -impl Hash for FunctionLeafPreimage { - fn hash(self) -> Field { - pedersen_hash(self.serialize(), GENERATOR_INDEX__FUNCTION_LEAF) - } -} - -#[test] -fn serialization_of_empty() { - let data: FunctionLeafPreimage = dep::std::unsafe::zeroed(); - let serialized = data.serialize(); - let deserialized = FunctionLeafPreimage::deserialize(serialized); - assert(data.eq(deserialized)); -} - -#[test] -fn empty_hash() { - let data: FunctionLeafPreimage = dep::std::unsafe::zeroed(); - let hash = data.hash(); - - // Value from function_leaf_preimage.test.ts "computes a function leaf" test - assert_eq(hash, 0x1f2e3193c7187347a099ee7cb5d6ac077da6b18706fe5508e658a3d0a05494f7); -} - -#[test] -fn compute_function_leaf() { - let leaf = FunctionLeafPreimage { selector: FunctionSelector::from_u32(27), is_internal: false, is_private: true, vk_hash: 1, acir_hash: 2 }; - - assert_eq(leaf.hash(), 0x1ad8ece7f40e63d011ae47c6ce6cdaf31d632a23f5cf35bbeaaf69c8302afdbc); -} 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 7de5fa7d536..c02e7e6b729 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/hash.nr @@ -1,10 +1,8 @@ use crate::address::{AztecAddress, EthAddress}; use crate::mocked::VerificationKey; use crate::abis::function_selector::FunctionSelector; -use crate::abis::function_leaf_preimage::FunctionLeafPreimage; use crate::abis::contract_class_function_leaf_preimage::ContractClassFunctionLeafPreimage; use crate::contract_class_id::ContractClassId; -use crate::abis::function_data::FunctionData; use crate::abis::side_effect::{SideEffect}; use crate::utils::uint256::U256; use crate::constants::{ @@ -62,30 +60,6 @@ pub fn hash_args(args: [Field; N]) -> Field { } } -// Calculate the function tree root from the sibling path and leaf preimage. -// -// TODO: The cpp code passes in components of the FunctionLeafPreimage and then -// builds it up. We should build it up and then pass the leaf preimage as a parameter. -// We can then choose to have a general method that takes in anything hashable -// and deduplicate the logic in `contract_tree_root_from_siblings` -pub fn function_tree_root_from_siblings( - selector: FunctionSelector, - is_internal: bool, - is_private: bool, - vk_hash: Field, - acir_hash: Field, - function_leaf_index: Field, - function_leaf_sibling_path: [Field; FUNCTION_TREE_HEIGHT] -) -> Field { - let function_leaf_preimage = FunctionLeafPreimage { selector, is_internal, is_private, vk_hash, acir_hash }; - - let function_leaf = function_leaf_preimage.hash(); - - let function_tree_root = root_from_sibling_path(function_leaf, function_leaf_index, function_leaf_sibling_path); - - function_tree_root -} - pub fn private_functions_root_from_siblings( selector: FunctionSelector, vk_hash: Field, @@ -129,11 +103,6 @@ pub fn stdlib_recursion_verification_key_compress_native_vk(_vk: VerificationKey 0 } -// TODO CPP uses blake2s for this -pub fn compute_new_contract_address_hash(new_contract_address: AztecAddress) -> Field { - dep::std::hash::pedersen_hash([new_contract_address.to_field()]) -} - pub fn compute_l2_to_l1_hash( contract_address: AztecAddress, rollup_version_id: Field, @@ -156,23 +125,6 @@ pub fn compute_l2_to_l1_hash( sha256_to_field(bytes.storage) } -pub fn compute_constructor_hash( - function_data: FunctionData, - args_hash: Field, - constructor_vk_hash: Field -) -> Field { - let function_data_hash = function_data.hash(); - - pedersen_hash( - [ - function_data_hash, - args_hash, - constructor_vk_hash - ], - GENERATOR_INDEX__CONSTRUCTOR - ) -} - // Computes sha256 hash of 2 input hashes stored in 4 fields. // // This method is bn254 specific. Two fields is needed in order to diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/tests/private_circuit_public_inputs_builder.nr b/noir-projects/noir-protocol-circuits/crates/types/src/tests/private_circuit_public_inputs_builder.nr index 5af237338e1..b3b81d77bb9 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/tests/private_circuit_public_inputs_builder.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/tests/private_circuit_public_inputs_builder.nr @@ -4,7 +4,7 @@ use crate::{ private_circuit_public_inputs::PrivateCircuitPublicInputs, read_request::ReadRequest, side_effect::{SideEffect, SideEffectLinkedToNoteHash} }, - address::{AztecAddress, compute_initialization_hash}, hash::{compute_constructor_hash, hash_args}, + address::{AztecAddress, compute_initialization_hash}, hash::{hash_args}, header::Header, messaging::l2_to_l1_message::L2ToL1Message, tests::fixtures }; use crate::constants::{ diff --git a/yarn-project/circuits.js/src/hash/__snapshots__/hash.test.ts.snap b/yarn-project/circuits.js/src/hash/__snapshots__/hash.test.ts.snap index eb8173eec0c..b56496d5d71 100644 --- a/yarn-project/circuits.js/src/hash/__snapshots__/hash.test.ts.snap +++ b/yarn-project/circuits.js/src/hash/__snapshots__/hash.test.ts.snap @@ -10,8 +10,6 @@ exports[`hash compute secret message hash 1`] = `Fr<0x0dc06f2167e2cd19adf738d1f3 exports[`hash computes commitment nonce 1`] = `Fr<0x10ebab01bc813263ef92ed71b9c781ad3ef58019b66a8f71304d2f72d7defe4d>`; -exports[`hash computes function tree root 1`] = `Fr<0x29ca48b7e335d43385addf19b70e9b05693a8f56cc09ef8cbbc374a40dadbf09>`; - exports[`hash computes public data tree leaf slot 1`] = `Fr<0x14114ab3dbdd0a1ccc5c4fe68dd576f3c6cd79708770e06ab4086398cdd828f4>`; exports[`hash computes public data tree value 1`] = `Fr<0x0000000000000000000000000000000000000000000000000000000000000003>`; @@ -24,8 +22,6 @@ exports[`hash computes unique commitment 1`] = `Fr<0x1cbdcecec4fe92f6638eb6a8dad exports[`hash hashes VK 1`] = `Buffer<0x048110667f80b02f77b7d744976657cea9a7c5f1dd2340ea1c579a7ebfd54e55>`; -exports[`hash hashes constructor info 1`] = `Fr<0x12e9b6121beff98b9d2d5cbd79989d49d3d3fd8734c786e9f24a06ef56001e2e>`; - exports[`hash hashes empty function args 1`] = `Fr<0x0000000000000000000000000000000000000000000000000000000000000000>`; exports[`hash hashes function args 1`] = `Fr<0x1e73c794bf82a06462cd300a7c8bda272f1efd4fc86b383529821aed6af362ea>`; diff --git a/yarn-project/circuits.js/src/hash/hash.test.ts b/yarn-project/circuits.js/src/hash/hash.test.ts index 5e57311863d..ce91f4c10d7 100644 --- a/yarn-project/circuits.js/src/hash/hash.test.ts +++ b/yarn-project/circuits.js/src/hash/hash.test.ts @@ -1,19 +1,17 @@ import { times } from '@aztec/foundation/collection'; import { setupCustomSnapshotSerializers } from '@aztec/foundation/testing'; -import { AztecAddress, Fr, FunctionData, FunctionSelector, SideEffect, SideEffectLinkedToNoteHash } from '../index.js'; +import { AztecAddress, Fr, SideEffect, SideEffectLinkedToNoteHash } from '../index.js'; import { makeAztecAddress, makeVerificationKey } from '../tests/factories.js'; import { computeCommitmentNonce, computeCommitmentsHash, - computeFunctionTreeRoot, computeMessageSecretHash, computeNullifierHash, computePublicDataTreeLeafSlot, computePublicDataTreeValue, computeUniqueCommitment, computeVarArgsHash, - hashConstructor, hashVK, siloNoteHash, siloNullifier, @@ -27,19 +25,6 @@ describe('hash', () => { expect(res).toMatchSnapshot(); }); - it('computes function tree root', () => { - const res = computeFunctionTreeRoot([new Fr(0n), new Fr(0n), new Fr(0n), new Fr(0n)]); - expect(res).toMatchSnapshot(); - }); - - it('hashes constructor info', () => { - const functionData = new FunctionData(FunctionSelector.empty(), false, true, true); - const argsHash = new Fr(42); - const vkHash = Buffer.alloc(32); - const res = hashConstructor(functionData, argsHash, vkHash); - expect(res).toMatchSnapshot(); - }); - it('computes commitment nonce', () => { const nullifierZero = new Fr(123n); const commitmentIndex = 456; diff --git a/yarn-project/circuits.js/src/hash/hash.ts b/yarn-project/circuits.js/src/hash/hash.ts index 44cc9ec964f..3ead475e6f1 100644 --- a/yarn-project/circuits.js/src/hash/hash.ts +++ b/yarn-project/circuits.js/src/hash/hash.ts @@ -8,14 +8,8 @@ import { numToUInt8, numToUInt16BE, numToUInt32BE } from '@aztec/foundation/seri import { Buffer } from 'buffer'; import chunk from 'lodash.chunk'; -import { - ARGS_HASH_CHUNK_COUNT, - ARGS_HASH_CHUNK_LENGTH, - FUNCTION_TREE_HEIGHT, - GeneratorIndex, -} from '../constants.gen.js'; -import { MerkleTreeCalculator } from '../merkle/merkle_tree_calculator.js'; -import type { FunctionData, SideEffect, SideEffectLinkedToNoteHash } from '../structs/index.js'; +import { ARGS_HASH_CHUNK_COUNT, ARGS_HASH_CHUNK_LENGTH, GeneratorIndex } from '../constants.gen.js'; +import type { SideEffect, SideEffectLinkedToNoteHash } from '../structs/index.js'; import { VerificationKey } from '../structs/verification_key.js'; /** @@ -60,55 +54,6 @@ export function hashVK(vkBuf: Buffer) { // return crypto::pedersen_hash::hash_buffer(preimage_data, hash_index); } -let functionTreeRootCalculator: MerkleTreeCalculator | undefined; -/** - * The "zero leaf" of the function tree is the hash of 5 zero fields. - * TODO: Why can we not just use a zero field as the zero leaf? Complicates things perhaps unnecessarily? - */ -function getFunctionTreeRootCalculator() { - if (!functionTreeRootCalculator) { - const functionTreeZeroLeaf = pedersenHash(new Array(5).fill(Buffer.alloc(32))).toBuffer(); - functionTreeRootCalculator = new MerkleTreeCalculator(FUNCTION_TREE_HEIGHT, functionTreeZeroLeaf); - } - return functionTreeRootCalculator; -} - -/** - * Computes a function tree from function leaves. - * @param fnLeaves - The function leaves to be included in the contract function tree. - * @returns All nodes of the tree. - */ -export function computeFunctionTree(fnLeaves: Fr[]) { - const leaves = fnLeaves.map(fr => fr.toBuffer()); - return getFunctionTreeRootCalculator() - .computeTree(leaves) - .nodes.map(b => Fr.fromBuffer(b)); -} - -/** - * Computes a function tree root from function leaves. - * @param fnLeaves - The function leaves to be included in the contract function tree. - * @returns The function tree root. - */ -export function computeFunctionTreeRoot(fnLeaves: Fr[]) { - const leaves = fnLeaves.map(fr => fr.toBuffer()); - return Fr.fromBuffer(getFunctionTreeRootCalculator().computeTreeRoot(leaves)); -} - -/** - * Computes a constructor hash. - * @param functionData - Constructor's function data. - * @param argsHash - Constructor's arguments hashed. - * @param constructorVKHash - Hash of the constructor's verification key. - * @returns The constructor hash. - */ -export function hashConstructor(functionData: FunctionData, argsHash: Fr, constructorVKHash: Buffer): Fr { - return pedersenHash( - [functionData.hash().toBuffer(), argsHash.toBuffer(), constructorVKHash], - GeneratorIndex.CONSTRUCTOR, - ); -} - /** * Computes a commitment nonce, which will be used to create a unique commitment. * @param nullifierZero - The first nullifier in the tx.