Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
field_gt_trace.cpp
Go to the documentation of this file.
2
3#include <memory>
4
9
10namespace bb::avm2::tracegen {
11
12using simulation::LimbsComparisonWitness;
13using simulation::U256Decomposition;
14
18{
19 using C = Column;
20
21 // We precompute the inverses up to 4.
22 std::array<FF, 5> precomputed_inverses = { 0, 1, 2, 3, 4 };
23 FF::batch_invert(precomputed_inverses);
24
25 uint32_t row = 1;
26 for (const auto& event : events) {
27 // Copy the things that will need range checks since we'll mutate them in the shifts
28 U256Decomposition a_limbs = event.a_limbs;
29 LimbsComparisonWitness p_sub_a_witness = event.p_sub_a_witness;
30 U256Decomposition b_limbs = event.b_limbs;
31 LimbsComparisonWitness p_sub_b_witness = event.p_sub_b_witness;
32 LimbsComparisonWitness res_witness = event.res_witness;
33
34 bool sel_gt = event.operation == simulation::FieldGreaterOperation::GREATER_THAN;
35 bool sel_dec = event.operation == simulation::FieldGreaterOperation::CANONICAL_DECOMPOSITION;
36
37 int8_t cmp_rng_ctr = event.operation == simulation::FieldGreaterOperation::GREATER_THAN ? 4 : 1;
38
39 auto write_row = [&]() {
40 FF cmp_rng_ctr_inv = precomputed_inverses.at(static_cast<size_t>(cmp_rng_ctr));
41 trace.set(row,
42 { { { C::ff_gt_sel, 1 },
43 { C::ff_gt_a, event.a },
44 { C::ff_gt_b, event.b },
45 { C::ff_gt_result, event.gt_result },
46 { C::ff_gt_sel_dec, sel_dec },
47 { C::ff_gt_sel_gt, sel_gt },
48 { C::ff_gt_constant_128, 128 },
49 // No conversion available from uint128_t to FF. Yikes.
50 { C::ff_gt_a_lo, uint256_t::from_uint128(a_limbs.lo) },
51 { C::ff_gt_a_hi, uint256_t::from_uint128(a_limbs.hi) },
52 { C::ff_gt_p_a_borrow, p_sub_a_witness.borrow },
53 { C::ff_gt_p_sub_a_lo, uint256_t::from_uint128(p_sub_a_witness.lo) },
54 { C::ff_gt_p_sub_a_hi, uint256_t::from_uint128(p_sub_a_witness.hi) },
55 { C::ff_gt_b_lo, uint256_t::from_uint128(b_limbs.lo) },
56 { C::ff_gt_b_hi, uint256_t::from_uint128(b_limbs.hi) },
57 { C::ff_gt_p_b_borrow, p_sub_b_witness.borrow },
58 { C::ff_gt_p_sub_b_lo, uint256_t::from_uint128(p_sub_b_witness.lo) },
59 { C::ff_gt_p_sub_b_hi, uint256_t::from_uint128(p_sub_b_witness.hi) },
60 { C::ff_gt_borrow, res_witness.borrow },
61 { C::ff_gt_res_lo, uint256_t::from_uint128(res_witness.lo) },
62 { C::ff_gt_res_hi, uint256_t::from_uint128(res_witness.hi) },
63 { C::ff_gt_cmp_rng_ctr, cmp_rng_ctr },
64 { C::ff_gt_sel_shift_rng, cmp_rng_ctr > 0 },
65 { C::ff_gt_cmp_rng_ctr_inv, cmp_rng_ctr_inv } } });
66 };
67
68 while (cmp_rng_ctr >= 0) {
69 write_row();
70 row++;
71
72 sel_gt = false;
73 sel_dec = false;
74
75 // shift the limbs to be range checked
76 a_limbs.lo = p_sub_a_witness.lo;
77 a_limbs.hi = p_sub_a_witness.hi;
78 p_sub_a_witness.lo = b_limbs.lo;
79 p_sub_a_witness.hi = b_limbs.hi;
80 b_limbs.lo = p_sub_b_witness.lo;
81 b_limbs.hi = p_sub_b_witness.hi;
82 p_sub_b_witness.lo = res_witness.lo;
83 p_sub_b_witness.hi = res_witness.hi;
84 res_witness.lo = 0;
85 res_witness.hi = 0;
86
87 cmp_rng_ctr--;
88 }
89 }
90}
91
95 .add<lookup_ff_gt_a_hi_range_settings, InteractionType::LookupGeneric>();
96
97} // namespace bb::avm2::tracegen
void process(const simulation::EventEmitterInterface< simulation::FieldGreaterThanEvent >::Container &events, TraceContainer &trace)
static const InteractionDefinition interactions
InteractionDefinition & add(auto &&... args)
static constexpr uint256_t from_uint128(const uint128_t a) noexcept
Definition uint256.hpp:94
TestTraceContainer trace
lookup_settings< lookup_ff_gt_a_lo_range_settings_ > lookup_ff_gt_a_lo_range_settings
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
simulation::PublicDataTreeReadWriteEvent event
static void batch_invert(C &coeffs) noexcept