Skip to content

Commit

Permalink
feat!: Internal as a macro (#4898)
Browse files Browse the repository at this point in the history
Implement internal functions as a macro. Internal functions are now
written with an `aztec(internal)` decorator as opposed to the `internal`
keyword.

---------

Co-authored-by: Tom French <tom@tomfren.ch>
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 5, 2024
1 parent 245d801 commit 73d640a
Show file tree
Hide file tree
Showing 24 changed files with 159 additions and 75 deletions.
21 changes: 21 additions & 0 deletions docs/docs/misc/migration_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ The prelude consists of

#include_code prelude /noir-projects/aztec-nr/aztec/src/prelude.nr rust

### `internal` is now a macro

The `internal` keyword is now removed from Noir, and is replaced by an `aztec(internal)` attribute in the function. The resulting behavior is exactly the same: these functions will only be callable from within the same contract.

Before:
```rust
#[aztec(private)]
internal fn double(input: Field) -> Field {
input * 2
}
```

After:
```rust
#[aztec(private)]
#[aztec(internal)]
fn double(input: Field) -> Field {
input * 2
}
```

### [Aztec.nr] No SafeU120 anymore!
Noir now have overflow checks by default. So we don't need SafeU120 like libraries anymore.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ contract AppSubscription {
}

#[aztec(public)]
internal fn init(
#[aztec(internal)]
fn init(
target_address: AztecAddress,
subscription_token_address: AztecAddress,
subscription_recipient_address: AztecAddress,
Expand All @@ -101,12 +102,14 @@ contract AppSubscription {
}

#[aztec(public)]
internal fn assert_not_expired(expiry_block_number: Field) {
#[aztec(internal)]
fn assert_not_expired(expiry_block_number: Field) {
assert((context.block_number()) as u64 < expiry_block_number as u64);
}

#[aztec(public)]
internal fn assert_block_number(expiry_block_number: Field) {
#[aztec(internal)]
fn assert_block_number(expiry_block_number: Field) {
assert(
(context.block_number() + SUBSCRIPTION_DURATION_IN_BLOCKS) as u64
>= expiry_block_number as u64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ contract CardGame {
}

#[aztec(public)]
internal fn on_game_joined(game: u32, player: AztecAddress, deck_strength: u32) {
#[aztec(internal)]
fn on_game_joined(game: u32, player: AztecAddress, deck_strength: u32) {
let game_storage = storage.games.at(game as Field);

let mut game_data = game_storage.read();
Expand Down Expand Up @@ -87,7 +88,8 @@ contract CardGame {
}

#[aztec(public)]
internal fn on_card_played(game: u32, player: AztecAddress, card_as_field: Field) {
#[aztec(internal)]
fn on_card_played(game: u32, player: AztecAddress, card_as_field: Field) {
let game_storage = storage.games.at(game as Field);

let mut game_data = game_storage.read();
Expand Down Expand Up @@ -117,7 +119,8 @@ contract CardGame {
}

#[aztec(public)]
internal fn on_cards_claimed(game: u32, player: AztecAddress, cards_hash: Field) {
#[aztec(internal)]
fn on_cards_claimed(game: u32, player: AztecAddress, cards_hash: Field) {
let game_storage = storage.games.at(game as Field);
let mut game_data = game_storage.read();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,11 @@ contract Child {
fn value(input: Field) -> Field {
input + context.chain_id() + context.version()
}

fn check_sender(call_context: CallContext) {
assert(
call_context.msg_sender.eq(call_context.storage_contract_address), "Sender must be this contract"
);
}

// Returns a sum of the input and the chain id and version of the contract in private circuit public input's return_values.
// Can only be called from this contract.
#[aztec(private)]
#[aztec(internal)]
fn valueInternal(input: Field) -> Field {
check_sender(inputs.call_context);
input + context.chain_id() + context.version()
}

Expand Down Expand Up @@ -83,8 +76,8 @@ contract Child {

// Increments `current_value` by `new_value`. Can only be called from this contract.
#[aztec(public)]
#[aztec(internal)]
fn pubIncValueInternal(new_value: Field) -> Field {
check_sender(inputs.call_context);
let old_value = storage.current_value.read();
storage.current_value.write(old_value + new_value);
emit_unencrypted_log(&mut context, new_value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ contract DocsExample {
}

#[aztec(public)]
internal fn update_leader(account: AztecAddress, points: u8) {
#[aztec(internal)]
fn update_leader(account: AztecAddress, points: u8) {
let new_leader = Leader { account, points };
storage.leader.write(new_leader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ contract EasyPrivateVoting {
// docs:end:constructor
// docs:start:initialize
#[aztec(public)] // annotation to mark function as public and expose public context
internal fn _initialize(admin: AztecAddress) { // internal - can only be called by contract
#[aztec(internal)]
fn _initialize(admin: AztecAddress) { // internal - can only be called by contract
storage.admin.write(admin);
storage.voteEnded.write(false);
}
Expand All @@ -49,7 +50,8 @@ contract EasyPrivateVoting {

// docs:start:add_to_tally_public
#[aztec(public)]
internal fn add_to_tally_public(candidate: Field) {
#[aztec(internal)]
fn add_to_tally_public(candidate: Field) {
assert(storage.voteEnded.read() == false, "Vote has ended"); // assert that vote has not ended
let new_tally = storage.tally.at(candidate).read() + 1;
storage.tally.at(candidate).write(new_tally);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ contract EcdsaAccount {
}

#[aztec(private)]
internal fn cancel_authwit(outer_hash: Field) {
#[aztec(internal)]
fn cancel_authwit(outer_hash: Field) {
context.push_new_nullifier(outer_hash, 0);
}

#[aztec(public)]
internal fn approve_public_authwit(outer_hash: Field) {
#[aztec(internal)]
fn approve_public_authwit(outer_hash: Field) {
let actions = AccountActions::public(&mut context, ACCOUNT_ACTIONS_STORAGE_SLOT, is_valid_impl);
actions.approve_public_authwit(outer_hash)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ contract FPC {
}

#[aztec(public)]
internal fn _initialize(other_asset: AztecAddress, fee_asset: AztecAddress) {
#[aztec(internal)]
fn _initialize(other_asset: AztecAddress, fee_asset: AztecAddress) {
storage.other_asset.initialize(other_asset);
storage.fee_asset.initialize(fee_asset);
}
Expand Down Expand Up @@ -63,12 +64,14 @@ contract FPC {
}

#[aztec(public)]
internal fn prepare_fee(from: AztecAddress, amount: Field, asset: AztecAddress, nonce: Field) {
#[aztec(internal)]
fn prepare_fee(from: AztecAddress, amount: Field, asset: AztecAddress, nonce: Field) {
let _res = Token::at(asset).transfer_public(context, from, context.this_address(), amount, nonce);
}

#[aztec(public)]
internal fn pay_fee(refund_address: AztecAddress, amount: Field, asset: AztecAddress) {
#[aztec(internal)]
fn pay_fee(refund_address: AztecAddress, amount: Field, asset: AztecAddress) {
let refund = context.call_public_function(
storage.fee_asset.read_public(),
FunctionSelector::from_signature("pay_fee(Field)"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ contract InclusionProofs {
}

#[aztec(public)]
internal fn _initialize(value: Field) {
#[aztec(internal)]
fn _initialize(value: Field) {
storage.public_value.write(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ contract Lending {
}

#[aztec(public)]
internal fn _deposit(owner: AztecAddress, amount: Field, collateral_asset: AztecAddress) {
#[aztec(internal)]
fn _deposit(owner: AztecAddress, amount: Field, collateral_asset: AztecAddress) {
let _asset = Lending::at(context.this_address()).update_accumulator(context);

let coll_asset = storage.collateral_asset.read();
Expand Down Expand Up @@ -163,7 +164,8 @@ contract Lending {
}

#[aztec(public)]
internal fn _withdraw(owner: AztecAddress, recipient: AztecAddress, amount: Field) {
#[aztec(internal)]
fn _withdraw(owner: AztecAddress, recipient: AztecAddress, amount: Field) {
let asset = Lending::at(context.this_address()).update_accumulator(context);
let price = PriceFeed::at(asset.oracle).get_price(context);

Expand Down Expand Up @@ -220,7 +222,8 @@ contract Lending {
}

#[aztec(public)]
internal fn _borrow(owner: AztecAddress, to: AztecAddress, amount: Field) {
#[aztec(internal)]
fn _borrow(owner: AztecAddress, to: AztecAddress, amount: Field) {
let asset = Lending::at(context.this_address()).update_accumulator(context);
let price = PriceFeed::at(asset.oracle).get_price(context);

Expand Down Expand Up @@ -282,7 +285,8 @@ contract Lending {
}

#[aztec(public)]
internal fn _repay(owner: AztecAddress, amount: Field, stable_coin: AztecAddress) {
#[aztec(internal)]
fn _repay(owner: AztecAddress, amount: Field, stable_coin: AztecAddress) {
let asset = Lending::at(context.this_address()).update_accumulator(context);

// To ensure that private is using the correct token.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ contract SchnorrAccount {
}

#[aztec(private)]
internal fn cancel_authwit(outer_hash: Field) {
#[aztec(internal)]
fn cancel_authwit(outer_hash: Field) {
context.push_new_nullifier(outer_hash, 0);
}

#[aztec(public)]
internal fn approve_public_authwit(outer_hash: Field) {
#[aztec(internal)]
fn approve_public_authwit(outer_hash: Field) {
let actions = AccountActions::public(&mut context, ACCOUNT_ACTIONS_STORAGE_SLOT, is_valid_impl);
actions.approve_public_authwit(outer_hash)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ contract SchnorrHardcodedAccount {
}

#[aztec(private)]
internal fn cancel_authwit(outer_hash: Field) {
#[aztec(internal)]
fn cancel_authwit(outer_hash: Field) {
context.push_new_nullifier(outer_hash, 0);
}

#[aztec(public)]
internal fn approve_public_authwit(outer_hash: Field) {
#[aztec(internal)]
fn approve_public_authwit(outer_hash: Field) {
let actions = AccountActions::public(&mut context, ACCOUNT_ACTIONS_STORAGE_SLOT, is_valid_impl);
actions.approve_public_authwit(outer_hash)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ contract SchnorrSingleKeyAccount {
}

#[aztec(private)]
internal fn cancel_authwit(outer_hash: Field) {
#[aztec(internal)]
fn cancel_authwit(outer_hash: Field) {
context.push_new_nullifier(outer_hash, 0);
}

#[aztec(public)]
internal fn approve_public_authwit(outer_hash: Field) {
#[aztec(internal)]
fn approve_public_authwit(outer_hash: Field) {
let actions = AccountActions::public(&mut context, ACCOUNT_ACTIONS_STORAGE_SLOT, is_valid_impl);
actions.approve_public_authwit(outer_hash)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ contract SlowTree {
// docs:end:read_at_private
// docs:start:assert_current_root
#[aztec(public)]
internal fn _assert_current_root(caller: Field, expected: Field) {
#[aztec(internal)]
fn _assert_current_root(caller: Field, expected: Field) {
let root = storage.trees.at(caller).current_root();
assert(root == expected, "Root does not match expected");
}
Expand Down Expand Up @@ -115,7 +116,8 @@ contract SlowTree {
// docs:end:update_at_private
// docs:start:_update
#[aztec(public)]
internal fn _update(
#[aztec(internal)]
fn _update(
caller: Field,
index: Field,
new_value: Field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ contract TokenBlacklist {
}

#[aztec(public)]
internal fn _init_slow_tree(caller: AztecAddress) {
#[aztec(internal)]
fn _init_slow_tree(caller: AztecAddress) {
assert(storage.admin.read().eq(caller), "caller is not admin");
}

///////
#[aztec(public)]
internal fn _initialize(new_admin: AztecAddress, slow_updates_contract: AztecAddress) {
#[aztec(internal)]
fn _initialize(new_admin: AztecAddress, slow_updates_contract: AztecAddress) {
assert(!new_admin.is_zero(), "invalid admin");
storage.admin.write(new_admin);
// docs:start:write_slow_update_public
Expand Down Expand Up @@ -282,13 +284,15 @@ contract TokenBlacklist {
/// Internal ///

#[aztec(public)]
internal fn _increase_public_balance(to: AztecAddress, amount: Field) {
#[aztec(internal)]
fn _increase_public_balance(to: AztecAddress, amount: Field) {
let new_balance = storage.public_balances.at(to).read().add(U128::from_integer(amount));
storage.public_balances.at(to).write(new_balance);
}

#[aztec(public)]
internal fn _reduce_total_supply(amount: Field) {
#[aztec(internal)]
fn _reduce_total_supply(amount: Field) {
// Only to be called from burn.
let new_supply = storage.total_supply.read().sub(U128::from_integer(amount));
storage.total_supply.write(new_supply);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ contract TokenBridge {
// docs:end:read_token

#[aztec(public)]
internal fn _initialize(token: AztecAddress) {
#[aztec(internal)]
fn _initialize(token: AztecAddress) {
storage.token.write(token);
}

Expand All @@ -147,14 +148,16 @@ contract TokenBridge {
// Also, note that user hashes their secret in private and only sends the hash in public
// meaning only user can `redeem_shield` at a later time with their secret.
#[aztec(public)]
internal fn _call_mint_on_token(amount: Field, secret_hash: Field) {
#[aztec(internal)]
fn _call_mint_on_token(amount: Field, secret_hash: Field) {
Token::at(storage.token.read()).mint_private(context, amount, secret_hash);
}
// docs:end:call_mint_on_token

// docs:start:assert_token_is_same
#[aztec(public)]
internal fn _assert_token_is_same(token: AztecAddress) {
#[aztec(internal)]
fn _assert_token_is_same(token: AztecAddress) {
assert(storage.token.read().eq(token), "Token address is not the same as seen in storage");
}
// docs:end:assert_token_is_same
Expand Down
Loading

0 comments on commit 73d640a

Please sign in to comment.