mirror of
https://github.com/arnaucube/ark-r1cs-std.git
synced 2026-01-17 03:01:29 +01:00
Upgrade to work with latest ark-ff (#90)
Co-authored-by: Sun <huachuang20@gmail.com> Co-authored-by: Pratyush Mishra <pratyushmishra@berkeley.edu>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use ark_ff::{BitIteratorBE, Field, FpParameters, PrimeField};
|
||||
use ark_ff::{BitIteratorBE, Field, PrimeField};
|
||||
|
||||
use crate::{fields::fp::FpVar, prelude::*, Assignment, ToConstraintFieldGadget, Vec};
|
||||
use ark_relations::r1cs::{
|
||||
@@ -396,7 +396,7 @@ impl<F: Field> Boolean<F> {
|
||||
// a XOR (NOT b) = NOT(a XOR b)
|
||||
(is @ &Is(_), not @ &Not(_)) | (not @ &Not(_), is @ &Is(_)) => {
|
||||
Ok(is.xor(¬.not())?.not())
|
||||
}
|
||||
},
|
||||
// a XOR b = (NOT a) XOR (NOT b)
|
||||
(&Is(ref a), &Is(ref b)) | (&Not(ref a), &Not(ref b)) => Ok(Is(a.xor(b)?)),
|
||||
}
|
||||
@@ -438,7 +438,7 @@ impl<F: Field> Boolean<F> {
|
||||
// a OR b = NOT ((NOT a) AND (NOT b))
|
||||
(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),
|
||||
}
|
||||
}
|
||||
@@ -604,7 +604,7 @@ impl<F: Field> Boolean<F> {
|
||||
Is(_) | Not(_) => {
|
||||
r.cs()
|
||||
.enforce_constraint(r.lc(), lc!() + Variable::One, lc!() + Variable::One)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -651,7 +651,7 @@ impl<F: Field> Boolean<F> {
|
||||
// If the number of bits is less than the size of the field,
|
||||
// then we do not need to enforce that the element is less than
|
||||
// the modulus.
|
||||
if bits.len() >= F::Params::MODULUS_BITS as usize {
|
||||
if bits.len() >= F::MODULUS_BIT_SIZE as usize {
|
||||
Self::enforce_in_field_le(bits)?;
|
||||
}
|
||||
Ok(crate::fields::fp::AllocatedFp::new(value, variable, cs.clone()).into())
|
||||
@@ -946,7 +946,7 @@ impl<F: Field> CondSelectGadget<F> for Boolean<F> {
|
||||
)?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1247,30 +1247,30 @@ mod test {
|
||||
(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,
|
||||
@@ -1278,18 +1278,18 @@ mod test {
|
||||
) => {
|
||||
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,
|
||||
@@ -1297,7 +1297,7 @@ mod test {
|
||||
) => {
|
||||
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
|
||||
assert_eq!(v.value(), Ok(false));
|
||||
}
|
||||
},
|
||||
(
|
||||
OpType::NegatedAllocatedTrue,
|
||||
OpType::NegatedAllocatedFalse,
|
||||
@@ -1305,14 +1305,14 @@ mod test {
|
||||
) => {
|
||||
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,
|
||||
@@ -1320,7 +1320,7 @@ mod test {
|
||||
) => {
|
||||
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
|
||||
assert_eq!(v.value(), Ok(false));
|
||||
}
|
||||
},
|
||||
(
|
||||
OpType::NegatedAllocatedFalse,
|
||||
OpType::NegatedAllocatedTrue,
|
||||
@@ -1328,7 +1328,7 @@ mod test {
|
||||
) => {
|
||||
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::one());
|
||||
assert_eq!(v.value(), Ok(true));
|
||||
}
|
||||
},
|
||||
(
|
||||
OpType::NegatedAllocatedFalse,
|
||||
OpType::NegatedAllocatedFalse,
|
||||
@@ -1336,7 +1336,7 @@ mod test {
|
||||
) => {
|
||||
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
|
||||
assert_eq!(v.value(), Ok(false));
|
||||
}
|
||||
},
|
||||
|
||||
_ => unreachable!(),
|
||||
}
|
||||
@@ -1409,85 +1409,85 @@ mod test {
|
||||
(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 = {:?})",
|
||||
@@ -1531,49 +1531,49 @@ mod test {
|
||||
(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,
|
||||
@@ -1581,7 +1581,7 @@ mod test {
|
||||
) => {
|
||||
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
|
||||
assert_eq!(v.value(), Ok(false));
|
||||
}
|
||||
},
|
||||
(
|
||||
OpType::NegatedAllocatedTrue,
|
||||
OpType::NegatedAllocatedFalse,
|
||||
@@ -1589,18 +1589,18 @@ mod test {
|
||||
) => {
|
||||
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,
|
||||
@@ -1608,7 +1608,7 @@ mod test {
|
||||
) => {
|
||||
assert_eq!(cs.assigned_value(v.variable()).unwrap(), Fr::zero());
|
||||
assert_eq!(v.value(), Ok(false));
|
||||
}
|
||||
},
|
||||
(
|
||||
OpType::NegatedAllocatedFalse,
|
||||
OpType::NegatedAllocatedFalse,
|
||||
@@ -1616,14 +1616,14 @@ mod test {
|
||||
) => {
|
||||
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
|
||||
);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1642,9 +1642,9 @@ mod test {
|
||||
|
||||
let cs = ConstraintSystem::<Fr>::new_ref();
|
||||
|
||||
let native_bits: Vec<_> = BitIteratorLE::new(r.into_repr()).collect();
|
||||
let native_bits: Vec<_> = BitIteratorLE::new(r.into_bigint()).collect();
|
||||
let bits = Vec::new_witness(cs.clone(), || Ok(native_bits))?;
|
||||
Boolean::enforce_smaller_or_equal_than_le(&bits, s.into_repr())?;
|
||||
Boolean::enforce_smaller_or_equal_than_le(&bits, s.into_bigint())?;
|
||||
|
||||
assert!(cs.is_satisfied().unwrap());
|
||||
}
|
||||
@@ -1658,11 +1658,11 @@ mod test {
|
||||
let s2 = r.double();
|
||||
let cs = ConstraintSystem::<Fr>::new_ref();
|
||||
|
||||
let native_bits: Vec<_> = BitIteratorLE::new(r.into_repr()).collect();
|
||||
let native_bits: Vec<_> = BitIteratorLE::new(r.into_bigint()).collect();
|
||||
let bits = Vec::new_witness(cs.clone(), || Ok(native_bits))?;
|
||||
Boolean::enforce_smaller_or_equal_than_le(&bits, s.into_repr())?;
|
||||
Boolean::enforce_smaller_or_equal_than_le(&bits, s.into_bigint())?;
|
||||
if r < s2 {
|
||||
Boolean::enforce_smaller_or_equal_than_le(&bits, s2.into_repr())?;
|
||||
Boolean::enforce_smaller_or_equal_than_le(&bits, s2.into_bigint())?;
|
||||
}
|
||||
|
||||
assert!(cs.is_satisfied().unwrap());
|
||||
@@ -1693,7 +1693,7 @@ mod test {
|
||||
let cs = ConstraintSystem::<Fr>::new_ref();
|
||||
|
||||
let mut bits = vec![];
|
||||
for b in BitIteratorBE::new(r.into_repr()).skip(1) {
|
||||
for b in BitIteratorBE::new(r.into_bigint()).skip(1) {
|
||||
bits.push(Boolean::new_witness(cs.clone(), || Ok(b))?);
|
||||
}
|
||||
bits.reverse();
|
||||
@@ -1796,7 +1796,7 @@ mod test {
|
||||
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 = BitIteratorLE::new(f.into_bigint()).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)?;
|
||||
@@ -1806,7 +1806,7 @@ mod test {
|
||||
|
||||
for _ in 0..1000 {
|
||||
let f = Fr::from(u64::rand(rng));
|
||||
let bits = BitIteratorLE::new(f.into_repr()).collect::<Vec<_>>();
|
||||
let bits = BitIteratorLE::new(f.into_bigint()).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)?;
|
||||
|
||||
@@ -6,7 +6,7 @@ macro_rules! make_uint {
|
||||
#[doc = $native_doc_name]
|
||||
#[doc = " type."]
|
||||
pub mod $mod_name {
|
||||
use ark_ff::{Field, FpParameters, One, PrimeField, Zero};
|
||||
use ark_ff::{Field, One, PrimeField, Zero};
|
||||
use core::borrow::Borrow;
|
||||
use core::convert::TryFrom;
|
||||
use num_bigint::BigUint;
|
||||
@@ -103,17 +103,17 @@ macro_rules! make_uint {
|
||||
match *b {
|
||||
Boolean::Constant(b) => {
|
||||
value.as_mut().map(|v| *v |= $native::from(b));
|
||||
}
|
||||
},
|
||||
Boolean::Is(ref b) => match b.value() {
|
||||
Ok(b) => {
|
||||
value.as_mut().map(|v| *v |= $native::from(b));
|
||||
}
|
||||
},
|
||||
Err(_) => value = None,
|
||||
},
|
||||
Boolean::Not(ref b) => match b.value() {
|
||||
Ok(b) => {
|
||||
value.as_mut().map(|v| *v |= $native::from(!b));
|
||||
}
|
||||
},
|
||||
Err(_) => value = None,
|
||||
},
|
||||
}
|
||||
@@ -171,13 +171,13 @@ macro_rules! make_uint {
|
||||
{
|
||||
// Make some arbitrary bounds for ourselves to avoid overflows
|
||||
// in the scalar field
|
||||
assert!(F::Params::MODULUS_BITS >= 2 * $size);
|
||||
assert!(F::MODULUS_BIT_SIZE >= 2 * $size);
|
||||
|
||||
// Support up to 128
|
||||
assert!($size <= 128);
|
||||
|
||||
assert!(operands.len() >= 1);
|
||||
assert!($size * operands.len() <= F::Params::MODULUS_BITS as usize);
|
||||
assert!($size * operands.len() <= F::MODULUS_BIT_SIZE as usize);
|
||||
|
||||
if operands.len() == 1 {
|
||||
return Ok(operands[0].clone());
|
||||
@@ -202,13 +202,13 @@ macro_rules! make_uint {
|
||||
match op.value {
|
||||
Some(val) => {
|
||||
result_value.as_mut().map(|v| *v += BigUint::from(val));
|
||||
}
|
||||
},
|
||||
|
||||
None => {
|
||||
// If any of our operands have unknown value, we won't
|
||||
// know the value of the result
|
||||
result_value = None;
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// Iterate over each bit_gadget of the operand and add the operand to
|
||||
@@ -221,18 +221,18 @@ macro_rules! make_uint {
|
||||
|
||||
// Add coeff * bit_gadget
|
||||
lc += (coeff, bit.variable());
|
||||
}
|
||||
},
|
||||
Boolean::Not(ref bit) => {
|
||||
all_constants = false;
|
||||
|
||||
// Add coeff * (1 - bit_gadget) = coeff * ONE - coeff * bit_gadget
|
||||
lc = lc + (coeff, Variable::One) - (coeff, bit.variable());
|
||||
}
|
||||
},
|
||||
Boolean::Constant(bit) => {
|
||||
if bit {
|
||||
lc += (coeff, Variable::One);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
coeff.double_in_place();
|
||||
@@ -407,7 +407,7 @@ macro_rules! make_uint {
|
||||
match bit {
|
||||
&Boolean::Constant(bit) => {
|
||||
assert_eq!(bit, ((b.value()? >> i) & 1 == 1));
|
||||
}
|
||||
},
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
@@ -416,8 +416,8 @@ macro_rules! make_uint {
|
||||
|
||||
for x in v.iter().zip(expected_to_be_same.iter()) {
|
||||
match x {
|
||||
(&Boolean::Constant(true), &Boolean::Constant(true)) => {}
|
||||
(&Boolean::Constant(false), &Boolean::Constant(false)) => {}
|
||||
(&Boolean::Constant(true), &Boolean::Constant(true)) => {},
|
||||
(&Boolean::Constant(false), &Boolean::Constant(false)) => {},
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use ark_ff::{Field, FpParameters, PrimeField, ToConstraintField};
|
||||
use ark_ff::{Field, PrimeField, ToConstraintField};
|
||||
|
||||
use ark_relations::r1cs::{ConstraintSystemRef, Namespace, SynthesisError};
|
||||
|
||||
@@ -152,7 +152,7 @@ impl<F: Field> UInt8<F> {
|
||||
let values_len = values.len();
|
||||
let field_elements: Vec<F> = ToConstraintField::<F>::to_field_elements(values).unwrap();
|
||||
|
||||
let max_size = 8 * (F::Params::CAPACITY / 8) as usize;
|
||||
let max_size = 8 * ((F::MODULUS_BIT_SIZE - 1) / 8) as usize;
|
||||
let mut allocated_bits = Vec::new();
|
||||
for field_element in field_elements.into_iter() {
|
||||
let fe = AllocatedFp::new_input(cs.clone(), || Ok(field_element))?;
|
||||
@@ -335,7 +335,7 @@ impl<ConstraintF: Field> AllocVar<u8, ConstraintF> for UInt8<ConstraintF> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses the `Vec<UInt8<ConstraintF>>` in fixed-sized `ConstraintF::Params::CAPACITY` chunks and
|
||||
/// Parses the `Vec<UInt8<ConstraintF>>` in fixed-sized `ConstraintF::MODULUS_BIT_SIZE - 1` chunks and
|
||||
/// converts each chunk, which is assumed to be little-endian, to its `FpVar<ConstraintF>`
|
||||
/// representation.
|
||||
/// This is the gadget counterpart to the `[u8]` implementation of
|
||||
@@ -343,7 +343,7 @@ impl<ConstraintF: Field> AllocVar<u8, ConstraintF> for UInt8<ConstraintF> {
|
||||
impl<ConstraintF: PrimeField> ToConstraintFieldGadget<ConstraintF> for [UInt8<ConstraintF>] {
|
||||
#[tracing::instrument(target = "r1cs")]
|
||||
fn to_constraint_field(&self) -> Result<Vec<FpVar<ConstraintF>>, SynthesisError> {
|
||||
let max_size = (ConstraintF::Params::CAPACITY / 8) as usize;
|
||||
let max_size = ((ConstraintF::MODULUS_BIT_SIZE - 1) / 8) as usize;
|
||||
self.chunks(max_size)
|
||||
.map(|chunk| Boolean::le_bits_to_fp_var(chunk.to_bits_le()?.as_slice()))
|
||||
.collect::<Result<Vec<_>, SynthesisError>>()
|
||||
@@ -363,7 +363,7 @@ mod test {
|
||||
use crate::fields::fp::FpVar;
|
||||
use crate::prelude::AllocationMode::{Constant, Input, Witness};
|
||||
use crate::{prelude::*, ToConstraintFieldGadget, Vec};
|
||||
use ark_ff::{FpParameters, PrimeField, ToConstraintField};
|
||||
use ark_ff::{PrimeField, ToConstraintField};
|
||||
use ark_relations::r1cs::{ConstraintSystem, SynthesisError};
|
||||
use ark_std::rand::distributions::Uniform;
|
||||
use ark_std::rand::Rng;
|
||||
@@ -426,8 +426,8 @@ mod test {
|
||||
|
||||
for x in v.iter().zip(expected_to_be_same.iter()) {
|
||||
match x {
|
||||
(&Boolean::Constant(true), &Boolean::Constant(true)) => {}
|
||||
(&Boolean::Constant(false), &Boolean::Constant(false)) => {}
|
||||
(&Boolean::Constant(true), &Boolean::Constant(true)) => {},
|
||||
(&Boolean::Constant(false), &Boolean::Constant(false)) => {},
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
@@ -475,7 +475,7 @@ mod test {
|
||||
#[test]
|
||||
fn test_uint8_to_constraint_field() -> Result<(), SynthesisError> {
|
||||
let mut rng = ark_std::test_rng();
|
||||
let max_size = (<Fr as PrimeField>::Params::CAPACITY / 8) as usize;
|
||||
let max_size = ((<Fr as PrimeField>::MODULUS_BIT_SIZE - 1) / 8) as usize;
|
||||
|
||||
let modes = [Input, Witness, Constant];
|
||||
for mode in &modes {
|
||||
|
||||
Reference in New Issue
Block a user