Skip to content
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

feat: Sync from noir #9332

Merged
merged 20 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .noir-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ae87d287ab1fae0f999dfd0d1166fbddb927ba97
62404d7ff349ddf7551f2efd865adafc5213a742
36 changes: 19 additions & 17 deletions noir-projects/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use dep::aztec::{
protocol_types::{address::AztecAddress, constants::GENERATOR_INDEX__NOTE_NULLIFIER, hash::poseidon2_hash_with_separator},
note::{note_header::NoteHeader, note_interface::NullifiableNote, utils::compute_note_hash_for_nullify},
oracle::random::random, keys::getters::get_nsk_app, context::PrivateContext, macros::notes::note
protocol_types::{
address::AztecAddress, constants::GENERATOR_INDEX__NOTE_NULLIFIER,
hash::poseidon2_hash_with_separator,
},
note::{
note_header::NoteHeader, note_interface::NullifiableNote,
utils::compute_note_hash_for_nullify,
}, oracle::random::random, keys::getters::get_nsk_app, context::PrivateContext,
macros::notes::note,
};

// docs:start:address_note_def
Expand All @@ -18,26 +24,24 @@ pub struct AddressNote {

impl NullifiableNote for AddressNote {

fn compute_nullifier(self, context: &mut PrivateContext, note_hash_for_nullify: Field) -> Field {
fn compute_nullifier(
self,
context: &mut PrivateContext,
note_hash_for_nullify: Field,
) -> Field {
let secret = context.request_nsk_app(self.npk_m_hash);
poseidon2_hash_with_separator(
[
note_hash_for_nullify,
secret
],
GENERATOR_INDEX__NOTE_NULLIFIER as Field
[note_hash_for_nullify, secret],
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
)
}

unconstrained fn compute_nullifier_without_context(self) -> Field {
let note_hash_for_nullify = compute_note_hash_for_nullify(self);
let secret = get_nsk_app(self.npk_m_hash);
poseidon2_hash_with_separator(
[
note_hash_for_nullify,
secret
],
GENERATOR_INDEX__NOTE_NULLIFIER as Field
[note_hash_for_nullify, secret],
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
)
}
}
Expand All @@ -48,9 +52,7 @@ impl AddressNote {
// malicious sender could use non-random values to make the note less private. But they already know the full
// note pre-image anyway, and so the recipient already trusts them to not disclose this information. We can
// therefore assume that the sender will cooperate in the random value generation.
let randomness = unsafe {
random()
};
let randomness = unsafe { random() };
AddressNote { address, npk_m_hash, randomness, header: NoteHeader::empty() }
}
// docs:end:address_note_def
Expand Down
13 changes: 7 additions & 6 deletions noir-projects/aztec-nr/authwit/src/account.nr
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use dep::aztec::{
context::PrivateContext,
protocol_types::constants::{GENERATOR_INDEX__COMBINED_PAYLOAD, GENERATOR_INDEX__TX_NULLIFIER},
hash::poseidon2_hash_with_separator
hash::poseidon2_hash_with_separator,
};

use crate::entrypoint::{app::AppPayload, fee::FeePayload};
use crate::auth::{IS_VALID_SELECTOR, compute_authwit_message_hash};

pub struct AccountActions<Context> {
context: Context,
is_valid_impl: fn(&mut PrivateContext, Field) -> bool,
context: Context,
is_valid_impl: fn(&mut PrivateContext, Field) -> bool,
}

impl<Context> AccountActions<Context> {
Expand Down Expand Up @@ -40,15 +40,16 @@ impl AccountActions<&mut PrivateContext> {

let combined_payload_hash = poseidon2_hash_with_separator(
[app_payload.hash(), fee_payload.hash()],
GENERATOR_INDEX__COMBINED_PAYLOAD
GENERATOR_INDEX__COMBINED_PAYLOAD,
);
assert(valid_fn(self.context, combined_payload_hash));

fee_payload.execute_calls(self.context);
self.context.end_setup();
app_payload.execute_calls(self.context);
if cancellable {
let tx_nullifier = poseidon2_hash_with_separator([app_payload.nonce], GENERATOR_INDEX__TX_NULLIFIER);
let tx_nullifier =
poseidon2_hash_with_separator([app_payload.nonce], GENERATOR_INDEX__TX_NULLIFIER);
self.context.push_nullifier(tx_nullifier);
}
}
Expand All @@ -73,7 +74,7 @@ impl AccountActions<&mut PrivateContext> {
self.context.msg_sender(),
self.context.chain_id(),
self.context.version(),
inner_hash
inner_hash,
);
let valid_fn = self.is_valid_impl;
assert(valid_fn(self.context, message_hash) == true, "Message not authorized by account");
Expand Down
120 changes: 67 additions & 53 deletions noir-projects/aztec-nr/authwit/src/auth.nr
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use dep::aztec::protocol_types::{
abis::function_selector::FunctionSelector, address::AztecAddress,
constants::{
GENERATOR_INDEX__AUTHWIT_INNER, GENERATOR_INDEX__AUTHWIT_OUTER, GENERATOR_INDEX__AUTHWIT_NULLIFIER,
CANONICAL_AUTH_REGISTRY_ADDRESS
},
hash::poseidon2_hash_with_separator
GENERATOR_INDEX__AUTHWIT_INNER, GENERATOR_INDEX__AUTHWIT_OUTER,
GENERATOR_INDEX__AUTHWIT_NULLIFIER, CANONICAL_AUTH_REGISTRY_ADDRESS,
}, hash::poseidon2_hash_with_separator,
};
use dep::aztec::{context::{PrivateContext, PublicContext, gas::GasOpts}, hash::hash_args_array};

Expand Down Expand Up @@ -201,7 +200,11 @@ global IS_VALID_SELECTOR = 0x47dacd73; // 4 last bytes of poseidon2_hash_bytes("
*/
// docs:start:assert_current_call_valid_authwit
pub fn assert_current_call_valid_authwit(context: &mut PrivateContext, on_behalf_of: AztecAddress) {
let inner_hash = compute_inner_authwit_hash([context.msg_sender().to_field(), context.selector().to_field(), context.args_hash]);
let inner_hash = compute_inner_authwit_hash([
context.msg_sender().to_field(),
context.selector().to_field(),
context.args_hash,
]);
assert_inner_hash_valid_authwit(context, on_behalf_of, inner_hash);
}
// docs:end:assert_current_call_valid_authwit
Expand All @@ -215,15 +218,19 @@ pub fn assert_current_call_valid_authwit(context: &mut PrivateContext, on_behalf
* @param on_behalf_of The address that have authorized the current call
* @param inner_hash The hash of the message to authorize
*/
pub fn assert_inner_hash_valid_authwit(context: &mut PrivateContext, on_behalf_of: AztecAddress, inner_hash: Field) {
pub fn assert_inner_hash_valid_authwit(
context: &mut PrivateContext,
on_behalf_of: AztecAddress,
inner_hash: Field,
) {
// We perform a static call here and not a standard one to ensure that the account contract cannot re-enter.
let result: Field = context.static_call_private_function(
on_behalf_of,
comptime {
FunctionSelector::from_signature("verify_private_authwit(Field)")
},
[inner_hash]
).unpack_into();
let result: Field = context
.static_call_private_function(
on_behalf_of,
comptime { FunctionSelector::from_signature("verify_private_authwit(Field)") },
[inner_hash],
)
.unpack_into();
assert(result == IS_VALID_SELECTOR, "Message not authorized by account");
// Compute the nullifier, similar computation to the outer hash, but without the chain_id and version.
// Those should already be handled in the verification, so we just need something to nullify, that allow same inner_hash for multiple actors.
Expand All @@ -245,11 +252,13 @@ pub fn assert_inner_hash_valid_authwit(context: &mut PrivateContext, on_behalf_o
// docs:start:assert_current_call_valid_authwit_public
pub fn assert_current_call_valid_authwit_public(
context: &mut PublicContext,
on_behalf_of: AztecAddress
on_behalf_of: AztecAddress,
) {
let inner_hash = compute_inner_authwit_hash(
[(*context).msg_sender().to_field(), (*context).selector().to_field(), (*context).get_args_hash()]
);
let inner_hash = compute_inner_authwit_hash([
(*context).msg_sender().to_field(),
(*context).selector().to_field(),
(*context).get_args_hash(),
]);
assert_inner_hash_valid_authwit_public(context, on_behalf_of, inner_hash);
}
// docs:end:assert_current_call_valid_authwit_public
Expand All @@ -265,15 +274,19 @@ pub fn assert_current_call_valid_authwit_public(
*
* @param on_behalf_of The address that have authorized the `inner_hash`
*/
pub fn assert_inner_hash_valid_authwit_public(context: &mut PublicContext, on_behalf_of: AztecAddress, inner_hash: Field) {
let result: Field = context.call_public_function(
CANONICAL_AUTH_REGISTRY_ADDRESS,
comptime {
FunctionSelector::from_signature("consume((Field),Field)")
},
[on_behalf_of.to_field(), inner_hash].as_slice(),
GasOpts::default()
).deserialize_into();
pub fn assert_inner_hash_valid_authwit_public(
context: &mut PublicContext,
on_behalf_of: AztecAddress,
inner_hash: Field,
) {
let result: Field = context
.call_public_function(
CANONICAL_AUTH_REGISTRY_ADDRESS,
comptime { FunctionSelector::from_signature("consume((Field),Field)") },
[on_behalf_of.to_field(), inner_hash].as_slice(),
GasOpts::default(),
)
.deserialize_into();
assert(result == IS_VALID_SELECTOR, "Message not authorized by account");
}

Expand All @@ -298,10 +311,11 @@ pub fn compute_authwit_message_hash_from_call<let N: u32>(
chain_id: Field,
version: Field,
selector: FunctionSelector,
args: [Field; N]
args: [Field; N],
) -> Field {
let args_hash = hash_args_array(args);
let inner_hash = compute_inner_authwit_hash([caller.to_field(), selector.to_field(), args_hash]);
let inner_hash =
compute_inner_authwit_hash([caller.to_field(), selector.to_field(), args_hash]);
compute_authwit_message_hash(consumer, chain_id, version, inner_hash)
}
// docs:end:compute_authwit_message_hash_from_call
Expand Down Expand Up @@ -329,7 +343,7 @@ pub fn compute_inner_authwit_hash<let N: u32>(args: [Field; N]) -> Field {
pub fn compute_authwit_nullifier(on_behalf_of: AztecAddress, inner_hash: Field) -> Field {
poseidon2_hash_with_separator(
[on_behalf_of.to_field(), inner_hash],
GENERATOR_INDEX__AUTHWIT_NULLIFIER
GENERATOR_INDEX__AUTHWIT_NULLIFIER,
)
}

Expand All @@ -341,15 +355,15 @@ pub fn compute_authwit_nullifier(on_behalf_of: AztecAddress, inner_hash: Field)
* @param version The version of the chain that the message is being consumed on
* @param inner_hash The hash of the "inner" message that is being consumed
*/
pub fn compute_authwit_message_hash(consumer: AztecAddress, chain_id: Field, version: Field, inner_hash: Field) -> Field {
pub fn compute_authwit_message_hash(
consumer: AztecAddress,
chain_id: Field,
version: Field,
inner_hash: Field,
) -> Field {
poseidon2_hash_with_separator(
[
consumer.to_field(),
chain_id,
version,
inner_hash
],
GENERATOR_INDEX__AUTHWIT_OUTER
[consumer.to_field(), chain_id, version, inner_hash],
GENERATOR_INDEX__AUTHWIT_OUTER,
)
}

Expand All @@ -362,14 +376,14 @@ pub fn compute_authwit_message_hash(consumer: AztecAddress, chain_id: Field, ver
* @param authorize True if the message should be authorized, false if it should be revoked
*/
pub fn set_authorized(context: &mut PublicContext, message_hash: Field, authorize: bool) {
context.call_public_function(
CANONICAL_AUTH_REGISTRY_ADDRESS,
comptime {
FunctionSelector::from_signature("set_authorized(Field,bool)")
},
[message_hash, authorize as Field].as_slice(),
GasOpts::default()
).assert_empty();
context
.call_public_function(
CANONICAL_AUTH_REGISTRY_ADDRESS,
comptime { FunctionSelector::from_signature("set_authorized(Field,bool)") },
[message_hash, authorize as Field].as_slice(),
GasOpts::default(),
)
.assert_empty();
}

/**
Expand All @@ -380,12 +394,12 @@ pub fn set_authorized(context: &mut PublicContext, message_hash: Field, authoriz
* @param reject True if all authwits should be rejected, false otherwise
*/
pub fn set_reject_all(context: &mut PublicContext, reject: bool) {
context.call_public_function(
CANONICAL_AUTH_REGISTRY_ADDRESS,
comptime {
FunctionSelector::from_signature("set_reject_all(bool)")
},
[context.this_address().to_field(), reject as Field].as_slice(),
GasOpts::default()
).assert_empty();
context
.call_public_function(
CANONICAL_AUTH_REGISTRY_ADDRESS,
comptime { FunctionSelector::from_signature("set_reject_all(bool)") },
[context.this_address().to_field(), reject as Field].as_slice(),
GasOpts::default(),
)
.assert_empty();
}
27 changes: 18 additions & 9 deletions noir-projects/aztec-nr/authwit/src/cheatcodes.nr
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
use dep::aztec::{
protocol_types::address::AztecAddress,
context::{public_context::PublicContext, call_interfaces::CallInterface}, test::helpers::cheatcodes,
oracle::execution::{get_block_number, get_contract_address}, hash::hash_args
context::{public_context::PublicContext, call_interfaces::CallInterface},
test::helpers::cheatcodes, oracle::execution::{get_block_number, get_contract_address},
hash::hash_args,
};

use crate::auth::{compute_inner_authwit_hash, compute_authwit_message_hash, set_authorized};

pub fn add_private_authwit_from_call_interface<C, let M: u32, T, P, Env>(
on_behalf_of: AztecAddress,
caller: AztecAddress,
call_interface: C
) where C: CallInterface<M, T, P, Env> {
call_interface: C,
)
where
C: CallInterface<M, T, P, Env>,
{
let target = call_interface.get_contract_address();
let inputs = cheatcodes::get_private_context_inputs(get_block_number());
let chain_id = inputs.tx_context.chain_id;
let version = inputs.tx_context.version;
let args_hash = hash_args(call_interface.get_args());
let selector = call_interface.get_selector();
let inner_hash = compute_inner_authwit_hash([caller.to_field(), selector.to_field(), args_hash]);
let inner_hash =
compute_inner_authwit_hash([caller.to_field(), selector.to_field(), args_hash]);
let message_hash = compute_authwit_message_hash(target, chain_id, version, inner_hash);
cheatcodes::add_authwit(on_behalf_of, message_hash);
}

pub fn add_public_authwit_from_call_interface<C, let M: u32, T, P, Env>(
on_behalf_of: AztecAddress,
caller: AztecAddress,
call_interface: C
) where C: CallInterface<M, T, P, Env> {
call_interface: C,
)
where
C: CallInterface<M, T, P, Env>,
{
let current_contract = get_contract_address();
cheatcodes::set_contract_address(on_behalf_of);
let target = call_interface.get_contract_address();
Expand All @@ -35,9 +43,10 @@ pub fn add_public_authwit_from_call_interface<C, let M: u32, T, P, Env>(
let version = inputs.tx_context.version;
let args_hash = hash_args(call_interface.get_args());
let selector = call_interface.get_selector();
let inner_hash = compute_inner_authwit_hash([caller.to_field(), selector.to_field(), args_hash]);
let inner_hash =
compute_inner_authwit_hash([caller.to_field(), selector.to_field(), args_hash]);
let message_hash = compute_authwit_message_hash(target, chain_id, version, inner_hash);
let mut context = PublicContext::new(|| {panic( f"Provide args hash manually")});
let mut context = PublicContext::new(|| { panic(f"Provide args hash manually") });
context.args_hash = Option::some(args_hash);
set_authorized(&mut context, message_hash, true);
cheatcodes::set_contract_address(current_contract);
Expand Down
Loading
Loading