Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 24, 2024
1 parent 461f8d0 commit 1c7e8ad
Show file tree
Hide file tree
Showing 408 changed files with 5,429 additions and 8,529 deletions.
26 changes: 9 additions & 17 deletions noir-projects/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
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 @@ -24,15 +18,11 @@ 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,
GENERATOR_INDEX__NOTE_NULLIFIER as Field
)
}

Expand All @@ -41,7 +31,7 @@ impl NullifiableNote for AddressNote {
let secret = get_nsk_app(self.npk_m_hash);
poseidon2_hash_with_separator(
[note_hash_for_nullify, secret],
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
GENERATOR_INDEX__NOTE_NULLIFIER as Field
)
}
}
Expand All @@ -52,7 +42,9 @@ 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
9 changes: 4 additions & 5 deletions noir-projects/aztec-nr/authwit/src/account.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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};
Expand Down Expand Up @@ -40,16 +40,15 @@ 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 @@ -74,7 +73,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
115 changes: 53 additions & 62 deletions noir-projects/aztec-nr/authwit/src/auth.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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 @@ -200,11 +201,13 @@ 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([
let inner_hash = compute_inner_authwit_hash(
[
context.msg_sender().to_field(),
context.selector().to_field(),
context.args_hash,
]);
context.args_hash
]
);
assert_inner_hash_valid_authwit(context, on_behalf_of, inner_hash);
}
// docs:end:assert_current_call_valid_authwit
Expand All @@ -218,19 +221,15 @@ 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 @@ -252,13 +251,15 @@ pub fn assert_inner_hash_valid_authwit(
// 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([
let inner_hash = compute_inner_authwit_hash(
[
(*context).msg_sender().to_field(),
(*context).selector().to_field(),
(*context).get_args_hash(),
]);
(*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 @@ -274,19 +275,15 @@ 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 @@ -311,11 +308,10 @@ 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 @@ -343,7 +339,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 @@ -355,15 +351,10 @@ 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,
GENERATOR_INDEX__AUTHWIT_OUTER
)
}

Expand All @@ -376,14 +367,14 @@ pub fn compute_authwit_message_hash(
* @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 @@ -394,12 +385,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();
}
25 changes: 8 additions & 17 deletions noir-projects/aztec-nr/authwit/src/cheatcodes.nr
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
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>(
on_behalf_of: AztecAddress,
caller: AztecAddress,
call_interface: C,
)
pub fn add_private_authwit_from_call_interface<C, let M: u32>(on_behalf_of: AztecAddress, caller: AztecAddress, call_interface: C)
where
C: CallInterface<M>,
{
C: CallInterface<M>, {
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>(
on_behalf_of: AztecAddress,
caller: AztecAddress,
call_interface: C,
call_interface: C
)
where
C: CallInterface<M>,
{
C: CallInterface<M>, {
let current_contract = get_contract_address();
cheatcodes::set_contract_address(on_behalf_of);
let target = call_interface.get_contract_address();
Expand All @@ -43,8 +35,7 @@ where
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") });
context.args_hash = Option::some(args_hash);
Expand Down
6 changes: 3 additions & 3 deletions noir-projects/aztec-nr/authwit/src/entrypoint/app.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dep::aztec::prelude::PrivateContext;
use dep::aztec::protocol_types::{
constants::GENERATOR_INDEX__SIGNATURE_PAYLOAD, hash::poseidon2_hash_with_separator,
traits::{Hash, Serialize},
traits::{Hash, Serialize}
};

use crate::entrypoint::function_call::FunctionCall;
Expand Down Expand Up @@ -62,14 +62,14 @@ impl AppPayload {
call.target_address,
call.function_selector,
call.args_hash,
call.is_static,
call.is_static
);
} else {
let _result = context.call_private_function_with_packed_args(
call.target_address,
call.function_selector,
call.args_hash,
call.is_static,
call.is_static
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions noir-projects/aztec-nr/authwit/src/entrypoint/fee.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dep::aztec::prelude::PrivateContext;
use dep::aztec::protocol_types::{
constants::GENERATOR_INDEX__FEE_PAYLOAD, hash::poseidon2_hash_with_separator,
traits::{Hash, Serialize},
traits::{Hash, Serialize}
};
use crate::entrypoint::function_call::FunctionCall;

Expand Down Expand Up @@ -61,14 +61,14 @@ impl FeePayload {
call.target_address,
call.function_selector,
call.args_hash,
call.is_static,
call.is_static
);
} else {
let _result = context.call_private_function_with_packed_args(
call.target_address,
call.function_selector,
call.args_hash,
call.is_static,
call.is_static
);
}
}
Expand Down
Loading

0 comments on commit 1c7e8ad

Please sign in to comment.