use ark_ff::{BitIteratorBE, Field, FpParameters, PrimeField};
use crate::{fields::fp::FpVar, prelude::*, Assignment, ToConstraintFieldGadget, Vec};
use ark_relations::r1cs::{
ConstraintSystemRef, LinearCombination, Namespace, SynthesisError, Variable,
};
use core::borrow::Borrow;
#[derive(Clone, Debug, Eq, PartialEq)]
#[must_use]
pub struct AllocatedBit<F: Field> {
variable: Variable,
cs: ConstraintSystemRef<F>,
}
pub(crate) fn bool_to_field<F: Field>(val: impl Borrow<bool>) -> F {
if *val.borrow() {
F::one()
} else {
F::zero()
}
}
impl<F: Field> AllocatedBit<F> {
pub fn value(&self) -> Result<bool, SynthesisError> {
let value = self.cs.assigned_value(self.variable).get()?;
if value.is_zero() {
Ok(false)
} else if value.is_one() {
Ok(true)
} else {
unreachable!("Incorrect value assigned: {:?}", value);
}
}
pub fn variable(&self) -> Variable {
self.variable
}
fn new_witness_without_booleanity_check<T: Borrow<bool>>(
cs: ConstraintSystemRef<F>,
f: impl FnOnce() -> Result<T, SynthesisError>,
) -> Result<Self, SynthesisError> {
let variable = cs.new_witness_variable(|| f().map(bool_to_field))?;
Ok(Self { variable, cs })
}
#[tracing::instrument(target = "r1cs")]
pub fn xor(&self, b: &Self) -> Result<Self, SynthesisError> {
let result = Self::new_witness_without_booleanity_check(self.cs.clone(), || {
Ok(self.value()? ^ b.value()?)
})?;
self.cs.enforce_constraint(
lc!() + self.variable + self.variable,
lc!() + b.variable,
lc!() + self.variable + b.variable - result.variable,
)?;
Ok(result)
}
#[tracing::instrument(target = "r1cs")]
pub fn and(&self, b: &Self) -> Result<Self, SynthesisError> {
let result = Self::new_witness_without_booleanity_check(self.cs.clone(), || {
Ok(self.value()? & b.value()?)
})?;
self.cs.enforce_constraint(
lc!() + self.variable,
lc!() + b.variable,
lc!() + result.variable,
)?;
Ok(result)
}
#[tracing::instrument(target = "r1cs")]
pub fn or(&self, b: &Self) -> Result<Self, SynthesisError> {
let result = Self::new_witness_without_booleanity_check(self.cs.clone(), || {
Ok(self.value()? | b.value()?)
})?;
self.cs.enforce_constraint(
lc!() + Variable::One - self.variable,
lc!() + Variable::One - b.variable,
lc!() + Variable::One - result.variable,
)?;
Ok(result)
}
#[tracing::instrument(target = "r1cs")]
pub fn and_not(&self, b: &Self) -> Result<Self, SynthesisError> {
let result = Self::new_witness_without_booleanity_check(self.cs.clone(), || {
Ok(self.value()? & !b.value()?)
})?;
self.cs.enforce_constraint(
lc!() + self.variable,
lc!() + Variable::One - b.variable,
lc!() + result.variable,
)?;
Ok(result)
}
#[tracing::instrument(target = "r1cs")]
pub fn nor(&self, b: &Self) -> Result<Self, SynthesisError> {
let result = Self::new_witness_without_booleanity_check(self.cs.clone(), || {
Ok(!(self.value()? | b.value()?))
})?;
self.cs.enforce_constraint(
lc!() + Variable::One - self.variable,
lc!() + Variable::One - b.variable,
lc!() + result.variable,
)?;
Ok(result)
}
}
impl<F: Field> AllocVar<bool, F> for AllocatedBit<F> {
fn new_variable<T: Borrow<bool>>(
cs: impl Into<Namespace<F>>,
f: impl FnOnce() -> Result<T, SynthesisError>,
mode: AllocationMode,
) -> Result<Self, SynthesisError> {
let ns = cs.into();
let cs = ns.cs();
if mode == AllocationMode::Constant {
let variable = if *f()?.borrow() {
Variable::One
} else {
Variable::Zero
};
Ok(Self { variable, cs })
} else {
let variable = if mode == AllocationMode::Input {
cs.new_input_variable(|| f().map(bool_to_field))?
} else {
cs.new_witness_variable(|| f().map(bool_to_field))?
};
cs.enforce_constraint(lc!() + Variable::One - variable, lc!() + variable, lc!())?;
Ok(Self { variable, cs })
}
}
}
impl<F: Field> CondSelectGadget<F> for AllocatedBit<F> {
#[tracing::instrument(target = "r1cs")]
fn conditionally_select(
cond: &Boolean<F>,
true_val: &Self,
false_val: &Self,
) -> Result<Self, SynthesisError> {
let res = Boolean::conditionally_select(
cond,
&true_val.clone().into(),
&false_val.clone().into(),
)?;
match res {
Boolean::Is(a) => Ok(a),
_ => unreachable!("Impossible"),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[must_use]
pub enum Boolean<F: Field> {
Is(AllocatedBit<F>),
Not(AllocatedBit<F>),
Constant(bool),
}
impl<F: Field> R1CSVar<F> for Boolean<F> {
type Value = bool;
fn cs(&self) -> ConstraintSystemRef<F> {
match self {
Self::Is(a) | Self::Not(a) => a.cs.clone(),
_ => ConstraintSystemRef::None,
}
}
fn value(&self) -> Result<Self::Value, SynthesisError> {
match self {
Boolean::Constant(c) => Ok(*c),
Boolean::Is(ref v) => v.value(),
Boolean::Not(ref v) => v.value().map(|b| !b),
}
}
}
impl<F: Field> Boolean<F> {
pub const TRUE: Self = Boolean::Constant(true);
pub const FALSE: Self = Boolean::Constant(false);
pub fn lc(&self) -> LinearCombination<F> {
match self {
Boolean::Constant(false) => lc!(),
Boolean::Constant(true) => lc!() + Variable::One,
Boolean::Is(v) => v.variable().into(),
Boolean::Not(v) => lc!() + Variable::One - v.variable(),
}
}
pub fn constant_vec_from_bytes(values: &[u8]) -> Vec<Self> {
let mut bits = vec![];
for byte in values {
for i in 0..8 {
bits.push(Self::Constant(((byte >> i) & 1u8) == 1u8));
}
}
bits
}
pub fn constant(b: bool) -> Self {
Boolean::Constant(b)
}
pub fn not(&self) -> Self {
match *self {
Boolean::Constant(c) => Boolean::Constant(!c),
Boolean::Is(ref v) => Boolean::Not(v.clone()),
Boolean::Not(ref v) => Boolean::Is(v.clone()),
}
}
#[tracing::instrument(target = "r1cs")]
pub fn xor<'a>(&'a self, other: &'a Self) -> Result<Self, SynthesisError> {
use Boolean::*;
match (self, other) {
(&Constant(false), x) | (x, &Constant(false)) => Ok(x.clone()),
(&Constant(true), x) | (x, &Constant(true)) => Ok(x.not()),
(is @ &Is(_), not @ &Not(_)) | (not @ &Not(_), is @ &Is(_)) => {
Ok(is.xor(¬.not())?.not())
}
(&Is(ref a), &Is(ref b)) | (&Not(ref a), &Not(ref b)) => Ok(Is(a.xor(b)?)),
}
}
#[tracing::instrument(target = "r1cs")]
pub fn or<'a>(&'a self, other: &'a Self) -> Result<Self, SynthesisError> {
use Boolean::*;
match (self, other) {
(&Constant(false), x) | (x, &Constant(false)) => Ok(x.clone()),
(&Constant(true), _) | (_, &Constant(true)) => Ok(Constant(true)),
(a @ &Is(_), b @ &Not(_)) | (b @ &Not(_), a @ &Is(_)) | (b @ &Not(_), a @ &Not(_)) => {
Ok(a.not().and(&b.not())?.not())
}
(&Is(ref a), &Is(ref b)) => a.or(b).map(From::from),
}
}
#[tracing::instrument(target = "r1cs")]
pub fn and<'a>(&'a self, other: &'a Self) -> Result<Self, SynthesisError> {
use Boolean::*;
match (self, other) {
(&Constant(false), _) | (_, &Constant(false)) => Ok(Constant(false)),
(&Constant(true), x) | (x, &Constant(true)) => Ok(x.clone()),
(&Is(ref is), &Not(ref not)) | (&Not(ref not), &Is(ref is)) => Ok(Is(is.and_not(not)?)),
(&Not(ref a), &Not(ref b)) => Ok(Is(a.nor(b)?)),
(&Is(ref a), &Is(ref b)) => Ok(Is(a.and(b)?)),
}
}
#[tracing::instrument(target = "r1cs")]
pub fn kary_and(bits: &[Self]) -> Result<Self, SynthesisError> {
assert!(!bits.is_empty());
let mut cur: Option<Self> = None;
for next in bits {
cur = if let Some(b) = cur {
Some(b.and(next)?)
} else {
Some(next.clone())
};
}
Ok(cur.expect("should not be 0"))
}
#[tracing::instrument(target = "r1cs")]
pub fn kary_or(bits: &[Self]) -> Result<Self, SynthesisError> {
assert!(!bits.is_empty());
let mut cur: Option<Self> = None;
for next in bits {
cur = if let Some(b) = cur {
Some(b.or(next)?)
} else {
Some(next.clone())
};
}
Ok(cur.expect("should not be 0"))
}
#[tracing::instrument(target = "r1cs")]
pub fn kary_nand(bits: &[Self]) -> Result<Self, SynthesisError> {
Ok(Self::kary_and(bits)?.not())
}
#[tracing::instrument(target = "r1cs")]
fn enforce_kary_nand(bits: &[Self]) -> Result<(), SynthesisError> {
use Boolean::*;
let r = Self::kary_nand(bits)?;
match r {
Constant(true) => Ok(()),
Constant(false) => Err(SynthesisError::AssignmentMissing),
Is(_) | Not(_) => {
r.cs()
.enforce_constraint(r.lc(), lc!() + Variable::One, lc!() + Variable::One)
}
}
}
#[tracing::instrument(target = "r1cs", skip(bits))]
pub fn le_bits_to_fp_var(bits: &[Self]) -> Result<FpVar<F>, SynthesisError>
where
F: PrimeField,
{
let mut value = None;
let cs = bits.cs();
let should_construct_value = (!cs.is_in_setup_mode()) || bits.is_constant();
if should_construct_value {
let bits = bits.iter().map(|b| b.value().unwrap()).collect::<Vec<_>>();
let bytes = bits
.chunks(8)
.map(|c| {
let mut value = 0u8;
for (i, &bit) in c.iter().enumerate() {
value += (bit as u8) << i;
}
value
})
.collect::<Vec<_>>();
value = Some(F::from_le_bytes_mod_order(&bytes));
}
if bits.is_constant() {
Ok(FpVar::constant(value.unwrap()))
} else {
let mut power = F::one();
let mut combined_lc = LinearCombination::zero();
bits.iter().for_each(|b| {
combined_lc = &combined_lc + (power, b.lc());
power.double_in_place();
});
let variable = cs.new_lc(combined_lc)?;
if bits.len() >= F::Params::MODULUS_BITS as usize {
Self::enforce_in_field_le(bits)?;
}
Ok(crate::fields::fp::AllocatedFp::new(value, variable, cs.clone()).into())
}
}
#[tracing::instrument(target = "r1cs")]
pub fn enforce_in_field_le(bits: &[Self]) -> Result<(), SynthesisError> {
let mut b = F::characteristic().to_vec();
assert_eq!(b[0] % 2, 1);
b[0] -= 1;
let run = Self::enforce_smaller_or_equal_than_le(bits, b)?;
assert!(run.is_empty());
Ok(())
}
#[tracing::instrument(target = "r1cs", skip(element))]
pub fn enforce_smaller_or_equal_than_le<'a>(
bits: &[Self],
element: impl AsRef<[u64]>,
) -> Result<Vec<Self>, SynthesisError> {
let b: &[u64] = element.as_ref();
let mut bits_iter = bits.iter().rev();
let mut last_run = Boolean::constant(true);
let mut current_run = vec![];
let mut element_num_bits = 0;
for _ in BitIteratorBE::without_leading_zeros(b) {
element_num_bits += 1;
}
if bits.len() > element_num_bits {
let mut or_result = Boolean::constant(false);
for should_be_zero in &bits[element_num_bits..] {
or_result = or_result.or(should_be_zero)?;
let _ = bits_iter.next().unwrap();
}
or_result.enforce_equal(&Boolean::constant(false))?;
}
for (b, a) in BitIteratorBE::without_leading_zeros(b).zip(bits_iter.by_ref()) {
if b {
current_run.push(a.clone());
} else {
if !current_run.is_empty() {
current_run.push(last_run.clone());
last_run = Self::kary_and(¤t_run)?;
current_run.truncate(0);
}
Self::enforce_kary_nand(&[last_run.clone(), a.clone()])?;
}
}
assert!(bits_iter.next().is_none());
Ok(current_run)
}
#[tracing::instrument(target = "r1cs", skip(first, second))]
pub fn select<T: CondSelectGadget<F>>(
&self,
first: &T,
second: &T,
) -> Result<T, SynthesisError> {
T::conditionally_select(&self, first, second)
}
}
impl<F: Field> From<AllocatedBit<F>> for Boolean<F> {
fn from(b: AllocatedBit<F>) -> Self {
Boolean::Is(b)
}
}
impl<F: Field> AllocVar<bool, F> for Boolean<F> {
fn new_variable<T: Borrow<bool>>(
cs: impl Into<Namespace<F>>,
f: impl FnOnce() -> Result<T, SynthesisError>,
mode: AllocationMode,
) -> Result<Self, SynthesisError> {
if mode == AllocationMode::Constant {
Ok(Boolean::Constant(*f()?.borrow()))
} else {
AllocatedBit::new_variable(cs, f, mode).map(Boolean::from)
}
}
}
impl<F: Field> EqGadget<F> for Boolean<F> {
#[tracing::instrument(target = "r1cs")]
fn is_eq(&self, other: &Self) -> Result<Boolean<F>, SynthesisError> {
Ok(self.xor(other)?.not())
}
#[tracing::instrument(target = "r1cs")]
fn conditional_enforce_equal(
&self,
other: &Self,
condition: &Boolean<F>,
) -> Result<(), SynthesisError> {
use Boolean::*;
let one = Variable::One;
let difference = match (self, other) {
(Constant(true), Constant(true)) | (Constant(false), Constant(false)) => return Ok(()),
(Constant(_), Constant(_)) => return Err(SynthesisError::AssignmentMissing),
(Constant(true), Is(a)) | (Is(a), Constant(true)) => lc!() + one - a.variable(),
(Constant(false), Is(a)) | (Is(a), Constant(false)) => lc!() + a.variable(),
(Constant(true), Not(a)) | (Not(a), Constant(true)) => lc!() + a.variable(),
(Constant(false), Not(a)) | (Not(a), Constant(false)) => lc!() + one - a.variable(),
(Is(a), Is(b)) => lc!() + b.variable() - a.variable(),
(Is(a), Not(b)) | (Not(b), Is(a)) => lc!() + one - b.variable() - a.variable(),
(Not(a), Not(b)) => lc!() + a.variable() - b.variable(),
};
if condition != &Constant(false) {
let cs = self.cs().or(other.cs()).or(condition.cs());
cs.enforce_constraint(lc!() + difference, condition.lc(), lc!())?;
}
Ok(())
}
#[tracing::instrument(target = "r1cs")]
fn conditional_enforce_not_equal(
&self,
other: &Self,
should_enforce: &Boolean<F>,
) -> Result<(), SynthesisError> {
use Boolean::*;
let one = Variable::One;
let difference = match (self, other) {
(Constant(true), Constant(false)) | (Constant(false), Constant(true)) => return Ok(()),
(Constant(_), Constant(_)) => return Err(SynthesisError::AssignmentMissing),
(Constant(true), Is(a)) | (Is(a), Constant(true)) => lc!() + one - a.variable(),
(Constant(false), Is(a)) | (Is(a), Constant(false)) => lc!() + a.variable(),
(Constant(true), Not(a)) | (Not(a), Constant(true)) => lc!() + a.variable(),
(Constant(false), Not(a)) | (Not(a), Constant(false)) => lc!() + one - a.variable(),
(Is(a), Is(b)) => lc!() + b.variable() - a.variable(),
(Is(a), Not(b)) | (Not(b), Is(a)) => lc!() + one - b.variable() - a.variable(),
(Not(a), Not(b)) => lc!() + a.variable() - b.variable(),
};
if should_enforce != &Constant(false) {
let cs = self.cs().or(other.cs()).or(should_enforce.cs());
cs.enforce_constraint(difference, should_enforce.lc(), should_enforce.lc())?;
}
Ok(())
}
}
impl<F: Field> ToBytesGadget<F> for Boolean<F> {
#[tracing::instrument(target = "r1cs")]
fn to_bytes(&self) -> Result<Vec<UInt8<F>>, SynthesisError> {
let value = self.value().map(u8::from).ok();
let mut bits = [Boolean::FALSE; 8];
bits[0] = self.clone();
Ok(vec![UInt8 { bits, value }])
}
}
impl<F: PrimeField> ToConstraintFieldGadget<F> for Boolean<F> {
#[tracing::instrument(target = "r1cs")]
fn to_constraint_field(&self) -> Result<Vec<FpVar<F>>, SynthesisError> {
let var = From::from(self.clone());
Ok(vec![var])
}
}
impl<F: Field> CondSelectGadget<F> for Boolean<F> {
#[tracing::instrument(target = "r1cs")]
fn conditionally_select(
cond: &Boolean<F>,
true_val: &Self,
false_val: &Self,
) -> Result<Self, SynthesisError> {
use Boolean::*;
match cond {
Constant(true) => Ok(true_val.clone()),
Constant(false) => Ok(false_val.clone()),
cond @ Not(_) => Self::conditionally_select(&cond.not(), false_val, true_val),
cond @ Is(_) => match (true_val, false_val) {
(x, &Constant(false)) => cond.and(x),
(&Constant(false), x) => cond.not().and(x),
(&Constant(true), x) => cond.or(x),
(x, &Constant(true)) => cond.not().or(x),
(a, b) => {
let cs = cond.cs();
let result: Boolean<F> =
AllocatedBit::new_witness_without_booleanity_check(cs.clone(), || {
let cond = cond.value()?;
Ok(if cond { a.value()? } else { b.value()? })
})?
.into();
cs.enforce_constraint(
cond.lc(),
lc!() + a.lc() - b.lc(),
lc!() + result.lc() - b.lc(),
)?;
Ok(result)
}
},
}
}
}
#[cfg(test)]
mod test {
use super::{AllocatedBit, Boolean};
use crate::prelude::*;
use ark_ff::{BitIteratorBE, BitIteratorLE, Field, One, PrimeField, UniformRand, Zero};
use ark_relations::r1cs::{ConstraintSystem, Namespace, SynthesisError};
use ark_test_curves::bls12_381::Fr;
#[test]
fn test_boolean_to_byte() -> Result<(), SynthesisError> {
for val in [true, false].iter() {
let cs = ConstraintSystem::<Fr>::new_ref();
let a = Boolean::new_witness(cs.clone(), || Ok(*val))?;
let bytes = a.to_bytes()?;
assert_eq!(bytes.len(), 1);
let byte = &bytes[0];
assert_eq!(byte.value()?, *val as u8);
for (i, bit) in byte.bits.iter().enumerate() {
assert_eq!(bit.value()?, (byte.value()? >> i) & 1 == 1);
}
}
Ok(())
}
#[test]
fn test_xor() -> Result<(), SynthesisError> {
for a_val in [false, true].iter().copied() {
for b_val in [false, true].iter().copied() {
let cs = ConstraintSystem::<Fr>::new_ref();
let a = AllocatedBit::new_witness(cs.clone(), || Ok(a_val))?;
let b = AllocatedBit::new_witness(cs.clone(), || Ok(b_val))?;
let c = AllocatedBit::xor(&a, &b)?;
assert_eq!(c.value()?, a_val ^ b_val);
assert!(cs.is_satisfied().unwrap());
assert_eq!(a.value()?, (a_val));
assert_eq!(b.value()?, (b_val));
assert_eq!(c.value()?, (a_val ^ b_val));
}
}
Ok(())
}
#[test]
fn test_or() -> Result<(), SynthesisError> {
for a_val in [false, true].iter().copied() {
for b_val in [false, true].iter().copied() {
let cs = ConstraintSystem::<Fr>::new_ref();
let a = AllocatedBit::new_witness(cs.clone(), || Ok(a_val))?;
let b = AllocatedBit::new_witness(cs.clone(), || Ok(b_val))?;
let c = AllocatedBit::or(&a, &b)?;
assert_eq!(c.value()?, a_val | b_val);
assert!(cs.is_satisfied().unwrap());
assert_eq!(a.value()?, (a_val));
assert_eq!(b.value()?, (b_val));
assert_eq!(c.value()?, (a_val | b_val));
}
}
Ok(())
}
#[test]
fn test_and() -> Result<(), SynthesisError> {
for a_val in [false, true].iter().copied() {
for b_val in [false, true].iter().copied() {
let cs = ConstraintSystem::<Fr>::new_ref();
let a = AllocatedBit::new_witness(cs.clone(), || Ok(a_val))?;
let b = AllocatedBit::new_witness(cs.clone(), || Ok(b_val))?;
let c = AllocatedBit::and(&a, &b)?;
assert_eq!(c.value()?, a_val & b_val);
assert!(cs.is_satisfied().unwrap());
assert_eq!(a.value()?, (a_val));
assert_eq!(b.value()?, (b_val));
assert_eq!(c.value()?, (a_val & b_val));
}
}
Ok(())
}
#[test]
fn test_and_not() -> Result<(), SynthesisError> {
for a_val in [false, true].iter().copied() {
for b_val in [false, true].iter().copied() {
let cs = ConstraintSystem::<Fr>::new_ref();
let a = AllocatedBit::new_witness(cs.clone(), || Ok(a_val))?;
let b = AllocatedBit::new_witness(cs.clone(), || Ok(b_val))?;
let c = AllocatedBit::and_not(&a, &b)?;
assert_eq!(c.value()?, a_val & !b_val);
assert!(cs.is_satisfied().unwrap());
assert_eq!(a.value()?, (a_val));
assert_eq!(b.value()?, (b_val));
assert_eq!(c.value()?, (a_val & !b_val));
}
}
Ok(())
}
#[test]
fn test_nor() -> Result<(), SynthesisError> {
for a_val in [false, true].iter().copied() {
for b_val in [false, true].iter().copied() {
let cs = ConstraintSystem::<Fr>::new_ref();
let a = AllocatedBit::new_witness(cs.clone(), || Ok(a_val))?;
let b = AllocatedBit::new_witness(cs.clone(), || Ok(b_val))?;
let c = AllocatedBit::nor(&a, &b)?;
assert_eq!(c.value()?, !a_val & !b_val);
assert!(cs.is_satisfied().unwrap());
assert_eq!(a.value()?, (a_val));
assert_eq!(b.value()?, (b_val));
assert_eq!(c.value()?, (!a_val & !b_val));
}
}
Ok(())
}
#[test]
fn test_enforce_equal() -> Result<(), SynthesisError> {
for a_bool in [false, true].iter().cloned() {
for b_bool in [false, true].iter().cloned() {
for a_neg in [false, true].iter().cloned() {
for b_neg in [false, true].iter().cloned() {
let cs = ConstraintSystem::<Fr>::new_ref();
let mut a = Boolean::new_witness(cs.clone(), || Ok(a_bool))?;
let mut b = Boolean::new_witness(cs.clone(), || Ok(b_bool))?;
if a_neg {
a = a.not();
}
if b_neg {
b = b.not();
}
a.enforce_equal(&b)?;
assert_eq!(
cs.is_satisfied().unwrap(),
(a_bool ^ a_neg) == (b_bool ^ b_neg)
);
}
}
}
}
Ok(())
}
#[test]
fn test_conditional_enforce_equal() -> Result<(), SynthesisError> {
for a_bool in [false, true].iter().cloned() {
for b_bool in [false, true].iter().cloned() {
for a_neg in [false, true].iter().cloned() {
for b_neg in [false, true].iter().cloned() {
let cs = ConstraintSystem::<Fr>::new_ref();
let mut a = Boolean::new_witness(cs.clone(), || Ok(a_bool))?;
let mut b = Boolean::new_witness(cs.clone(), || Ok(b_bool))?;
if a_neg {
a = a.not();
}
if b_neg {
b = b.not();
}
a.conditional_enforce_equal(&b, &Boolean::constant(true))?;
assert_eq!(
cs.is_satisfied().unwrap(),
(a_bool ^ a_neg) == (b_bool ^ b_neg)
);
let cs = ConstraintSystem::<Fr>::new_ref();
let mut a = Boolean::new_witness(cs.clone(), || Ok(a_bool))?;
let mut b = Boolean::new_witness(cs.clone(), || Ok(b_bool))?;
if a_neg {
a = a.not();
}
if b_neg {
b = b.not();
}
let false_cond =
Boolean::new_witness(ark_relations::ns!(cs, "cond"), || Ok(false))?;
a.conditional_enforce_equal(&b, &false_cond)?;
assert!(cs.is_satisfied().unwrap());
}
}
}
}
Ok(())
}
#[test]
fn test_boolean_negation() -> Result<(), SynthesisError> {
let cs = ConstraintSystem::<Fr>::new_ref();
let mut b = Boolean::new_witness(cs.clone(), || Ok(true))?;
assert!(matches!(b, Boolean::Is(_)));
b = b.not();
assert!(matches!(b, Boolean::Not(_)));
b = b.not();
assert!(matches!(b, Boolean::Is(_)));
b = Boolean::Constant(true);
assert!(matches!(b, Boolean::Constant(true)));
b = b.not();
assert!(matches!(b, Boolean::Constant(false)));
b = b.not();
assert!(matches!(b, Boolean::Constant(true)));
Ok(())
}
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
enum OpType {
True,
False,
AllocatedTrue,
AllocatedFalse,
NegatedAllocatedTrue,
NegatedAllocatedFalse,
}
const VARIANTS: [OpType; 6] = [
OpType::True,
OpType::False,
OpType::AllocatedTrue,
OpType::AllocatedFalse,
OpType::NegatedAllocatedTrue,
OpType::NegatedAllocatedFalse,
];
fn construct<F: Field>(
ns: Namespace<F>,
operand: OpType,
) -> Result<Boolean<F>, SynthesisError> {
let cs = ns.cs();
let b = match operand {
OpType::True => Boolean::constant(true),
OpType::False => Boolean::constant(false),
OpType::AllocatedTrue => Boolean::new_witness(cs, || Ok(true))?,
OpType::AllocatedFalse => Boolean::new_witness(cs, || Ok(false))?,
OpType::NegatedAllocatedTrue => Boolean::new_witness(cs, || Ok(true))?.not(),
OpType::NegatedAllocatedFalse => Boolean::new_witness(cs, || Ok(false))?.not(),
};
Ok(b)
}
#[test]
fn test_boolean_xor() -> Result<(), SynthesisError> {
for first_operand in VARIANTS.iter().cloned() {
for second_operand in VARIANTS.iter().cloned() {
let cs = ConstraintSystem::<Fr>::new_ref();
let a = construct(ark_relations::ns!(cs, "a"), first_operand)?;
let b = construct(ark_relations::ns!(cs, "b"), second_operand)?;
let c = Boolean::xor(&a, &b)?;
assert!(cs.is_satisfied().unwrap());
match (first_operand, second_operand, c) {
(OpType::True, OpType::True, Boolean::Constant(false)) => (),
(OpType::True, OpType::False, Boolean::Constant(true)) => (),
(OpType::True, OpType::AllocatedTrue, Boolean::Not(_)) => (),
(OpType::True, OpType::AllocatedFalse, Boolean::Not(_)) => (),
(OpType::True, OpType::NegatedAllocatedTrue, Boolean::Is(_)) => (),
(OpType::True, OpType::NegatedAllocatedFalse, Boolean::Is(_)) => (),
(OpType::False, OpType::True, Boolean::Constant(true)) => (),
(OpType::False, OpType::False, Boolean::Constant(false)) => (),
(OpType::False, OpType::AllocatedTrue, Boolean::Is(_)) => (),
(OpType::False, OpType::AllocatedFalse, Boolean::Is(_)) => (),
(OpType::False, OpType::NegatedAllocatedTrue, Boolean::Not(_)) => (),
(OpType::False, OpType::NegatedAllocatedFalse, Boolean::Not(_)) => (),
(OpType::AllocatedTrue, OpType::True, Boolean::Not(_)) => (),
(OpType::AllocatedTrue, OpType::False, Boolean::Is(_)) => (),
(OpType::AllocatedTrue, OpType::AllocatedTrue, Boolean::Is(ref v)) => {
assert_eq!(v.value(), Ok(false));
}
(OpType::AllocatedTrue, OpType::AllocatedFalse, Boolean::Is(ref v)) => {
assert_eq!(v.value(), Ok(true));
}
(OpType::AllocatedTrue, OpType::NegatedAllocatedTrue, Boolean::Not(ref v)) => {
assert_eq!(v.value(), Ok(false));
}
(OpType::AllocatedTrue, OpType::NegatedAllocatedFalse, Boolean::Not(ref v)) => {
assert_eq!(v.value(), Ok(true));
}
(OpType::AllocatedFalse, OpType::True, Boolean::Not(_)) => (),
(OpType::AllocatedFalse, OpType::False, Boolean::Is(_)) => (),
(OpType::AllocatedFalse, OpType::AllocatedTrue, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::one());
assert_eq!(v.value(), Ok(true));
}
(OpType::AllocatedFalse, OpType::AllocatedFalse, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(OpType::AllocatedFalse, OpType::NegatedAllocatedTrue, Boolean::Not(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::one());
assert_eq!(v.value(), Ok(true));
}
(
OpType::AllocatedFalse,
OpType::NegatedAllocatedFalse,
Boolean::Not(ref v),
) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(OpType::NegatedAllocatedTrue, OpType::True, Boolean::Is(_)) => (),
(OpType::NegatedAllocatedTrue, OpType::False, Boolean::Not(_)) => (),
(OpType::NegatedAllocatedTrue, OpType::AllocatedTrue, Boolean::Not(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(OpType::NegatedAllocatedTrue, OpType::AllocatedFalse, Boolean::Not(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::one());
assert_eq!(v.value(), Ok(true));
}
(
OpType::NegatedAllocatedTrue,
OpType::NegatedAllocatedTrue,
Boolean::Is(ref v),
) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(
OpType::NegatedAllocatedTrue,
OpType::NegatedAllocatedFalse,
Boolean::Is(ref v),
) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::one());
assert_eq!(v.value(), Ok(true));
}
(OpType::NegatedAllocatedFalse, OpType::True, Boolean::Is(_)) => (),
(OpType::NegatedAllocatedFalse, OpType::False, Boolean::Not(_)) => (),
(OpType::NegatedAllocatedFalse, OpType::AllocatedTrue, Boolean::Not(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::one());
assert_eq!(v.value(), Ok(true));
}
(
OpType::NegatedAllocatedFalse,
OpType::AllocatedFalse,
Boolean::Not(ref v),
) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(
OpType::NegatedAllocatedFalse,
OpType::NegatedAllocatedTrue,
Boolean::Is(ref v),
) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::one());
assert_eq!(v.value(), Ok(true));
}
(
OpType::NegatedAllocatedFalse,
OpType::NegatedAllocatedFalse,
Boolean::Is(ref v),
) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
_ => unreachable!(),
}
}
}
Ok(())
}
#[test]
fn test_boolean_cond_select() -> Result<(), SynthesisError> {
for condition in VARIANTS.iter().cloned() {
for first_operand in VARIANTS.iter().cloned() {
for second_operand in VARIANTS.iter().cloned() {
let cs = ConstraintSystem::<Fr>::new_ref();
let cond = construct(ark_relations::ns!(cs, "cond"), condition)?;
let a = construct(ark_relations::ns!(cs, "a"), first_operand)?;
let b = construct(ark_relations::ns!(cs, "b"), second_operand)?;
let c = cond.select(&a, &b)?;
assert!(
cs.is_satisfied().unwrap(),
"failed with operands: cond: {:?}, a: {:?}, b: {:?}",
condition,
first_operand,
second_operand,
);
assert_eq!(
c.value()?,
if cond.value()? {
a.value()?
} else {
b.value()?
}
);
}
}
}
Ok(())
}
#[test]
fn test_boolean_or() -> Result<(), SynthesisError> {
for first_operand in VARIANTS.iter().cloned() {
for second_operand in VARIANTS.iter().cloned() {
let cs = ConstraintSystem::<Fr>::new_ref();
let a = construct(ark_relations::ns!(cs, "a"), first_operand)?;
let b = construct(ark_relations::ns!(cs, "b"), second_operand)?;
let c = a.or(&b)?;
assert!(cs.is_satisfied().unwrap());
match (first_operand, second_operand, c.clone()) {
(OpType::True, OpType::True, Boolean::Constant(true)) => (),
(OpType::True, OpType::False, Boolean::Constant(true)) => (),
(OpType::True, OpType::AllocatedTrue, Boolean::Constant(true)) => (),
(OpType::True, OpType::AllocatedFalse, Boolean::Constant(true)) => (),
(OpType::True, OpType::NegatedAllocatedTrue, Boolean::Constant(true)) => (),
(OpType::True, OpType::NegatedAllocatedFalse, Boolean::Constant(true)) => (),
(OpType::False, OpType::True, Boolean::Constant(true)) => (),
(OpType::False, OpType::False, Boolean::Constant(false)) => (),
(OpType::False, OpType::AllocatedTrue, Boolean::Is(_)) => (),
(OpType::False, OpType::AllocatedFalse, Boolean::Is(_)) => (),
(OpType::False, OpType::NegatedAllocatedTrue, Boolean::Not(_)) => (),
(OpType::False, OpType::NegatedAllocatedFalse, Boolean::Not(_)) => (),
(OpType::AllocatedTrue, OpType::True, Boolean::Constant(true)) => (),
(OpType::AllocatedTrue, OpType::False, Boolean::Is(_)) => (),
(OpType::AllocatedTrue, OpType::AllocatedTrue, Boolean::Is(ref v)) => {
assert_eq!(v.value(), Ok(true));
}
(OpType::AllocatedTrue, OpType::AllocatedFalse, Boolean::Is(ref v)) => {
assert_eq!(v.value(), Ok(true));
}
(OpType::AllocatedTrue, OpType::NegatedAllocatedTrue, Boolean::Not(ref v)) => {
assert_eq!(v.value(), Ok(false));
}
(OpType::AllocatedTrue, OpType::NegatedAllocatedFalse, Boolean::Not(ref v)) => {
assert_eq!(v.value(), Ok(false));
}
(OpType::AllocatedFalse, OpType::True, Boolean::Constant(true)) => (),
(OpType::AllocatedFalse, OpType::False, Boolean::Is(_)) => (),
(OpType::AllocatedFalse, OpType::AllocatedTrue, Boolean::Is(ref v)) => {
assert_eq!(v.value(), Ok(true));
}
(OpType::AllocatedFalse, OpType::AllocatedFalse, Boolean::Is(ref v)) => {
assert_eq!(v.value(), Ok(false));
}
(OpType::AllocatedFalse, OpType::NegatedAllocatedTrue, Boolean::Not(ref v)) => {
assert_eq!(v.value(), Ok(true));
}
(
OpType::AllocatedFalse,
OpType::NegatedAllocatedFalse,
Boolean::Not(ref v),
) => {
assert_eq!(v.value(), Ok(false));
}
(OpType::NegatedAllocatedTrue, OpType::True, Boolean::Constant(true)) => (),
(OpType::NegatedAllocatedTrue, OpType::False, Boolean::Not(_)) => (),
(OpType::NegatedAllocatedTrue, OpType::AllocatedTrue, Boolean::Not(ref v)) => {
assert_eq!(v.value(), Ok(false));
}
(OpType::NegatedAllocatedTrue, OpType::AllocatedFalse, Boolean::Not(ref v)) => {
assert_eq!(v.value(), Ok(true));
}
(
OpType::NegatedAllocatedTrue,
OpType::NegatedAllocatedTrue,
Boolean::Not(ref v),
) => {
assert_eq!(v.value(), Ok(true));
}
(
OpType::NegatedAllocatedTrue,
OpType::NegatedAllocatedFalse,
Boolean::Not(ref v),
) => {
assert_eq!(v.value(), Ok(false));
}
(OpType::NegatedAllocatedFalse, OpType::True, Boolean::Constant(true)) => (),
(OpType::NegatedAllocatedFalse, OpType::False, Boolean::Not(_)) => (),
(OpType::NegatedAllocatedFalse, OpType::AllocatedTrue, Boolean::Not(ref v)) => {
assert_eq!(v.value(), Ok(false));
}
(
OpType::NegatedAllocatedFalse,
OpType::AllocatedFalse,
Boolean::Not(ref v),
) => {
assert_eq!(v.value(), Ok(false));
}
(
OpType::NegatedAllocatedFalse,
OpType::NegatedAllocatedTrue,
Boolean::Not(ref v),
) => {
assert_eq!(v.value(), Ok(false));
}
(
OpType::NegatedAllocatedFalse,
OpType::NegatedAllocatedFalse,
Boolean::Not(ref v),
) => {
assert_eq!(v.value(), Ok(false));
}
_ => panic!(
"this should never be encountered, in case: (a = {:?}, b = {:?}, c = {:?})",
a, b, c
),
}
}
}
Ok(())
}
#[test]
fn test_boolean_and() -> Result<(), SynthesisError> {
for first_operand in VARIANTS.iter().cloned() {
for second_operand in VARIANTS.iter().cloned() {
let cs = ConstraintSystem::<Fr>::new_ref();
let a = construct(ark_relations::ns!(cs, "a"), first_operand)?;
let b = construct(ark_relations::ns!(cs, "b"), second_operand)?;
let c = a.and(&b)?;
assert!(cs.is_satisfied().unwrap());
match (first_operand, second_operand, c) {
(OpType::True, OpType::True, Boolean::Constant(true)) => (),
(OpType::True, OpType::False, Boolean::Constant(false)) => (),
(OpType::True, OpType::AllocatedTrue, Boolean::Is(_)) => (),
(OpType::True, OpType::AllocatedFalse, Boolean::Is(_)) => (),
(OpType::True, OpType::NegatedAllocatedTrue, Boolean::Not(_)) => (),
(OpType::True, OpType::NegatedAllocatedFalse, Boolean::Not(_)) => (),
(OpType::False, OpType::True, Boolean::Constant(false)) => (),
(OpType::False, OpType::False, Boolean::Constant(false)) => (),
(OpType::False, OpType::AllocatedTrue, Boolean::Constant(false)) => (),
(OpType::False, OpType::AllocatedFalse, Boolean::Constant(false)) => (),
(OpType::False, OpType::NegatedAllocatedTrue, Boolean::Constant(false)) => (),
(OpType::False, OpType::NegatedAllocatedFalse, Boolean::Constant(false)) => (),
(OpType::AllocatedTrue, OpType::True, Boolean::Is(_)) => (),
(OpType::AllocatedTrue, OpType::False, Boolean::Constant(false)) => (),
(OpType::AllocatedTrue, OpType::AllocatedTrue, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::one());
assert_eq!(v.value(), Ok(true));
}
(OpType::AllocatedTrue, OpType::AllocatedFalse, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(OpType::AllocatedTrue, OpType::NegatedAllocatedTrue, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(OpType::AllocatedTrue, OpType::NegatedAllocatedFalse, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::one());
assert_eq!(v.value(), Ok(true));
}
(OpType::AllocatedFalse, OpType::True, Boolean::Is(_)) => (),
(OpType::AllocatedFalse, OpType::False, Boolean::Constant(false)) => (),
(OpType::AllocatedFalse, OpType::AllocatedTrue, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(OpType::AllocatedFalse, OpType::AllocatedFalse, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(OpType::AllocatedFalse, OpType::NegatedAllocatedTrue, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(OpType::AllocatedFalse, OpType::NegatedAllocatedFalse, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(OpType::NegatedAllocatedTrue, OpType::True, Boolean::Not(_)) => (),
(OpType::NegatedAllocatedTrue, OpType::False, Boolean::Constant(false)) => (),
(OpType::NegatedAllocatedTrue, OpType::AllocatedTrue, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(OpType::NegatedAllocatedTrue, OpType::AllocatedFalse, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(
OpType::NegatedAllocatedTrue,
OpType::NegatedAllocatedTrue,
Boolean::Is(ref v),
) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(
OpType::NegatedAllocatedTrue,
OpType::NegatedAllocatedFalse,
Boolean::Is(ref v),
) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(OpType::NegatedAllocatedFalse, OpType::True, Boolean::Not(_)) => (),
(OpType::NegatedAllocatedFalse, OpType::False, Boolean::Constant(false)) => (),
(OpType::NegatedAllocatedFalse, OpType::AllocatedTrue, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::one());
assert_eq!(v.value(), Ok(true));
}
(OpType::NegatedAllocatedFalse, OpType::AllocatedFalse, Boolean::Is(ref v)) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(
OpType::NegatedAllocatedFalse,
OpType::NegatedAllocatedTrue,
Boolean::Is(ref v),
) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
assert_eq!(v.value(), Ok(false));
}
(
OpType::NegatedAllocatedFalse,
OpType::NegatedAllocatedFalse,
Boolean::Is(ref v),
) => {
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::one());
assert_eq!(v.value(), Ok(true));
}
_ => {
panic!(
"unexpected behavior at {:?} AND {:?}",
first_operand, second_operand
);
}
}
}
}
Ok(())
}
#[test]
fn test_smaller_than_or_equal_to() -> Result<(), SynthesisError> {
let mut rng = ark_std::test_rng();
for _ in 0..1000 {
let mut r = Fr::rand(&mut rng);
let mut s = Fr::rand(&mut rng);
if r > s {
core::mem::swap(&mut r, &mut s)
}
let cs = ConstraintSystem::<Fr>::new_ref();
let native_bits: Vec<_> = BitIteratorLE::new(r.into_repr()).collect();
let bits = Vec::new_witness(cs.clone(), || Ok(native_bits))?;
Boolean::enforce_smaller_or_equal_than_le(&bits, s.into_repr())?;
assert!(cs.is_satisfied().unwrap());
}
for _ in 0..1000 {
let r = Fr::rand(&mut rng);
if r == -Fr::one() {
continue;
}
let s = r + Fr::one();
let s2 = r.double();
let cs = ConstraintSystem::<Fr>::new_ref();
let native_bits: Vec<_> = BitIteratorLE::new(r.into_repr()).collect();
let bits = Vec::new_witness(cs.clone(), || Ok(native_bits))?;
Boolean::enforce_smaller_or_equal_than_le(&bits, s.into_repr())?;
if r < s2 {
Boolean::enforce_smaller_or_equal_than_le(&bits, s2.into_repr())?;
}
assert!(cs.is_satisfied().unwrap());
}
Ok(())
}
#[test]
fn test_enforce_in_field() -> Result<(), SynthesisError> {
{
let cs = ConstraintSystem::<Fr>::new_ref();
let mut bits = vec![];
for b in BitIteratorBE::new(Fr::characteristic()).skip(1) {
bits.push(Boolean::new_witness(cs.clone(), || Ok(b))?);
}
bits.reverse();
Boolean::enforce_in_field_le(&bits)?;
assert!(!cs.is_satisfied().unwrap());
}
let mut rng = ark_std::test_rng();
for _ in 0..1000 {
let r = Fr::rand(&mut rng);
let cs = ConstraintSystem::<Fr>::new_ref();
let mut bits = vec![];
for b in BitIteratorBE::new(r.into_repr()).skip(1) {
bits.push(Boolean::new_witness(cs.clone(), || Ok(b))?);
}
bits.reverse();
Boolean::enforce_in_field_le(&bits)?;
assert!(cs.is_satisfied().unwrap());
}
Ok(())
}
#[test]
fn test_enforce_nand() -> Result<(), SynthesisError> {
{
let cs = ConstraintSystem::<Fr>::new_ref();
assert!(
Boolean::enforce_kary_nand(&[Boolean::new_constant(cs.clone(), false)?]).is_ok()
);
assert!(
Boolean::enforce_kary_nand(&[Boolean::new_constant(cs.clone(), true)?]).is_err()
);
}
for i in 1..5 {
for mut b in 0..(1 << i) {
for mut n in 0..(1 << i) {
let cs = ConstraintSystem::<Fr>::new_ref();
let mut expected = true;
let mut bits = vec![];
for _ in 0..i {
expected &= b & 1 == 1;
let bit = if n & 1 == 1 {
Boolean::new_witness(cs.clone(), || Ok(b & 1 == 1))?
} else {
Boolean::new_witness(cs.clone(), || Ok(b & 1 == 0))?.not()
};
bits.push(bit);
b >>= 1;
n >>= 1;
}
let expected = !expected;
Boolean::enforce_kary_nand(&bits)?;
if expected {
assert!(cs.is_satisfied().unwrap());
} else {
assert!(!cs.is_satisfied().unwrap());
}
}
}
}
Ok(())
}
#[test]
fn test_kary_and() -> Result<(), SynthesisError> {
for i in 1..15 {
for mut b in 0..(1 << i) {
let cs = ConstraintSystem::<Fr>::new_ref();
let mut expected = true;
let mut bits = vec![];
for _ in 0..i {
expected &= b & 1 == 1;
bits.push(Boolean::new_witness(cs.clone(), || Ok(b & 1 == 1))?);
b >>= 1;
}
let r = Boolean::kary_and(&bits)?;
assert!(cs.is_satisfied().unwrap());
if let Boolean::Is(ref r) = r {
assert_eq!(r.value()?, expected);
}
}
}
Ok(())
}
#[test]
fn test_bits_to_fp() -> Result<(), SynthesisError> {
use AllocationMode::*;
let rng = &mut ark_std::test_rng();
let cs = ConstraintSystem::<Fr>::new_ref();
let modes = [Input, Witness, Constant];
for &mode in modes.iter() {
for _ in 0..1000 {
let f = Fr::rand(rng);
let bits = BitIteratorLE::new(f.into_repr()).collect::<Vec<_>>();
let bits: Vec<_> =
AllocVar::new_variable(cs.clone(), || Ok(bits.as_slice()), mode)?;
let f = AllocVar::new_variable(cs.clone(), || Ok(f), mode)?;
let claimed_f = Boolean::le_bits_to_fp_var(&bits)?;
claimed_f.enforce_equal(&f)?;
}
for _ in 0..1000 {
let f = Fr::from(u64::rand(rng));
let bits = BitIteratorLE::new(f.into_repr()).collect::<Vec<_>>();
let bits: Vec<_> =
AllocVar::new_variable(cs.clone(), || Ok(bits.as_slice()), mode)?;
let f = AllocVar::new_variable(cs.clone(), || Ok(f), mode)?;
let claimed_f = Boolean::le_bits_to_fp_var(&bits)?;
claimed_f.enforce_equal(&f)?;
}
assert!(cs.is_satisfied().unwrap());
}
Ok(())
}
}