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: Ring> BoolVar<'ctx, F>
impl<'ctx, F: Ring> BoolVar<'ctx, F>
Sourcepub fn constant(value: bool) -> Self
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§impl<'ctx, F: Ring + PartialEq> BoolVar<'ctx, F>
impl<'ctx, F: Ring + PartialEq> BoolVar<'ctx, F>
Sourcepub fn witness(
ctx: Context<'ctx, F>,
init: impl for<'a> FnOnce(Values<'a, F>) -> bool,
) -> Self
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.
Sourcepub fn assert(var: Var<'ctx, F>) -> Self
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.
Trait Implementations§
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>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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