Skip to content

Commit

Permalink
migrate to avm
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed May 16, 2024
1 parent 5d6513a commit 52f6fa3
Show file tree
Hide file tree
Showing 21 changed files with 399 additions and 543 deletions.
2 changes: 1 addition & 1 deletion avm-transpiler/src/transpile_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl From<CompiledAcirContractArtifact> for TranspiledContractArtifact {
// TODO(4269): once functions are tagged for transpilation to AVM, check tag
if function
.custom_attributes
.contains(&"aztec(public-vm)".to_string())
.contains(&"aztec(public)".to_string())
{
info!(
"Transpiling AVM function {} on contract {}",
Expand Down
15 changes: 4 additions & 11 deletions noir-projects/aztec-nr/aztec/src/context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,33 @@ mod inputs;

mod private_context;
mod public_context;
mod avm_context;
mod interface;
mod gas;

use interface::{
ContextInterface, PrivateCallInterface, PublicCallInterface, PrivateVoidCallInterface,
PublicVoidCallInterface, AvmCallInterface, AvmVoidCallInterface
PublicVoidCallInterface
};
use private_context::PrivateContext;
use private_context::PackedReturns;
use public_context::PublicContext;
use public_context::FunctionReturns;
use avm_context::AvmContext;

struct Context {
private: Option<&mut PrivateContext>,
public: Option<&mut PublicContext>,
avm: Option<&mut AvmContext>,
}

impl Context {
pub fn private(context: &mut PrivateContext) -> Context {
Context { private: Option::some(context), public: Option::none(), avm: Option::none() }
Context { private: Option::some(context), public: Option::none() }
}

pub fn public(context: &mut PublicContext) -> Context {
Context { public: Option::some(context), private: Option::none(), avm: Option::none() }
}

pub fn avm(context: &mut AvmContext) -> Context {
Context { avm: Option::some(context), public: Option::none(), private: Option::none() }
Context { public: Option::some(context), private: Option::none() }
}

pub fn none() -> Context {
Context { public: Option::none(), private: Option::none(), avm: Option::none() }
Context { public: Option::none(), private: Option::none() }
}
}
1 change: 1 addition & 0 deletions noir-projects/aztec-nr/aztec/src/context/avm_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,4 @@ fn call_static_opcode<RET_SIZE>(
function_selector: Field
) -> ([Field; RET_SIZE], u8) {}
// ^ return data ^ success

2 changes: 0 additions & 2 deletions noir-projects/aztec-nr/aztec/src/context/inputs.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
mod private_context_inputs;
mod public_context_inputs;
mod avm_context_inputs;

use crate::context::inputs::private_context_inputs::PrivateContextInputs;
use crate::context::inputs::public_context_inputs::PublicContextInputs;
use crate::context::inputs::avm_context_inputs::AvmContextInputs;

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct PrivateContextInputs {
impl Empty for PrivateContextInputs {
fn empty() -> Self {
PrivateContextInputs {
call_context : CallContext::empty(),
call_context: CallContext::empty(),
historical_header: Header::empty(),
tx_context: TxContext::empty(),
start_side_effect_counter: 0 as u32,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
use crate::context::globals::public_global_variables::PublicGlobalVariables;
use dep::protocol_types::traits::Empty;

use dep::protocol_types::{abis::call_context::CallContext, abis::gas::Gas, header::Header, traits::Empty};

// PublicContextInputs are expected to be provided to each public function
// docs:start:public-context-inputs
struct PublicContextInputs {
call_context: CallContext,
historical_header: Header,

public_global_variables: PublicGlobalVariables,
start_side_effect_counter: u32,
gas_left: Gas,
transaction_fee: Field,
selector: Field,
args_hash: Field,
}
// docs:end:public-context-inputs

impl Empty for PublicContextInputs {
fn empty() -> Self {
PublicContextInputs {
call_context: CallContext::empty(),
historical_header: Header::empty(),
public_global_variables: PublicGlobalVariables::empty(),
start_side_effect_counter: 0 as u32,
gas_left: Gas::empty(),
transaction_fee: 0,
selector: 0,
args_hash: 0,
}
}
}
114 changes: 11 additions & 103 deletions noir-projects/aztec-nr/aztec/src/context/interface.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use dep::protocol_types::{abis::function_selector::FunctionSelector, address::{A
use crate::oracle::arguments;
use crate::context::private_context::PrivateContext;
use crate::context::public_context::PublicContext;
use crate::context::avm_context::AvmContext;
use crate::context::gas::GasOpts;
use crate::context::public_context::FunctionReturns;

Expand All @@ -19,8 +18,8 @@ trait ContextInterface {
}

// TEMPORARY: This trait is to promote sharing of the current public context
// and the upcoming AvmContext. This will be removed once the AvmContext is the default.
// If you are adding something here, then you should also implement it in the AvmContext.
// and the upcoming PublicContext. This will be removed once the PublicContext is the default.
// If you are adding something here, then you should also implement it in the PublicContext.
trait PublicContextInterface {
fn block_number(self) -> Field;
fn timestamp(self) -> u64;
Expand Down Expand Up @@ -112,120 +111,29 @@ impl PrivateVoidCallInterface {
}

struct PublicCallInterface<T> {
target_contract: AztecAddress,
selector: FunctionSelector,
args_hash: Field,
}

impl<T> PublicCallInterface<T> {
pub fn call<N>(self, context: &mut PublicContext) -> T where T: Deserialize<N> {
let returns = context.call_public_function_with_packed_args(
self.target_contract,
self.selector,
self.args_hash,
false,
false
);
returns.deserialize_into()
}

pub fn static_call<N>(self, context: &mut PublicContext) -> T where T: Deserialize<N> {
let returns = context.call_public_function_with_packed_args(self.target_contract, self.selector, self.args_hash, true, false);
returns.deserialize_into()
}

pub fn delegate_call<N>(self, context: &mut PublicContext) -> T where T: Deserialize<N> {
let returns = context.call_public_function_with_packed_args(self.target_contract, self.selector, self.args_hash, false, true);
returns.deserialize_into()
}

pub fn enqueue(self, context: &mut PrivateContext) {
context.call_public_function_with_packed_args(
self.target_contract,
self.selector,
self.args_hash,
false,
false
)
}

pub fn static_enqueue(self, context: &mut PrivateContext) {
context.call_public_function_with_packed_args(self.target_contract, self.selector, self.args_hash, true, false)
}

pub fn delegate_enqueue(self, context: &mut PrivateContext) {
context.call_public_function_with_packed_args(self.target_contract, self.selector, self.args_hash, false, true)
}
}

struct PublicVoidCallInterface {
target_contract: AztecAddress,
selector: FunctionSelector,
args_hash: Field
}

impl PublicVoidCallInterface {
pub fn call(self, context: &mut PublicContext) {
context.call_public_function_with_packed_args(
self.target_contract,
self.selector,
self.args_hash,
false,
false
).assert_empty()
}

pub fn static_call(self, context: &mut PublicContext) {
context.call_public_function_with_packed_args(self.target_contract, self.selector, self.args_hash, true, false).assert_empty();
}

pub fn delegate_call(self, context: &mut PublicContext) {
context.call_public_function_with_packed_args(self.target_contract, self.selector, self.args_hash, false, true).assert_empty();
}

pub fn enqueue(self, context: &mut PrivateContext) {
context.call_public_function_with_packed_args(
self.target_contract,
self.selector,
self.args_hash,
false,
false
)
}

pub fn static_enqueue(self, context: &mut PrivateContext) {
context.call_public_function_with_packed_args(self.target_contract, self.selector, self.args_hash, true, false)
}

pub fn delegate_enqueue(self, context: &mut PrivateContext) {
context.call_public_function_with_packed_args(self.target_contract, self.selector, self.args_hash, false, true)
}
}

struct AvmCallInterface<T> {
target_contract: AztecAddress,
selector: FunctionSelector,
args: [Field],
gas_opts: GasOpts,
}

impl<T> AvmCallInterface<T> {
impl<T> PublicCallInterface<T> {
pub fn with_gas(self: &mut Self, gas_opts: GasOpts) -> &mut Self {
self.gas_opts = gas_opts;
self
}

pub fn call<N>(self, context: &mut AvmContext) -> T where T: Deserialize<N> {
pub fn call<N>(self, context: &mut PublicContext) -> T where T: Deserialize<N> {
let returns = context.call_public_function(self.target_contract, self.selector, self.args, self.gas_opts);
returns.deserialize_into()
}

pub fn static_call<N>(self, context: &mut AvmContext) -> T where T: Deserialize<N> {
pub fn static_call<N>(self, context: &mut PublicContext) -> T where T: Deserialize<N> {
let returns = context.static_call_public_function(self.target_contract, self.selector, self.args, self.gas_opts);
returns.deserialize_into()
}

pub fn delegate_call<N>(self, context: &mut AvmContext) -> T where T: Deserialize<N> {
pub fn delegate_call<N>(self, context: &mut PublicContext) -> T where T: Deserialize<N> {
let returns = context.delegate_call_public_function(self.target_contract, self.selector, self.args);
returns.deserialize_into()
}
Expand Down Expand Up @@ -267,30 +175,30 @@ impl<T> AvmCallInterface<T> {
}
}

struct AvmVoidCallInterface {
struct PublicVoidCallInterface {
target_contract: AztecAddress,
selector: FunctionSelector,
args: [Field],
gas_opts: GasOpts,
}

impl AvmVoidCallInterface {
impl PublicVoidCallInterface {
pub fn with_gas(self: &mut Self, gas_opts: GasOpts) -> &mut Self {
self.gas_opts = gas_opts;
self
}

pub fn call<N>(self, context: &mut AvmContext) {
pub fn call<N>(self, context: &mut PublicContext) {
let returns = context.call_public_function(self.target_contract, self.selector, self.args, self.gas_opts);
returns.assert_empty()
}

pub fn static_call<N>(self, context: &mut AvmContext) {
pub fn static_call<N>(self, context: &mut PublicContext) {
let returns = context.static_call_public_function(self.target_contract, self.selector, self.args, self.gas_opts);
returns.assert_empty()
}

pub fn delegate_call<N>(self, context: &mut AvmContext) {
pub fn delegate_call<N>(self, context: &mut PublicContext) {
let returns = context.delegate_call_public_function(self.target_contract, self.selector, self.args);
returns.assert_empty()
}
Expand Down
Loading

0 comments on commit 52f6fa3

Please sign in to comment.