Enum ark_r1cs_std::bits::boolean::Boolean[][src]

#[must_use]pub enum Boolean<F: Field> {
    Is(AllocatedBit<F>),
    Not(AllocatedBit<F>),
    Constant(bool),
}

Represents a boolean value in the constraint system which is guaranteed to be either zero or one.

Variants

Existential view of the boolean variable.

Not(AllocatedBit<F>)

Negated view of the boolean variable.

Constant(bool)

Constant (not an allocated variable).

Implementations

impl<F: Field> Boolean<F>[src]

pub const TRUE: Self[src]

The constant true.

pub const FALSE: Self[src]

The constant false.

pub fn lc(&self) -> LinearCombination<F>[src]

Constructs a LinearCombination from Self’s variables according to the following map.

  • Boolean::Constant(true) => lc!() + Variable::One
  • Boolean::Constant(false) => lc!()
  • Boolean::Is(v) => lc!() + v.variable()
  • Boolean::Not(v) => lc!() + Variable::One - v.variable()

pub fn constant_vec_from_bytes(values: &[u8]) -> Vec<Self>[src]

Constructs a Boolean vector from a slice of constant u8. The u8s are decomposed in little-endian manner.

This does not create any new variables or constraints.

// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;

let cs = ConstraintSystem::<Fr>::new_ref();
let t = Boolean::<Fr>::TRUE;
let f = Boolean::<Fr>::FALSE;

let bits = vec![f, t];
let generated_bits = Boolean::constant_vec_from_bytes(&[2]);
bits[..2].enforce_equal(&generated_bits[..2])?;
assert!(cs.is_satisfied().unwrap());

pub fn constant(b: bool) -> Self[src]

Constructs a constant Boolean with value b.

This does not create any new variables or constraints.

// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_r1cs_std::prelude::*;

let true_var = Boolean::<Fr>::TRUE;
let false_var = Boolean::<Fr>::FALSE;

true_var.enforce_equal(&Boolean::constant(true))?;
false_var.enforce_equal(&Boolean::constant(false))?;

pub fn not(&self) -> Self[src]

Negates self.

This does not create any new variables or constraints.

// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;

let cs = ConstraintSystem::<Fr>::new_ref();

let a = Boolean::new_witness(cs.clone(), || Ok(true))?;
let b = Boolean::new_witness(cs.clone(), || Ok(false))?;

a.not().enforce_equal(&b)?;
b.not().enforce_equal(&a)?;

a.not().enforce_equal(&Boolean::FALSE)?;
b.not().enforce_equal(&Boolean::TRUE)?;

assert!(cs.is_satisfied().unwrap());

pub fn xor<'a>(&'a self, other: &'a Self) -> Result<Self, SynthesisError>[src]

Outputs self ^ other.

If at least one of self and other are constants, then this method does not create any constraints or variables.

// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;

let cs = ConstraintSystem::<Fr>::new_ref();

let a = Boolean::new_witness(cs.clone(), || Ok(true))?;
let b = Boolean::new_witness(cs.clone(), || Ok(false))?;

a.xor(&b)?.enforce_equal(&Boolean::TRUE)?;
b.xor(&a)?.enforce_equal(&Boolean::TRUE)?;

a.xor(&a)?.enforce_equal(&Boolean::FALSE)?;
b.xor(&b)?.enforce_equal(&Boolean::FALSE)?;

assert!(cs.is_satisfied().unwrap());

pub fn or<'a>(&'a self, other: &'a Self) -> Result<Self, SynthesisError>[src]

Outputs self | other.

If at least one of self and other are constants, then this method does not create any constraints or variables.

// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;

let cs = ConstraintSystem::<Fr>::new_ref();

let a = Boolean::new_witness(cs.clone(), || Ok(true))?;
let b = Boolean::new_witness(cs.clone(), || Ok(false))?;

