if_eq!
Run a block if two static values are equal. Control flow
if_eq! lhs, rhs { ... }
Compares two values known at compile time and, if they are equal, runs the block once in a fresh inner scope. Variables defined in the block are discarded afterwards; mut variables of the enclosing scopes can be assigned, which is how a value is chosen between branches. There is no else branch, pair it with if_ne!.
The condition is evaluated during compilation, so this selects between pieces of circuit, it cannot branch on witness values. See static evaluation.
Arguments
lhs,rhs- Static values or witness constants, scalars or vectors. Vectors are equal if they have the same length and elements; vectors of different lengths are simply unequal.
{ ... }- Required block. It may not end in a final expression.
Returns
Nothing, it is used as a statement.
Constraints
None of its own; the block's operations add theirs if it runs. Behaves the same in the static VM.
Example
(let x) static n = len! x let mut out = 0 if_eq! n, 3 { out = x[0] * x[1] } out
Notes
- A witness argument that is not a constant fails with
witness value is not a const. - A final expression in the block is an error (
if_eq blocks may not return a value).
Used in the standard library
bdlop/bdlop_milli_config, blake3/blake3_compress, blake3/blake3_core, lattice/lattice_assert_canonical, mersenne61/mldsa/mldsa_ntt_q, mersenne61/mldsa/mldsa_reduce, milli/mldsa/mldsa_reduce, polynomial/bit_reverse_indices, polynomial/mat_poly_iden, r1cs/r1cs_assert_field, slip/slip_placements, sym_mat_index, typed/typed_bytes_check, typed/typed_lex_chacha, typed/typed_lex_check, typed/typed_lex_width, typed/typed_width