arkworks-rs / snark

Interfaces for Relations and SNARKs for these relations
https://www.arkworks.rs
Apache License 2.0
770 stars 207 forks source link

ThreeBitCondNegLookupGadget for FpVar fails for constant bits #300

Closed weikengchen closed 3 years ago

weikengchen commented 3 years ago

When implementing ThreeBitCondNegLookupGadget, FpVar would invoke the implementation for constants (no CS needed), or the implementation for allocated bits. https://github.com/scipr-lab/zexe/blob/master/r1cs-std/src/fields/fp/mod.rs#L978

    fn three_bit_cond_neg_lookup(
        b: &[Boolean<F>],
        b0b1: &Boolean<F>,
        c: &[Self::TableConstant],
    ) -> Result<Self, SynthesisError> {
        debug_assert_eq!(b.len(), 3);
        debug_assert_eq!(c.len(), 4);

        if !b.cs().or(b0b1.cs()).is_none() {
            // We only have constants

            let lsb = usize::from(b[0].value()?);
            let msb = usize::from(b[1].value()?);
            let index = lsb + (msb << 1);
            let intermediate = c[index];

            let is_negative = b[2].value()?;
            let y = if is_negative {
                -intermediate
            } else {
                intermediate
            };
            Ok(Self::Constant(y))
        } else {
            AllocatedFp::three_bit_cond_neg_lookup(b, b0b1, c).map(Self::Var)
        }
    }

However, it seems that

if !b.cs().or(b0b1.cs()).is_none()

should be

if b.cs().or(b0b1.cs()).is_none()
weikengchen commented 3 years ago

This has been fixed in arkworks. Closed.