Skip to content

Commit

Permalink
chore!: move noir out of yarn-project (#4479)
Browse files Browse the repository at this point in the history
Fixes #4107
  • Loading branch information
superstar0402 committed Mar 19, 2024
1 parent 509f6c9 commit 1c212a2
Show file tree
Hide file tree
Showing 55 changed files with 648 additions and 825 deletions.
36 changes: 13 additions & 23 deletions address-note/src/address_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@
use dep::aztec::log::emit_encrypted_log;
// docs:end:encrypted_import
use dep::aztec::{
protocol_types::{
address::AztecAddress,
traits::Empty
},
note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_consumption,
},
oracle::{
rand::rand,
nullifier_key::get_nullifier_secret_key,
get_public_key::get_public_key,
},
hash::pedersen_hash,
context::PrivateContext
protocol_types::{address::AztecAddress, traits::Empty},
note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_consumption},
oracle::{rand::rand, nullifier_key::get_nullifier_secret_key, get_public_key::get_public_key},
hash::pedersen_hash, context::PrivateContext
};

global ADDRESS_NOTE_LEN: Field = 3;
Expand Down Expand Up @@ -88,22 +76,24 @@ impl NoteInterface<ADDRESS_NOTE_LEN> for AddressNote {
context,
(*context).this_address(),
slot,
Self::get_note_type_id(),
encryption_pub_key,
self.serialize_content(),
);
// docs:end:encrypted
}

fn get_note_type_id() -> Field {
// TODO(#4519): autogenerate
// python -c "print(int(''.join(str(ord(c)) for c in 'AddressNote')))"
6510010011410111511578111116101
}
}

impl AddressNote {
pub fn new(address: AztecAddress, owner: AztecAddress) -> Self {
let randomness = rand();
AddressNote {
address,
owner,
randomness,
header: NoteHeader::empty(),
}
AddressNote { address, owner, randomness, header: NoteHeader::empty() }
}
// docs:end:address_note_def
// docs:end:address_note_def
}
112 changes: 67 additions & 45 deletions authwit/src/account.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use dep::aztec::context::{PrivateContext, PublicContext, Context};
use dep::aztec::state_vars::{map::Map, public_state::PublicState};

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

