-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: optimize public call stack item hashing #7330
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...rojects/noir-protocol-circuits/crates/types/src/abis/public_call_stack_item_compressed.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use crate::abis::{call_context::CallContext, function_data::FunctionData, gas::Gas}; | ||
use crate::address::AztecAddress; | ||
use crate::constants::{GENERATOR_INDEX__CALL_STACK_ITEM, PUBLIC_CALL_STACK_ITEM_COMPRESSED_LENGTH}; | ||
use crate::traits::{Hash, Empty, Serialize, Deserialize}; | ||
use crate::utils::reader::Reader; | ||
|
||
/** | ||
* A compressed version of the PublicCallStackItem struct used to compute the "hash" | ||
* of a PublicCallStackItem. | ||
* | ||
* Historically, we have been zeroing most values in the PublicCallStackItem struct | ||
* to compute the hash involved when adding a PublicCallStackItem to the PublicCallStack. | ||
* | ||
* This struct is used to store the values that we did not zero out, and allow us to hash | ||
* only these, thereby skipping a lot of computation and saving us a lot of constraints | ||
* | ||
* Essentially this struct exists such that we don't have a `hash` function in the | ||
* PublicCallStackItem struct that practically throws away some values of the struct | ||
* without clearly indicating that it does so. | ||
*/ | ||
struct PublicCallStackItemCompressed { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add here a comment describing wtf is this and why we need it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will add 🫡 |
||
contract_address: AztecAddress, | ||
call_context: CallContext, | ||
function_data: FunctionData, | ||
args_hash: Field, | ||
returns_hash: Field, | ||
revert_code: u8, | ||
start_gas_left: Gas, | ||
end_gas_left: Gas, | ||
} | ||
|
||
impl Eq for PublicCallStackItemCompressed { | ||
fn eq(self, other: PublicCallStackItemCompressed) -> bool { | ||
(self.contract_address == other.contract_address) | ||
& (self.call_context == other.call_context) | ||
& (self.function_data == other.function_data) | ||
& (self.args_hash == other.args_hash) | ||
& (self.returns_hash == other.returns_hash) | ||
& (self.revert_code == other.revert_code) | ||
& (self.start_gas_left == other.start_gas_left) | ||
& (self.end_gas_left == other.end_gas_left) | ||
} | ||
} | ||
|
||
impl Hash for PublicCallStackItemCompressed { | ||
fn hash(self) -> Field { | ||
std::hash::pedersen_hash_with_separator(self.serialize(), GENERATOR_INDEX__CALL_STACK_ITEM) | ||
} | ||
} | ||
|
||
impl Empty for PublicCallStackItemCompressed { | ||
fn empty() -> Self { | ||
PublicCallStackItemCompressed { | ||
contract_address: AztecAddress::empty(), | ||
call_context: CallContext::empty(), | ||
function_data: FunctionData::empty(), | ||
args_hash: 0, | ||
returns_hash: 0, | ||
revert_code: 0, | ||
start_gas_left: Gas::empty(), | ||
end_gas_left: Gas::empty(), | ||
} | ||
} | ||
} | ||
|
||
impl Serialize<PUBLIC_CALL_STACK_ITEM_COMPRESSED_LENGTH> for PublicCallStackItemCompressed { | ||
fn serialize(self) -> [Field; PUBLIC_CALL_STACK_ITEM_COMPRESSED_LENGTH] { | ||
let mut fields: BoundedVec<Field, PUBLIC_CALL_STACK_ITEM_COMPRESSED_LENGTH> = BoundedVec::new(); | ||
|
||
fields.push(self.contract_address.to_field()); | ||
fields.extend_from_array(self.call_context.serialize()); | ||
fields.extend_from_array(self.function_data.serialize()); | ||
fields.push(self.args_hash); | ||
fields.push(self.returns_hash); | ||
fields.push(self.revert_code as Field); | ||
fields.extend_from_array(self.start_gas_left.serialize()); | ||
fields.extend_from_array(self.end_gas_left.serialize()); | ||
|
||
assert_eq(fields.len(), PUBLIC_CALL_STACK_ITEM_COMPRESSED_LENGTH); | ||
|
||
fields.storage | ||
} | ||
} | ||
|
||
impl Deserialize<PUBLIC_CALL_STACK_ITEM_COMPRESSED_LENGTH> for PublicCallStackItemCompressed { | ||
fn deserialize(fields: [Field; PUBLIC_CALL_STACK_ITEM_COMPRESSED_LENGTH]) -> PublicCallStackItemCompressed { | ||
let mut reader = Reader::new(fields); | ||
|
||
let item = PublicCallStackItemCompressed { | ||
contract_address: reader.read_struct(AztecAddress::deserialize), | ||
call_context: reader.read_struct(CallContext::deserialize), | ||
function_data: reader.read_struct(FunctionData::deserialize), | ||
args_hash: reader.read(), | ||
returns_hash: reader.read(), | ||
revert_code: reader.read() as u8, | ||
start_gas_left: reader.read_struct(Gas::deserialize), | ||
end_gas_left: reader.read_struct(Gas::deserialize), | ||
}; | ||
reader.finish(); | ||
item | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If my understanding is correct, I think this is exploitable. Imagine a public => public call. The AVM, when fullfilling the CALL opcode in the parent, asks the prover to provide a call stack item for the result of the call.
The prover, then spawns a child AVM, runs the function, and provides a call stack item.
Now the parent AVM exposes in its public inputs the hash of the nested call stack item, extracts the return values from the nested call stack item and writes the return values in the memory of the parent public function.
A malicious prover can, if the kernel does not check the return values when comparing the parent_function_avm_public_inputs.call_requests.hash vs the child_function_avm_public_inputs, provide to the parent AVM fake return values that will never be checked by the kernel to be equal to the ones actually returned by the child function. The parent function will think the child function returned something that wasn't returned
I think, the check
request_hash <=> actual hash
should check:In the case of private => public, since no data is exposed back to the caller, then only the intent needs to be checked, that is achieved in master, by this check:
In the case of public => public, it's what I expressed above, both the intent and any data from the result that changes behavior of the caller
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops. It's me that told Lasse that we didn't have to include the return_args hash. Because I think the public call request will be for the private functions only. It’s now being used between public calls because AVM can’t simulate the entire call request at once yet. But you are right it's an exploit at the moment and I guess we can include it now and remove it later!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shouldn't be too complicated i think, It's just adding a few fields to the
PublicCallStackItemCompressed
struct and adding to it the logic ofis_execution_request
to it (maybe we could call itis_request_from_private
?).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will throw them back in, should be fairly straight forward with the struct in place.
So additions would be: