Skip to content

Commit

Permalink
cleanup of PrivateCircuitPublicInputs.hash
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Feb 1, 2024
1 parent 9701a67 commit 2138b53
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 157 deletions.
1 change: 0 additions & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
62 changes: 31 additions & 31 deletions yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
61 changes: 2 additions & 59 deletions yarn-project/circuits.js/src/abis/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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,
),
Expand Down
1 change: 0 additions & 1 deletion yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
},
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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,
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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<Field, PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH> = 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)
}
}

Expand Down
Loading

0 comments on commit 2138b53

Please sign in to comment.