struct AccountActions {
Expand All @@ -11,57 +11,79 @@ struct AccountActions {
}

impl AccountActions {
pub fn init(context: Context, approved_action_storage_slot: Field, is_valid_impl: fn(&mut PrivateContext, Field) -> bool) -> Self {
AccountActions {
context: context,
is_valid_impl: is_valid_impl,
approved_action: Map::new(
context,
approved_action_storage_slot,
|context, slot| {
pub fn init(
context: Context,
approved_action_storage_slot: Field,
is_valid_impl: fn(&mut PrivateContext, Field) -> bool
) -> Self {
AccountActions {
context,
is_valid_impl,
approved_action: Map::new(
context,
approved_action_storage_slot,
|context, slot| {
PublicState::new(context, slot)
},
),
}
)
}
}
}

pub fn private(context: &mut PrivateContext, approved_action_storage_slot: Field, is_valid_impl: fn(&mut PrivateContext, Field) -> bool) -> Self {
AccountActions::init(Context::private(context), approved_action_storage_slot, is_valid_impl)
}
pub fn private(
context: &mut PrivateContext,
approved_action_storage_slot: Field,
is_valid_impl: fn(&mut PrivateContext, Field) -> bool
) -> Self {
AccountActions::init(
Context::private(context),
approved_action_storage_slot,
is_valid_impl
)
}

pub fn public(
context: &mut PublicContext,
approved_action_storage_slot: Field,
is_valid_impl: fn(&mut PrivateContext, Field) -> bool
) -> Self {
AccountActions::init(
Context::public(context),
approved_action_storage_slot,
is_valid_impl
)
}

// docs:start:entrypoint
pub fn entrypoint(self, app_payload: AppPayload, fee_payload: FeePayload) {
let valid_fn = self.is_valid_impl;
let mut private_context = self.context.private.unwrap();

pub fn public(context: &mut PublicContext, approved_action_storage_slot: Field, is_valid_impl: fn(&mut PrivateContext, Field) -> bool) -> Self {
AccountActions::init(Context::public(context), approved_action_storage_slot, is_valid_impl)
}

// docs:start:entrypoint
pub fn entrypoint(self, payload: EntrypointPayload) {
let message_hash = payload.hash();
let valid_fn = self.is_valid_impl;
let private_context = self.context.private.unwrap();
assert(valid_fn(private_context, message_hash));
payload.execute_calls(private_context);
}
// docs:end:entrypoint
let fee_hash = fee_payload.hash();
assert(valid_fn(private_context, fee_hash));
fee_payload.execute_calls(private_context);
private_context.capture_max_non_revertible_side_effect_counter();

pub fn is_valid(self, message_hash: Field) -> Field {
let valid_fn = self.is_valid_impl;
if (valid_fn(self.context.private.unwrap(), message_hash)) {
IS_VALID_SELECTOR
} else {
0
let app_hash = app_payload.hash();
assert(valid_fn(private_context, app_hash));
app_payload.execute_calls(private_context);
}
}
// docs:end:entrypoint

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
pub fn is_valid(self, message_hash: Field) -> Field {
let valid_fn = self.is_valid_impl;
if (valid_fn(self.context.private.unwrap(), message_hash)) {
IS_VALID_SELECTOR
} else {
0
}
}
}

pub fn internal_set_is_valid_storage(self, message_hash: Field, value: bool) {
self.approved_action.at(message_hash).write(value);
}
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 }
}

pub fn internal_set_is_valid_storage(self, message_hash: Field, value: bool) {
self.approved_action.at(message_hash).write(value);
}
}
17 changes: 3 additions & 14 deletions authwit/src/auth.nr
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
use dep::aztec::protocol_types::{
abis::function_selector::FunctionSelector,
address::AztecAddress,
constants::{
GENERATOR_INDEX__SIGNATURE_PAYLOAD,
},
hash::{
hash_args,
pedersen_hash,
},
};
use dep::aztec::context::{
PrivateContext,
PublicContext,
Context,
abis::function_selector::FunctionSelector, address::AztecAddress,
constants::{GENERATOR_INDEX__SIGNATURE_PAYLOAD}, hash::{hash_args, pedersen_hash}
};
use dep::aztec::context::{PrivateContext, PublicContext, Context};

global IS_VALID_SELECTOR = 0xe86ab4ff;
global IS_VALID_PUBLIC_SELECTOR = 0xf3661153;
Expand Down
119 changes: 3 additions & 116 deletions authwit/src/entrypoint.nr
Original file line number Diff line number Diff line change
@@ -1,116 +1,3 @@
use dep::aztec::context::PrivateContext;
use dep::aztec::protocol_types::{
abis::function_selector::FunctionSelector,
address::AztecAddress,
constants::GENERATOR_INDEX__SIGNATURE_PAYLOAD,
hash::pedersen_hash,
traits::{Hash, Serialize}
};

global ACCOUNT_MAX_CALLS: Field = 4;
// 1 (ARGS_HASH) + 1 (FUNCTION_SELECTOR) + 1 (TARGET_ADDRESS) + 1 (IS_PUBLIC)
global FUNCTION_CALL_SIZE: Field = 4;
// 3 * 32 + 1
global FUNCTION_CALL_SIZE_IN_BYTES: Field = 97;

struct FunctionCall {
args_hash: Field,
function_selector: FunctionSelector,
target_address: AztecAddress,
is_public: bool,
}

impl Serialize<FUNCTION_CALL_SIZE> for FunctionCall {
fn serialize(self) -> [Field; FUNCTION_CALL_SIZE] {
[self.args_hash, self.function_selector.to_field(), self.target_address.to_field(), self.is_public as Field]
}
}

