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 17 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
16 changes: 16 additions & 0 deletions barretenberg/cpp/pil/avm/avm_environment.pil
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

namespace avm_environment(256);

// The general pattern for environment lookups is as follows:
// - If we are performing an environment lookup, then we set q_environment_lookup
// - We can toggle what index in the
pol commit environment_selector;
pol commit q_environment_lookup;

// Note we are currently using arbitrary opcodes for each
// For this demo, caller is stored at index 0
#[SENDER_ENVIRONMENT]
q_environment_lookup * avm_main.sel_op_sender * (environment_selector) = 0;
Maddiaa0 marked this conversation as resolved.
Show resolved Hide resolved

#[ADDRESS_ENVIRONMENT]
q_environment_lookup * avm_main.sel_op_address * (environment_selector - 1) = 0;
Maddiaa0 marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 15 additions & 1 deletion barretenberg/cpp/pil/avm/avm_main.pil
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@
include "avm_mem.pil";
include "avm_alu.pil";
include "avm_binary.pil";
include "avm_environment.pil";

namespace avm_main(256);
//===== PUBLIC INPUTS =========================================================
pol public kernel_inputs;

// Envrionment lookup selector opcodes
Maddiaa0 marked this conversation as resolved.
Show resolved Hide resolved
// NOTE: we start with an arbitrary subset of operations - just these two for now
// CALLER
pol commit sel_op_sender;
// ADDRESS
pol commit sel_op_address;

#[LOOKUP_INTO_ENVIRONMENT]
// TODO: FIX not having the trailing is_public breaking compilation :(
avm_environment.q_environment_lookup { ia, avm_environment.environment_selector } in avm_environment.q_environment_lookup { kernel_inputs__is_public, clk };
Maddiaa0 marked this conversation as resolved.
Show resolved Hide resolved

//===== CONSTANT POLYNOMIALS ==================================================
pol constant clk(i) { i };
Expand Down Expand Up @@ -58,7 +72,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
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
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
Expand Up @@ -80,6 +80,8 @@
[[maybe_unused]] auto avm_byte_lookup_table_input_b = View(new_term.avm_byte_lookup_table_input_b); \
[[maybe_unused]] auto avm_byte_lookup_table_op_id = View(new_term.avm_byte_lookup_table_op_id); \
[[maybe_unused]] auto avm_byte_lookup_table_output = View(new_term.avm_byte_lookup_table_output); \
[[maybe_unused]] auto avm_environment_environment_selector = View(new_term.avm_environment_environment_selector); \
[[maybe_unused]] auto avm_environment_q_environment_lookup = View(new_term.avm_environment_q_environment_lookup); \
[[maybe_unused]] auto avm_main_alu_sel = View(new_term.avm_main_alu_sel); \
[[maybe_unused]] auto avm_main_bin_op_id = View(new_term.avm_main_bin_op_id); \
[[maybe_unused]] auto avm_main_bin_sel = View(new_term.avm_main_bin_sel); \
Expand All @@ -98,6 +100,7 @@
[[maybe_unused]] auto avm_main_ind_op_d = View(new_term.avm_main_ind_op_d); \
[[maybe_unused]] auto avm_main_internal_return_ptr = View(new_term.avm_main_internal_return_ptr); \
[[maybe_unused]] auto avm_main_inv = View(new_term.avm_main_inv); \
[[maybe_unused]] auto avm_main_kernel_inputs__is_public = View(new_term.avm_main_kernel_inputs__is_public); \
[[maybe_unused]] auto avm_main_last = View(new_term.avm_main_last); \
[[maybe_unused]] auto avm_main_mem_idx_a = View(new_term.avm_main_mem_idx_a); \
[[maybe_unused]] auto avm_main_mem_idx_b = View(new_term.avm_main_mem_idx_b); \
Expand All @@ -123,6 +126,7 @@
[[maybe_unused]] auto avm_main_sel_mov_a = View(new_term.avm_main_sel_mov_a); \
[[maybe_unused]] auto avm_main_sel_mov_b = View(new_term.avm_main_sel_mov_b); \
[[maybe_unused]] auto avm_main_sel_op_add = View(new_term.avm_main_sel_op_add); \
[[maybe_unused]] auto avm_main_sel_op_address = View(new_term.avm_main_sel_op_address); \
[[maybe_unused]] auto avm_main_sel_op_and = View(new_term.avm_main_sel_op_and); \
[[maybe_unused]] auto avm_main_sel_op_div = View(new_term.avm_main_sel_op_div); \
[[maybe_unused]] auto avm_main_sel_op_eq = View(new_term.avm_main_sel_op_eq); \
Expand All @@ -131,6 +135,7 @@
[[maybe_unused]] auto avm_main_sel_op_mul = View(new_term.avm_main_sel_op_mul); \
[[maybe_unused]] auto avm_main_sel_op_not = View(new_term.avm_main_sel_op_not); \
[[maybe_unused]] auto avm_main_sel_op_or = View(new_term.avm_main_sel_op_or); \
[[maybe_unused]] auto avm_main_sel_op_sender = View(new_term.avm_main_sel_op_sender); \
[[maybe_unused]] auto avm_main_sel_op_sub = View(new_term.avm_main_sel_op_sub); \
[[maybe_unused]] auto avm_main_sel_op_xor = View(new_term.avm_main_sel_op_xor); \
[[maybe_unused]] auto avm_main_sel_rng_16 = View(new_term.avm_main_sel_rng_16); \
Expand Down Expand Up @@ -173,6 +178,7 @@
[[maybe_unused]] auto perm_main_mem_ind_d = View(new_term.perm_main_mem_ind_d); \
[[maybe_unused]] auto lookup_byte_lengths = View(new_term.lookup_byte_lengths); \
[[maybe_unused]] auto lookup_byte_operations = View(new_term.lookup_byte_operations); \
[[maybe_unused]] auto lookup_into_environment = View(new_term.lookup_into_environment); \
[[maybe_unused]] auto incl_main_tag_err = View(new_term.incl_main_tag_err); \
[[maybe_unused]] auto incl_mem_tag_err = View(new_term.incl_mem_tag_err); \
[[maybe_unused]] auto lookup_u8_0 = View(new_term.lookup_u8_0); \
Expand All @@ -194,6 +200,7 @@
[[maybe_unused]] auto lookup_u16_14 = View(new_term.lookup_u16_14); \
[[maybe_unused]] auto lookup_byte_lengths_counts = View(new_term.lookup_byte_lengths_counts); \
[[maybe_unused]] auto lookup_byte_operations_counts = View(new_term.lookup_byte_operations_counts); \
[[maybe_unused]] auto lookup_into_environment_counts = View(new_term.lookup_into_environment_counts); \
[[maybe_unused]] auto incl_main_tag_err_counts = View(new_term.incl_main_tag_err_counts); \
[[maybe_unused]] auto incl_mem_tag_err_counts = View(new_term.incl_mem_tag_err_counts); \
[[maybe_unused]] auto lookup_u8_0_counts = View(new_term.lookup_u8_0_counts); \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@


