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

Gadgets for comparing to constant #117

Merged
merged 3 commits into from
Aug 31, 2022
Merged
Changes from 1 commit
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
193 changes: 192 additions & 1 deletion relation/src/gadgets/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,93 @@ impl<F: PrimeField> PlonkCircuit<F> {
let c = self.is_lt_internal(a, b)?;
self.logic_neg(c)
}

/// Returns a `BoolVar` indicating whether the variable `a` is less than a
/// given constant `val`.
pub fn is_lt_constant(&mut self, a: Variable, val: F) -> Result<BoolVar, CircuitError>
where
F: PrimeField,
{
self.check_var_bound(a)?;
let b = self.create_constant_variable(val)?;
self.is_lt(a, b)
}

/// Returns a `BoolVar` indicating whether the variable `a` is less than or
/// equal to a given constant `val`.
pub fn is_leq_constant(&mut self, a: Variable, val: F) -> Result<BoolVar, CircuitError>
where
F: PrimeField,
{
self.check_var_bound(a)?;
let b = self.create_constant_variable(val)?;
self.is_leq(a, b)
}

/// Returns a `BoolVar` indicating whether the variable `a` is greater than
/// a given constant `val`.
pub fn is_gt_constant(&mut self, a: Variable, val: F) -> Result<BoolVar, CircuitError>
where
F: PrimeField,
{
self.check_var_bound(a)?;
self.is_gt_constant_internal(a, &val)
}

/// Returns a `BoolVar` indicating whether the variable `a` is greater than
/// or equal a given constant `val`.
pub fn is_geq_constant(&mut self, a: Variable, val: F) -> Result<BoolVar, CircuitError>
where
F: PrimeField,
{
self.check_var_bound(a)?;
let b = self.create_constant_variable(val)?;
self.is_geq(a, b)
}

/// Enforce the variable `a` to be less than a
/// given constant `val`.
pub fn enforce_lt_constant(&mut self, a: Variable, val: F) -> Result<(), CircuitError>
where
F: PrimeField,
{
self.check_var_bound(a)?;
let b = self.create_constant_variable(val)?;
self.enforce_lt(a, b)
}

/// Enforce the variable `a` to be less than or
/// equal to a given constant `val`.
pub fn enforce_leq_constant(&mut self, a: Variable, val: F) -> Result<(), CircuitError>
where
F: PrimeField,
{
self.check_var_bound(a)?;
let b = self.create_constant_variable(val)?;
self.enforce_leq(a, b)
}

/// Enforce the variable `a` to be greater than
/// a given constant `val`.
pub fn enforce_gt_constant(&mut self, a: Variable, val: F) -> Result<(), CircuitError>
where
F: PrimeField,
{
self.check_var_bound(a)?;
let b = self.create_constant_variable(val)?;
self.enforce_gt(a, b)
}

/// Enforce the variable `a` to be greater than
/// or equal a given constant `val`.
pub fn enforce_geq_constant(&mut self, a: Variable, val: F) -> Result<(), CircuitError>
where
F: PrimeField,
{
self.check_var_bound(a)?;
let b = self.create_constant_variable(val)?;
self.enforce_geq(a, b)
}
}

/// Private helper functions for comparison gate
Expand Down Expand Up @@ -216,7 +303,23 @@ mod test {
test_enforce_le(b, a)?;
test_enforce_leq(b, a)?;
test_enforce_ge(b, a)?;
test_enforce_geq(b, a)
test_enforce_geq(b, a)?;
test_is_le_constant(a, b)?;
test_is_leq_constant(a, b)?;
test_is_ge_constant(a, b)?;
test_is_geq_constant(a, b)?;
test_enforce_le_constant(a, b)?;
test_enforce_leq_constant(a, b)?;
test_enforce_ge_constant(a, b)?;
test_enforce_geq_constant(a, b)?;
test_is_le_constant(b, a)?;
test_is_leq_constant(b, a)?;
test_is_ge_constant(b, a)?;
test_is_geq_constant(b, a)?;
test_enforce_le_constant(b, a)?;
test_enforce_leq_constant(b, a)?;
test_enforce_ge_constant(b, a)?;
test_enforce_geq_constant(b, a)
})
}

