Skip to main content

Module circuit

Module circuit 

Source
Expand description

Utilities for creating arithmetic circuits.

A Circuit holds the additions and multiplications making up a computation over a ring F, along with assertions that two values in the computation are equal. The inputs to the computation are constants, or witnesses, whose values are chosen by the prover. Proof systems consume circuits to prove that the assertions hold, without revealing the witnesses.

Circuits are built by writing plain Rust over Var, which implements the algebra traits from commonware_math. This allows the same code to be generic over F and Var<F>. The building code runs in one of two modes:

  • build records only the circuit itself (verifier mode),
  • build_with_values also computes every value in the computation as the circuit is constructed (prover mode).

Because both modes run the same code, the prover and the verifier construct the same circuit.

§Example

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

// Constrain a witness `x` to satisfy `x^3 + x + 5 = 35`.
let (valued, _) = build_with_values(|ctx| {
    let x = Var::witness(ctx, |_| F::from(3u64));
    let out = x.clone() * &x * &x + &x + &Var::constant(ctx, F::from(5u64));
    out.assert_eq(&Var::constant(ctx, F::from(35u64)));
    Vec::new()
});
assert!(valued.is_satisfied());

§Caveats

§Witness Closures

The init closure passed to Var::witness must not use the Context, for example by creating new vars: the build deadlocks, hanging without an error. Compute the witness value using only the Values view the closure receives.

§Inversion

Field::inv requires that inverting zero produce zero. Circuit-backed vars deviate: inverting zero adds an unsatisfiable constraint instead, with no error when building. Generic code relying on inv(0) = 0 will produce circuits that can never be satisfied.

Structs§

BoolVar
A circuit value constrained to be 0 or 1.
Circuit
An arithmetic circuit over F.
Context
A handle for recording operations into a circuit being built.
Selector
A tool for selecting among multiple items.
ValuedCircuit
A circuit together with concrete values for its whole computation.
Values
A view of the values assigned so far during prover-mode construction.
Var
A value in a circuit being built.

Enums§

CircuitIdx
Identifies a value in a Circuit: a constant, a witness, or the output of an operation.

Functions§

build
Build a circuit without computing an assignment (verifier mode).
build_with_values
Build a circuit while simultaneously computing the assignment (prover mode).