Skip to content

Commit 45f9eae

Browse files
committed
feat(avm): initial, underconstrained to_radix avm gadget
1 parent 95c6b4a commit 45f9eae

19 files changed

Lines changed: 848 additions & 157 deletions

barretenberg/cpp/pil/avm/avm_main.pil

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ include "avm_mem.pil";
33
include "avm_alu.pil";
44
include "avm_binary.pil";
55
include "avm_kernel.pil";
6+
include "gadgets/avm_conversion.pil";
67

78
namespace avm_main(256);
89
// Kernel lookup selector opcodes
@@ -25,6 +26,9 @@ namespace avm_main(256);
2526
pol commit sel_op_coinbase;
2627
pol commit sel_op_timestamp;
2728

29+
//===== Gadget Selectors ======================================================
30+
pol commit sel_op_radix_le;
31+
2832
//===== CONSTANT POLYNOMIALS ==================================================
2933
pol constant clk(i) { i };
3034
pol constant first = [1] + [0]*; // Used mostly to toggle off the first row consisting
@@ -167,6 +171,8 @@ namespace avm_main(256);
167171
sel_op_fee_per_da_gas * (1 - sel_op_fee_per_da_gas) = 0;
168172
sel_op_transaction_fee * (1 - sel_op_transaction_fee) = 0;
169173

174+
sel_op_radix_le * (1 - sel_op_radix_le) = 0;
175+
170176
sel_op_add * (1 - sel_op_add) = 0;
171177
sel_op_sub * (1 - sel_op_sub) = 0;
172178
sel_op_mul * (1 - sel_op_mul) = 0;
@@ -451,6 +457,11 @@ namespace avm_main(256);
451457
is
452458
avm_binary.start {avm_binary.clk, avm_binary.acc_ia, avm_binary.acc_ib, avm_binary.acc_ic, avm_binary.op_id, avm_binary.in_tag};
453459

460+
#[PERM_MAIN_CONV]
461+
sel_op_radix_le {clk, ia, ic, id}
462+
is
463+
avm_conversion.to_radix_le_sel {avm_conversion.clk, avm_conversion.input, avm_conversion.radix, avm_conversion.num_limbs};
464+
454465
#[PERM_MAIN_MEM_A]
455466
mem_op_a {clk, mem_idx_a, ia, rwa
456467
, r_in_tag, w_in_tag, sel_mov_a, sel_cmov}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
include "../avm_main.pil";
2+
3+
namespace avm_conversion(256);
4+
5+
pol commit clk;
6+
7+
// Selector for Radix Operation
8+
pol commit to_radix_le_sel;
9+
to_radix_le_sel * (1 - to_radix_le_sel) = 0;
10+
11+
// ===== DRAFT: Planned Constraints for To Radix LE
12+
// Similar to the binary trace; multi-row decomposition of the input using the number of limbs specified as the row count.
13+
// (1) limb_ctr' - limb_ctr + 1 = 0; // Next row decrements the limb_ctr
14+
// (2) Check equality to 0 of limb_ctr to terminate the operations.
15+
// (3) An accumulation column to track the partial re-composition of the limbs
16+
// (4) Range check each row.limb < radix
17+
// (5) TODO: Is there a risk of over/under flow.
18+
19+
pol commit input;
20+
pol commit radix;
21+
pol commit num_limbs;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
#pragma once
3+
#include "../../relation_parameters.hpp"
4+
#include "../../relation_types.hpp"
5+
#include "./declare_views.hpp"
6+
7+
namespace bb::Avm_vm {
8+
9+
template <typename FF> struct Avm_conversionRow {
10+
FF avm_conversion_to_radix_le_sel{};
11+
};
12+
13+
inline std::string get_relation_label_avm_conversion(int index)
14+
{
15+
switch (index) {}
16+
return std::to_string(index);
17+
}
18+
19+
template <typename FF_> class avm_conversionImpl {
20+
public:
21+
using FF = FF_;
22+
23+
static constexpr std::array<size_t, 1> SUBRELATION_PARTIAL_LENGTHS{
24+
3,
25+
};
26+
27+
template <typename ContainerOverSubrelations, typename AllEntities>
28+
void static accumulate(ContainerOverSubrelations& evals,
29+
const AllEntities& new_term,
30+
[[maybe_unused]] const RelationParameters<FF>&,
31+
[[maybe_unused]] const FF& scaling_factor)
32+
{
33+
34+
// Contribution 0
35+
{
36+
Avm_DECLARE_VIEWS(0);
37+
38+
auto tmp = (avm_conversion_to_radix_le_sel * (-avm_conversion_to_radix_le_sel + FF(1)));
39+
tmp *= scaling_factor;
40+
std::get<0>(evals) += tmp;
41+
}
42+
}
43+
};
44+
45+
template <typename FF> using avm_conversion = Relation<avm_conversionImpl<FF>>;
46+
47+
} // namespace bb::Avm_vm

0 commit comments

Comments
 (0)