Expand Down Expand Up @@ -316,4 +419,92 @@ mod test {
}
Ok(())
}
fn test_is_le_constant<F: PrimeField>(a: &F, b: &F) -> Result<(), CircuitError> {
let mut circuit = PlonkCircuit::<F>::new_turbo_plonk();
let expected_result = if a < b { F::one() } else { F::zero() };
let a = circuit.create_variable(*a)?;

let c = circuit.is_lt_constant(a, *b)?;
assert!(circuit.witness(c.into())?.eq(&expected_result));
assert!(circuit.check_circuit_satisfiability(&[]).is_ok());
Ok(())
}
fn test_is_leq_constant<F: PrimeField>(a: &F, b: &F) -> Result<(), CircuitError> {
mrain marked this conversation as resolved.
Show resolved Hide resolved
let mut circuit = PlonkCircuit::<F>::new_turbo_plonk();
let expected_result = if a <= b { F::one() } else { F::zero() };
let a = circuit.create_variable(*a)?;

let c = circuit.is_leq_constant(a, *b)?;
assert!(circuit.witness(c.into())?.eq(&expected_result));
assert!(circuit.check_circuit_satisfiability(&[]).is_ok());
Ok(())
}
fn test_is_ge_constant<F: PrimeField>(a: &F, b: &F) -> Result<(), CircuitError> {
let mut circuit = PlonkCircuit::<F>::new_turbo_plonk();
let expected_result = if a > b { F::one() } else { F::zero() };
let a = circuit.create_variable(*a)?;

let c = circuit.is_gt_constant(a, *b)?;
assert!(circuit.witness(c.into())?.eq(&expected_result));
assert!(circuit.check_circuit_satisfiability(&[]).is_ok());
Ok(())
}
fn test_is_geq_constant<F: PrimeField>(a: &F, b: &F) -> Result<(), CircuitError> {
let mut circuit = PlonkCircuit::<F>::new_turbo_plonk();
let expected_result = if a >= b { F::one() } else { F::zero() };
let a = circuit.create_variable(*a)?;

let c = circuit.is_geq_constant(a, *b)?;
assert!(circuit.witness(c.into())?.eq(&expected_result));
assert!(circuit.check_circuit_satisfiability(&[]).is_ok());
Ok(())
}
fn test_enforce_le_constant<F: PrimeField>(a: &F, b: &F) -> Result<(), CircuitError> {
let mut circuit = PlonkCircuit::<F>::new_turbo_plonk();
let expected_result = a < b;
let a = circuit.create_variable(*a)?;
circuit.enforce_lt_constant(a, *b)?;
if expected_result {
assert!(circuit.check_circuit_satisfiability(&[]).is_ok())
} else {
assert!(circuit.check_circuit_satisfiability(&[]).is_err());
}
Ok(())
}
fn test_enforce_leq_constant<F: PrimeField>(a: &F, b: &F) -> Result<(), CircuitError> {
let mut circuit = PlonkCircuit::<F>::new_turbo_plonk();
let expected_result = a <= b;
let a = circuit.create_variable(*a)?;
circuit.enforce_leq_constant(a, *b)?;
if expected_result {
assert!(circuit.check_circuit_satisfiability(&[]).is_ok())
} else {
assert!(circuit.check_circuit_satisfiability(&[]).is_err());
}
Ok(())
}
fn test_enforce_ge_constant<F: PrimeField>(a: &F, b: &F) -> Result<(), CircuitError> {
let mut circuit = PlonkCircuit::<F>::new_turbo_plonk();
let expected_result = a > b;
let a = circuit.create_variable(*a)?;
circuit.enforce_gt_constant(a, *b)?;
if expected_result {
assert!(circuit.check_circuit_satisfiability(&[]).is_ok())
} else {
assert!(circuit.check_circuit_satisfiability(&[]).is_err());
}
Ok(())
}
fn test_enforce_geq_constant<F: PrimeField>(a: &F, b: &F) -> Result<(), CircuitError> {
let mut circuit = PlonkCircuit::<F>::new_turbo_plonk();
let expected_result = a >= b;
let a = circuit.create_variable(*a)?;
circuit.enforce_geq_constant(a, *b)?;
if expected_result {
assert!(circuit.check_circuit_satisfiability(&[]).is_ok())
} else {
assert!(circuit.check_circuit_satisfiability(&[]).is_err());
}
Ok(())
}
}