Skip to content
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

feat(vm): reading kernel state opcodes #5739

Merged
merged 55 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
8853132
avm_logderivative
Maddiaa0 Apr 3, 2024
abc3cba
temp: chall line up
Maddiaa0 Apr 4, 2024
8b88dcd
fix: degree too low for lookup relations
Maddiaa0 Apr 5, 2024
c8d9601
chore: rename validate trace proof to check circuit, make another met…
Maddiaa0 Apr 5, 2024
c695905
chore: remove dangling code
Maddiaa0 Apr 5, 2024
40eeb8f
Merge branch 'master' into md/04-03-avm_logderivative
Maddiaa0 Apr 5, 2024
619175b
chore: further cleanup
Maddiaa0 Apr 5, 2024
65c3159
chore: from powdr codegen
Maddiaa0 Apr 8, 2024
61d10f6
temp
Maddiaa0 Apr 11, 2024
7e84550
feat: bb support for public input columns
Maddiaa0 Apr 11, 2024
c054aca
Merge branch 'master' into md/04-11-feat_bb_support_for_public_input_…
Maddiaa0 Apr 11, 2024
f38773f
merge fixy
Maddiaa0 Apr 11, 2024
a4262dc
chore: test structure
Maddiaa0 Apr 11, 2024
cf4bb86
🧹
Maddiaa0 Apr 11, 2024
12a9789
use pilgen
Maddiaa0 Apr 12, 2024
14d15cc
Merge branch 'master' into md/04-11-feat_bb_support_for_public_input_…
Maddiaa0 Apr 12, 2024
b0f8041
feat: example caller and address opcode
Maddiaa0 Apr 12, 2024
d5813a2
feat: generalise builder, move after review
Maddiaa0 Apr 15, 2024
6265c61
Merge branch 'master' into md/04-12-feat_example_caller_and_address_o…
Maddiaa0 Apr 17, 2024
59b221c
fix: add tests for all call context opcodes
Maddiaa0 Apr 18, 2024
b613cf5
Merge branch 'master' into md/04-11-feat_bb_support_for_public_input_…
Maddiaa0 Apr 18, 2024
81a2500
Merge branch 'md/04-11-feat_bb_support_for_public_input_columns' into…
Maddiaa0 Apr 18, 2024
c934c61
chore: update pil comments
Maddiaa0 Apr 18, 2024
a8ceaf8
fix: remove redundant comment
Maddiaa0 Apr 18, 2024
af343db
fix: some negative tests
Maddiaa0 Apr 21, 2024
9388ecc
temp
Maddiaa0 Apr 29, 2024
9e7aad0
Merge branch 'master' into md/04-12-feat_example_caller_and_address_o…
Maddiaa0 May 7, 2024
859a3df
chore: remove l1 gas - no longer exists
Maddiaa0 May 7, 2024
b4a47b3
chore: rearrange where relations live, based on review
Maddiaa0 May 7, 2024
79cf849
Merge branch 'master' into md/04-11-feat_bb_support_for_public_input_…
Maddiaa0 May 7, 2024
faac5b1
chore: update codegen
Maddiaa0 May 7, 2024
7e70e7d
Merge branch 'md/04-11-feat_bb_support_for_public_input_columns' into…
Maddiaa0 May 7, 2024
4bb42b8
feat: add cpp constants gen to calculate kernel offsets
Maddiaa0 May 8, 2024
bccfd67
feat: add global variable opcodes
Maddiaa0 May 8, 2024
2a74110
fix: transaction fee selector test
Maddiaa0 May 8, 2024
1285ccf
fix: update tags to line up with kernel
Maddiaa0 May 8, 2024
60836ca
Merge branch 'master' into md/04-12-feat_example_caller_and_address_o…
Maddiaa0 May 8, 2024
e2bcca8
feat: parallelise check circuit relations
Maddiaa0 May 8, 2024
7bff191
fix: forge fmt after constant gen
Maddiaa0 May 8, 2024
115f17c
fix: no futures for wasm builds
Maddiaa0 May 8, 2024
de663f1
feat: fix segfaults in parallel circuit checking
Maddiaa0 May 9, 2024
c910000
fix: missed log
Maddiaa0 May 9, 2024
fd728c7
fix: incorrect kernel tests
Maddiaa0 May 9, 2024
66f958e
Merge branch 'master' into md/04-11-feat_bb_support_for_public_input_…
Maddiaa0 May 9, 2024
ecdf742
🧹
Maddiaa0 May 9, 2024
3f31357
Merge branch 'md/04-11-feat_bb_support_for_public_input_columns' into…
Maddiaa0 May 9, 2024
d6aeda8
feat: use codegen, vector public inputs - pubs now misaligned
Maddiaa0 May 9, 2024
ea48aca
fix: update codegen
Maddiaa0 May 9, 2024
e1e2cb8
fix: remove legacy codegen files
Maddiaa0 May 10, 2024
3be1242
Merge branch 'master' into md/04-12-feat_example_caller_and_address_o…
Maddiaa0 May 10, 2024
bee84aa
fix: relation tags + removal of function opcode
Maddiaa0 May 10, 2024
449f3da
fmt fix
Maddiaa0 May 10, 2024
700c9a1
Merge branch 'master' into md/04-12-feat_example_caller_and_address_o…
Maddiaa0 May 10, 2024
3cf29f3
fix: remove template brackets limit:
Maddiaa0 May 10, 2024
19fab2e
Merge branch 'master' into md/04-12-feat_example_caller_and_address_o…
Maddiaa0 May 10, 2024
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
40 changes: 40 additions & 0 deletions barretenberg/cpp/pil/avm/avm_kernel.pil
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
include "avm_main.pil";
include "constants.pil";

