Refactor NIZK/MT verification to return a bit

This commit is contained in:
Pratyush Mishra
2020-08-29 13:04:13 -07:00
parent 6cca9327be
commit d2d1b61d08
6 changed files with 46 additions and 111 deletions

View File

@@ -7,6 +7,7 @@ use r1cs_core::{lc, ConstraintSystemRef, LinearCombination, Namespace, Synthesis
/// Represents a variable in the constraint system which is guaranteed
/// to be either zero or one.
#[derive(Clone, Debug, Eq, PartialEq)]
#[must_use]
pub struct AllocatedBit<F: Field> {
variable: Variable,
cs: ConstraintSystemRef<F>,
@@ -216,6 +217,7 @@ impl<F: Field> CondSelectGadget<F> for AllocatedBit<F> {
/// This is a boolean value which may be either a constant or
/// an interpretation of an `AllocatedBit`.
#[derive(Clone, Debug, Eq, PartialEq)]
#[must_use]
pub enum Boolean<F: Field> {
/// Existential view of the boolean variable
Is(AllocatedBit<F>),
@@ -245,6 +247,12 @@ impl<F: Field> R1CSVar<F> for Boolean<F> {
}
impl<F: Field> Boolean<F> {
/// Returns the constrant `true`.
pub const TRUE: Self = Boolean::Constant(true);
/// Returns the constrant `false`.
pub const FALSE: Self = Boolean::Constant(false);
pub fn lc(&self) -> LinearCombination<F> {
match self {
Boolean::Constant(false) => lc!(),

View File

@@ -87,45 +87,3 @@ impl<T: EqGadget<F> + R1CSVar<F>, F: Field> EqGadget<F> for [T] {
}
}
}
pub trait OrEqualsGadget<ConstraintF: Field>
where
Self: Sized,
{
/// If `should_enforce == true`, enforce that `self` equals
/// (a) `first` (if `cond` is `true`)
/// (b) `second` (if `cond` is `false`)
fn conditional_enforce_equal_or(
&self,
cond: &Boolean<ConstraintF>,
first: &Self,
second: &Self,
should_enforce: &Boolean<ConstraintF>,
) -> Result<(), SynthesisError>;
fn enforce_equal_or(
&self,
cond: &Boolean<ConstraintF>,
first: &Self,
second: &Self,
) -> Result<(), SynthesisError> {
self.conditional_enforce_equal_or(cond, first, second, &Boolean::Constant(true))
}
}
impl<ConstraintF, T> OrEqualsGadget<ConstraintF> for T
where
ConstraintF: Field,
T: Sized + EqGadget<ConstraintF> + CondSelectGadget<ConstraintF>,
{
fn conditional_enforce_equal_or(
&self,
cond: &Boolean<ConstraintF>,
first: &Self,
second: &Self,
should_enforce: &Boolean<ConstraintF>,
) -> Result<(), SynthesisError> {
let match_opt = cond.select(first, second)?;
self.conditional_enforce_equal(&match_opt, should_enforce)
}
}