Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions barretenberg/cpp/pil/vm2/context.pil
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This is a virtual gadget, which is part of the execution trace.
namespace execution;

pol commit context_id;
pol commit pc;
pol commit msg_sender;
pol commit contract_address;

pol commit is_static;
is_static * (1 - is_static) = 0;
9 changes: 9 additions & 0 deletions barretenberg/cpp/pil/vm2/context_stack.pil
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace context_stack;

pol commit context_id;
pol commit pc;
pol commit msg_sender;
pol commit contract_address;

pol commit is_static;
is_static * (1 - is_static) = 0;
3 changes: 2 additions & 1 deletion barretenberg/cpp/pil/vm2/execution.pil
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ include "poseidon2_perm.pil";
include "scalar_mul.pil";
include "to_radix.pil";
include "ff_gt.pil";
include "context.pil;"
include "context_stack.pil";

namespace execution;

Expand All @@ -29,7 +31,6 @@ pol commit op1, op2, op3, op4;
// resolved operands
pol commit rop1, rop2, rop3, rop4;

pol commit pc;
pol commit bytecode_id;
pol commit clk;
pol commit last;
Expand Down
8 changes: 4 additions & 4 deletions barretenberg/cpp/src/barretenberg/vm2/generated/columns.hpp

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions barretenberg/cpp/src/barretenberg/vm2/generated/flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "relations/bc_retrieval.hpp"
#include "relations/bitwise.hpp"
#include "relations/class_id_derivation.hpp"
#include "relations/context.hpp"
#include "relations/context_stack.hpp"
#include "relations/ecc.hpp"
#include "relations/execution.hpp"
#include "relations/ff_gt.hpp"
Expand Down Expand Up @@ -94,12 +96,12 @@ class AvmFlavor {
static constexpr bool HasZK = false;

static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 44;
static constexpr size_t NUM_WITNESS_ENTITIES = 914;
static constexpr size_t NUM_WITNESS_ENTITIES = 923;
static constexpr size_t NUM_SHIFTED_ENTITIES = 134;
static constexpr size_t NUM_WIRES = NUM_WITNESS_ENTITIES + NUM_PRECOMPUTED_ENTITIES;
// We have two copies of the witness entities, so we subtract the number of fixed ones (they have no shift), one for
// the unshifted and one for the shifted
static constexpr size_t NUM_ALL_ENTITIES = 1092;
static constexpr size_t NUM_ALL_ENTITIES = 1101;

// In the sumcheck univariate computation, we divide the trace in chunks and each chunk is
// evenly processed by all the threads. This constant defines the maximum number of rows
Expand All @@ -122,6 +124,8 @@ class AvmFlavor {
avm2::bc_retrieval<FF_>,
avm2::bitwise<FF_>,
avm2::class_id_derivation<FF_>,
avm2::context<FF_>,
avm2::context_stack<FF_>,
avm2::ecc<FF_>,
avm2::execution<FF_>,
avm2::ff_gt<FF_>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// AUTOGENERATED FILE
#pragma once

#include <string_view>

#include "barretenberg/relations/relation_parameters.hpp"
#include "barretenberg/relations/relation_types.hpp"

namespace bb::avm2 {

template <typename FF_> class contextImpl {
public:
using FF = FF_;

static constexpr std::array<size_t, 1> SUBRELATION_PARTIAL_LENGTHS = { 3 };

template <typename ContainerOverSubrelations, typename AllEntities>
void static accumulate(ContainerOverSubrelations& evals,
const AllEntities& new_term,
[[maybe_unused]] const RelationParameters<FF>&,
[[maybe_unused]] const FF& scaling_factor)
{

{
using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>;
auto tmp = new_term.execution_is_static * (FF(1) - new_term.execution_is_static);
tmp *= scaling_factor;
std::get<0>(evals) += typename Accumulator::View(tmp);
}
}
};

template <typename FF> class context : public Relation<contextImpl<FF>> {
public:
static constexpr const std::string_view NAME = "context";

static std::string get_subrelation_label(size_t index)
{
switch (index) {}
return std::to_string(index);
}
};

} // namespace bb::avm2
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// AUTOGENERATED FILE
#pragma once

#include <string_view>

#include "barretenberg/relations/relation_parameters.hpp"
#include "barretenberg/relations/relation_types.hpp"

namespace bb::avm2 {

template <typename FF_> class context_stackImpl {
public:
using FF = FF_;

static constexpr std::array<size_t, 1> SUBRELATION_PARTIAL_LENGTHS = { 3 };

template <typename ContainerOverSubrelations, typename AllEntities>
void static accumulate(ContainerOverSubrelations& evals,
const AllEntities& new_term,
[[maybe_unused]] const RelationParameters<FF>&,
[[maybe_unused]] const FF& scaling_factor)
{

{
using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>;
auto tmp = new_term.context_stack_is_static * (FF(1) - new_term.context_stack_is_static);
tmp *= scaling_factor;
std::get<0>(evals) += typename Accumulator::View(tmp);
}
}
};

template <typename FF> class context_stack : public Relation<context_stackImpl<FF>> {
public:
static constexpr const std::string_view NAME = "context_stack";

static std::string get_subrelation_label(size_t index)
{
switch (index) {}
return std::to_string(index);
}
};

} // namespace bb::avm2
92 changes: 85 additions & 7 deletions barretenberg/cpp/src/barretenberg/vm2/simulation/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,38 @@ class ContextInterface {
virtual bool halted() const = 0;
virtual void halt() = 0;

virtual uint32_t get_context_id() const = 0;

// Environment.
virtual const AztecAddress& get_address() const = 0;
virtual const AztecAddress& get_msg_sender() const = 0;
virtual std::span<const FF> get_calldata() const = 0;
virtual bool get_is_static() const = 0;

// Events
virtual void emit_context_snapshot() = 0;
virtual ContextEvent get_current_context() = 0;
};

// The context for a single nested call.
class Context : public ContextInterface {
class BaseContext : public ContextInterface {
public:
Context(AztecAddress address,
AztecAddress msg_sender,
std::span<const FF> calldata,
bool is_static,
std::unique_ptr<BytecodeManagerInterface> bytecode,
std::unique_ptr<MemoryInterface> memory)
BaseContext(uint32_t context_id,
AztecAddress address,
AztecAddress msg_sender,
std::span<const FF> calldata,
bool is_static,
std::unique_ptr<BytecodeManagerInterface> bytecode,
std::unique_ptr<MemoryInterface> memory,
EventEmitterInterface<ContextStackEvent>& ctx_stack_events)
: address(address)
, msg_sender(msg_sender)
, calldata(calldata.begin(), calldata.end())
, is_static(is_static)
, context_id(context_id)
, bytecode(std::move(bytecode))
, memory(std::move(memory))
, ctx_stack_events(ctx_stack_events)
{}

// Having getters and setters make it easier to mock the context.
Expand All @@ -66,26 +76,94 @@ class Context : public ContextInterface {
bool halted() const override { return has_halted; }
void halt() override { has_halted = true; }

uint32_t get_context_id() const override { return context_id; }

// Environment.
const AztecAddress& get_address() const override { return address; }
const AztecAddress& get_msg_sender() const override { return msg_sender; }
std::span<const FF> get_calldata() const override { return calldata; }
bool get_is_static() const override { return is_static; }

// Event Emitting
void emit_context_snapshot() override
{
ctx_stack_events.emit({ .id = context_id,
.next_pc = next_pc,
.msg_sender = msg_sender,
.contract_addr = address,
.is_static = is_static });
};

ContextEvent get_current_context() override
{
return {
.id = context_id, .pc = pc, .msg_sender = msg_sender, .contract_addr = address, .is_static = is_static
};
};
Comment on lines +88 to +102
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit strange (the assymetry) that the Context delegates emission of ContextEvents but does "own" and emit the stack events itself.

No need to change, let's just keep an eye on it.


private:
// Environment.
AztecAddress address;
AztecAddress msg_sender;
std::vector<FF> calldata;
bool is_static;

uint32_t context_id;

// Machine state.
uint32_t pc = 0;
uint32_t next_pc = 0;
bool has_halted = false;
std::vector<FF> nested_returndata;
std::unique_ptr<BytecodeManagerInterface> bytecode;
std::unique_ptr<MemoryInterface> memory;

// Emiiters
EventEmitterInterface<ContextStackEvent>& ctx_stack_events;
};

// TODO(ilyas): flesh these out in the cpp file, these are just temporary
class EnqueuedCallContext : public BaseContext {
public:
EnqueuedCallContext(uint32_t context_id,
AztecAddress address,
AztecAddress msg_sender,
std::span<const FF> calldata,
bool is_static,
std::unique_ptr<BytecodeManagerInterface> bytecode,
std::unique_ptr<MemoryInterface> memory,
EventEmitterInterface<ContextStackEvent>& ctx_stack_events)
: BaseContext(context_id,
address,
msg_sender,
calldata,
is_static,
std::move(bytecode),
std::move(memory),
ctx_stack_events)
{}
};

// Parameters for a nested call need to be changed
class NestedContext : public BaseContext {
public:
NestedContext(uint32_t context_id,
AztecAddress address,
AztecAddress msg_sender,
std::span<const FF> calldata,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So IIUC for the moment you split the classes but they are kind of the same and you'll be changing in other PR

bool is_static,
std::unique_ptr<BytecodeManagerInterface> bytecode,
std::unique_ptr<MemoryInterface> memory,
EventEmitterInterface<ContextStackEvent>& ctx_stack_events)
: BaseContext(context_id,
address,
msg_sender,
calldata,
is_static,
std::move(bytecode),
std::move(memory),
ctx_stack_events)
{}
};

} // namespace bb::avm2::simulation
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,63 @@

#include <cstdint>

#include "barretenberg/vm2/common/aztec_types.hpp"

namespace bb::avm2::simulation {

struct ContextEvent {
uint32_t whatever_is_needed = 0;
uint32_t id;
// uint32_t parent_id;

// State
uint32_t pc;
AztecAddress msg_sender;
AztecAddress contract_addr;
bool is_static;

// Calldata info
// uint32_t cd_addr;
// uint32_t cd_size_addr;

// Returndata info
// uint32_t rd_addr;
// uint32_t rd_size_addr;

// Success
// bool nested_ctx_success;

// Gas
// uint32_t l2_gas_used;
// uint32_t l2_gas_limit;
// uint32_t da_gas_used;
// uint32_t da_gas_limit;

// Tree State
// TreeSnapshots tree_state;
};

struct ContextStackEvent {
uint32_t whatever_is_snapshot = 0;
uint32_t id;
// uint32_t parent_id;

// State
uint32_t next_pc;
AztecAddress msg_sender;
AztecAddress contract_addr;
bool is_static;

// Calldata info
// uint32_t cd_addr;
// uint32_t cd_size_addr;

// Gas
// uint32_t l2_gas_used;
// uint32_t l2_gas_limit;
// uint32_t da_gas_used;
// uint32_t da_gas_limit;

// Tree State
// TreeSnapshots tree_state;
};

} // namespace bb::avm2::simulation
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct EventsContainer {
EventEmitterInterface<FieldGreaterThanEvent>::Container field_gt;
EventEmitterInterface<MerkleCheckEvent>::Container merkle_check;
EventEmitterInterface<RangeCheckEvent>::Container range_check;
EventEmitterInterface<ContextStackEvent>::Container context_stack;
};

} // namespace bb::avm2::simulation
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ struct ExecutionEvent {
// For sorting in tracegen.
uint32_t order;

uint32_t pc;
BytecodeId bytecode_id;
Instruction wire_instruction;
ExecutionOpCode opcode;
Expand Down
Loading