Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed Mar 25, 2024
1 parent 0d66d5d commit f06283c
Show file tree
Hide file tree
Showing 11 changed files with 685 additions and 566 deletions.
16 changes: 8 additions & 8 deletions noir-projects/aztec-nr/aztec/src/context/avm_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ use dep::protocol_types::traits::Serialize;
use dep::protocol_types::abis::function_selector::FunctionSelector;
use dep::protocol_types::abis::public_circuit_public_inputs::PublicCircuitPublicInputs;
use dep::protocol_types::constants::RETURN_VALUES_LENGTH;
use crate::context::inputs::PublicContextInputs;
use crate::context::inputs::avm_context_inputs::AvmContextInputs;
use crate::context::interface::ContextInterface;
use crate::context::interface::PublicContextInterface;

struct AVMContext {}
struct AVMContext {
inputs: AvmContextInputs,
}

impl AVMContext {
pub fn new() -> Self {
AVMContext {}
pub fn new(inputs: AvmContextInputs) -> Self {
AVMContext { inputs }
}

pub fn origin(self) -> AztecAddress {
Expand Down Expand Up @@ -190,16 +192,14 @@ impl ContextInterface for AVMContext {
version()
}
fn selector(self) -> FunctionSelector {
assert(false, "'selector' not implemented!");
FunctionSelector::zero()
FunctionSelector::from_field(self.inputs.selector)
}
fn get_header(self) -> Header {
assert(false, "'get_header' not implemented!");
Header::empty()
}
fn get_args_hash(self) -> Field {
assert(false, "'get_args_hash' not implemented!");
0
self.inputs.args_hash
}
}

Expand Down
2 changes: 2 additions & 0 deletions noir-projects/aztec-nr/aztec/src/context/inputs.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
struct AvmContextInputs {
selector: Field,
args_hash: Field,
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ contract AvmTest {
// context.contract_call_depth()
// }

#[aztec(public-vm)]
fn check_selector() {
assert(context.selector() == FunctionSelector::from_signature("check_selector()"));
}

#[aztec(public-vm)]
fn get_args_hash(_a: u8, _fields: [Field; 3]) -> pub Field {
context.get_args_hash()
}

#[aztec(public-vm)]
fn emit_unencrypted_log() {
context.accumulate_unencrypted_logs(/*event_selector=*/ 5, /*message=*/ [10, 20, 30]);
Expand Down
9 changes: 8 additions & 1 deletion noir/noir-repo/aztec_macros/src/transforms/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ pub fn transform_vm_function(
let create_context = create_avm_context()?;
func.def.body.0.insert(0, create_context);

// Add the inputs to the params (last!)
let input = create_inputs("AvmContextInputs");
func.def.parameters.push(input);

// We want the function to be seen as a public function
func.def.is_unconstrained = true;

Expand Down Expand Up @@ -354,11 +358,14 @@ fn create_context(ty: &str, params: &[Param]) -> Result<Vec<Statement>, AztecMac
/// // ...
/// }
fn create_avm_context() -> Result<Statement, AztecMacroError> {
// Create the inputs to the context
let inputs_expression = variable("inputs");

let let_context = mutable_assignment(
"context", // Assigned to
call(
variable_path(chained_dep!("aztec", "context", "AVMContext", "new")), // Path
vec![], // args
vec![inputs_expression], // args
),
);

Expand Down

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions yarn-project/simulator/src/avm/avm_context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe('Avm Context', () => {
allSameExcept(context.environment, {
address: newAddress,
storageAddress: newAddress,
calldata: newCalldata,
// Calldata also includes AvmContextInputs
calldata: expect.objectContaining(newCalldata),
isStaticCall: false,
}),
);
Expand All @@ -41,7 +42,8 @@ describe('Avm Context', () => {
allSameExcept(context.environment, {
address: newAddress,
storageAddress: newAddress,
calldata: newCalldata,
// Calldata also includes AvmContextInputs
calldata: expect.objectContaining(newCalldata),
isStaticCall: true,
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe('Execution Environment', () => {
allSameExcept(executionEnvironment, {
address: newAddress,
storageAddress: newAddress,
calldata,
// Calldata also includes AvmContextInputs
calldata: expect.objectContaining(calldata),
}),
);
});
Expand All @@ -27,7 +28,8 @@ describe('Execution Environment', () => {
allSameExcept(executionEnvironment, {
address: newAddress,
isDelegateCall: true,
calldata,
// Calldata also includes AvmContextInputs
calldata: expect.objectContaining(calldata),
}),
);
});
Expand All @@ -41,7 +43,8 @@ describe('Execution Environment', () => {
address: newAddress,
storageAddress: newAddress,
isStaticCall: true,
calldata,
// Calldata also includes AvmContextInputs
calldata: expect.objectContaining(calldata),
}),
);
});
Expand Down
17 changes: 16 additions & 1 deletion yarn-project/simulator/src/avm/avm_execution_environment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { FunctionSelector, GlobalVariables } from '@aztec/circuits.js';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { pedersenHash } from '@aztec/foundation/crypto';
import { EthAddress } from '@aztec/foundation/eth-address';
import { Fr } from '@aztec/foundation/fields';

class AvmContextInputs {
constructor(private selector: Fr, private argsHash: Fr) {}

public toFields(): Fr[] {
return [this.selector, this.argsHash];
}
}

/**
* Contains variables that remain constant during AVM execution
* These variables are provided by the public kernel circuit
Expand Down Expand Up @@ -40,7 +49,13 @@ export class AvmExecutionEnvironment {
// containing all functions, and function selector will become an application-level mechanism
// (e.g. first few bytes of calldata + compiler-generated jump table)
public readonly temporaryFunctionSelector: FunctionSelector,
) {}
) {
const inputs = new AvmContextInputs(
temporaryFunctionSelector.toField(),
pedersenHash(calldata.map(word => word.toBuffer())),
);
this.calldata = [...calldata, ...inputs.toFields()];
}

public deriveEnvironmentForNestedCall(
address: AztecAddress,
Expand Down
Loading

0 comments on commit f06283c

Please sign in to comment.