Skip to content

Commit

Permalink
chore: formatted noir-contracts and aztec-nr
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind authored Nov 22, 2023
1 parent f9981d5 commit a73c4aa
Show file tree
Hide file tree
Showing 107 changed files with 699 additions and 1,352 deletions.
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn deserialize(serialized_note: [Field; ADDRESS_NOTE_LEN]) -> AddressNote {
AddressNote::deserialize(serialized_note)
}

fn serialize(note: AddressNote) -> [Field; ADDRESS_NOTE_LEN]{
fn serialize(note: AddressNote) -> [Field; ADDRESS_NOTE_LEN] {
note.serialize()
}

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/address-note/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod address_note;
mod address_note;
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/authwit/src/account.nr
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ impl AccountActions {
pub fn internal_set_is_valid_storage(self, message_hash: Field, value: bool) {
self.approved_action.at(message_hash).write(value);
}
}
}
44 changes: 18 additions & 26 deletions yarn-project/aztec-nr/authwit/src/auth.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,46 @@ global IS_VALID_PUBLIC_SELECTOR = 0xf3661153;
// 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 result = context.call_private_function(on_behalf_of.address, IS_VALID_SELECTOR, [message_hash])[0];
context.push_new_nullifier(message_hash, EMPTY_NULLIFIED_COMMITMENT);
assert(result == IS_VALID_SELECTOR, "Message not authorized by account");
let result = context.call_private_function(on_behalf_of.address, IS_VALID_SELECTOR, [message_hash])[0];
context.push_new_nullifier(message_hash, EMPTY_NULLIFIED_COMMITMENT);
assert(result == IS_VALID_SELECTOR, "Message not authorized by account");
}
// docs:end:assert_valid_authwit

// 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(), context.this_address(), context.selector(), context.args_hash],
GENERATOR_INDEX__SIGNATURE_PAYLOAD
);
assert_valid_authwit(context, on_behalf_of, message_hash);
// message_hash = H(caller, contract_this, selector, args_hash)
let message_hash = pedersen_hash([context.msg_sender(), context.this_address(), context.selector(), context.args_hash],
GENERATOR_INDEX__SIGNATURE_PAYLOAD);
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 result = context.call_public_function(on_behalf_of.address, IS_VALID_PUBLIC_SELECTOR, [message_hash])[0];
context.push_new_nullifier(message_hash, EMPTY_NULLIFIED_COMMITMENT);
assert(result == IS_VALID_SELECTOR, "Message not authorized by account");
let result = context.call_public_function(on_behalf_of.address, IS_VALID_PUBLIC_SELECTOR, [message_hash])[0];
context.push_new_nullifier(message_hash, EMPTY_NULLIFIED_COMMITMENT);
assert(result == IS_VALID_SELECTOR, "Message not authorized by account");
}
// docs:end:assert_valid_authwit_public

// 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(), context.this_address(), context.selector(), context.args_hash],
GENERATOR_INDEX__SIGNATURE_PAYLOAD
);
assert_valid_authwit_public(context, on_behalf_of, message_hash);
// message_hash = H(caller, contract_this, selector, args_hash)
let message_hash = pedersen_hash([context.msg_sender(), context.this_address(), context.selector(), context.args_hash],
GENERATOR_INDEX__SIGNATURE_PAYLOAD);
assert_valid_authwit_public(context, on_behalf_of, message_hash);
}
// 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: Field,
args: [Field; N]
) -> Field {
let args_hash = hash_args(args);
pedersen_hash([caller.address, target.address, selector, args_hash], GENERATOR_INDEX__SIGNATURE_PAYLOAD)
pub fn compute_authwit_message_hash<N>(caller: AztecAddress, target: AztecAddress, selector: Field, args: [Field; N]) -> Field {
let args_hash = hash_args(args);
pedersen_hash([caller.address, target.address, selector, args_hash],
GENERATOR_INDEX__SIGNATURE_PAYLOAD)
}
// docs:end:compute_authwit_message_hash
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/authwit/src/auth_witness.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ fn get_auth_witness_oracle<N>(_message_hash: Field) -> [Field; N] {}

unconstrained pub fn get_auth_witness<N>(message_hash: Field) -> [Field; N] {
get_auth_witness_oracle(message_hash)
}
}
4 changes: 2 additions & 2 deletions yarn-project/aztec-nr/authwit/src/entrypoint.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::aztec::types::vec::BoundedVec;
use dep::aztec::hash::pedersen_hash;
use dep::aztec::context::PrivateContext;
use dep::aztec::private_call_stack_item::PrivateCallStackItem;
use dep::aztec::public_call_stack_item::PublicCallStackItem;
use dep::aztec::public_call_stack_item::PublicCallStackItem;
use dep::aztec::constants_gen::GENERATOR_INDEX__SIGNATURE_PAYLOAD;

