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

pub enum Boolean<F: Field> {
    Is(AllocatedBool<F>),
    Not(AllocatedBool<F>),
    Constant(bool),
}
Expand description

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

Variants

Existential view of the boolean variable.

Negated view of the boolean variable.

Constant(bool)

Constant (not an allocated variable).

Implementations

The constant true.

The constant false.

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()

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());

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))?;

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());

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());

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());

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());

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());

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());

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());

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

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() .

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

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

Allocates a new variable of type Self in the ConstraintSystem cs. The mode of allocation is decided by mode. Read more

Allocates a new constant of type Self in the ConstraintSystem cs. Read more

Allocates a new public input of type Self in the ConstraintSystem cs. Read more

Allocates a new private witness of type Self in the ConstraintSystem cs. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

If cond == &Boolean::TRUE, then this returns true_value; else, returns false_value. Read more

Returns an element of values whose index in represented by position. position is an array of boolean that represents an unsigned integer in big endian order. Read more

Formats the value using the given formatter. Read more

Output a Boolean value representing whether self.value() == other.value(). Read more

If should_enforce == true, enforce that self and other are equal; else, enforce a vacuously true statement. Read more

If should_enforce == true, enforce that self and other are not equal; else, enforce a vacuously true statement. Read more

Output a Boolean value representing whether self.value() != other.value(). Read more

Enforce that self and other are equal. Read more

Enforce that self and other are not equal. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

Returns the underlying ConstraintSystemRef. Read more

Returns the value that is assigned to self in the underlying ConstraintSystem. Read more

Returns true if self is a circuit-generation-time constant.

Outputs the canonical little-endian bit-wise representation of self. Read more

Outputs a possibly non-unique little-endian bit-wise representation of self. Read more

Outputs the canonical big-endian bit-wise representation of self.

Outputs a possibly non-unique big-endian bit-wise representation of self. Read more

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

Outputs a possibly non-unique byte decomposition of self. Read more

Converts self to FpVar<ConstraintF> variables.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.