loop!
Run a block a static number of times. Loops and mapping
loop! count[, i] { ... }
Unrolls the block count times at compile time. Each iteration runs in a fresh inner scope: variables defined in the block are discarded at the end of the iteration, while variables of the enclosing scopes can be read and, if declared mut, assigned. If a counter name is given it is bound to the iteration index, a static scalar from 0 to count - 1.
There is no data dependent control flow in nbv, the number of iterations is always known at compile time. See blocks.
Arguments
count- Static scalar (or a witness constant), the number of iterations.
i- Optional. A bare variable name bound to the static iteration index inside the block.
{ ... }- Required block. It may not end in a final expression.
Returns
Nothing, it is used as a statement (loop is in the grammar's statement list).
Constraints
None of its own; the block's operations add their constraints once per iteration. Behaves the same in the static VM.
Example
(let x) let mut acc = 0 loop! 3, i { acc = acc + x[i] * x[i] } acc
Using an outer vector by name moves it out of the outer scope, so the second iteration no longer finds it. Clone outer vectors that are used inside a loop:
(let x) let mut acc = 0 loop! 2 { acc = acc + sum(x) } acc
Notes
- A final expression in the block is an error (
loop blocks may not return a value); useloop_flat_map!orloop_map_sum!to build a value. - Every iteration interprets the block again. Element-by-element index writes in a
loop!are much slower to compile than whole-vector operations or the mapping precompiles, so prefer those for large vectors. shadow_loop!is the same loop without the inner scope.
Used in the standard library
bdlop/bdlop_linear_nizk_verify, bits/from_bits, bits/from_bits_static, bits/lt_bits, bits/lte_bits, bits/lte_negone, bits/one_hot_bits, bits/to_bits, bits/to_bits_exact, bits/to_bits_exact_static, blake3/blake3_compress, blake3/blake3_core, chacha/chacha_block, copy_into, dilithium/mldsa/mldsa44_verify_static, dilithium/mldsa/mldsa44_w_approx, dilithium/mldsa/mldsa44_w_approx_static, dilithium/mldsa/mldsa_intt_native, dilithium/mldsa/mldsa_ntt_native, dilithium/mldsa/mldsa_zeta_table_native, ext/ext_eq_eval, ext/ext_poly_eval, ext/ext_recompose, is_zero, keccak/keccak_f1600, keccak/keccak_sponge_shake, lattice/lattice_assert_canonical, lattice/lattice_digest, mat_transpose, mersenne61/mldsa/mldsa44_w_approx, mersenne61/mldsa/mldsa_intt, mersenne61/mldsa/mldsa_ntt_q, mersenne61/mldsa/mldsa_pointwise, milli/mldsa/mldsa44_w_approx, milli/mldsa/mldsa_ct1, milli/mldsa/mldsa_intt_sub, milli/mldsa/mldsa_ntt_z, milli/mldsa/mldsa_pointwise, milli/mldsa/mldsa_reduce, mldsa/mldsa44_finish, mldsa/mldsa44_parse, mldsa/mldsa44_parse_sig, mldsa/mldsa_assert_below_q, mldsa/mldsa_div_rem_q, mldsa/mldsa_expand_a, mldsa/mldsa_hint_unpack, mldsa/mldsa_rej_ntt_poly, mldsa/mldsa_sample_in_ball, mldsa/mldsa_use_hint, mldsa/mldsa_zeta_table, oraccle/oraccle_squeeze, polynomial/bit_reverse_indices, polynomial/ntt, polynomial/poly_norm_inf, polynomial/poly_norm_inf_lt, polynomial/poly_norm_l1, pow, r1cs/r1cs_assert_field, r1cs/r1cs_into_output, reverse_static, slip/slip_mask_transcript, slip/slip_placements, slip/slip_r1cs_arg_verify, typed/typed_bytes_check, typed/typed_lattice_digest_bits, typed/typed_lex_chacha, typed/typed_lex_check, typed/typed_varint_len, typed/typed_varint_static