namespace avm_kernel(256);
pol public kernel_inputs;
pol commit kernel_sel;

// The general pattern for environment lookups is as follows:
Maddiaa0 marked this conversation as resolved.
Show resolved Hide resolved
// Each kernel opcode related to some fixed positions in the `public kernel_inputs` polynomial
// We can lookup into a fixed index of this polynomial by including constraints that force the value
// of kernel_sel to the value relevant to the given opcode that is active

#[SENDER_KERNEL]
Maddiaa0 marked this conversation as resolved.
Show resolved Hide resolved
avm_main.sel_op_sender * (kernel_sel - constants.SENDER_SELECTOR) = 0;

#[ADDRESS_KERNEL]
avm_main.sel_op_address * (kernel_sel - constants.ADDRESS_SELECTOR) = 0;

#[PORTAL_KERNEL]
avm_main.sel_op_portal * (kernel_sel - constants.PORTAL_SELECTOR) = 0;

#[FUNCTION_KERNEL]
avm_main.sel_op_function_selector * (kernel_sel - constants.FUNCTION_SELECTOR) = 0;

#[FEE_DA_GAS_KERNEL]
avm_main.sel_op_fee_per_da_gas * (kernel_sel - constants.FEE_PER_DA_GAS_SELECTOR) = 0;

#[FEE_L1_GAS_KERNEL]
avm_main.sel_op_fee_per_l1_gas * (kernel_sel - constants.FEE_PER_L1_GAS_SELECTOR) = 0;

#[FEE_L2_GAS_KERNEL]
avm_main.sel_op_fee_per_l2_gas * (kernel_sel - constants.FEE_PER_L2_GAS_SELECTOR) = 0;

// Note: in the future, with some codegen adjustments, this column will not be needed
// as we can just add every entry in the public kernel_inputs to the lookup table
pol commit q_public_input_kernel_add_to_table;

#[LOOKUP_INTO_KERNEL]
// TODO: FIX not having the trailing is_public breaking compilation :(
avm_main.q_kernel_lookup { avm_main.ia, kernel_sel } in q_public_input_kernel_add_to_table { kernel_inputs__is_public, avm_main.clk };
Copy link
Contributor

