-
Notifications
You must be signed in to change notification settings - Fork 609
feat: AUTHWIT cancellations #4799
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,8 +1,9 @@ | ||||||||||
| use dep::aztec::context::{PrivateContext, PublicContext, Context}; | ||||||||||
| use dep::aztec::state_vars::{Map, PublicMutable}; | ||||||||||
| use dep::aztec::protocol_types::{address::AztecAddress, abis::function_selector::FunctionSelector, hash::{pedersen_hash}}; | ||||||||||
|
|
||||||||||
| use crate::entrypoint::{app::AppPayload, fee::FeePayload}; | ||||||||||
| use crate::auth::IS_VALID_SELECTOR; | ||||||||||
| use crate::auth::{IS_VALID_SELECTOR, compute_outer_authwit_hash}; | ||||||||||
|
|
||||||||||
| struct AccountActions { | ||||||||||
| context: Context, | ||||||||||
|
|
@@ -69,21 +70,37 @@ impl AccountActions { | |||||||||
| } | ||||||||||
| // docs:end:entrypoint | ||||||||||
|
|
||||||||||
| pub fn is_valid(self, message_hash: Field) -> Field { | ||||||||||
| // docs:start:spend_private_authwit | ||||||||||
| pub fn spend_private_authwit(self, inner_hash: Field) -> Field { | ||||||||||
| let context = self.context.private.unwrap(); | ||||||||||
| // The `inner_hash` is "siloed" with the `msg_sender` to ensure that only it can | ||||||||||
| // consume the message. | ||||||||||
| // This ensures that contracts cannot consume messages that are not intended for them. | ||||||||||
| let message_hash = compute_outer_authwit_hash(context.msg_sender(), inner_hash); | ||||||||||
| let valid_fn = self.is_valid_impl; | ||||||||||
| if (valid_fn(self.context.private.unwrap(), message_hash)) { | ||||||||||
| IS_VALID_SELECTOR | ||||||||||
| } else { | ||||||||||
| 0 | ||||||||||
| } | ||||||||||
| assert(valid_fn(context, message_hash) == true, "Message not authorized by account"); | ||||||||||
| context.push_new_nullifier(message_hash, 0); | ||||||||||
| IS_VALID_SELECTOR | ||||||||||
| } | ||||||||||
| // docs:end:spend_private_authwit | ||||||||||
|
|
||||||||||
| pub fn is_valid_public(self, message_hash: Field) -> Field { | ||||||||||
| let value = self.approved_action.at(message_hash).read(); | ||||||||||
| if (value) { IS_VALID_SELECTOR } else { 0 } | ||||||||||
| // docs:start:spend_public_authwit | ||||||||||
| pub fn spend_public_authwit(self, inner_hash: Field) -> Field { | ||||||||||
| let context = self.context.public.unwrap(); | ||||||||||
| // The `inner_hash` is "siloed" with the `msg_sender` to ensure that only it can | ||||||||||
| // consume the message. | ||||||||||
| // This ensures that contracts cannot consume messages that are not intended for them. | ||||||||||
| let message_hash = compute_outer_authwit_hash(context.msg_sender(), inner_hash); | ||||||||||
|
Contributor
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.
Suggested change
|
||||||||||
| let is_valid = self.approved_action.at(message_hash).read(); | ||||||||||
| assert(is_valid == true, "Message not authorized by account"); | ||||||||||
| context.push_new_nullifier(message_hash, 0); | ||||||||||
| IS_VALID_SELECTOR | ||||||||||
| } | ||||||||||
| // docs:end:spend_public_authwit | ||||||||||
|
|
||||||||||
| pub fn internal_set_is_valid_storage(self, message_hash: Field, value: bool) { | ||||||||||
| self.approved_action.at(message_hash).write(value); | ||||||||||
| // docs:start:approve_public_authwit | ||||||||||
| pub fn approve_public_authwit(self, message_hash: Field) { | ||||||||||
| self.approved_action.at(message_hash).write(true); | ||||||||||
| } | ||||||||||
| // docs:end:approve_public_authwit | ||||||||||
| } | ||||||||||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,78 +1,48 @@ | ||
| use dep::aztec::protocol_types::{ | ||
| abis::function_selector::FunctionSelector, address::AztecAddress, | ||
| constants::{GENERATOR_INDEX__AUTHWIT}, hash::{hash_args, pedersen_hash} | ||
| constants::{GENERATOR_INDEX__AUTHWIT_INNER, GENERATOR_INDEX__AUTHWIT_OUTER}, | ||
| hash::{hash_args, pedersen_hash} | ||
| }; | ||
| use dep::aztec::context::{PrivateContext, PublicContext, Context}; | ||
|
|
||
| global IS_VALID_SELECTOR = 0xe86ab4ff; | ||
| global IS_VALID_PUBLIC_SELECTOR = 0xf3661153; | ||
|
|
||
| // @todo #2676 Should use different generator than the payload to limit probability of collisions. | ||
|
|
||
| // docs:start:assert_valid_authwit | ||
| // Assert that `on_behalf_of` have authorized `message_hash` with a valid authentication witness | ||
| pub fn assert_valid_authwit( | ||
| context: &mut PrivateContext, | ||
| on_behalf_of: AztecAddress, | ||
| message_hash: Field | ||
| ) { | ||
| let is_valid_selector = FunctionSelector::from_field(IS_VALID_SELECTOR); | ||
| let result = context.call_private_function(on_behalf_of, is_valid_selector, [message_hash])[0]; | ||
| context.push_new_nullifier(message_hash, 0); | ||
| assert(result == IS_VALID_SELECTOR, "Message not authorized by account"); | ||
| } | ||
| // docs:end:assert_valid_authwit | ||
| global IS_VALID_SELECTOR = 0xabf64ad4; // 4 first bytes of keccak256("IS_VALID()") | ||
|
|
||
| // docs:start:assert_current_call_valid_authwit | ||
| // Assert that `on_behalf_of` have authorized the current call with a valid authentication witness | ||
| pub fn assert_current_call_valid_authwit(context: &mut PrivateContext, on_behalf_of: AztecAddress) { | ||
| // message_hash = H(caller, contract_this, selector, args_hash) | ||
| let message_hash = pedersen_hash( | ||
| [ | ||
| context.msg_sender().to_field(), context.this_address().to_field(), context.selector().to_field(), context.args_hash | ||
| ], | ||
| GENERATOR_INDEX__AUTHWIT | ||
| ); | ||
| assert_valid_authwit(context, on_behalf_of, message_hash); | ||
| } | ||
| // docs:end:assert_current_call_valid_authwit | ||
|
|
||
| // docs:start:assert_valid_authwit_public | ||
| // Assert that `on_behalf_of` have authorized `message_hash` in a public context | ||
| pub fn assert_valid_authwit_public(context: &mut PublicContext, on_behalf_of: AztecAddress, message_hash: Field) { | ||
| let is_valid_public_selector = FunctionSelector::from_field(IS_VALID_PUBLIC_SELECTOR); | ||
| let result = context.call_public_function(on_behalf_of, is_valid_public_selector, [message_hash])[0]; | ||
| context.push_new_nullifier(message_hash, 0); | ||
| let function_selector = FunctionSelector::from_signature("spend_private_authwit(Field)"); | ||
| let inner_hash = compute_inner_authwit_hash([context.msg_sender().to_field(), context.selector().to_field(), context.args_hash]); | ||
| let result = context.call_private_function(on_behalf_of, function_selector, [inner_hash])[0]; | ||
| assert(result == IS_VALID_SELECTOR, "Message not authorized by account"); | ||
| } | ||
| // docs:end:assert_valid_authwit_public | ||
| // docs:end:assert_current_call_valid_authwit | ||
|
|
||
| // docs:start:assert_current_call_valid_authwit_public | ||
| // Assert that `on_behalf_of` have authorized the current call in a public context | ||
| pub fn assert_current_call_valid_authwit_public(context: &mut PublicContext, on_behalf_of: AztecAddress) { | ||
| // message_hash = H(caller, contract_this, selector, args_hash) | ||
| let message_hash = pedersen_hash( | ||
| [ | ||
| context.msg_sender().to_field(), context.this_address().to_field(), context.selector().to_field(), context.args_hash | ||
| ], | ||
| GENERATOR_INDEX__AUTHWIT | ||
| ); | ||
| assert_valid_authwit_public(context, on_behalf_of, message_hash); | ||
| let function_selector = FunctionSelector::from_signature("spend_public_authwit(Field)"); | ||
| let inner_hash = compute_inner_authwit_hash([context.msg_sender().to_field(), context.selector().to_field(), context.args_hash]); | ||
| let result = context.call_public_function(on_behalf_of, function_selector, [inner_hash])[0]; | ||
| assert(result == IS_VALID_SELECTOR, "Message not authorized by account"); | ||
| } | ||
| // docs:end:assert_current_call_valid_authwit_public | ||
|
|
||
| // docs:start:compute_authwit_message_hash | ||
| // Compute the message hash to be used by an authentication witness | ||
| pub fn compute_authwit_message_hash<N>( | ||
| caller: AztecAddress, | ||
| target: AztecAddress, | ||
| selector: FunctionSelector, | ||
| args: [Field; N] | ||
| ) -> Field { | ||
| pub fn compute_call_authwit_hash<N>(caller: AztecAddress, consumer: AztecAddress, selector: FunctionSelector, args: [Field; N]) -> Field { | ||
| let args_hash = hash_args(args); | ||
| let inner_hash = compute_inner_authwit_hash([caller.to_field(), selector.to_field(), args_hash]); | ||
| compute_outer_authwit_hash(consumer, inner_hash) | ||
| } | ||
| // docs:end:compute_authwit_message_hash | ||
|
|
||
| pub fn compute_inner_authwit_hash<N>(args: [Field; N]) -> Field { | ||
| pedersen_hash(args, GENERATOR_INDEX__AUTHWIT_INNER) | ||
| } | ||
|
|
||
| pub fn compute_outer_authwit_hash(consumer: AztecAddress, inner_hash: Field) -> Field { | ||
| pedersen_hash( | ||
| [caller.to_field(), target.to_field(), selector.to_field(), args_hash], | ||
| GENERATOR_INDEX__AUTHWIT | ||
| [consumer.to_field(), inner_hash], | ||
| GENERATOR_INDEX__AUTHWIT_OUTER | ||
| ) | ||
| } | ||
| // docs:end:compute_authwit_message_hash |
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I think a small comment here is helpful.