Skip to content

Commit

Permalink
refactor: Optimize private call stack item hash for gate count
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Jul 3, 2024
1 parent fa9c94d commit 9ed5785
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,17 @@ impl Deserialize<PRIVATE_CALL_STACK_ITEM_LENGTH> for PrivateCallStackItem {

impl Hash for PrivateCallStackItem {
fn hash(self) -> Field {
pedersen_hash(self.serialize(), GENERATOR_INDEX__CALL_STACK_ITEM)
// We don't need a hash of the entire private call stack item, just a few pieces.
// See Issue(#7092) for more details.
pedersen_hash([
self.contract_address.to_field(),
self.public_inputs.call_context.hash(),
self.function_data.hash(),
self.public_inputs.args_hash,
self.public_inputs.returns_hash,
self.public_inputs.start_side_effect_counter as Field,
self.public_inputs.end_side_effect_counter as Field,
], GENERATOR_INDEX__CALL_STACK_ITEM)
}
}

Expand Down Expand Up @@ -85,6 +95,6 @@ fn empty_hash() {
let hash = item.hash();

// Value from private_call_stack_item.test.ts "computes empty item hash" test
let test_data_empty_hash = 0x157022d579f892f06461fb895cdf5550b24329e15e7a41df14f9dad582fa1bc5;
let test_data_empty_hash = 0x2a876837d4bf400bf6787126331db78e53027a9ba5d91d9c974dbab1d41fe37c;
assert_eq(hash, test_data_empty_hash);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PrivateCallStackItem computes empty item hash 1`] = `Fr<0x157022d579f892f06461fb895cdf5550b24329e15e7a41df14f9dad582fa1bc5>`;
exports[`PrivateCallStackItem computes empty item hash 1`] = `Fr<0x2a876837d4bf400bf6787126331db78e53027a9ba5d91d9c974dbab1d41fe37c>`;

exports[`PrivateCallStackItem computes hash 1`] = `Fr<0x029b1573da033e679c68a55e05987602c5e73419c4fdfdd6104e178a9a360834>`;
exports[`PrivateCallStackItem computes hash 1`] = `Fr<0x12a821d797cdb85d17a98ad9e763334df45f8de9d5e027050ddac007f35fc6ac>`;
7 changes: 6 additions & 1 deletion yarn-project/circuits.js/src/structs/call_context.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { FunctionSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { pedersenHash } from '@aztec/foundation/crypto';
import { Fr } from '@aztec/foundation/fields';
import { BufferReader, FieldReader, serializeToBuffer, serializeToFields } from '@aztec/foundation/serialize';
import { type FieldsOf } from '@aztec/foundation/types';

import { CALL_CONTEXT_LENGTH } from '../constants.gen.js';
import { CALL_CONTEXT_LENGTH, GeneratorIndex } from '../constants.gen.js';

/**
* Call context.
Expand Down Expand Up @@ -125,4 +126,8 @@ export class CallContext {
callContext.sideEffectCounter === this.sideEffectCounter
);
}

hash(): Fr {
return pedersenHash(this.toFields(), GeneratorIndex.CALL_CONTEXT);
}
}
11 changes: 10 additions & 1 deletion yarn-project/circuits.js/src/structs/private_call_stack_item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ export class PrivateCallStackItem {
* @returns Hash.
*/
public hash(): Fr {
return pedersenHash(this.toFields(), GeneratorIndex.CALL_STACK_ITEM);
const fields = [
this.contractAddress.toField(),
this.publicInputs.callContext.hash(),
this.functionData.hash(),
this.publicInputs.argsHash,
this.publicInputs.returnsHash,
this.publicInputs.startSideEffectCounter,
this.publicInputs.endSideEffectCounter,
];
return pedersenHash(fields, GeneratorIndex.CALL_STACK_ITEM);
}
}

0 comments on commit 9ed5785

Please sign in to comment.