Choose a reason for hiding this comment

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

Whenever we have a lookup/perm involving the main trace, we define them in the main pil file. It is a convention that we try to follow.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would rather see kernel_sel defined in main trace. Then, all the above relations above can be moved in the main trace actually. The avm_kernel.pil would simply defined by the kernel_inputs and q_public_input_kernel_add_to_table

50 changes: 48 additions & 2 deletions barretenberg/cpp/pil/avm/avm_main.pil
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,30 @@
include "avm_mem.pil";
include "avm_alu.pil";
include "avm_binary.pil";
include "avm_kernel.pil";

namespace avm_main(256);
// Kernel lookup selector opcodes
pol commit q_kernel_lookup;

// CALL CONTEXT
pol commit sel_op_sender;
pol commit sel_op_address;
pol commit sel_op_portal;
pol commit sel_op_function_selector;

// FEES
pol commit sel_op_fee_per_l1_gas;
pol commit sel_op_fee_per_l2_gas;
pol commit sel_op_fee_per_da_gas;

// GLOBALS
pol commit sel_op_chain_id;
pol commit sel_op_version;
pol commit sel_op_block_number;
pol commit sel_op_coinbase;
pol commit sel_op_timestamp;


//===== CONSTANT POLYNOMIALS ==================================================
pol constant clk(i) { i };
Expand Down Expand Up @@ -58,7 +80,7 @@ namespace avm_main(256);
pol commit sel_op_lt;
// LTE
pol commit sel_op_lte;

// Helper selector to characterize an ALU chiplet selector
pol commit alu_sel;

Expand Down Expand Up @@ -121,6 +143,18 @@ namespace avm_main(256);
pol commit last;

// Relations on type constraints
sel_op_sender * (1 - sel_op_sender) = 0;
sel_op_address * (1 - sel_op_address) = 0;
sel_op_portal * (1 - sel_op_portal) = 0;
sel_op_function_selector * (1 - sel_op_function_selector) = 0;
sel_op_chain_id * (1 - sel_op_chain_id) = 0;
sel_op_version * (1 - sel_op_version) = 0;
sel_op_block_number * (1 - sel_op_block_number) = 0;
sel_op_coinbase * (1 - sel_op_coinbase) = 0;
sel_op_timestamp * (1 - sel_op_timestamp) = 0;
sel_op_fee_per_l1_gas * (1 - sel_op_fee_per_l1_gas) = 0;
sel_op_fee_per_l2_gas * (1 - sel_op_fee_per_l2_gas) = 0;
sel_op_fee_per_da_gas * (1 - sel_op_fee_per_da_gas) = 0;

sel_op_add * (1 - sel_op_add) = 0;
sel_op_sub * (1 - sel_op_sub) = 0;
Expand Down Expand Up @@ -208,6 +242,15 @@ namespace avm_main(256);
// (ib * inv - 1 + op_div_err) = 0 && op_err * (1 - inv) = 0
// This works in combination with op_div_err * (sel_op_div - 1) = 0;
// Drawback is the need to paralllelize the latter.

//===== KERNEL LOOKUPS =======================================================
pol KERNEL_SELECTORS = (
sel_op_sender + sel_op_address + sel_op_portal + sel_op_function_selector + sel_op_chain_id + sel_op_version +
sel_op_block_number + sel_op_coinbase + sel_op_timestamp + sel_op_fee_per_l1_gas + sel_op_fee_per_l2_gas + sel_op_fee_per_da_gas
);
// Ensure that only one kernel lookup is active when the kernel_sel is active
#[KERNEL_ACTIVE_CHECK]
KERNEL_SELECTORS * (1 - q_kernel_lookup) = 0;

//===== CONTROL FLOW =======================================================
//===== JUMP ===============================================================
Expand Down Expand Up @@ -243,7 +286,10 @@ namespace avm_main(256);

