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 uint32_t row = 1;
51 for (const auto& event : events) {
52 // Copy the things that will need range checks since we'll mutate them in the shifts
53 U256Decomposition a_limbs = event.a_limbs;
54 LimbsComparisonWitness p_sub_a_witness = event.p_sub_a_witness;
55 U256Decomposition b_limbs = event.b_limbs;
56 LimbsComparisonWitness p_sub_b_witness = event.p_sub_b_witness;
57 LimbsComparisonWitness res_witness = event.res_witness;
58
59 bool sel_gt = event.operation == simulation::FieldGreaterOperation::GREATER_THAN;
60 bool sel_dec = event.operation == simulation::FieldGreaterOperation::CANONICAL_DECOMPOSITION;
61
62 int init_ctr = sel_gt ? 4 : 1;
63
64 for (int cmp_rng_ctr = init_ctr; cmp_rng_ctr >= 0; cmp_rng_ctr--) {
65
66 bool is_end = (cmp_rng_ctr == 0);
67 trace.set(row,
68 { { { C::ff_gt_sel, 1 },
69 { C::ff_gt_a, event.a },
70 { C::ff_gt_b, event.b },
71 { C::ff_gt_result, event.gt_result },
72 { C::ff_gt_sel_dec, sel_dec ? 1 : 0 },
73 { C::ff_gt_sel_gt, sel_gt ? 1 : 0 },
74 { C::ff_gt_end, is_end ? 1 : 0 },
75 { C::ff_gt_constant_128, 128 },
76 { C::ff_gt_a_lo, a_limbs.lo },
77 { C::ff_gt_a_hi, a_limbs.hi },
78 { C::ff_gt_p_a_borrow, p_sub_a_witness.borrow ? 1 : 0 },
79 { C::ff_gt_p_sub_a_lo, p_sub_a_witness.lo },
80 { C::ff_gt_p_sub_a_hi, p_sub_a_witness.hi },
81 { C::ff_gt_b_lo, b_limbs.lo },
82 { C::ff_gt_b_hi, b_limbs.hi },
83 { C::ff_gt_p_b_borrow, p_sub_b_witness.borrow ? 1 : 0 },
84 { C::ff_gt_p_sub_b_lo, p_sub_b_witness.lo },
85 { C::ff_gt_p_sub_b_hi, p_sub_b_witness.hi },
86 { C::ff_gt_borrow, res_witness.borrow ? 1 : 0 },
87 { C::ff_gt_res_lo, res_witness.lo },
88 { C::ff_gt_res_hi, res_witness.hi },
89 { C::ff_gt_cmp_rng_ctr, cmp_rng_ctr } } });
90
91 row++;
92
93 // After the first row, sel_gt and sel_dec are no longer active.
94 // Subsequent rows are shift rows for completing the range checks.
95 sel_gt = false;
96 sel_dec = false;
97
98 // Shift the limbs left for range checking in the next row.
99 // The shifting pattern ensures all limbs are eventually range-checked via a_lo and a_hi columns:
100 // p_sub_a → a_limbs
101 // b_limbs → p_sub_a_witness
102 // p_sub_b_witness → b_limbs
103 // res_witness → p_sub_b_witness
104 // This corresponds to the PIL shift constraints (SHIFT_P_SUB_A_TO_A_LO, etc.)
105 a_limbs.lo = p_sub_a_witness.lo;
106 a_limbs.hi = p_sub_a_witness.hi;
107 p_sub_a_witness.lo = b_limbs.lo;
108 p_sub_a_witness.hi = b_limbs.hi;
109 b_limbs.lo = p_sub_b_witness.lo;
110 b_limbs.hi = p_sub_b_witness.hi;
111 p_sub_b_witness.lo = res_witness.lo;
112 p_sub_b_witness.hi = res_witness.hi;
113 res_witness.lo = 0;
114 res_witness.hi = 0;
115 }
116 }
117}
118
122 .add<InteractionType::LookupGeneric, lookup_ff_gt_a_hi_range_settings>();
123
124} // 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
simulation::PublicDataTreeReadWriteEvent event
Settings to be passed ot GenericLookupRelationImpl.