Skip to content

Commit e2b1361

Browse files
authored
feat(avm): vm2 initial context (#12972)
This adds the initial work for `context` and `context_stack` to vm2. The interfaces will still need to be updated in the future, but it sets up the points where the context events will happen (i.e. at the end of the main execution loop and within `call`). This also outlines the contents of the two events albeit commented out for now (until we have the supported inputs in the context itself)
1 parent ea35802 commit e2b1361

26 files changed

+490
-73
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This is a virtual gadget, which is part of the execution trace.
2+
namespace execution;
3+
4+
pol commit context_id;
5+
pol commit pc;
6+
pol commit msg_sender;
7+
pol commit contract_address;
8+
9+
pol commit is_static;
10+
is_static * (1 - is_static) = 0;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace context_stack;
2+
3+
pol commit context_id;
4+
pol commit pc;
5+
pol commit msg_sender;
6+
pol commit contract_address;
7+
8+
pol commit is_static;
9+
is_static * (1 - is_static) = 0;

barretenberg/cpp/pil/vm2/execution.pil

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ include "poseidon2_perm.pil";
1717
include "scalar_mul.pil";
1818
include "to_radix.pil";
1919
include "ff_gt.pil";
20+
include "context.pil;"
21+
include "context_stack.pil";
2022

2123
namespace execution;
2224

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

32-
pol commit pc;
3334
pol commit bytecode_id;
3435
pol commit clk;
3536
pol commit last;

barretenberg/cpp/src/barretenberg/vm2/generated/columns.hpp

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

barretenberg/cpp/src/barretenberg/vm2/generated/flavor.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "relations/bc_retrieval.hpp"
2626
#include "relations/bitwise.hpp"
2727
#include "relations/class_id_derivation.hpp"
28+
#include "relations/context.hpp"
29+
#include "relations/context_stack.hpp"
2830
#include "relations/ecc.hpp"
2931
#include "relations/execution.hpp"
3032
#include "relations/ff_gt.hpp"
@@ -94,12 +96,12 @@ class AvmFlavor {
9496
static constexpr bool HasZK = false;
9597

9698
static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 44;
97-
static constexpr size_t NUM_WITNESS_ENTITIES = 914;
99+
static constexpr size_t NUM_WITNESS_ENTITIES = 923;
98100
static constexpr size_t NUM_SHIFTED_ENTITIES = 134;
99101
static constexpr size_t NUM_WIRES = NUM_WITNESS_ENTITIES + NUM_PRECOMPUTED_ENTITIES;
100102
// We have two copies of the witness entities, so we subtract the number of fixed ones (they have no shift), one for
101103
// the unshifted and one for the shifted
102-
static constexpr size_t NUM_ALL_ENTITIES = 1092;
104+
static constexpr size_t NUM_ALL_ENTITIES = 1101;
103105

104106
// In the sumcheck univariate computation, we divide the trace in chunks and each chunk is
105107
// evenly processed by all the threads. This constant defines the maximum number of rows
@@ -122,6 +124,8 @@ class AvmFlavor {
122124
avm2::bc_retrieval<FF_>,
123125
avm2::bitwise<FF_>,
124126
avm2::class_id_derivation<FF_>,
127+
avm2::context<FF_>,
128+
avm2::context_stack<FF_>,
125129
avm2::ecc<FF_>,
126130
avm2::execution<FF_>,
127131
avm2::ff_gt<FF_>,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// AUTOGENERATED FILE
2+
#pragma once
3+
4+
#include <string_view>
5+
6+
#include "barretenberg/relations/relation_parameters.hpp"
7+
#include "barretenberg/relations/relation_types.hpp"
8+
9+
namespace bb::avm2 {
10+
11+
template <typename FF_> class contextImpl {
12+
public:
13+
using FF = FF_;
14+
15+
static constexpr std::array<size_t, 1> SUBRELATION_PARTIAL_LENGTHS = { 3 };
16+
17+
template <typename ContainerOverSubrelations, typename AllEntities>
18+
void static accumulate(ContainerOverSubrelations& evals,
19+
const AllEntities& new_term,
20+
[[maybe_unused]] const RelationParameters<FF>&,
21+
[[maybe_unused]] const FF& scaling_factor)
22+
{
23+
24+
{
25+
using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>;
26+
auto tmp = new_term.execution_is_static * (FF(1) - new_term.execution_is_static);
27+
tmp *= scaling_factor;
28+
std::get<0>(evals) += typename Accumulator::View(tmp);
29+
}
30+
}
31+
};
32+
33+
template <typename FF> class context : public Relation<contextImpl<FF>> {
34+
public:
35+
static constexpr const std::string_view NAME = "context";
36+
37+
static std::string get_subrelation_label(size_t index)
38+
{
39+
switch (index) {}
40+
return std::to_string(index);
41+
}
42+
};
43+
44+
} // namespace bb::avm2
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// AUTOGENERATED FILE
2+
#pragma once
3+
4+
#include <string_view>
5+
6+
#include "barretenberg/relations/relation_parameters.hpp"
7+
#include "barretenberg/relations/relation_types.hpp"
8+
9+
namespace bb::avm2 {
10+
11+
template <typename FF_> class context_stackImpl {
12+
public:
13+
using FF = FF_;
14+
15+
static constexpr std::array<size_t, 1> SUBRELATION_PARTIAL_LENGTHS = { 3 };
16+
17+
template <typename ContainerOverSubrelations, typename AllEntities>
18+
void static accumulate(ContainerOverSubrelations& evals,
19+
const AllEntities& new_term,
20+
[[maybe_unused]] const RelationParameters<FF>&,
21+
[[maybe_unused]] const FF& scaling_factor)
22+
{
23+
24+
{
25+
using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>;
26+
auto tmp = new_term.context_stack_is_static * (FF(1) - new_term.context_stack_is_static);
27+
tmp *= scaling_factor;
28+
std::get<0>(evals) += typename Accumulator::View(tmp);
29+
}
30+
}
31+
};
32+
33+
template <typename FF> class context_stack : public Relation<context_stackImpl<FF>> {
34+
public:
35+
static constexpr const std::string_view NAME = "context_stack";
36+
37+
static std::string get_subrelation_label(size_t index)
38+
{
39+
switch (index) {}
40+
return std::to_string(index);
41+
}
42+
};
43+
44+
} // namespace bb::avm2

barretenberg/cpp/src/barretenberg/vm2/simulation/context.hpp

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,38 @@ class ContextInterface {
3030
virtual bool halted() const = 0;
3131
virtual void halt() = 0;
3232

33+
virtual uint32_t get_context_id() const = 0;
34+
3335
// Environment.
3436
virtual const AztecAddress& get_address() const = 0;
3537
virtual const AztecAddress& get_msg_sender() const = 0;
3638
virtual std::span<const FF> get_calldata() const = 0;
3739
virtual bool get_is_static() const = 0;
40+
41+
// Events
42+
virtual void emit_context_snapshot() = 0;
43+
virtual ContextEvent get_current_context() = 0;
3844
};
3945

4046
// The context for a single nested call.
41-
class Context : public ContextInterface {
47+
class BaseContext : public ContextInterface {
4248
public:
43-
Context(AztecAddress address,
44-
AztecAddress msg_sender,
45-
std::span<const FF> calldata,
46-
bool is_static,
47-
std::unique_ptr<BytecodeManagerInterface> bytecode,
48-
std::unique_ptr<MemoryInterface> memory)
49+
BaseContext(uint32_t context_id,
50+
AztecAddress address,
51+
AztecAddress msg_sender,
52+
std::span<const FF> calldata,
53+
bool is_static,
54+
std::unique_ptr<BytecodeManagerInterface> bytecode,
55+
std::unique_ptr<MemoryInterface> memory,
56+
EventEmitterInterface<ContextStackEvent>& ctx_stack_events)
4957
: address(address)
5058
, msg_sender(msg_sender)
5159
, calldata(calldata.begin(), calldata.end())
5260
, is_static(is_static)
61+
, context_id(context_id)
5362
, bytecode(std::move(bytecode))
5463
, memory(std::move(memory))
64+
, ctx_stack_events(ctx_stack_events)
5565
{}
5666

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

79+
uint32_t get_context_id() const override { return context_id; }
80+
6981
// Environment.
7082
const AztecAddress& get_address() const override { return address; }
7183
const AztecAddress& get_msg_sender() const override { return msg_sender; }
7284
std::span<const FF> get_calldata() const override { return calldata; }
7385
bool get_is_static() const override { return is_static; }
7486

87+
// Event Emitting
88+
void emit_context_snapshot() override
89+
{
90+
ctx_stack_events.emit({ .id = context_id,
91+
.next_pc = next_pc,
92+
.msg_sender = msg_sender,
93+
.contract_addr = address,
94+
.is_static = is_static });
95+
};
96+
97+
ContextEvent get_current_context() override
98+
{
99+
return {
100+
.id = context_id, .pc = pc, .msg_sender = msg_sender, .contract_addr = address, .is_static = is_static
101+
};
102+
};
103+
75104
private:
76105
// Environment.
77106
AztecAddress address;
78107
AztecAddress msg_sender;
79108
std::vector<FF> calldata;
80109
bool is_static;
81110

111+
uint32_t context_id;
112+
82113
// Machine state.
83114
uint32_t pc = 0;
84115
uint32_t next_pc = 0;
85116
bool has_halted = false;
86117
std::vector<FF> nested_returndata;
87118
std::unique_ptr<BytecodeManagerInterface> bytecode;
88119
std::unique_ptr<MemoryInterface> memory;
120+
121+
// Emiiters
122+
EventEmitterInterface<ContextStackEvent>& ctx_stack_events;
123+
};
124+
125+
// TODO(ilyas): flesh these out in the cpp file, these are just temporary
126+
class EnqueuedCallContext : public BaseContext {
127+
public:
128+
EnqueuedCallContext(uint32_t context_id,
129+
AztecAddress address,
130+
AztecAddress msg_sender,
131+
std::span<const FF> calldata,
132+
bool is_static,
133+
std::unique_ptr<BytecodeManagerInterface> bytecode,
134+
std::unique_ptr<MemoryInterface> memory,
135+
EventEmitterInterface<ContextStackEvent>& ctx_stack_events)
136+
: BaseContext(context_id,
137+
address,
138+
msg_sender,
139+
calldata,
140+
is_static,
141+
std::move(bytecode),
142+
std::move(memory),
143+
ctx_stack_events)
144+
{}
145+
};
146+
147+
// Parameters for a nested call need to be changed
148+
class NestedContext : public BaseContext {
149+
public:
150+
NestedContext(uint32_t context_id,
151+
AztecAddress address,
152+
AztecAddress msg_sender,
153+
std::span<const FF> calldata,
154+
bool is_static,
155+
std::unique_ptr<BytecodeManagerInterface> bytecode,
156+
std::unique_ptr<MemoryInterface> memory,
157+
EventEmitterInterface<ContextStackEvent>& ctx_stack_events)
158+
: BaseContext(context_id,
159+
address,
160+
msg_sender,
161+
calldata,
162+
is_static,
163+
std::move(bytecode),
164+
std::move(memory),
165+
ctx_stack_events)
166+
{}
89167
};
90168

91169
} // namespace bb::avm2::simulation

barretenberg/cpp/src/barretenberg/vm2/simulation/events/context_events.hpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,63 @@
22

33
#include <cstdint>
44

5+
#include "barretenberg/vm2/common/aztec_types.hpp"
6+
57
namespace bb::avm2::simulation {
68

79
struct ContextEvent {
8-
uint32_t whatever_is_needed = 0;
10+
uint32_t id;
11+
// uint32_t parent_id;
12+
13+
// State
14+
uint32_t pc;
15+
AztecAddress msg_sender;
16+
AztecAddress contract_addr;
17+
bool is_static;
18+
19+
// Calldata info
20+
// uint32_t cd_addr;
21+
// uint32_t cd_size_addr;
22+
23+
// Returndata info
24+
// uint32_t rd_addr;
25+
// uint32_t rd_size_addr;
26+
27+
// Success
28+
// bool nested_ctx_success;
29+
30+
// Gas
31+
// uint32_t l2_gas_used;
32+
// uint32_t l2_gas_limit;
33+
// uint32_t da_gas_used;
34+
// uint32_t da_gas_limit;
35+
36+
// Tree State
37+
// TreeSnapshots tree_state;
938
};
1039

1140
struct ContextStackEvent {
12-
uint32_t whatever_is_snapshot = 0;
41+
uint32_t id;
42+
// uint32_t parent_id;
43+
44+
// State
45+
uint32_t next_pc;
46+
AztecAddress msg_sender;
47+
AztecAddress contract_addr;
48+
bool is_static;
49+
50+
// Calldata info
51+
// uint32_t cd_addr;
52+
// uint32_t cd_size_addr;
53+
54+
// Gas
55+
// uint32_t l2_gas_used;
56+
// uint32_t l2_gas_limit;
57+
// uint32_t da_gas_used;
58+
// uint32_t da_gas_limit;
59+
60+
// Tree State
61+
// TreeSnapshots tree_state;
1362
};
1463

1564
} // namespace bb::avm2::simulation

barretenberg/cpp/src/barretenberg/vm2/simulation/events/events_container.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct EventsContainer {
4141
EventEmitterInterface<FieldGreaterThanEvent>::Container field_gt;
4242
EventEmitterInterface<MerkleCheckEvent>::Container merkle_check;
4343
EventEmitterInterface<RangeCheckEvent>::Container range_check;
44+
EventEmitterInterface<ContextStackEvent>::Container context_stack;
4445
};
4546

4647
} // namespace bb::avm2::simulation

0 commit comments

Comments
 (0)