a.or(&b)?.enforce_equal(&Boolean::TRUE)?;
b.or(&a)?.enforce_equal(&Boolean::TRUE)?;

a.or(&a)?.enforce_equal(&Boolean::TRUE)?;
b.or(&b)?.enforce_equal(&Boolean::FALSE)?;

assert!(cs.is_satisfied().unwrap());

pub fn and<'a>(&'a self, other: &'a Self) -> Result<Self, SynthesisError>[src]

Outputs self & other.

If at least one of self and other are constants, then this method does not create any constraints or variables.

// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;

let cs = ConstraintSystem::<Fr>::new_ref();

let a = Boolean::new_witness(cs.clone(), || Ok(true))?;
let b = Boolean::new_witness(cs.clone(), || Ok(false))?;

a.and(&a)?.enforce_equal(&Boolean::TRUE)?;

a.and(&b)?.enforce_equal(&Boolean::FALSE)?;
b.and(&a)?.enforce_equal(&Boolean::FALSE)?;
b.and(&b)?.enforce_equal(&Boolean::FALSE)?;

assert!(cs.is_satisfied().unwrap());

pub fn kary_and(bits: &[Self]) -> Result<Self, SynthesisError>[src]

Outputs bits[0] & bits[1] & ... & bits.last().unwrap().

// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;

let cs = ConstraintSystem::<Fr>::new_ref();

let a = Boolean::new_witness(cs.clone(), || Ok(true))?;
let b = Boolean::new_witness(cs.clone(), || Ok(false))?;
let c = Boolean::new_witness(cs.clone(), || Ok(true))?;

Boolean::kary_and(&[a.clone(), b.clone(), c.clone()])?.enforce_equal(&Boolean::FALSE)?;
Boolean::kary_and(&[a.clone(), c.clone()])?.enforce_equal(&Boolean::TRUE)?;

assert!(cs.is_satisfied().unwrap());

pub fn kary_or(bits: &[Self]) -> Result<Self, SynthesisError>[src]

Outputs bits[0] | bits[1] | ... | bits.last().unwrap().

// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;

let cs = ConstraintSystem::<Fr>::new_ref();

let a = Boolean::new_witness(cs.clone(), || Ok(true))?;
let b = Boolean::new_witness(cs.clone(), || Ok(false))?;
let c = Boolean::new_witness(cs.clone(), || Ok(false))?;

Boolean::kary_or(&[a.clone(), b.clone(), c.clone()])?.enforce_equal(&Boolean::TRUE)?;
Boolean::kary_or(&[a.clone(), c.clone()])?.enforce_equal(&Boolean::TRUE)?;
Boolean::kary_or(&[b.clone(), c.clone()])?.enforce_equal(&Boolean::FALSE)?;

assert!(cs.is_satisfied().unwrap());

pub fn kary_nand(bits: &[Self]) -> Result<Self, SynthesisError>[src]

Outputs (bits[0] & bits[1] & ... & bits.last().unwrap()).not().

// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;

let cs = ConstraintSystem::<Fr>::new_ref();

let a = Boolean::new_witness(cs.clone(), || Ok(true))?;
let b = Boolean::new_witness(cs.clone(), || Ok(false))?;
let c = Boolean::new_witness(cs.clone(), || Ok(true))?;

Boolean::kary_nand(&[a.clone(), b.clone(), c.clone()])?.enforce_equal(&Boolean::TRUE)?;
Boolean::kary_nand(&[a.clone(), c.clone()])?.enforce_equal(&Boolean::FALSE)?;
Boolean::kary_nand(&[b.clone(), c.clone()])?.enforce_equal(&Boolean::TRUE)?;

assert!(cs.is_satisfied().unwrap());

pub fn le_bits_to_fp_var(bits: &[Self]) -> Result<FpVar<F>, SynthesisError> where
    F: PrimeField
[src]

Convert a little-endian bitwise representation of a field element to FpVar<F>

pub fn enforce_in_field_le(bits: &[Self]) -> Result<(), SynthesisError>[src]

