Assertions

Establishing relationships and constraints between inputs, outputs and variables is key to building a ZK program.

assert :: Boolean -> Comp ()

To generate a constraint, use predicates like eq that produce Boolean results as the argument for assert. This will create constraints that are fulfilled when the predicate is true.

Predicates

Equality

The eq predicate returns true when the expressions on both sides of the equation evaluate to the same value.

eq :: Field   -> Field   -> Boolean
eq :: Boolean -> Boolean -> Boolean
eq :: UInt w  -> UInt w  -> Boolean

Inequality

Similar to eq, but returns the opposite result.

neq :: Field   -> Field   -> Boolean
neq :: Boolean -> Boolean -> Boolean
neq :: UInt w  -> UInt w  -> Boolean

Comparisons

Less than or equal to

Less than

Greater than or equal to

Greater than

Logical operations

Any functions that produces Boolean will do, here are some common built-in Boolean operators.

Conjunction

Disjunction

Exclusive disjunction

Complement

Examples

square

Here's another program that takes two inputs and asserts that the second input is the square of the first input:

Compiling this function would result in:

The resulting constraint system has two input variables, $0 and $1, and an additional constraint stating that $1 should be the square of $0, which is exactly what we want.

Note that because square returns () , it would generate no output variable when compiled.

Last updated