#pragma once

#include "barretenberg/relations/generic_lookup/generic_lookup_relation.hpp"

#include <cstddef>
#include <tuple>

namespace bb {

/**
* @brief This class contains an example of how to set LookupSettings classes used by the
* GenericLookupRelationImpl class to specify a scaled lookup
*
* @details To create your own lookup:
* 1) Create a copy of this class and rename it
* 2) Update all the values with the ones needed for your lookup
* 3) Update "DECLARE_LOOKUP_IMPLEMENTATIONS_FOR_ALL_SETTINGS" and "DEFINE_LOOKUP_IMPLEMENTATIONS_FOR_ALL_SETTINGS" to
* include the new settings
* 4) Add the relation with the chosen settings to Relations in the flavor (for example,"`
* using Relations = std::tuple<GenericLookupRelation<ExampleXorLookupSettings,
* FF>>;)`
*
*/
class lookup_into_environment_lookup_settings {
public:
/**
* @brief The number of read terms (how many lookups we perform) in each row
*
*/
static constexpr size_t READ_TERMS = 1;
/**
* @brief The number of write terms (how many additions to the lookup table we make) in each row
*
*/
static constexpr size_t WRITE_TERMS = 1;

/**
* @brief The type of READ_TERM used for each read index (basic and scaled)
*
*/
static constexpr size_t READ_TERM_TYPES[READ_TERMS] = { 0 };

/**
* @brief They type of WRITE_TERM used for each write index
*
*/
static constexpr size_t WRITE_TERM_TYPES[WRITE_TERMS] = { 0 };

/**
* @brief How many values represent a single lookup object. This value is used by the automatic read term
* implementation in the relation in case the lookup is a basic or scaled tuple and in the write term if it's a
* basic tuple
*
*/
static constexpr size_t LOOKUP_TUPLE_SIZE = 2;

/**
* @brief The polynomial degree of the relation telling us if the inverse polynomial value needs to be computed
*
*/
static constexpr size_t INVERSE_EXISTS_POLYNOMIAL_DEGREE = 4;

/**
* @brief The degree of the read term if implemented arbitrarily. This value is not used by basic and scaled read
* terms, but will cause compilation error if not defined
*
*/
static constexpr size_t READ_TERM_DEGREE = 0;

/**
* @brief The degree of the write term if implemented arbitrarily. This value is not used by the basic write
* term, but will cause compilation error if not defined
*
*/

static constexpr size_t WRITE_TERM_DEGREE = 0;

/**
* @brief If this method returns true on a row of values, then the inverse polynomial exists at this index.
* Otherwise the value needs to be set to zero.
*
* @details If this is true then the lookup takes place in this row
*
*/

template <typename AllEntities> static inline auto inverse_polynomial_is_computed_at_row(const AllEntities& in)
{
return (in.avm_environment_q_environment_lookup == 1 || in.avm_environment_q_environment_lookup == 1);
}

/**
* @brief Subprocedure for computing the value deciding if the inverse polynomial value needs to be checked in this
* row
*
* @tparam Accumulator Type specified by the lookup relation
* @tparam AllEntities Values/Univariates of all entities row
* @param in Value/Univariate of all entities at row/edge
* @return Accumulator
*/

template <typename Accumulator, typename AllEntities>
static inline auto compute_inverse_exists(const AllEntities& in)
{
using View = typename Accumulator::View;
const auto is_operation = View(in.avm_environment_q_environment_lookup);
const auto is_table_entry = View(in.avm_environment_q_environment_lookup);
return (is_operation + is_table_entry - is_operation * is_table_entry);
}

/**
* @brief Get all the entities for the lookup when need to update them
*
* @details The generic structure of this tuple is described in ./generic_lookup_relation.hpp . The following is
description for the current case:
The entities are returned as a tuple of references in the following order (this is for ):
* - The entity/polynomial used to store the product of the inverse values
* - The entity/polynomial that specifies how many times the lookup table entry at this row has been looked up
* - READ_TERMS entities/polynomials that enable individual lookup operations
* - The entity/polynomial that enables adding an entry to the lookup table in this row
* - LOOKUP_TUPLE_SIZE entities/polynomials representing the basic tuple being looked up as the first read term
* - LOOKUP_TUPLE_SIZE entities/polynomials representing the previous accumulators in the second read term
(scaled tuple)
* - LOOKUP_TUPLE_SIZE entities/polynomials representing the shifts in the second read term (scaled tuple)
* - LOOKUP_TUPLE_SIZE entities/polynomials representing the current accumulators in the second read term
(scaled tuple)
* - LOOKUP_TUPLE_SIZE entities/polynomials representing basic tuples added to the table
*
* @return All the entities needed for the lookup
*/

template <typename AllEntities> static inline auto get_const_entities(const AllEntities& in)
{

return std::forward_as_tuple(in.lookup_into_environment,
in.lookup_into_environment_counts,
in.avm_environment_q_environment_lookup,
in.avm_environment_q_environment_lookup,
in.avm_main_ia,
in.avm_environment_environment_selector,
in.avm_main_kernel_inputs__is_public,
in.avm_main_clk);
}

/**
* @brief Get all the entities for the lookup when we only need to read them
* @details Same as in get_const_entities, but nonconst
*
* @return All the entities needed for the lookup
*/

template <typename AllEntities> static inline auto get_nonconst_entities(AllEntities& in)
{

return std::forward_as_tuple(in.lookup_into_environment,
in.lookup_into_environment_counts,
in.avm_environment_q_environment_lookup,
in.avm_environment_q_environment_lookup,
in.avm_main_ia,
in.avm_environment_environment_selector,
in.avm_main_kernel_inputs__is_public,
in.avm_main_clk);
}
};

template <typename FF_>
using lookup_into_environment_relation = GenericLookupRelation<lookup_into_environment_lookup_settings, FF_>;
template <typename FF_> using lookup_into_environment = GenericLookup<lookup_into_environment_lookup_settings, FF_>;

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

#define Spike_DECLARE_VIEWS(index) \
using Accumulator = typename std::tuple_element<index, ContainerOverSubrelations>::type; \
using View = typename Accumulator::View; \
[[maybe_unused]] auto Spike_first = View(new_term.Spike_first); \
[[maybe_unused]] auto Spike_kernel_inputs = View(new_term.Spike_kernel_inputs); \
[[maybe_unused]] auto Spike_x = View(new_term.Spike_x);
Loading
Loading