diff --git a/l1-contracts/src/core/libraries/ConstantsGen.sol b/l1-contracts/src/core/libraries/ConstantsGen.sol index 4e9d03808528..dd33f0038f37 100644 --- a/l1-contracts/src/core/libraries/ConstantsGen.sol +++ b/l1-contracts/src/core/libraries/ConstantsGen.sol @@ -79,7 +79,6 @@ library Constants { uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 201; uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674; uint256 internal constant CALL_PRIVATE_FUNCTION_RETURN_SIZE = 210; - uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH = 188; uint256 internal constant COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP = 2048; uint256 internal constant NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP = 2048; uint256 internal constant PUBLIC_DATA_WRITES_NUM_BYTES_PER_BASE_ROLLUP = 1024; diff --git a/yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap b/yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap index 1497235e4efc..3062c58963de 100644 --- a/yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap +++ b/yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap @@ -10,41 +10,41 @@ exports[`abis Computes an empty sideeffect hash 1`] = `"0x27b1d0839a5b23baf12a8 exports[`abis compute private call stack item hash 1`] = ` Fr { - "asBigInt": 12187345511405217717040217531423286257305914329376428594135414078733109256018n, + "asBigInt": 20809484660315254497535468009354600907499263083154533860853878665971818104219n, "asBuffer": { "data": [ - 26, - 241, - 203, - 9, - 80, - 135, - 163, - 233, - 54, - 161, - 69, - 247, - 77, - 223, - 148, - 99, - 177, - 7, - 65, - 132, - 142, - 175, - 255, - 49, - 18, - 190, - 219, - 83, - 245, + 46, 1, + 192, + 60, + 242, 95, - 82, + 113, + 243, + 178, + 218, + 79, + 199, + 55, + 206, + 59, + 194, + 234, + 24, + 158, + 215, + 200, + 157, + 95, + 167, + 67, + 42, + 204, + 172, + 237, + 37, + 5, + 155, ], "type": "Buffer", }, diff --git a/yarn-project/circuits.js/src/abis/abis.ts b/yarn-project/circuits.js/src/abis/abis.ts index ad99c3d03bd1..3e8d7bf1589f 100644 --- a/yarn-project/circuits.js/src/abis/abis.ts +++ b/yarn-project/circuits.js/src/abis/abis.ts @@ -10,18 +10,15 @@ import chunk from 'lodash.chunk'; import { FUNCTION_SELECTOR_NUM_BYTES, FUNCTION_TREE_HEIGHT, - GeneratorIndex, - PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH + GeneratorIndex } from '../constants.gen.js'; import { MerkleTreeCalculator } from '../merkle/merkle_tree_calculator.js'; import { - CallContext, ContractDeploymentData, FunctionData, FunctionLeafPreimage, NewContractData, PrivateCallStackItem, - PrivateCircuitPublicInputs, PublicCallStackItem, PublicCircuitPublicInputs, SideEffect, @@ -353,60 +350,6 @@ function computeContractDeploymentDataHash(data: ContractDeploymentData): Fr { ); } -function computeCallContextHash(input: CallContext) { - return pedersenHash( - [ - input.msgSender.toBuffer(), - input.storageContractAddress.toBuffer(), - input.portalContractAddress.toBuffer(), - input.functionSelector.toBuffer(), - boolToBuffer(input.isDelegateCall, 32), - boolToBuffer(input.isStaticCall, 32), - boolToBuffer(input.isContractDeployment, 32), - numToUInt32BE(input.startSideEffectCounter, 32), - ], - GeneratorIndex.CALL_CONTEXT, - ); -} - -function computePrivateInputsHash(input: PrivateCircuitPublicInputs) { - const toHash = [ - computeCallContextHash(input.callContext), - input.argsHash.toBuffer(), - ...input.returnValues.map(fr => fr.toBuffer()), - ...input.readRequests - .map(rr => rr.toFields()) - .flat() - .map(fr => fr.toBuffer()), - ...input.newCommitments - .map(n => n.toFields()) - .flat() - .map(fr => fr.toBuffer()), - ...input.newNullifiers - .map(n => n.toFields()) - .flat() - .map(fr => fr.toBuffer()), - ...input.privateCallStackHashes.map(fr => fr.toBuffer()), - ...input.publicCallStackHashes.map(fr => fr.toBuffer()), - ...input.newL2ToL1Msgs.map(fr => fr.toBuffer()), - input.endSideEffectCounter.toBuffer(), - ...input.encryptedLogsHash.map(fr => fr.toBuffer()), - ...input.unencryptedLogsHash.map(fr => fr.toBuffer()), - input.encryptedLogPreimagesLength.toBuffer(), - input.unencryptedLogPreimagesLength.toBuffer(), - ...(input.historicalHeader.toFields().map(fr => fr.toBuffer()) as Buffer[]), - computeContractDeploymentDataHash(input.contractDeploymentData).toBuffer(), - input.chainId.toBuffer(), - input.version.toBuffer(), - ]; - if (toHash.length != PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH) { - throw new Error( - `Incorrect number of input fields when hashing PrivateCircuitPublicInputs ${toHash.length}, ${PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH}`, - ); - } - return pedersenHash(toHash, GeneratorIndex.PRIVATE_CIRCUIT_PUBLIC_INPUTS); -} - /** * Computes a call stack item hash. * @param callStackItem - The call stack item. @@ -418,7 +361,7 @@ export function computePrivateCallStackItemHash(callStackItem: PrivateCallStackI [ callStackItem.contractAddress.toBuffer(), computeFunctionDataHash(callStackItem.functionData).toBuffer(), - computePrivateInputsHash(callStackItem.publicInputs), + callStackItem.publicInputs.hash().toBuffer(), ], GeneratorIndex.CALL_STACK_ITEM, ), diff --git a/yarn-project/circuits.js/src/constants.gen.ts b/yarn-project/circuits.js/src/constants.gen.ts index 6d4e1f535902..5b7df09b5c2e 100644 --- a/yarn-project/circuits.js/src/constants.gen.ts +++ b/yarn-project/circuits.js/src/constants.gen.ts @@ -65,7 +65,6 @@ export const CONTRACT_STORAGE_READ_LENGTH = 2; export const PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 201; export const GET_NOTES_ORACLE_RETURN_LENGTH = 674; export const CALL_PRIVATE_FUNCTION_RETURN_SIZE = 210; -export const PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH = 188; export const COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP = 2048; export const NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP = 2048; export const PUBLIC_DATA_WRITES_NUM_BYTES_PER_BASE_ROLLUP = 1024; diff --git a/yarn-project/circuits.js/src/structs/__snapshots__/call_stack_item.test.ts.snap b/yarn-project/circuits.js/src/structs/__snapshots__/call_stack_item.test.ts.snap index d54ec645a35b..866e12cbb119 100644 --- a/yarn-project/circuits.js/src/structs/__snapshots__/call_stack_item.test.ts.snap +++ b/yarn-project/circuits.js/src/structs/__snapshots__/call_stack_item.test.ts.snap @@ -2,41 +2,41 @@ exports[`PrivateCallStackItem computes hash 1`] = ` Fr { - "asBigInt": 20211835094457188239301134546342553663959029760227448898294272588084026667910n, + "asBigInt": 18753511695134949302571620808052147141303876532454748772518515365970855750244n, "asBuffer": { "data": [ - 44, - 175, - 126, - 70, - 125, - 139, - 43, + 41, 118, - 26, - 81, - 126, - 221, - 198, - 219, - 65, - 196, - 232, - 191, - 214, - 222, - 18, - 149, - 88, - 100, - 79, 28, + 137, + 68, + 13, + 118, + 183, + 92, + 239, + 142, + 84, + 234, 235, - 175, - 176, - 129, - 243, - 134, + 13, + 55, + 245, + 66, + 184, + 218, + 252, + 190, + 14, + 252, + 58, + 237, + 34, + 161, + 161, + 6, + 106, + 100, ], "type": "Buffer", }, diff --git a/yarn-project/circuits.js/src/structs/__snapshots__/private_circuit_public_inputs.test.ts.snap b/yarn-project/circuits.js/src/structs/__snapshots__/private_circuit_public_inputs.test.ts.snap new file mode 100644 index 000000000000..f6c7acfcc30e --- /dev/null +++ b/yarn-project/circuits.js/src/structs/__snapshots__/private_circuit_public_inputs.test.ts.snap @@ -0,0 +1,44 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`PrivateCircuitPublicInputs hash matches snapshot 1`] = ` +Fr { + "asBigInt": 7655509707748365385586683354008259293573299722789964529445054772581519738939n, + "asBuffer": { + "data": [ + 16, + 236, + 221, + 108, + 242, + 63, + 20, + 45, + 47, + 147, + 239, + 244, + 141, + 4, + 72, + 218, + 247, + 23, + 13, + 105, + 121, + 159, + 29, + 162, + 114, + 51, + 52, + 186, + 54, + 94, + 72, + 59, + ], + "type": "Buffer", + }, +} +`; diff --git a/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.test.ts b/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.test.ts index 8b8753e51ccb..f1d2d822f5d7 100644 --- a/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.test.ts +++ b/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.test.ts @@ -31,4 +31,10 @@ describe('PrivateCircuitPublicInputs', () => { const fields = inputs.toFields(); expect(fields.length).toBe(PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH); }); + + it('hash matches snapshot', () => { + const target = makePrivateCircuitPublicInputs(327); + const hash = target.hash(); + expect(hash).toMatchSnapshot(); + }); }); diff --git a/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.ts b/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.ts index c5f7332d6c68..ee341ea67ae0 100644 --- a/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.ts +++ b/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.ts @@ -11,6 +11,7 @@ import { import { FieldsOf } from '@aztec/foundation/types'; import { + GeneratorIndex, MAX_NEW_COMMITMENTS_PER_CALL, MAX_NEW_L2_TO_L1_MSGS_PER_CALL, MAX_NEW_NULLIFIERS_PER_CALL, @@ -24,6 +25,7 @@ import { import { CallContext } from './call_context.js'; import { ContractDeploymentData, Header, SideEffect, SideEffectLinkedToNoteHash } from './index.js'; import { NullifierKeyValidationRequest } from './nullifier_key_validation_request.js'; +import { pedersenHash } from '@aztec/foundation/crypto'; /** * Public inputs to a private circuit. @@ -282,4 +284,13 @@ export class PrivateCircuitPublicInputs { toFields(): Fr[] { return serializeToFieldArray(...PrivateCircuitPublicInputs.getFields(this)); } + + hash(): Fr { + return Fr.fromBuffer( + pedersenHash( + this.toFields().map(field => field.toBuffer()), + GeneratorIndex.PRIVATE_CIRCUIT_PUBLIC_INPUTS, + ), + ); + } } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/private_circuit_public_inputs.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/private_circuit_public_inputs.nr index 0df8915d1a4c..e98ec81c419e 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/private_circuit_public_inputs.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/abis/private_circuit_public_inputs.nr @@ -20,7 +20,6 @@ use crate::constants::{ MAX_NEW_L2_TO_L1_MSGS_PER_CALL, NUM_FIELDS_PER_SHA256, RETURN_VALUES_LENGTH, - PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH, PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH, GENERATOR_INDEX__PRIVATE_CIRCUIT_PUBLIC_INPUTS, }; @@ -63,38 +62,8 @@ struct PrivateCircuitPublicInputs { } impl Hash for PrivateCircuitPublicInputs { - fn hash(self) -> Field { - // TODO(https://github.com/AztecProtocol/aztec-packages/issues/3595) - let mut fields: BoundedVec = BoundedVec::new(0); - fields.push(self.call_context.hash()); - fields.push(self.args_hash); - fields.extend_from_array(self.return_values); - - for i in 0..MAX_READ_REQUESTS_PER_CALL{ - fields.extend_from_array(self.read_requests[i].serialize()); - } - for i in 0..MAX_NEW_COMMITMENTS_PER_CALL{ - fields.extend_from_array(self.new_commitments[i].serialize()); - } - for i in 0..MAX_NEW_NULLIFIERS_PER_CALL{ - fields.extend_from_array(self.new_nullifiers[i].serialize()); - } - fields.extend_from_array(self.private_call_stack_hashes); - fields.extend_from_array(self.public_call_stack_hashes); - fields.extend_from_array(self.new_l2_to_l1_msgs); - fields.push(self.end_side_effect_counter as Field); - fields.extend_from_array(self.encrypted_logs_hash); - fields.extend_from_array(self.unencrypted_logs_hash); - fields.push(self.encrypted_log_preimages_length); - fields.push(self.unencrypted_log_preimages_length); - fields.extend_from_array(self.historical_header.serialize()); - fields.push(self.contract_deployment_data.hash()); - fields.push(self.chain_id); - fields.push(self.version); - - assert_eq(fields.len(), PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH, "Incorrect number of input fields when hashing PrivateCircuitPublicInputs"); - - pedersen_hash(fields.storage, GENERATOR_INDEX__PRIVATE_CIRCUIT_PUBLIC_INPUTS) + fn hash(self) -> Field { + pedersen_hash(self.serialize(), GENERATOR_INDEX__PRIVATE_CIRCUIT_PUBLIC_INPUTS) } } diff --git a/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr b/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr index b2d842bb03ac..d7628f42e30d 100644 --- a/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr +++ b/yarn-project/noir-protocol-circuits/src/crates/types/src/constants.nr @@ -103,7 +103,7 @@ global FUNCTION_DATA_LENGTH: Field = 4; global CONTRACT_DEPLOYMENT_DATA_LENGTH: Field = 6; // Change this ONLY if you have changed the PrivateCircuitPublicInputs structure. // In other words, if the structure/size of the public inputs of a function call changes then we should change this -// constant as well CALL_PRIVATE_FUNCTION_RETURN_SIZE and PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH +// constant as well CALL_PRIVATE_FUNCTION_RETURN_SIZE global PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH: Field = 204; global CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH: Field = 3; global CONTRACT_STORAGE_READ_LENGTH: Field = 2; @@ -111,7 +111,6 @@ global CONTRACT_STORAGE_READ_LENGTH: Field = 2; global PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH: Field = 201; global GET_NOTES_ORACLE_RETURN_LENGTH: Field = 674; global CALL_PRIVATE_FUNCTION_RETURN_SIZE: Field = 210; -global PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH: Field = 188; global COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP: Field = 2048; global NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP: Field = 2048; global PUBLIC_DATA_WRITES_NUM_BYTES_PER_BASE_ROLLUP: Field = 1024;