Skip to main content
Version: dev

Assert Function

Noir includes a special assert function which will explicitly constrain the predicate/comparison expression that follows to be true. If this expression is false at runtime, the program will fail to be proven. Example:

fn main(x : Field, y : Field) {
assert(x == y);
}

Assertions only work for predicate operations, such as ==. If there's any ambiguity on the operation, the program will fail to compile. For example, it is unclear if assert(x + y) would check for x + y == 0 or simply would return true.

You can optionally provide a message to be logged when the assertion fails:

assert(x == y, "x and y are not equal");

Aside string literals, the optional message can be a format string or any other type supported as input for Noir's print functions. This feature lets you incorporate runtime variables into your failed assertion logs:

assert(x == y, f"Expected x == y, but got {x} == {y}");

Using a variable as an assertion message directly:

struct myStruct {
myField: Field
}

let s = myStruct { myField: y };
assert(s.myField == x, s);