-
Notifications
You must be signed in to change notification settings - Fork 598
feat(avm): vm2 initial context #12972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c0318b2
scaffolding
IlyasRidhuan 05425d0
remove context provider
IlyasRidhuan fbe2f0b
start adding pil
IlyasRidhuan 0f2f514
add tests
IlyasRidhuan be0941a
clean up
IlyasRidhuan 25be95b
no more seg fault in mock
IlyasRidhuan 53e70de
cleanup
IlyasRidhuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
barretenberg/cpp/src/barretenberg/vm2/generated/relations/context.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
44 changes: 44 additions & 0 deletions
44
barretenberg/cpp/src/barretenberg/vm2/generated/relations/context_stack.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -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 | ||
| }; | ||
| }; | ||
|
|
||
| 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, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.