Skip to content

Commit 2631096

Browse files
committed
feat(avm): support col arrays and add keccak cols
1 parent 593f810 commit 2631096

File tree

7 files changed

+89
-16
lines changed

7 files changed

+89
-16
lines changed

barretenberg/cpp/pil/vm2/execution.pil

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ include "range_check.pil";
1010
include "bitwise.pil";
1111
include "merkle_check.pil";
1212
include "memory.pil";
13+
include "keccakf1600.pil";
1314
include "precomputed.pil";
1415
include "sha256.pil";
1516
include "ecc.pil";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace keccakf1600;
2+
3+
// Dummy.
4+
pol commit keccak[1000];
5+
6+
// Dummy.
7+
keccak[0] = keccak[0];
8+
keccak[1] = keccak[1];

barretenberg/cpp/src/barretenberg/vm2/common/macros.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
// The following defines a FOR_EACH recursive macro.
55
// It can be used as: FOR_EACH(macro, a1, a2, a3, ..., aN)
66
// If something starts failing when adding columns, add another layer here.
7-
#define EXPAND(...) EXPAND5(EXPAND5(EXPAND5(EXPAND5(__VA_ARGS__))))
7+
#define EXPAND(...) EXPAND6(EXPAND6(EXPAND6(EXPAND6(__VA_ARGS__))))
8+
#define EXPAND6(...) EXPAND5(EXPAND5(EXPAND5(EXPAND5(__VA_ARGS__))))
89
#define EXPAND5(...) EXPAND4(EXPAND4(EXPAND4(EXPAND4(__VA_ARGS__))))
910
#define EXPAND4(...) EXPAND3(EXPAND3(EXPAND3(EXPAND3(__VA_ARGS__))))
1011
#define EXPAND3(...) EXPAND2(EXPAND2(EXPAND2(EXPAND2(__VA_ARGS__))))
@@ -13,4 +14,4 @@
1314
#define PARENS ()
1415
#define FOR_EACH(macro, ...) __VA_OPT__(EXPAND(FOR_EACH_HELPER(macro, __VA_ARGS__)))
1516
#define FOR_EACH_HELPER(macro, a1, ...) macro(a1) __VA_OPT__(FOR_EACH_AGAIN PARENS(macro, __VA_ARGS__))
16-
#define FOR_EACH_AGAIN() FOR_EACH_HELPER
17+
#define FOR_EACH_AGAIN() FOR_EACH_HELPER

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

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

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "relations/execution.hpp"
1616
#include "relations/ff_gt.hpp"
1717
#include "relations/instr_fetching.hpp"
18+
#include "relations/keccakf1600.hpp"
1819
#include "relations/memory.hpp"
1920
#include "relations/merkle_check.hpp"
2021
#include "relations/nullifier_check.hpp"
@@ -50,10 +51,10 @@ namespace bb::avm2 {
5051

5152
struct AvmFlavorVariables {
5253
static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 45;
53-
static constexpr size_t NUM_WITNESS_ENTITIES = 1026;
54+
static constexpr size_t NUM_WITNESS_ENTITIES = 2026;
5455
static constexpr size_t NUM_SHIFTED_ENTITIES = 135;
5556
static constexpr size_t NUM_WIRES = NUM_WITNESS_ENTITIES + NUM_PRECOMPUTED_ENTITIES;
56-
static constexpr size_t NUM_ALL_ENTITIES = 1206;
57+
static constexpr size_t NUM_ALL_ENTITIES = 2206;
5758

5859
// Need to be templated for recursive verifier
5960
template <typename FF_>
@@ -72,6 +73,7 @@ struct AvmFlavorVariables {
7273
avm2::execution<FF_>,
7374
avm2::ff_gt<FF_>,
7475
avm2::instr_fetching<FF_>,
76+
avm2::keccakf1600<FF_>,
7577
avm2::memory<FF_>,
7678
avm2::merkle_check<FF_>,
7779
avm2::nullifier_check<FF_>,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 keccakf1600Impl {
12+
public:
13+
using FF = FF_;
14+
15+
static constexpr std::array<size_t, 2> SUBRELATION_PARTIAL_LENGTHS = { 2, 2 };
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.keccakf1600_keccak_0_ - new_term.keccakf1600_keccak_0_);
27+
tmp *= scaling_factor;
28+
std::get<0>(evals) += typename Accumulator::View(tmp);
29+
}
30+
{
31+
using Accumulator = typename std::tuple_element_t<1, ContainerOverSubrelations>;
32+
auto tmp = (new_term.keccakf1600_keccak_1_ - new_term.keccakf1600_keccak_1_);
33+
tmp *= scaling_factor;
34+
std::get<1>(evals) += typename Accumulator::View(tmp);
35+
}
36+
}
37+
};
38+
39+
template <typename FF> class keccakf1600 : public Relation<keccakf1600Impl<FF>> {
40+
public:
41+
static constexpr const std::string_view NAME = "keccakf1600";
42+
43+
static std::string get_subrelation_label(size_t index)
44+
{
45+
switch (index) {}
46+
return std::to_string(index);
47+
}
48+
};
49+
50+
} // namespace bb::avm2