//===== CONTROL_FLOW_CONSISTENCY ============================================
pol INTERNAL_CALL_STACK_SELECTORS = (first + sel_internal_call + sel_internal_return + sel_halt);
pol OPCODE_SELECTORS = (sel_op_add + sel_op_sub + sel_op_div + sel_op_mul + sel_op_not + sel_op_eq + sel_op_and + sel_op_or + sel_op_xor);
pol OPCODE_SELECTORS = (
sel_op_add + sel_op_sub + sel_op_div + sel_op_mul + sel_op_not + sel_op_eq + sel_op_and + sel_op_or +
sel_op_xor + KERNEL_SELECTORS
);

// Program counter must increment if not jumping or returning
#[PC_INCREMENT]
Expand Down
11 changes: 11 additions & 0 deletions barretenberg/cpp/pil/avm/constants.pil
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

// NOTE: the constants in this file line up to the indexes of values in the
// `PublicKernelInputs.nr` object
namespace constants(256);
pol SENDER_SELECTOR = 0;
pol ADDRESS_SELECTOR = 1;
pol PORTAL_SELECTOR = 2;
pol FUNCTION_SELECTOR = 3;
pol FEE_PER_DA_GAS_SELECTOR = 9;
pol FEE_PER_L1_GAS_SELECTOR = 11;
pol FEE_PER_L2_GAS_SELECTOR = 13;
3 changes: 3 additions & 0 deletions barretenberg/cpp/pil/spike/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Spike machine

A spike machine for testing new PIL functionality
8 changes: 8 additions & 0 deletions barretenberg/cpp/pil/spike/spike.pil
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

namespace Spike(16);

pol constant first = [1] + [0]*;
pol commit x;
pol public kernel_inputs;

x - first = 0;
23 changes: 23 additions & 0 deletions barretenberg/cpp/scripts/compile_avm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
Copy link
Contributor

Choose a reason for hiding this comment

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

Very welcome script! Thx.

use_zsh_alias() {
# Run Zsh command, source .zshrc, and then execute the alias
zsh -i -c "$1"
}

# Compile
use_zsh_alias "bb_pil pil/avm/avm_main.pil --name Avm"

# Format generated folders
root_dir="src"

# Find all directories named 'generate' under the specified root directory
find "$root_dir" -type d -name 'generate' | while read dir_path; do
echo "Processing directory: $dir_path"

# Find all C/C++ source files in these directories and format them
find "$dir_path" -type f \( -iname '*.hpp' -o -iname '*.cpp' \) -exec clang-format -i {} +
done


# Build vm tests
cmake --build --preset clang16 --target vm_tests
3 changes: 3 additions & 0 deletions barretenberg/cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# Temp, increase this flag to enable vm compilation
set(CMAKE_CXX_FLAGS "-fbracket-depth=512")

# Enable the following warnings project wide.
# If any compilation issues arise in the future, they should not be silenced here but rather in the
# module's own CMakeLists.txt by adding conditional compilation flags like the following
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

#pragma once
#include "../../relation_parameters.hpp"
#include "../../relation_types.hpp"
#include "./declare_views.hpp"

namespace bb::Avm_vm {

template <typename FF> struct Avm_environmentRow {
FF avm_environment_environment_selector{};
FF avm_environment_q_environment_lookup{};
FF avm_main_sel_op_address{};
FF avm_main_sel_op_sender{};
};

inline std::string get_relation_label_avm_environment(int index)
{
switch (index) {
case 0:
return "SENDER_ENVIRONMENT";

case 1:
return "ADDRESS_ENVIRONMENT";
}
return std::to_string(index);
}

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

static constexpr std::array<size_t, 2> SUBRELATION_PARTIAL_LENGTHS{
4,
4,
};

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)
{

// Contribution 0
{
Avm_DECLARE_VIEWS(0);

auto tmp = ((avm_environment_q_environment_lookup * avm_main_sel_op_sender) *
avm_environment_environment_selector);
tmp *= scaling_factor;
std::get<0>(evals) += tmp;
}
// Contribution 1
{
Avm_DECLARE_VIEWS(1);

auto tmp = ((avm_environment_q_environment_lookup * avm_main_sel_op_address) *
(avm_environment_environment_selector - FF(1)));
tmp *= scaling_factor;
std::get<1>(evals) += tmp;
}
}
};

