Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(audit): lt #338

Merged
merged 8 commits into from
Feb 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 62 additions & 9 deletions plonky2x/core/src/frontend/ops/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,27 +171,23 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
/// The less than operation (<).
pub fn lt<Lhs, Rhs>(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable
where
Lhs: LessThanOrEqual<L, D, Lhs>,
Rhs: Sub<L, D, Rhs, Output = Lhs> + One<L, D>,
Rhs: LessThanOrEqual<L, D, Lhs>,
{
let one = self.one::<Rhs>();
let upper_bound = rhs.sub(one, self);
self.lte(lhs, upper_bound)
let lte = rhs.lte(lhs, self);
self.not(lte)
}

/// The greater than operation (>).
pub fn gt<Lhs, Rhs>(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable
where
Lhs: Sub<L, D, Lhs, Output = Rhs> + One<L, D>,
Rhs: LessThanOrEqual<L, D, Rhs>,
Lhs: LessThanOrEqual<L, D, Rhs>,
{
self.lt(rhs, lhs)
}

/// The greater than or equal to operation (>=).
pub fn gte<Lhs, Rhs>(&mut self, lhs: Lhs, rhs: Rhs) -> BoolVariable
where
Lhs: Sub<L, D, Lhs, Output = Rhs> + One<L, D>,
Rhs: LessThanOrEqual<L, D, Lhs>,
{
self.lte(rhs, lhs)
Expand Down Expand Up @@ -221,11 +217,68 @@ impl<L: PlonkParameters<D>, const D: usize> LessThanOrEqual<L, D> for Variable {
generator.output
}
}

mod tests {
#[allow(unused_imports)]
use crate::prelude::{BoolVariable, DefaultBuilder, U32Variable};

#[test]
fn test_math_lt() {
let mut builder = DefaultBuilder::new();

let v0 = builder.read::<U32Variable>();
let v1 = builder.read::<U32Variable>();
let result = builder.read::<BoolVariable>();
let computed_result = builder.lt(v0, v1);
builder.assert_is_equal(result, computed_result);

let circuit = builder.build();

let test_cases = [
(5u32, 0u32, false),
(0u32, 10u32, true),
(10u32, 5u32, false),
];

for test_case in test_cases.iter() {
let mut input = circuit.input();
input.write::<U32Variable>(test_case.0);
input.write::<U32Variable>(test_case.1);
input.write::<BoolVariable>(test_case.2);

let (proof, output) = circuit.prove(&input);
circuit.verify(&proof, &input, &output);
}
}

#[test]
fn test_math_lte() {
let mut builder = DefaultBuilder::new();

let v0 = builder.read::<U32Variable>();
let v1 = builder.read::<U32Variable>();
let result = builder.read::<BoolVariable>();
let computed_result = builder.lte(v0, v1);
builder.assert_is_equal(result, computed_result);

let circuit = builder.build();

let test_cases = [
(0u32, 0u32, true),
(0u32, 100u32, true),
(10u32, 0u32, false),
];

for test_case in test_cases.iter() {
let mut input = circuit.input();
input.write::<U32Variable>(test_case.0);
input.write::<U32Variable>(test_case.1);
input.write::<BoolVariable>(test_case.2);

let (proof, output) = circuit.prove(&input);
circuit.verify(&proof, &input, &output);
}
}

#[test]
fn test_math_gt() {
let mut builder = DefaultBuilder::new();
Expand Down
Loading