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 uint32_t row = 1;
22 for (const auto& event : events) {
23 // Copy the things that will need range checks since we'll mutate them in the shifts
24 U256Decomposition a_limbs = event.a_limbs;
25 LimbsComparisonWitness p_sub_a_witness = event.p_sub_a_witness;
26 U256Decomposition b_limbs = event.b_limbs;
27 LimbsComparisonWitness p_sub_b_witness = event.p_sub_b_witness;
28 LimbsComparisonWitness res_witness = event.res_witness;
29
30 bool sel_gt = event.operation == simulation::FieldGreaterOperation::GREATER_THAN;
31 bool sel_dec = event.operation == simulation::FieldGreaterOperation::CANONICAL_DECOMPOSITION;
32
33 int8_t cmp_rng_ctr = event.operation == simulation::FieldGreaterOperation::GREATER_THAN ? 4 : 1;
34
35 auto write_row = [&]() {
36 FF cmp_rng_ctr_inv = cmp_rng_ctr > 0 ? FF(cmp_rng_ctr).invert() : FF::zero();
37 trace.set(row,
38 { { { C::ff_gt_sel, 1 },
39 { C::ff_gt_a, event.a },
40 { C::ff_gt_b, event.b },
41 { C::ff_gt_result, event.gt_result },
42 { C::ff_gt_sel_dec, sel_dec },
43 { C::ff_gt_sel_gt, sel_gt },
44 { C::ff_gt_constant_128, 128 },
45 // No conversion available from uint128_t to FF. Yikes.
46 { C::ff_gt_a_lo, uint256_t::from_uint128(a_limbs.lo) },
47 { C::ff_gt_a_hi, uint256_t::from_uint128(a_limbs.hi) },
48 { C::ff_gt_p_a_borrow, p_sub_a_witness.borrow },
49 { C::ff_gt_p_sub_a_lo, uint256_t::from_uint128(p_sub_a_witness.lo) },
50 { C::ff_gt_p_sub_a_hi, uint256_t::from_uint128(p_sub_a_witness.hi) },
51 { C::ff_gt_b_lo, uint256_t::from_uint128(b_limbs.lo) },
52 { C::ff_gt_b_hi, uint256_t::from_uint128(b_limbs.hi) },
53 { C::ff_gt_p_b_borrow, p_sub_b_witness.borrow },
54 { C::ff_gt_p_sub_b_lo, uint256_t::from_uint128(p_sub_b_witness.lo) },
55 { C::ff_gt_p_sub_b_hi, uint256_t::from_uint128(p_sub_b_witness.hi) },
56 { C::ff_gt_borrow, res_witness.borrow },
57 { C::ff_gt_res_lo, uint256_t::from_uint128(res_witness.lo) },
58 { C::ff_gt_res_hi, uint256_t::from_uint128(res_witness.hi) },
59 { C::ff_gt_cmp_rng_ctr, cmp_rng_ctr },
60 { C::ff_gt_sel_shift_rng, cmp_rng_ctr > 0 },
61 { C::ff_gt_cmp_rng_ctr_inv, cmp_rng_ctr_inv } } });
62 };
63
64 while (cmp_rng_ctr >= 0) {
65 write_row();
66 row++;
67
68 sel_gt = false;
69 sel_dec = false;
70
71 // shift the limbs to be range checked
72 a_limbs.lo = p_sub_a_witness.lo;
73 a_limbs.hi = p_sub_a_witness.hi;
74 p_sub_a_witness.lo = b_limbs.lo;
75 p_sub_a_witness.hi = b_limbs.hi;
76 b_limbs.lo = p_sub_b_witness.lo;
77 b_limbs.hi = p_sub_b_witness.hi;
78 p_sub_b_witness.lo = res_witness.lo;
79 p_sub_b_witness.hi = res_witness.hi;
80 res_witness.lo = 0;
81 res_witness.hi = 0;
82
83 cmp_rng_ctr--;
84 }
85 }
86}
87
91 .add<lookup_ff_gt_a_hi_range_settings, InteractionType::LookupGeneric>();
92
93} // 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
simulation::PublicDataTreeReadWriteEvent event
static constexpr field zero()