template <typename FF> using avm_environment = Relation<avm_environmentImpl<FF>>;

} // namespace bb::Avm_vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@

#pragma once
#include "../../relation_parameters.hpp"
#include "../../relation_types.hpp"
#include "./declare_views.hpp"

namespace bb::Avm_vm {

template <typename FF> struct Avm_kernelRow {
FF avm_kernel_kernel_sel{};
FF avm_main_sel_op_address{};
FF avm_main_sel_op_fee_per_da_gas{};
FF avm_main_sel_op_fee_per_l1_gas{};
FF avm_main_sel_op_fee_per_l2_gas{};
FF avm_main_sel_op_function_selector{};
FF avm_main_sel_op_portal{};
FF avm_main_sel_op_sender{};
};

inline std::string get_relation_label_avm_kernel(int index)
{
switch (index) {
case 0:
return "SENDER_KERNEL";

case 1:
return "ADDRESS_KERNEL";

case 2:
return "PORTAL_KERNEL";

case 3:
return "FUNCTION_KERNEL";

case 4:
return "FEE_DA_GAS_KERNEL";

case 5:
return "FEE_L1_GAS_KERNEL";

case 6:
return "FEE_L2_GAS_KERNEL";
}
return std::to_string(index);
}

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

static constexpr std::array<size_t, 7> SUBRELATION_PARTIAL_LENGTHS{
3, 3, 3, 3, 3, 3, 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)
{

// Contribution 0
{
Avm_DECLARE_VIEWS(0);

auto tmp = (avm_main_sel_op_sender * (avm_kernel_kernel_sel - FF(0)));
tmp *= scaling_factor;
std::get<0>(evals) += tmp;
}
// Contribution 1
{
Avm_DECLARE_VIEWS(1);

auto tmp = (avm_main_sel_op_address * (avm_kernel_kernel_sel - FF(1)));
tmp *= scaling_factor;
std::get<1>(evals) += tmp;
}
// Contribution 2
{
Avm_DECLARE_VIEWS(2);

auto tmp = (avm_main_sel_op_portal * (avm_kernel_kernel_sel - FF(2)));
tmp *= scaling_factor;
std::get<2>(evals) += tmp;
}
// Contribution 3
{
Avm_DECLARE_VIEWS(3);

auto tmp = (avm_main_sel_op_function_selector * (avm_kernel_kernel_sel - FF(3)));
tmp *= scaling_factor;
std::get<3>(evals) += tmp;
}
// Contribution 4
{
Avm_DECLARE_VIEWS(4);

auto tmp = (avm_main_sel_op_fee_per_da_gas * (avm_kernel_kernel_sel - FF(9)));
tmp *= scaling_factor;
std::get<4>(evals) += tmp;
}
// Contribution 5
{
Avm_DECLARE_VIEWS(5);

auto tmp = (avm_main_sel_op_fee_per_l1_gas * (avm_kernel_kernel_sel - FF(11)));
tmp *= scaling_factor;
std::get<5>(evals) += tmp;
}
// Contribution 6
{
Avm_DECLARE_VIEWS(6);

auto tmp = (avm_main_sel_op_fee_per_l2_gas * (avm_kernel_kernel_sel - FF(13)));
tmp *= scaling_factor;
std::get<6>(evals) += tmp;
}
}
};

template <typename FF> using avm_kernel = Relation<avm_kernelImpl<FF>>;

} // namespace bb::Avm_vm
Loading
Loading