Skip to content

Commit

Permalink
chore: Delete unused hash functions (#5231)
Browse files Browse the repository at this point in the history
Deletes hashing functions related to the old contract tree, no longer
used now.
  • Loading branch information
spalladino authored Mar 15, 2024
1 parent 137c13e commit fed70a1
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod contract_class_function_leaf_preimage;

mod function_selector;
mod function_data;
mod function_leaf_preimage;

mod global_variables;

Expand Down

This file was deleted.

48 changes: 0 additions & 48 deletions noir-projects/noir-protocol-circuits/crates/types/src/hash.nr
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -62,30 +60,6 @@ pub fn hash_args<N>(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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>`;
Expand All @@ -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>`;
Expand Down
17 changes: 1 addition & 16 deletions yarn-project/circuits.js/src/hash/hash.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand Down
59 changes: 2 additions & 57 deletions yarn-project/circuits.js/src/hash/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit fed70a1

Please sign in to comment.