Skip to content

Commit

Permalink
feat: Check initializer by default in private functions
Browse files Browse the repository at this point in the history
  • Loading branch information
spalladino committed Feb 28, 2024
1 parent 0414372 commit bb03f28
Show file tree
Hide file tree
Showing 17 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ contract AppSubscriptionContract {

// Constructs the contract
#[aztec(private)]
#[aztec(initializer)]
fn constructor(
target_address: AztecAddress,
subscription_recipient_address: AztecAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ contract Counter {

// docs:start:constructor
#[aztec(private)]
#[aztec(initializer)]
fn constructor(headstart: u64, owner: AztecAddress) {
let counters = storage.counters;
counters.at(owner).add(headstart, owner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ contract EasyPrivateToken {
* initialize the contract's initial state variables.
*/
#[aztec(private)]
#[aztec(initializer)]
fn constructor(initial_supply: u64, owner: AztecAddress) {
let balances = storage.balances;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ contract EasyPrivateVoting {
// docs:end:storage_struct

// docs:start:constructor
#[aztec(private)] // annotation to mark function as private and expose private context
#[aztec(private)]
#[aztec(initializer)] // annotation to mark function as private and expose private context
fn constructor(admin: AztecAddress) { // called when contract is deployed
context.call_public_function(
// we cannot update public state directly from private function but we can call public function (which queues it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ contract EcdsaAccount {

// Creates a new account out of an ECDSA public key to use for signature verification
#[aztec(private)]
#[aztec(initializer)]
fn constructor(signing_pub_key_x: pub [u8; 32], signing_pub_key_y: pub [u8; 32]) {
let this = context.this_address();
let mut pub_key_note = EcdsaPublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ contract Escrow {
// Creates a new instance
// docs:start:constructor
#[aztec(private)]
#[aztec(initializer)]
fn constructor(owner: pub AztecAddress) {
let this = context.this_address();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ contract FPC {
}

#[aztec(private)]
#[aztec(initializer)]
fn constructor(other_asset: AztecAddress, fee_asset: AztecAddress) {
let selector = FunctionSelector::from_signature("_initialize((Field),(Field))");
context.call_public_function(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ contract ImportTest {
};

#[aztec(private)]
#[aztec(initializer)]
fn constructor(
) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ contract InclusionProofs {
}

#[aztec(private)]
#[aztec(initializer)]
fn constructor(public_value: Field) {
let selector = FunctionSelector::from_signature("_initialize(Field)");
context.call_public_function(context.this_address(), selector, [public_value]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ contract Lending {

// Constructs the contract.
#[aztec(private)]
#[aztec(initializer)]
fn constructor(
) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ contract SchnorrAccount {

// Constructs the contract
#[aztec(private)]
#[aztec(initializer)]
fn constructor(signing_pub_key_x: pub Field, signing_pub_key_y: pub Field) {
let this = context.this_address();
// docs:start:initialize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ contract StatefulTest {
}

#[aztec(private)]
#[aztec(initcheck)]
fn create_note(owner: AztecAddress, value: Field) {
if (value != 0) {
let loc = storage.notes.at(owner);
Expand All @@ -34,6 +33,7 @@ contract StatefulTest {
}

#[aztec(private)]
#[aztec(noinitcheck)]
fn create_note_no_init_check(owner: AztecAddress, value: Field) {
if (value != 0) {
let loc = storage.notes.at(owner);
Expand All @@ -54,6 +54,7 @@ contract StatefulTest {
}

#[aztec(private)]
#[aztec(noinitcheck)]
fn destroy_and_create_no_init_check(recipient: AztecAddress, amount: Field) {
let sender = context.msg_sender();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ contract Test {
}

#[aztec(private)]
#[aztec(initializer)]
// docs:start:empty-constructor
fn constructor() {}
// docs:end:empty-constructor
Expand Down Expand Up @@ -227,6 +228,7 @@ contract Test {

// Forcefully emits a nullifier (for testing purposes)
#[aztec(private)]
#[aztec(noinitcheck)]
fn emit_nullifier(nullifier: Field) {
context.push_new_nullifier(nullifier, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ contract TokenBlacklist {

// docs:start:constructor
#[aztec(private)]
#[aztec(initializer)]
fn constructor(admin: AztecAddress, slow_updates_contract: AztecAddress) {
// docs:end:constructor
let selector = FunctionSelector::from_signature("_initialize((Field),(Field))");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ contract TokenBridge {

// Constructs the contract.
#[aztec(private)]
#[aztec(initializer)]
fn constructor(token: AztecAddress) {
let selector = FunctionSelector::from_signature("_initialize((Field))");
context.call_public_function(context.this_address(), selector, [token.to_field()]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ contract Token {

// docs:start:constructor
#[aztec(private)]
#[aztec(initializer)]
fn constructor(admin: AztecAddress, name: str<31>, symbol: str<31>, decimals: u8) {
let selector = FunctionSelector::from_signature("_initialize((Field),(Field),(Field),u8)");
let name_s = FieldCompressedString::from_string(name);
Expand Down
16 changes: 10 additions & 6 deletions noir/aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,22 +435,26 @@ fn transform_module(
}
}

let has_initializer = module.functions.iter().any(|func| func.def.attributes.secondary.iter().any(|attr| is_custom_attribute(&attr, "aztec(initializer)")));

for func in module.functions.iter_mut() {
let mut is_private = false;
let mut is_public = false;
let mut is_public_vm = false;
let mut is_initializer = false;
let mut skip_init_check = true; // Default to true once we're confident that the approach works
let mut insert_init_check = has_initializer;

for secondary_attribute in func.def.attributes.secondary.clone() {
if is_custom_attribute(&secondary_attribute, "aztec(private)") {
is_private = true;
} else if is_custom_attribute(&secondary_attribute, "aztec(initializer)") {
is_initializer = true;
} else if is_custom_attribute(&secondary_attribute, "aztec(initcheck)") {
skip_init_check = false;
insert_init_check = false;
} else if is_custom_attribute(&secondary_attribute, "aztec(noinitcheck)") {
insert_init_check = false;
} else if is_custom_attribute(&secondary_attribute, "aztec(public)") {
is_public = true;
insert_init_check = false;
} else if is_custom_attribute(&secondary_attribute, "aztec(public-vm)") {
is_public_vm = true;
}
Expand All @@ -463,7 +467,7 @@ fn transform_module(
func,
storage_defined,
is_initializer,
skip_init_check,
insert_init_check,
)
.map_err(|err| (err, crate_graph.root_file_id))?;
has_transformed_module = true;
Expand Down Expand Up @@ -655,14 +659,14 @@ fn transform_function(
func: &mut NoirFunction,
storage_defined: bool,
is_initializer: bool,
skip_init_check: bool,
insert_init_check: bool,
) -> Result<(), AztecMacroError> {
let context_name = format!("{}Context", ty);
let inputs_name = format!("{}ContextInputs", ty);
let return_type_name = format!("{}CircuitPublicInputs", ty);

// Add initialization check
if !skip_init_check {
if insert_init_check {
if ty == "Public" {
let error = AztecMacroError::UnsupportedAttributes {
span: func.def.name.span(),
Expand Down

0 comments on commit bb03f28

Please sign in to comment.