Skip to main content

BoolVar

Struct BoolVar 

Source
pub struct BoolVar<'ctx, F> { /* private fields */ }
Expand description

A circuit value constrained to be 0 or 1.

A BoolVar wraps a Var together with a guarantee that it holds a boolean: every constructor either produces a value that is boolean by construction, or adds the constraint b * (1 - b) == 0 enforcing it. Holding that guarantee in the type lets later operations skip re-checking: Self::select and the boolean combinators below are sound precisely because their inputs are already known to be boolean.

The motivating use is scalar multiplication in a circuit, where a scalar is decomposed into bits (each a BoolVar) and a point is accumulated by conditionally adding with Self::select.

§Native Vars

Like Var, a BoolVar built from a native value (see Self::constant) lives outside the circuit until combined with a circuit-backed value. Self::assert on a native, non-boolean var therefore panics rather than recording an unsatisfiable constraint, in keeping with the native-var semantics described on Var.

§Example

use commonware_cryptography::zk::circuit::{build_with_values, BoolVar, Var};
use commonware_math::test::F;

// Use a bit to choose between two values, then check the choice.
let (valued, _) = build_with_values(|ctx| {
    let bit = BoolVar::witness(ctx, |_| true);
    let a = Var::constant(ctx, F::from(7u64));
    let b = Var::constant(ctx, F::from(9u64));
    bit.select(&a, &b).assert_eq(&Var::constant(ctx, F::from(7u64)));
    Vec::new()
});
assert!(valued.is_satisfied());

Implementations§

Source§

impl<'ctx, F> BoolVar<'ctx, F>

Source

pub const fn var(&self) -> &Var<'ctx, F>

The underlying Var, whose value is 0 or 1.

Source

pub fn into_var(self) -> Var<'ctx, F>

Consume this BoolVar, returning the underlying Var.

Source§

impl<'ctx, F: Ring> BoolVar<'ctx, F>

Source

pub fn constant(value: bool) -> Self

Create a boolean constant with a fixed, public value.

Like Var::native, the value lives outside the circuit until it is combined with a circuit-backed var. No constraint is added: the value is boolean by construction.

Source

pub fn assert_eq(&self, other: &Self)

Assert that this var equals other.

Source§

impl<'ctx, F: Ring + PartialEq> BoolVar<'ctx, F>

Source

pub fn witness( ctx: Context<'ctx, F>, init: impl for<'a> FnOnce(Values<'a, F>) -> bool, ) -> Self

Allocate a fresh boolean witness, constrained to be 0 or 1.

In prover mode, init receives the values assigned so far and must return the bit’s value. In verifier mode, init does not run. The constraint b * (1 - b) == 0 is added in both modes.

init must not use the Context: doing so deadlocks. See Values.

Source

pub fn assert(var: Var<'ctx, F>) -> Self

Constrain an arbitrary var to be boolean and wrap it.

Adds the constraint var * (1 - var) == 0, which holds exactly when var is 0 or 1.

§Panics

Panics if var is a native var whose value is not 0 or 1, since there is no circuit to record the failed constraint in. See Var::assert_eq.

Source§

impl<'ctx, F: Ring> BoolVar<'ctx, F>

Source

pub fn select( &self, on_true: &Var<'ctx, F>, on_false: &Var<'ctx, F>, ) -> Var<'ctx, F>

Select between two vars based on this bit.

Returns on_true when the bit is 1 and on_false when it is 0, computed as on_false + b * (on_true - on_false) with a single multiplication.

Trait Implementations§

Source§

impl<'ctx, F: Ring> BitAnd for BoolVar<'ctx, F>

Source§

type Output = BoolVar<'ctx, F>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl<'ctx, F: Ring> BitOr for BoolVar<'ctx, F>

Source§

type Output = BoolVar<'ctx, F>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl<'ctx, F: Clone> Clone for BoolVar<'ctx, F>

Source§

fn clone(&self) -> BoolVar<'ctx, F>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: Debug> Debug for BoolVar<'_, F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'ctx, F: Ring> Not for BoolVar<'ctx, F>

Source§

type Output = BoolVar<'ctx, F>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more

Auto Trait Implementations§

§

impl<'ctx, F> !RefUnwindSafe for BoolVar<'ctx, F>

§

impl<'ctx, F> !UnwindSafe for BoolVar<'ctx, F>

§

impl<'ctx, F> Freeze for BoolVar<'ctx, F>
where F: Freeze,

§

impl<'ctx, F> Send for BoolVar<'ctx, F>
where F: Send,

§

impl<'ctx, F> Sync for BoolVar<'ctx, F>
where F: Sync + Send,

§

impl<'ctx, F> Unpin for BoolVar<'ctx, F>
where F: Unpin,

§

impl<'ctx, F> UnsafeUnpin for BoolVar<'ctx, F>
where F: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more