Enforces that bits, when interpreted as a integer, is less than F::characteristic(), That is, interpret bits as a little-endian integer, and enforce that this integer is “in the field Z_p”, where p = F::characteristic() .

pub fn enforce_smaller_or_equal_than_le<'a>(
    bits: &[Self],
    element: impl AsRef<[u64]>
) -> Result<Vec<Self>, SynthesisError>
[src]

Enforces that bits is less than or equal to element, when both are interpreted as (little-endian) integers.

pub fn select<T: CondSelectGadget<F>>(
    &self,
    first: &T,
    second: &T
) -> Result<T, SynthesisError>
[src]

Conditionally selects one of first and second based on the value of self:

If self.is_eq(&Boolean::TRUE), this outputs first; else, it outputs second.

// We'll use the BLS12-381 scalar field for our constraints.
use ark_test_curves::bls12_381::Fr;
use ark_relations::r1cs::*;
use ark_r1cs_std::prelude::*;

let cs = ConstraintSystem::<Fr>::new_ref();

let a = Boolean::new_witness(cs.clone(), || Ok(true))?;
let b = Boolean::new_witness(cs.clone(), || Ok(false))?;

let cond = Boolean::new_witness(cs.clone(), || Ok(true))?;

cond.select(&a, &b)?.enforce_equal(&Boolean::TRUE)?;
cond.select(&b, &a)?.enforce_equal(&Boolean::FALSE)?;

assert!(cs.is_satisfied().unwrap());

Trait Implementations

impl<F: Field> AllocVar<bool, F> for Boolean<F>[src]

impl<F: Clone + Field> Clone for Boolean<F>[src]

impl<F: Field> CondSelectGadget<F> for Boolean<F>[src]

impl<F: Debug + Field> Debug for Boolean<F>[src]

impl<F: Eq + Field> Eq for Boolean<F>[src]

impl<F: Field> EqGadget<F> for Boolean<F>[src]

impl<F: Field> From<AllocatedBit<F>> for Boolean<F>[src]

impl<BF, P> From<Boolean<<P as CubicExtParameters>::BasePrimeField>> for CubicExtVar<BF, P> where
    BF: FieldVar<P::BaseField, P::BasePrimeField>,
    &'a BF: FieldOpsBounds<'a, P::BaseField, BF>,
    P: CubicExtVarParams<BF>, 
[src]

impl<BF, P> From<Boolean<<P as QuadExtParameters>::BasePrimeField>> for QuadExtVar<BF, P> where
    BF: FieldVar<P::BaseField, P::BasePrimeField>,
    &'a BF: FieldOpsBounds<'a, P::BaseField, BF>,
    P: QuadExtVarParams<BF>, 
[src]

impl<F: PrimeField> From<Boolean<F>> for FpVar<F>[src]

impl<F: PartialEq + Field> PartialEq<Boolean<F>> for Boolean<F>[src]

impl<F: Field> R1CSVar<F> for Boolean<F>[src]

type Value = bool

The type of the “native” value that Self represents in the constraint system. Read more

impl<F: Field> StructuralEq for Boolean<F>[src]

impl<F: Field> StructuralPartialEq for Boolean<F>[src]

impl<F: Field> ToBitsGadget<F> for Boolean<F>[src]

impl<F: Field> ToBytesGadget<F> for Boolean<F>[src]

fn to_bytes(&self) -> Result<Vec<UInt8<F>>, SynthesisError>[src]

Outputs 1u8 if self is true, and 0u8 otherwise.

impl<F: PrimeField> ToConstraintFieldGadget<F> for Boolean<F>[src]

Auto Trait Implementations

impl<F> !RefUnwindSafe for Boolean<F>

impl<F> !Send for Boolean<F>

impl<F> !Sync for Boolean<F>

impl<F> Unpin for Boolean<F>

impl<F> !UnwindSafe for Boolean<F>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,