impl FunctionCall {
fn to_be_bytes(self) -> [u8; FUNCTION_CALL_SIZE_IN_BYTES] {
let mut bytes: [u8; FUNCTION_CALL_SIZE_IN_BYTES] = [0; FUNCTION_CALL_SIZE_IN_BYTES];
let args_hash_bytes = self.args_hash.to_be_bytes(32);
for i in 0..32 { bytes[i] = args_hash_bytes[i]; }
let function_selector_bytes = self.function_selector.to_field().to_be_bytes(32);
for i in 0..32 { bytes[i + 32] = function_selector_bytes[i]; }
let target_address_bytes = self.target_address.to_field().to_be_bytes(32);
for i in 0..32 { bytes[i + 64] = target_address_bytes[i]; }
bytes[96] = self.is_public as u8;
bytes
}
}

// FUNCTION_CALL_SIZE * ACCOUNT_MAX_CALLS + 1
global ENTRYPOINT_PAYLOAD_SIZE: Field = 17;
// FUNCTION_CALL_SIZE_IN_BYTES * ACCOUNT_MAX_CALLS + 32
global ENTRYPOINT_PAYLOAD_SIZE_IN_BYTES: Field = 420;

// Note: If you change the following struct you have to update default_entrypoint.ts
// docs:start:entrypoint-struct
struct EntrypointPayload {
function_calls: [FunctionCall; ACCOUNT_MAX_CALLS],
nonce: Field,
}
// docs:end:entrypoint-struct

impl Serialize<ENTRYPOINT_PAYLOAD_SIZE> for EntrypointPayload {
// Serializes the entrypoint struct
fn serialize(self) -> [Field; ENTRYPOINT_PAYLOAD_SIZE] {
let mut fields: BoundedVec<Field, ENTRYPOINT_PAYLOAD_SIZE> = BoundedVec::new(0);
for call in self.function_calls {
fields.extend_from_array(call.serialize());
}
fields.push(self.nonce);
fields.storage
}
}

impl Hash for EntrypointPayload {
fn hash(self) -> Field {
pedersen_hash(
self.serialize(),
GENERATOR_INDEX__SIGNATURE_PAYLOAD
)
}
}

impl EntrypointPayload {
// Serializes the payload as an array of bytes. Useful for hashing with sha256.
fn to_be_bytes(self) -> [u8; ENTRYPOINT_PAYLOAD_SIZE_IN_BYTES] {
let mut bytes: [u8; ENTRYPOINT_PAYLOAD_SIZE_IN_BYTES] = [0; ENTRYPOINT_PAYLOAD_SIZE_IN_BYTES];

for i in 0..ACCOUNT_MAX_CALLS {
let item_bytes = self.function_calls[i].to_be_bytes();
for j in 0..FUNCTION_CALL_SIZE_IN_BYTES {
bytes[i * FUNCTION_CALL_SIZE_IN_BYTES + j] = item_bytes[j];
}
}

let nonce_bytes = self.nonce.to_be_bytes(32);
let nonce_offset = FUNCTION_CALL_SIZE_IN_BYTES * ACCOUNT_MAX_CALLS;
for j in 0..32 {
bytes[nonce_offset + j] = nonce_bytes[j];
}

bytes
}

// Executes all private and public calls
// docs:start:entrypoint-execute-calls
fn execute_calls(self, context: &mut PrivateContext) {
for call in self.function_calls {
if !call.target_address.is_zero() {
if call.is_public {
context.call_public_function_with_packed_args(
call.target_address, call.function_selector, call.args_hash
);
} else {
let _result = context.call_private_function_with_packed_args(
call.target_address, call.function_selector, call.args_hash
);
}
}
}
}
// docs:end:entrypoint-execute-calls
}
mod fee;
mod app;
mod function_call;
Loading

0 comments on commit 1c212a2

Please sign in to comment.