Skip to content

Commit

Permalink
feat(Aztec.nr): Kernel return types abstraction (#1924)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 authored Sep 1, 2023
1 parent 3fe5508 commit 3a8e702
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,59 @@ contract Child {
#[aztec(private)]
fn value(
input: Field,
) {
context.return_values.push(input + context.chain_id() + context.version());
) -> Field {
input + context.chain_id() + context.version()
}

// 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)]
fn valueInternal(
input: Field,
) {
) -> Field {
assert(inputs.call_context.msg_sender == inputs.call_context.storage_contract_address);
context.return_values.push(input + context.chain_id() + context.version());
input + context.chain_id() + context.version()
}

// Returns base_value + 42.
#[aztec(public)]
fn pubGetValue(base_value: Field) {
fn pubGetValue(base_value: Field) -> Field {
let returnValue = base_value + context.chain_id() + context.version() + context.block_number() + context.timestamp();

context.return_values.push(returnValue);
returnValue
}

// Sets `current_value` to `new_value`
#[aztec(public)]
fn pubSetValue(new_value: Field) {
fn pubSetValue(new_value: Field) -> Field {
let storage = Storage::init();
storage.current_value.write(new_value);
let _hash = emit_unencrypted_log(new_value);
context.return_values.push(new_value);

new_value
}

// Increments `current_value` by `new_value`
#[aztec(public)]
fn pubIncValue(new_value: Field) {
fn pubIncValue(new_value: Field) -> Field {
let storage = Storage::init();
let old_value = storage.current_value.read();
storage.current_value.write(old_value + new_value);
let _hash = emit_unencrypted_log(new_value);
context.return_values.push(new_value);

new_value
}

// Increments `current_value` by `new_value`. Can only be called from this contract.
#[aztec(public)]
fn pubIncValueInternal(new_value: Field) {
#[aztec(public)]
fn pubIncValueInternal(new_value: Field) -> Field {
let storage = Storage::init();
assert(inputs.call_context.msg_sender == inputs.call_context.storage_contract_address);
let old_value = storage.current_value.read();
storage.current_value.write(old_value + new_value);
let _hash = emit_unencrypted_log(new_value);
context.return_values.push(new_value);

new_value
}

#[aztec(public)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ contract EcdsaAccount {
payload.execute_calls(&mut context);
}


// Creates a new account out of an ECDSA public key to use for signature verification
#[aztec(private)]
fn constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract ExamplePublicStateIncrement {

// a = 100;
#[aztec(public)]
open internal fn initialise_a() {
internal fn initialise_a() {
let storage = Storage::init();
storage.a.write(100);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ contract ImportTest {
#[aztec(private)]
fn main(
target: Field
) {
) -> Field {
let test_contract_instance = TestPrivateContextInterface::at(target);
let return_values = test_contract_instance.testCodeGen(
&mut context,
Expand All @@ -45,7 +45,7 @@ contract ImportTest {
}
);

context.return_values.push(return_values[0]);
return_values[0]
}

// Calls the getThisAddress on the Test contract at the target address
Expand All @@ -54,10 +54,11 @@ contract ImportTest {
#[aztec(private)]
fn callNoArgs(
target: Field
) {
) -> Field {
let test_contract_instance = TestPrivateContextInterface::at(target);
let return_values = test_contract_instance.getThisAddress(&mut context);
context.return_values.push(return_values[0]);

return_values[0]
}

// Calls the createNullifierPublic on the Test contract at the target address
Expand All @@ -77,10 +78,11 @@ contract ImportTest {
#[aztec(public)]
fn pubCallOpenFn(
target: Field,
) {
) -> Field {
let test_contract_instance = TestPublicContextInterface::at(target);
let ret = test_contract_instance.createNullifierPublic(context, 1, 2);
context.return_values.push(ret[0]);

ret[0]
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract Lending {
loan_to_value: Field,
collateral_asset: Field,
stable_coin: Field,
) {
) -> Field {
let storage = Storage::init();
let asset_loc = storage.assets.at(0);
let asset = asset_loc.read();
Expand All @@ -54,13 +54,13 @@ contract Lending {
storage.collateral_asset.write(collateral_asset);
storage.stable_coin.write(stable_coin);

context.return_values.push(1);
1
}

// Create a position.
// keccak256("update_accumulator()") >> 224 -> 0x1873b536
#[aztec(public)]
fn update_accumulator() {
fn update_accumulator() -> Asset {
let storage = Storage::init();

let asset_loc = storage.assets.at(0);
Expand All @@ -82,7 +82,7 @@ contract Lending {
asset_loc.write(asset);
}

context.return_values.push_array(asset.serialise());
asset
}

// This don't need to be on behalf of self. We should be able to repay on behalf of someone else.
Expand All @@ -105,19 +105,20 @@ contract Lending {
owner: Field,
amount: Field,
collateral_asset: Field,
) {
) -> Field {
Token::at(collateral_asset).transfer_from_pub(context, context.msg_sender(), context.this_address(), amount);
let return_values = context.call_public_function(context.this_address(), 0x08506e50, [owner, amount, collateral_asset]);
context.return_values.push(return_values[0]);

return_values[0]
}

#[aztec(public)]
// keccak256("_deposit(field,field,field)") >> 224 -> 0x08506e50
open internal fn _deposit(
internal fn _deposit(
owner: Field,
amount: Field,
collateral_asset: Field,
) {
) -> Field {
let _asset = Lending::at(context.this_address()).update_accumulator(context);
let storage = Storage::init();

Expand All @@ -128,7 +129,7 @@ contract Lending {
let collateral = coll_loc.read();
coll_loc.write(collateral + amount);

context.return_values.push(1);
1
}

#[aztec(private)]
Expand All @@ -145,19 +146,20 @@ contract Lending {
fn withdraw_public(
to: Field,
amount: Field,
) {
) -> Field {
// _withdraw(msg.sender, to, amount);
let return_values = context.call_public_function(context.this_address(), 0x5af6f634, [context.msg_sender(), to, amount]);
context.return_values.push(return_values[0]);

return_values[0]
}

// keccak256("_withdraw(field,field,field)") >> 224 -> 0x5af6f634
#[aztec(public)]
open internal fn _withdraw(
internal fn _withdraw(
owner: Field,
recipient: Field,
amount: Field
) {
) -> Field {
let asset = Lending::at(context.this_address()).update_accumulator(context);
let price = PriceFeed::at(asset.oracle_address).get_price(context);

Expand All @@ -182,7 +184,7 @@ contract Lending {
let collateral_asset = storage.collateral_asset.read();
Token::at(collateral_asset).transfer_pub(context, recipient, amount);

context.return_values.push(1);
1
}

#[aztec(private)]
Expand All @@ -200,19 +202,20 @@ contract Lending {
fn borrow_public(
to: Field,
amount: Field
) {
) -> Field {
// _borrow(msg.sender, to, amount)
let return_values = context.call_public_function(context.this_address(), 0xceffa31a, [context.msg_sender(), to, amount]);
context.return_values.push(return_values[0]);

return_values[0]
}

// keccak256("_borrow(field,field,field)") >> 224 -> 0xceffa31a
#[aztec(public)]
open internal fn _borrow(
internal fn _borrow(
owner: Field,
to: Field,
amount: Field
) {
) -> Field {
let asset = Lending::at(context.this_address()).update_accumulator(context);
let price = PriceFeed::at(asset.oracle_address).get_price(context);

Expand All @@ -233,7 +236,7 @@ contract Lending {
let stable_coin = storage.stable_coin.read();
Token::at(stable_coin).owner_mint_pub(context, to, amount);

context.return_values.push(1);
1
}

#[aztec(private)]
Expand All @@ -254,16 +257,17 @@ contract Lending {
owner: Field,
amount: Field,
stable_coin: Field,
) {
) -> Field {
// Should probably just burn the tokens actually :thinking:
Token::at(stable_coin).transfer_from_pub(context, context.msg_sender(), context.this_address(), amount);
let return_values = context.call_public_function(context.this_address(), 0xfa94ab54, [owner, amount, stable_coin]);
context.return_values.push(return_values[0]);

return_values[0]
}

// keccak256("_repay(field,field,field)") >> 224 -> 0xfa94ab54
#[aztec(public)]
open internal fn _repay(
internal fn _repay(
owner: Field,
amount: Field,
stable_coin: Field,
Expand All @@ -279,7 +283,7 @@ contract Lending {

storage.static_debt.at(owner).write(debt_returns.static_debt as Field);

context.return_values.push(1);
1
}

unconstrained fn get_asset(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ fn serialiseAsset(asset: Asset) -> [Field; ASSET_SERIALISED_LEN] {
}

impl Asset {
fn serialise (self: Self) -> [Field; ASSET_SERIALISED_LEN] {
// TODO(Maddiaa): Rename all serialise -> serialize
fn serialize (self: Self) -> [Field; ASSET_SERIALISED_LEN] {
serialiseAsset(self)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract MultiTransfer {
owner: Field, // Owner of the asset
batch_transfer_selector: Field, // Function selector for transfer
note_offsets: [Field; 4], // Offsets from which 4 notes of the owner would be read.
) {
) -> [Field; 4] {
// First batch transfer call
let return_values_1 = context.call_private_function(asset, batch_transfer_selector, [
owner,
Expand All @@ -43,7 +43,6 @@ contract MultiTransfer {
note_offsets[0],
]);
let result1 = return_values_1[0];
context.return_values.push(result1);

// Second batch transfer call
let return_values_2 = context.call_private_function(asset, batch_transfer_selector, [
Expand All @@ -57,7 +56,6 @@ contract MultiTransfer {
note_offsets[1],
]);
let result2 = return_values_2[0];
context.return_values.push(result2);

// Third batch transfer call
let return_values_3 = context.call_private_function(asset, batch_transfer_selector, [
Expand All @@ -71,7 +69,6 @@ contract MultiTransfer {
note_offsets[2],
]);
let result3 = return_values_3[0];
context.return_values.push(result3);

// Fourth batch transfer call
let return_values_4 = context.call_private_function(asset, batch_transfer_selector, [
Expand All @@ -85,6 +82,7 @@ contract MultiTransfer {
note_offsets[3],
]);
let result4 = return_values_4[0];
context.return_values.push(result4);

[result1, result2, result3, result4]
}
}
Loading

0 comments on commit 3a8e702

Please sign in to comment.