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.
Sourcepub fn select(
&self,
on_true: &Var<'ctx, F>,
on_false: &Var<'ctx, F>,
) -> Var<'ctx, F>
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.
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.