global ACCOUNT_MAX_CALLS: Field = 4;
Expand Down Expand Up @@ -105,4 +105,4 @@ impl EntrypointPayload {
}
}
// docs:end:entrypoint-execute-calls
}
}
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/authwit/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod account;
mod auth_witness;
mod auth;
mod entrypoint;
mod entrypoint;
1 change: 0 additions & 1 deletion yarn-project/aztec-nr/aztec/src/abi.nr
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ impl ContractStorageUpdateRequest {
}
}


struct PublicCircuitPublicInputs {
call_context: CallContext,
args_hash: Field,
Expand Down
5 changes: 3 additions & 2 deletions yarn-project/aztec-nr/aztec/src/address.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ use crate::constants_gen::GENERATOR_INDEX__CONTRACT_ADDRESS;
use crate::hash::pedersen_hash;

pub fn compute_address(pub_key_x: Field, pub_key_y: Field, partial_address: Field) -> Field {
pedersen_hash([pub_key_x, pub_key_y, partial_address], GENERATOR_INDEX__CONTRACT_ADDRESS)
}
pedersen_hash([pub_key_x, pub_key_y, partial_address],
GENERATOR_INDEX__CONTRACT_ADDRESS)
}
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/aztec/src/context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -616,4 +616,4 @@ impl Context {
private: Option::none()
}
}
}
}
34 changes: 17 additions & 17 deletions yarn-project/aztec-nr/aztec/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ use crate::constants_gen::{
};

pub fn sha256_to_field<N>(bytes_to_hash: [u8; N]) -> Field {
let sha256_hashed = sha256(bytes_to_hash);
let sha256_hashed = sha256(bytes_to_hash);

// Convert it to a field element
let mut v = 1;
let mut high = 0 as Field;
let mut low = 0 as Field;
// Convert it to a field element
let mut v = 1;
let mut high = 0 as Field;
let mut low = 0 as Field;

for i in 0..16 {
high = high + (sha256_hashed[15 - i] as Field) * v;
low = low + (sha256_hashed[16 + 15 - i] as Field) * v;
v = v * 256;
}
for i in 0..16 {
high = high + (sha256_hashed[15 - i] as Field) * v;
low = low + (sha256_hashed[16 + 15 - i] as Field) * v;
v = v * 256;
}

// Abuse that a % p + b % p = (a + b) % p and that low < p
let hash_in_a_field = low + high * v;
// Abuse that a % p + b % p = (a + b) % p and that low < p
let hash_in_a_field = low + high * v;

hash_in_a_field
hash_in_a_field
}

