Skip to content

Commit

Permalink
add unit_test
Browse files Browse the repository at this point in the history
  • Loading branch information
mrain committed Aug 30, 2022
1 parent a3412df commit a207c42
Showing 1 changed file with 105 additions and 1 deletion.
106 changes: 105 additions & 1 deletion relation/src/gadgets/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,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 @@ -403,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> {
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(())
}
}

0 comments on commit a207c42

Please sign in to comment.