Skip to content

Commit

Permalink
feat!: deduplicating circuit types + typing everything (AztecProtocol…
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Dec 13, 2023
1 parent d2e8304 commit 8c4684f
Show file tree
Hide file tree
Showing 165 changed files with 2,105 additions and 1,832 deletions.
6 changes: 3 additions & 3 deletions docs/docs/dev_docs/contracts/syntax/context.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ As shown in the snippet, the application context is made up of 4 main structures

First of all, the call context.

#include_code call-context /yarn-project/aztec-nr/aztec/src/abi.nr rust
#include_code call-context /yarn-project/noir-protocol-circuits/src/crates/types/src/abis/call_context.nr rust

The call context contains information about the current call being made:

Expand All @@ -75,13 +75,13 @@ The call context contains information about the current call being made:

Another structure that is contained within the context is the Block Header object. This object is a special one as it contains all of the roots of Aztec's data trees.

#include_code block-header /yarn-project/aztec-nr/aztec/src/abi.nr rust
#include_code block-header /yarn-project/noir-protocol-circuits/src/crates/types/src/abis/block_header.nr rust

### Contract Deployment Data

Just like with the `is_contract_deployment` flag mentioned earlier. This data will only be set to true when the current transaction is one in which a contract is being deployed.

#include_code contract-deployment-data /yarn-project/aztec-nr/aztec/src/abi.nr rust
#include_code contract-deployment-data /yarn-project/noir-protocol-circuits/src/crates/types/src/contrakt/deployment_data.nr rust

### Private Global Variables

Expand Down
4 changes: 2 additions & 2 deletions yarn-project/acir-simulator/src/acvm/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export function extractPrivateCircuitPublicInputs(
witnessReader.readField(),
witnessReader.readField(),
witnessReader.readField(),
Fr.ZERO,
Fr.ZERO, // TODO(#3441)
witnessReader.readField(),
witnessReader.readField(),
);
Expand Down Expand Up @@ -261,7 +261,7 @@ export function extractPublicCircuitPublicInputs(partialWitness: ACVMWitness, ac
witnessReader.readField(),
witnessReader.readField(),
witnessReader.readField(),
Fr.ZERO,
Fr.ZERO, // TODO(#3441)
witnessReader.readField(),
witnessReader.readField(),
);
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ export class AztecNodeService implements AztecNode {
roots[MerkleTreeId.CONTRACT_TREE],
roots[MerkleTreeId.L1_TO_L2_MESSAGES_TREE],
roots[MerkleTreeId.ARCHIVE],
Fr.ZERO,
Fr.ZERO, // TODO(#3441)
roots[MerkleTreeId.PUBLIC_DATA_TREE],
globalsHash,
);
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/aztec-nr/address-note/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ compiler_version = ">=0.18.0"
type = "lib"

[dependencies]
aztec = { path = "../aztec" }
aztec = { path = "../aztec" }
protocol_types = { path = "../../../../noir-protocol-circuits/src/crates/types" }
13 changes: 7 additions & 6 deletions yarn-project/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// docs:start:encrypted_import
use dep::aztec::log::emit_encrypted_log;
// docs:end:encrypted_import
use dep::protocol_types::address::AztecAddress;
use dep::aztec::{
note::{
note_header::NoteHeader,
Expand All @@ -21,14 +22,14 @@ global ADDRESS_NOTE_LEN: Field = 3;
// docs:start:address_note_def
// Stores an address
struct AddressNote {
address: Field,
owner: Field,
address: AztecAddress,
owner: AztecAddress,
randomness: Field,
header: NoteHeader,
}

impl AddressNote {
pub fn new(address: Field, owner: Field) -> Self {
pub fn new(address: AztecAddress, owner: AztecAddress) -> Self {
let randomness = rand();
AddressNote {
address,
Expand All @@ -41,13 +42,13 @@ impl AddressNote {


pub fn serialize(self) -> [Field; ADDRESS_NOTE_LEN]{
[self.address, self.owner, self.randomness]
[self.address.to_field(), self.owner.to_field(), self.randomness]
}

pub fn deserialize(serialized_note: [Field; ADDRESS_NOTE_LEN]) -> Self {
AddressNote {
address: serialized_note[0],
owner: serialized_note[1],
address: AztecAddress::from_field(serialized_note[0]),
owner: AztecAddress::from_field(serialized_note[1]),
randomness: serialized_note[2],
header: NoteHeader::empty(),
}
Expand Down
36 changes: 24 additions & 12 deletions yarn-project/aztec-nr/authwit/src/auth.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
use dep::protocol_types::constants::{EMPTY_NULLIFIED_COMMITMENT, GENERATOR_INDEX__SIGNATURE_PAYLOAD};
use dep::aztec::{
context::{PrivateContext, PublicContext, Context},
types::address::AztecAddress,
abi::hash_args,
hash::pedersen_hash,
use dep::protocol_types::{
abis::function_selector::FunctionSelector,
address::AztecAddress,
constants::{
EMPTY_NULLIFIED_COMMITMENT,
GENERATOR_INDEX__SIGNATURE_PAYLOAD,
},
hash::{
hash_args,
pedersen_hash,
},
};
use dep::aztec::context::{
PrivateContext,
PublicContext,
Context,
};

global IS_VALID_SELECTOR = 0xe86ab4ff;
Expand All @@ -14,7 +24,8 @@ 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];
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, EMPTY_NULLIFIED_COMMITMENT);
assert(result == IS_VALID_SELECTOR, "Message not authorized by account");
}
Expand All @@ -24,7 +35,7 @@ pub fn assert_valid_authwit(context: &mut PrivateContext, on_behalf_of: AztecAdd
// 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],
let message_hash = pedersen_hash([context.msg_sender().to_field(), context.this_address().to_field(), context.selector().to_field(), context.args_hash],
GENERATOR_INDEX__SIGNATURE_PAYLOAD);
assert_valid_authwit(context, on_behalf_of, message_hash);
}
Expand All @@ -33,7 +44,8 @@ pub fn assert_current_call_valid_authwit(context: &mut PrivateContext, on_behalf
// 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];
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, EMPTY_NULLIFIED_COMMITMENT);
assert(result == IS_VALID_SELECTOR, "Message not authorized by account");
}
Expand All @@ -43,17 +55,17 @@ pub fn assert_valid_authwit_public(context: &mut PublicContext, on_behalf_of: Az
// 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],
let message_hash = pedersen_hash([context.msg_sender().to_field(), context.this_address().to_field(), context.selector().to_field(), 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 {
pub fn compute_authwit_message_hash<N>(caller: AztecAddress, target: AztecAddress, selector: FunctionSelector, args: [Field; N]) -> Field {
let args_hash = hash_args(args);
pedersen_hash([caller.address, target.address, selector, args_hash],
pedersen_hash([caller.to_field(), target.to_field(), selector.to_field(), args_hash],
GENERATOR_INDEX__SIGNATURE_PAYLOAD)
}
// docs:end:compute_authwit_message_hash
29 changes: 19 additions & 10 deletions yarn-project/aztec-nr/authwit/src/entrypoint.nr
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
use dep::aztec::abi;
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::protocol_types::constants::GENERATOR_INDEX__SIGNATURE_PAYLOAD;
use dep::protocol_types::{
abis::{
call_stack_item::{
PrivateCallStackItem,
PublicCallStackItem,
},
function_selector::FunctionSelector,
},
address::AztecAddress,
constants::GENERATOR_INDEX__SIGNATURE_PAYLOAD,
hash::pedersen_hash,
};

global ACCOUNT_MAX_CALLS: Field = 4;
// 1 (ARGS_HASH) + 1 (FUNCTION_SELECTOR) + 1 (TARGET_ADDRESS) + 1 (IS_PUBLIC)
Expand All @@ -14,23 +22,23 @@ global FUNCTION_CALL_SIZE_IN_BYTES: Field = 97;

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

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

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_be_bytes(32);
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_be_bytes(32);
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
Expand All @@ -42,6 +50,7 @@ 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],
Expand Down Expand Up @@ -91,7 +100,7 @@ impl EntrypointPayload {
// docs:start:entrypoint-execute-calls
fn execute_calls(self, context: &mut PrivateContext) {
for call in self.function_calls {
if call.target_address != 0 {
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
Expand Down
Loading

0 comments on commit 8c4684f

Please sign in to comment.