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
7
8namespace bb::avm2::tracegen {
9
10using simulation::LimbsComparisonWitness;
11using simulation::U256Decomposition;
12
46 TraceContainer& trace)
47{
48 using C = Column;
49
50 // We precompute the inverses up to 4.
51 std::array<FF, 5> precomputed_ctr_inverses = { 0, 1, 2, 3, 4 };
52 FF::batch_invert(precomputed_ctr_inverses);
53
54 uint32_t row = 1;
55 for (const auto& event : events) {
56 // Copy the things that will need range checks since we'll mutate them in the shifts
57 U256Decomposition a_limbs = event.a_limbs;
58 LimbsComparisonWitness p_sub_a_witness = event.p_sub_a_witness;
59 U256Decomposition b_limbs = event.b_limbs;
60 LimbsComparisonWitness p_sub_b_witness = event.p_sub_b_witness;
61 LimbsComparisonWitness res_witness = event.res_witness;
62
63 bool sel_gt = event.operation == simulation::FieldGreaterOperation::GREATER_THAN;
64 bool sel_dec = event.operation == simulation::FieldGreaterOperation::CANONICAL_DECOMPOSITION;
65
66 for (int cmp_rng_ctr = event.operation == simulation::FieldGreaterOperation::GREATER_THAN ? 4 : 1;
67 cmp_rng_ctr >= 0;
68 cmp_rng_ctr--) {
69
70 const FF& cmp_rng_ctr_inv = precomputed_ctr_inverses.at(static_cast<size_t>(cmp_rng_ctr));
71 trace.set(row,
72 { { { C::ff_gt_sel, 1 },
73 { C::ff_gt_a, event.a },
74 { C::ff_gt_b, event.b },
75 { C::ff_gt_result, event.gt_result },
76 { C::ff_gt_sel_dec, sel_dec ? 1 : 0 },
77 { C::ff_gt_sel_gt, sel_gt ? 1 : 0 },
78 { C::ff_gt_constant_128, 128 },
79 { C::ff_gt_a_lo, a_limbs.lo },
80 { C::ff_gt_a_hi, a_limbs.hi },
81 { C::ff_gt_p_a_borrow, p_sub_a_witness.borrow ? 1 : 0 },
82 { C::ff_gt_p_sub_a_lo, p_sub_a_witness.lo },
83 { C::ff_gt_p_sub_a_hi, p_sub_a_witness.hi },
84 { C::ff_gt_b_lo, b_limbs.lo },
85 { C::ff_gt_b_hi, b_limbs.hi },
86 { C::ff_gt_p_b_borrow, p_sub_b_witness.borrow ? 1 : 0 },
87 { C::ff_gt_p_sub_b_lo, p_sub_b_witness.lo },
88 { C::ff_gt_p_sub_b_hi, p_sub_b_witness.hi },
89 { C::ff_gt_borrow, res_witness.borrow ? 1 : 0 },
90 { C::ff_gt_res_lo, res_witness.lo },
91 { C::ff_gt_res_hi, res_witness.hi },
92 { C::ff_gt_cmp_rng_ctr, cmp_rng_ctr },
93 { C::ff_gt_sel_shift_rng, cmp_rng_ctr > 0 ? 1 : 0 },
94 { C::ff_gt_cmp_rng_ctr_inv, cmp_rng_ctr_inv } } });
95
96 row++;
97
98 // After the first row, sel_gt and sel_dec are no longer active.
99 // Subsequent rows are shift rows for completing the range checks.
100 sel_gt = false;
101 sel_dec = false;
102
103 // Shift the limbs left for range checking in the next row.
104 // The shifting pattern ensures all limbs are eventually range-checked via a_lo and a_hi columns:
105 // p_sub_a → a_limbs
106 // b_limbs → p_sub_a_witness
107 // p_sub_b_witness → b_limbs
108 // res_witness → p_sub_b_witness
109 // This corresponds to the PIL shift constraints (SHIFT_P_SUB_A_TO_A_LO, etc.)
110 a_limbs.lo = p_sub_a_witness.lo;
111 a_limbs.hi = p_sub_a_witness.hi;
112 p_sub_a_witness.lo = b_limbs.lo;
113 p_sub_a_witness.hi = b_limbs.hi;
114 b_limbs.lo = p_sub_b_witness.lo;
115 b_limbs.hi = p_sub_b_witness.hi;
116 p_sub_b_witness.lo = res_witness.lo;
117 p_sub_b_witness.hi = res_witness.hi;
118 res_witness.lo = 0;
119 res_witness.hi = 0;
120 }
121 }
122}
123
127 .add<lookup_ff_gt_a_hi_range_settings, InteractionType::LookupGeneric>();
128
129} // namespace bb::avm2::tracegen
void process(const simulation::EventEmitterInterface< simulation::FieldGreaterThanEvent >::Container &events, TraceContainer &trace)
Processes FieldGreaterThanEvent events and generates trace rows for the ff_gt gadget.
static const InteractionDefinition interactions
InteractionDefinition & add(auto &&... args)
TestTraceContainer trace
AvmFlavorSettings::FF FF
Definition field.hpp:10
lookup_settings< lookup_ff_gt_a_lo_range_settings_ > lookup_ff_gt_a_lo_range_settings
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
simulation::PublicDataTreeReadWriteEvent event
static void batch_invert(C &coeffs) noexcept
Batch invert a collection of field elements using Montgomery's trick.