Index Mathematics Conjectures nbv lettuce Widgets

len!

The length of a named variable, as a static scalar. Vectors

len! name

Returns the number of elements of a variable. Every vector length is known at compile time, so the result is always static, even for witness variables, and can be used for loop counts, slice bounds and vector definitions.

Arguments

name
A named variable, static or witness. Any other expression (a literal, a call, an indexed value) is an error. The variable is only inspected, not moved.

Returns

A static scalar holding the length. len! is an expression.

Constraints

None.

Example

(let x)
static n = len! x
# x has not been moved by len!
(n, x)

Notes

len! never moves its argument: it reads the variable's length from the scope without taking it, in every position (assignments, loop counts, function and precompile arguments).

A precompile takes the whole expression after it as its argument, so len! x - 1 parses as len!(x - 1) and fails with "argument should be a named variable". Write (len! x) - 1. For the same reason, inside another precompile's comma separated arguments write assert_eq! (len! a), (len! b). To take the length of an expression, assign it to a variable first.

(let x)
static n = len! x - 1
x * x
(let x)
static n = (len! x) - 1
let m = n
m

Used in the standard library

bdlop/bdlop_linear_nizk_verify, bdlop/bdlop_r1cs_verify, bits/add32_bits, bits/bytes_to_bits, bits/bytes_to_bits_static, bits/from_bits, bits/from_bits_static, bits/lt_bits, bits/lte_bits, bits/lte_negone, bits/one_hot_bits, bits/rotl32_bits, bits/to_bits, bits/to_bits_exact, bits/to_bits_exact_static, bits/xor_bits_vec_wtns, blake3/blake3_compress, blake3/blake3_core, blake3/blake3_g, blake3/blake3_rotate_rows, blake3/blake3_rotate_words, chacha/chacha_block, chacha/chacha_quarter_round, copy_into, dilithium/lattice/lattice_permutation, 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, ext/ext_eq_eval, ext/ext_eq_table, ext/ext_from_base, ext/ext_mle_eval, ext/ext_mul, ext/ext_mul_base, ext/ext_poly_eval, ext/ext_recompose, ext/ext_sum, ext/ext_vec_mul, ext/mle_eq_table, extend_to, inner_prod, is_zero, keccak/keccak_f1600, keccak/keccak_sponge_shake, lattice/lattice_absorb, lattice/lattice_assert_canonical, lattice/lattice_bits, lattice/lattice_bits_vec, lattice/lattice_digest, lattice/lattice_expand, lattice/lattice_squeeze, mat_transpose, mat_vec_mul, mersenne61/mldsa/mldsa_intt, mersenne61/mldsa/mldsa_ntt_q, mersenne61/mldsa/mldsa_pointwise, mersenne61/poseidon/poseidon_mersenne61, mersenne61/poseidon/poseidon_mersenne61_state, milli/mldsa/mldsa_ct1, milli/mldsa/mldsa_intt_sub, milli/mldsa/mldsa_ntt_z, milli/mldsa/mldsa_pointwise, milli/mldsa/mldsa_reduce, milli/poseidon/poseidon_milli, milli/poseidon/poseidon_milli_state, 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_limbs, mldsa/mldsa_sample_in_ball, mldsa/mldsa_use_hint, oraccle/oraccle_commit, oraccle/oraccle_sample_ternary_vec, oraccle/oraccle_squeeze, polynomial/bit_reverse_indices, polynomial/intt_negacyclic, polynomial/intt_negacyclic_vec, polynomial/mat_poly_compose_horizontal, polynomial/mat_poly_compose_vertical, polynomial/mat_poly_mul_vec, polynomial/ntt, polynomial/ntt_negacyclic, polynomial/ntt_negacyclic_mat, polynomial/ntt_negacyclic_vec, polynomial/poly_norm_inf, polynomial/poly_norm_inf_lt, polynomial/poly_norm_l1, polynomial/polynomial_coef_mul, polynomial/vec_poly_scalar_mul, polynomial/vec_polynomial_coef_mul, pow, r1cs/r1cs_assert_field, r1cs/r1cs_compose_vertical, r1cs/r1cs_from_output, r1cs/r1cs_from_vec, reverse_static, shift, size_cyclic, slice_to_end, slip/slip_mask_transcript, slip/slip_placements, slip/slip_r1cs_arg_verify, split_at, sym_mat_index, typed/typed_bytes_check, typed/typed_bytes_lattice, typed/typed_lattice_digest_bits, typed/typed_lex_chacha, typed/typed_lex_check