bb-pilcom/bb-pil-backend/src/vm_builder.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use crate::lookup_builder::{
66
use crate::permutation_builder::{get_inverses_from_permutations, Permutation, PermutationBuilder};
77
use crate::relation_builder::{get_shifted_polys, RelationBuilder};
88
use crate::utils::{flatten, sanitize_name, snake_case, sort_cols};
9+
use powdr_ast::analyzed::{Analyzed, Symbol};
910

1011
use dialoguer::Confirm;
1112
use itertools::Itertools;
12-
use powdr_ast::analyzed::Analyzed;
1313
use powdr_number::FieldElement;
1414

1515
/// All of the combinations of columns that are used in a bberg flavor file
@@ -127,27 +127,38 @@ fn get_all_col_names<F: FieldElement>(
127127
permutations: &[Permutation],
128128
lookups: &[Lookup],
129129
) -> ColumnGroups {
130+
// lambda to expand a symbol with length to symbol_0_, symbol_1_, ...
131+
// this makes it match the naming used when indexing into arrays
132+
let expand_symbol = |sym: &Symbol| {
133+
if sym.length.is_some() {
134+
(0..sym.length.unwrap())
135+
.map(move |i| format!("{}_{}_", sym.absolute_name, i))
136+
.collect_vec()
137+
} else {
138+
vec![sym.absolute_name.clone()]
139+
}
140+
};
130141
let constant = sort_cols(
131142
&analyzed
132143
.constant_polys_in_source_order()
133144
.iter()
134-
.map(|(sym, _)| sym.absolute_name.clone())
135-
.map(|n| sanitize_name(&n))
145+
.flat_map(|(sym, _)| expand_symbol(sym))
146+
.map(|name| sanitize_name(name.as_str()))
136147
.collect_vec(),
137148
);
138149
let committed = sort_cols(
139150
&analyzed
140151
.committed_polys_in_source_order()
141152
.iter()
142-
.map(|(sym, _)| sym.absolute_name.clone())
143-
.map(|n| sanitize_name(&n))
153+
.flat_map(|(sym, _)| expand_symbol(sym))
154+
.map(|name| sanitize_name(name.as_str()))
144155
.collect_vec(),
145156
);
146157
let public = analyzed
147158
.public_polys_in_source_order()
148159
.iter()
149-
.map(|(sym, _)| sym.absolute_name.clone())
150-
.map(|n| sanitize_name(&n))
160+
.flat_map(|(sym, _)| expand_symbol(sym))
161+
.map(|name| sanitize_name(name.as_str()))
151162
.collect_vec();
152163
let to_be_shifted = sort_cols(
153164
&get_shifted_polys(
@@ -158,12 +169,12 @@ fn get_all_col_names<F: FieldElement>(
158169
.collect_vec(),
159170
)
160171
.iter()
161-
.map(|n| sanitize_name(&n))
172+
.map(|name| sanitize_name(name.as_str()))
162173
.collect_vec(),
163174
);
164175
let shifted = to_be_shifted
165176
.iter()
166-
.map(|n| format!("{}_shift", n))
177+
.map(|name| format!("{}_shift", name))
167178
.collect_vec();
168179

169180
let inverses = flatten(&[

0 commit comments

Comments
 (0)