Skip to content

Commit

Permalink
refactor: optimize public call stack item hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Jul 3, 2024
1 parent 9ed5785 commit 411f896
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,20 @@ impl Hash for PublicCallStackItem {
self
};

// We don't need a hash of the entire public call stack item, just a few pieces.
// See Issue(#7092) for more details.
std::hash::pedersen_hash_with_separator([
item.contract_address.to_field(),
item.public_inputs.call_context.hash(),
item.function_data.hash(),
item.public_inputs.hash(),
item.public_inputs.args_hash,
item.public_inputs.returns_hash,
item.public_inputs.start_side_effect_counter as Field,
item.public_inputs.end_side_effect_counter as Field,
item.public_inputs.start_gas_left.da_gas as Field,
item.public_inputs.start_gas_left.l2_gas as Field,
item.public_inputs.end_gas_left.da_gas as Field,
item.public_inputs.end_gas_left.l2_gas as Field,
], GENERATOR_INDEX__CALL_STACK_ITEM)
}
}
Expand Down Expand Up @@ -70,7 +80,7 @@ mod tests {
let call_stack_item = PublicCallStackItem { contract_address, public_inputs, is_execution_request: true, function_data };

// Value from public_call_stack_item.test.ts "Computes a callstack item request hash" test
let test_data_call_stack_item_request_hash = 0x022a2b82af83606ae5a8d4955ef6215e54025193356318aefbde3b5026952953;
let test_data_call_stack_item_request_hash = 0x0a6a98d27839cf03b95f9a3f8c2007a38efec4fae652fc427349bc04a193abca;
assert_eq(call_stack_item.hash(), test_data_call_stack_item_request_hash);
}

Expand All @@ -88,7 +98,7 @@ mod tests {
let call_stack_item = PublicCallStackItem { contract_address, public_inputs, is_execution_request: false, function_data };

// Value from public_call_stack_item.test.ts "Computes a callstack item hash" test
let test_data_call_stack_item_hash = 0x23a1d22e7bf37df7d68e8fcbfb7e016c060194b7915e3771e2dcd72cea26e427;
let test_data_call_stack_item_hash = 0x0a6a98d27839cf03b95f9a3f8c2007a38efec4fae652fc427349bc04a193abca;
assert_eq(call_stack_item.hash(), test_data_call_stack_item_hash);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PublicCallStackItem Computes a callstack item hash 1`] = `"0x23a1d22e7bf37df7d68e8fcbfb7e016c060194b7915e3771e2dcd72cea26e427"`;
exports[`PublicCallStackItem Computes a callstack item hash 1`] = `"0x0a6a98d27839cf03b95f9a3f8c2007a38efec4fae652fc427349bc04a193abca"`;

exports[`PublicCallStackItem Computes a callstack item request hash 1`] = `"0x022a2b82af83606ae5a8d4955ef6215e54025193356318aefbde3b5026952953"`;
exports[`PublicCallStackItem Computes a callstack item request hash 1`] = `"0x0a6a98d27839cf03b95f9a3f8c2007a38efec4fae652fc427349bc04a193abca"`;

exports[`PublicCallStackItem computes empty item hash 1`] = `Fr<0x211932b705c9a5dbbaf2433d2ee4b0a896ef9fb720a4efbe7c1e783747c36588>`;
exports[`PublicCallStackItem computes empty item hash 1`] = `Fr<0x269507fd52cfaa29c326f8193a17b275710eb757143e262ac359ef1222bd9599>`;

exports[`PublicCallStackItem computes hash 1`] = `Fr<0x2d9862fe4fb3db6fe24996cbc3064346aa4551ccd41246c1ca6b002b8b0201fd>`;
exports[`PublicCallStackItem computes hash 1`] = `Fr<0x098302be0e857780f869732281b3282ebcce82948a1bdc2e4b4eb6d59e962b5b>`;
14 changes: 13 additions & 1 deletion yarn-project/circuits.js/src/structs/public_call_stack_item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,19 @@ export class PublicCallStackItem {
}

return pedersenHash(
[this.contractAddress, this.functionData.hash(), publicInputsToHash.hash()],
[
this.contractAddress.toField(),
this.publicInputs.callContext.hash(),
this.functionData.hash(),
this.publicInputs.argsHash,
this.publicInputs.returnsHash,
this.publicInputs.startSideEffectCounter,
this.publicInputs.endSideEffectCounter,
new Fr(this.publicInputs.startGasLeft.daGas),
new Fr(this.publicInputs.startGasLeft.l2Gas),
new Fr(this.publicInputs.endGasLeft.daGas),
new Fr(this.publicInputs.endGasLeft.l2Gas),
],
GeneratorIndex.CALL_STACK_ITEM,
);
}
Expand Down

0 comments on commit 411f896

Please sign in to comment.