Skip to content

Commit 3b8ba82

Browse files
ratankalianictian1
andauthored
fix(audit): lt (#338)
Co-authored-by: Chris T <chris@succinct.xyz>
1 parent e368d5b commit 3b8ba82

File tree

1 file changed

+62
-9
lines changed
  • plonky2x/core/src/frontend/ops

1 file changed

+62
-9
lines changed

plonky2x/core/src/frontend/ops/math.rs

+62-9
Original file line numberDiff line numberDiff line change
@@ -171,27 +171,23 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
171171
/// The less than operation (<).
172172
pub fn lt<Lhs, Rhs>(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable
173173
where
174-
Lhs: LessThanOrEqual<L, D, Lhs>,
175-
Rhs: Sub<L, D, Rhs, Output = Lhs> + One<L, D>,
174+
Rhs: LessThanOrEqual<L, D, Lhs>,
176175
{
177-
let one = self.one::<Rhs>();
178-
let upper_bound = rhs.sub(one, self);
179-
self.lte(lhs, upper_bound)
176+
let lte = rhs.lte(lhs, self);
177+
self.not(lte)
180178
}
181179

182180
/// The greater than operation (>).
183181
pub fn gt<Lhs, Rhs>(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable
184182
where
185-
Lhs: Sub<L, D, Lhs, Output = Rhs> + One<L, D>,
186-
Rhs: LessThanOrEqual<L, D, Rhs>,
183+
Lhs: LessThanOrEqual<L, D, Rhs>,
187184
{
188185
self.lt(rhs, lhs)
189186
}
190187

191188
/// The greater than or equal to operation (>=).
192189
pub fn gte<Lhs, Rhs>(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable
193190
where
194-
Lhs: Sub<L, D, Lhs, Output = Rhs> + One<L, D>,
195191
Rhs: LessThanOrEqual<L, D, Lhs>,
196192
{
197193
self.lte(rhs, lhs)
@@ -221,11 +217,68 @@ impl<L: PlonkParameters<D>, const D: usize> LessThanOrEqual<L, D> for Variable {
221217
generator.output
222218
}
223219
}
224-
225220
mod tests {
226221
#[allow(unused_imports)]
227222
use crate::prelude::{BoolVariable, DefaultBuilder, U32Variable};
228223

224+
#[test]
225+
fn test_math_lt() {
226+
let mut builder = DefaultBuilder::new();
227+
228+
let v0 = builder.read::<U32Variable>();
229+
let v1 = builder.read::<U32Variable>();
230+
let result = builder.read::<BoolVariable>();
231+
let computed_result = builder.lt(v0, v1);
232+
builder.assert_is_equal(result, computed_result);
233+
234+
let circuit = builder.build();
235+
236+
let test_cases = [
237+
(5u32, 0u32, false),
238+
(0u32, 10u32, true),
239+
(10u32, 5u32, false),
240+
];
241+
242+
for test_case in test_cases.iter() {
243+
let mut input = circuit.input();
244+
input.write::<U32Variable>(test_case.0);
245+
input.write::<U32Variable>(test_case.1);
246+
input.write::<BoolVariable>(test_case.2);
247+
248+
let (proof, output) = circuit.prove(&input);
249+
circuit.verify(&proof, &input, &output);
250+
}
251+
}
252+
253+
#[test]
254+
fn test_math_lte() {
255+
let mut builder = DefaultBuilder::new();
256+
257+
let v0 = builder.read::<U32Variable>();
258+
let v1 = builder.read::<U32Variable>();
259+
let result = builder.read::<BoolVariable>();
260+
let computed_result = builder.lte(v0, v1);
261+
builder.assert_is_equal(result, computed_result);
262+
263+
let circuit = builder.build();
264+
265+
let test_cases = [
266+
(0u32, 0u32, true),
267+
(0u32, 100u32, true),
268+
(10u32, 0u32, false),
269+
];
270+
271+
for test_case in test_cases.iter() {
272+
let mut input = circuit.input();
273+
input.write::<U32Variable>(test_case.0);
274+
input.write::<U32Variable>(test_case.1);
275+
input.write::<BoolVariable>(test_case.2);
276+
277+
let (proof, output) = circuit.prove(&input);
278+
circuit.verify(&proof, &input, &output);
279+
}
280+
}
281+
229282
#[test]
230283
fn test_math_gt() {
231284
let mut builder = DefaultBuilder::new();

0 commit comments

Comments
 (0)