pub fn compute_secret_hash(secret: Field) -> Field {
// TODO(#1205) This is probably not the right index to use
pedersen_hash([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET)
// TODO(#1205) This is probably not the right index to use
pedersen_hash([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET)
}

pub fn pedersen_hash<N>(inputs: [Field; N], hash_index: u32) -> Field {
pedersen_hash_with_separator(inputs, hash_index)
}
pedersen_hash_with_separator(inputs, hash_index)
}
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/aztec/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ mod public_call_stack_item;
mod selector;
mod state_vars;
mod types;
mod utils;
mod utils;
12 changes: 3 additions & 9 deletions yarn-project/aztec-nr/aztec/src/log.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ pub fn emit_encrypted_log<N>(
contract_address: Field,
storage_slot: Field,
encryption_pub_key: Point,
log: [Field; N],
log: [Field; N]
) {
let _ = oracle::logs::emit_encrypted_log(contract_address, storage_slot, encryption_pub_key, log);
context.accumulate_encrypted_logs(log);
}

pub fn emit_unencrypted_log<T>(
context: &mut PublicContext,
log: T,
) {
pub fn emit_unencrypted_log<T>(context: &mut PublicContext, log: T) {
let contract_address = context.this_address();
let event_selector = 5; // TODO: compute actual event selector.
let _ = oracle::logs::emit_unencrypted_log(contract_address, event_selector, log);
Expand All @@ -27,10 +24,7 @@ pub fn emit_unencrypted_log<T>(
// --> might be a better approach to force devs to make a public function call that emits the log if needed then
// it would be less easy to accidentally leak information.
// If we decide to keep this function around would make sense to wait for traits and then merge it with emit_unencrypted_log.
pub fn emit_unencrypted_log_from_private<T>(
context: &mut PrivateContext,
log: T,
) {
pub fn emit_unencrypted_log_from_private<T>(context: &mut PrivateContext, log: T) {
let contract_address = context.this_address();
let event_selector = 5; // TODO: compute actual event selector.
let _ = oracle::logs::emit_unencrypted_log(contract_address, event_selector, log);
Expand Down
6 changes: 2 additions & 4 deletions yarn-project/aztec-nr/aztec/src/messaging.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ use l1_to_l2_message_getter_data::make_l1_to_l2_message_getter_data;
use crate::abi::PublicContextInputs;
use crate::oracle::get_l1_to_l2_message::get_l1_to_l2_message_call;


// Returns the nullifier for the message
pub fn process_l1_to_l2_message(l1_to_l2_root: Field, storage_contract_address: Field, msg_key: Field, content: Field, secret: Field) -> Field{

pub fn process_l1_to_l2_message(l1_to_l2_root: Field, storage_contract_address: Field, msg_key: Field, content: Field, secret: Field) -> Field {
let returned_message = get_l1_to_l2_message_call(msg_key);
let l1_to_l2_message_data = make_l1_to_l2_message_getter_data(returned_message, 0, secret);

Expand All @@ -27,4 +25,4 @@ pub fn process_l1_to_l2_message(l1_to_l2_root: Field, storage_contract_address:

// Compute Nullifier
l1_to_l2_message_data.message.compute_nullifier()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ pub fn l1_to_l2_message_getter_len() -> Field {

pub fn make_l1_to_l2_message_getter_data<N>(fields: [Field; N], start: Field, secret: Field) -> L1ToL2MessageGetterData {
L1ToL2MessageGetterData {
message: L1ToL2Message::deserialize(arr_copy_slice(fields, [0; L1_TO_L2_MESSAGE_LENGTH], start), secret, fields[start + L1_TO_L2_MESSAGE_LENGTH]),
message: L1ToL2Message::deserialize(arr_copy_slice(fields, [0; L1_TO_L2_MESSAGE_LENGTH], start),
secret,
fields[start + L1_TO_L2_MESSAGE_LENGTH]),
leaf_index: fields[start + L1_TO_L2_MESSAGE_LENGTH],
sibling_path: arr_copy_slice(fields, [0; L1_TO_L2_MSG_TREE_HEIGHT], L1_TO_L2_MESSAGE_LENGTH + 1),
root: fields[start + L1_TO_L2_MESSAGE_LENGTH + L1_TO_L2_MSG_TREE_HEIGHT + 1],
sibling_path: arr_copy_slice(fields,
[0; L1_TO_L2_MSG_TREE_HEIGHT],
L1_TO_L2_MESSAGE_LENGTH + 1),
root: fields[start + L1_TO_L2_MESSAGE_LENGTH + L1_TO_L2_MSG_TREE_HEIGHT + 1]
}
}
}
2 changes: 1 addition & 1 deletion yarn-project/aztec-nr/aztec/src/note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ mod note_hash;
mod note_header;
mod note_interface;
mod note_viewer_options;
mod utils;
mod utils;
12 changes: 4 additions & 8 deletions yarn-project/aztec-nr/aztec/src/note/lifecycle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn create_note<Note, N>(
storage_slot: Field,
note: &mut Note,
note_interface: NoteInterface<Note, N>,
broadcast: bool,
broadcast: bool
) {
let contract_address = (*context).this_address();

Expand All @@ -41,7 +41,7 @@ pub fn create_note_hash_from_public<Note, N>(
context: &mut PublicContext,
storage_slot: Field,
note: &mut Note,
note_interface: NoteInterface<Note, N>,
note_interface: NoteInterface<Note, N>
) {
let contract_address = (*context).this_address();

Expand All @@ -53,11 +53,7 @@ pub fn create_note_hash_from_public<Note, N>(
context.push_new_note_hash(inner_note_hash);
}

pub fn destroy_note<Note, N>(
context: &mut PrivateContext,
note: Note,
note_interface: NoteInterface<Note, N>,
) {
pub fn destroy_note<Note, N>(context: &mut PrivateContext, note: Note, note_interface: NoteInterface<Note, N>) {
let mut nullifier = 0;
let mut nullified_commitment: Field = EMPTY_NULLIFIED_COMMITMENT;
let compute_nullifier = note_interface.compute_nullifier;
Expand All @@ -78,4 +74,4 @@ pub fn destroy_note<Note, N>(
assert(notify_nullified_note(nullifier, nullified_commitment) == 0);

context.push_new_nullifier(nullifier, nullified_commitment)
}
}
Loading

0 comments on commit a73c4aa

Please sign in to comment.