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
17 TraceContainer& trace)
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 { C::ff_gt_a_lo, a_limbs.lo },
50 { C::ff_gt_a_hi, a_limbs.hi },
51 { C::ff_gt_p_a_borrow, p_sub_a_witness.borrow },
52 { C::ff_gt_p_sub_a_lo, p_sub_a_witness.lo },
53 { C::ff_gt_p_sub_a_hi, p_sub_a_witness.hi },
54 { C::ff_gt_b_lo, b_limbs.lo },
55 { C::ff_gt_b_hi, b_limbs.hi },
56 { C::ff_gt_p_b_borrow, p_sub_b_witness.borrow },
57 { C::ff_gt_p_sub_b_lo, p_sub_b_witness.lo },
58 { C::ff_gt_p_sub_b_hi, p_sub_b_witness.hi },
59 { C::ff_gt_borrow, res_witness.borrow },
60 { C::ff_gt_res_lo, res_witness.lo },
61 { C::ff_gt_res_hi, res_witness.hi },
62 { C::ff_gt_cmp_rng_ctr, cmp_rng_ctr },
63 { C::ff_gt_sel_shift_rng, cmp_rng_ctr > 0 },
64 { C::ff_gt_cmp_rng_ctr_inv, cmp_rng_ctr_inv } } });
65 };
66
67 while (cmp_rng_ctr >= 0) {
68 write_row();
69 row++;
70
71 sel_gt = false;
72 sel_dec = false;
73
74 // shift the limbs to be range checked
75 a_limbs.lo = p_sub_a_witness.lo;
76 a_limbs.hi = p_sub_a_witness.hi;
77 p_sub_a_witness.lo = b_limbs.lo;
78 p_sub_a_witness.hi = b_limbs.hi;
79 b_limbs.lo = p_sub_b_witness.lo;
80 b_limbs.hi = p_sub_b_witness.hi;
81 p_sub_b_witness.lo = res_witness.lo;
82 p_sub_b_witness.hi = res_witness.hi;
83 res_witness.lo = 0;
84 res_witness.hi = 0;
85
86 cmp_rng_ctr--;
87 }
88 }
89}
90
94 .add<lookup_ff_gt_a_hi_range_settings, InteractionType::LookupGeneric>();
95
96} // namespace bb::avm2::tracegen
void process(const simulation::EventEmitterInterface< simulation::FieldGreaterThanEvent >::Container &events, TraceContainer &trace)
static const InteractionDefinition interactions
InteractionDefinition & add(auto &&... args)
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