Index Mathematics Conjectures nbv lettuce Widgets

typed_bytes_lattice

stdlib/typed/typed_bytes_lattice.nbv

typed_bytes_lattice(bytes, static types, static lens, static a_absorb)

Lattice sponge hash of the typed byte string bytes with the static schema types and lens, after constraining its structure with typed_bytes_check: dags LatticeSponge<H> with append_bytes(bytes) then squeeze_safe(), returning lattice_output_len elements of the field's Poseidon permutation. a_absorb is the absorption matrix of the sponge (LatticeSponge::<H>::matrices().0, row-major).

The bytes are packed as dags FieldScalar::from_unsafe_byte_stream: the byte length as an element, then bpe = ceil(e / 8) - 1 little-endian bytes per element (3 in Milli, 7 in Mersenne61), the last zero padded. Packed elements are below 2^(8 bpe), so their sponge bits are the constrained byte bits followed by zeros and absorbing them costs no extra rows; the cost is 8 rows per byte, the state decomposition per block of lattice_rate elements and one permutation.

Calls
calloc, lattice/lattice_bits_static, dilithium/lattice/lattice_kappa, dilithium/lattice/lattice_output_len, dilithium/lattice/lattice_permutation, dilithium/lattice/lattice_t, slice, slice_from_start, typed/typed_bytes_check, typed/typed_lattice_digest_bits
Precompiles
assert_lt!, clone!, concat!, div_ceil!, field_bit_width!, len!, loop_flat_map!, wtns!
Used by
typed/typed_lex_chacha

Source

# Lattice sponge hash of the typed byte string `bytes` with the static schema
# `types` and `lens`, after constraining its structure with
# typed_bytes_check: dags `LatticeSponge<H>` with `append_bytes(bytes)` then
# `squeeze_safe()`, returning `lattice_output_len` elements of the field's
# Poseidon permutation. `a_absorb` is the absorption matrix of the sponge
# (`LatticeSponge::<H>::matrices().0`, row-major).
#
# The bytes are packed as dags `FieldScalar::from_unsafe_byte_stream`: the
# byte length as an element, then `bpe = ceil(e / 8) - 1` little-endian bytes
# per element (3 in Milli, 7 in Mersenne61), the last zero padded. Packed
# elements are below 2^(8 bpe), so their sponge bits are the constrained byte
# bits followed by zeros and absorbing them costs no extra rows; the cost is
# 8 rows per byte, the state decomposition per block of `lattice_rate`
# elements and one permutation.
(bytes, static types, static lens, static a_absorb)

static n = len! bytes
assert_lt! 0, n
static e = field_bit_width!
static bpe = (div_ceil! e, 8) - 1
static chunks = div_ceil! n, bpe
static count = chunks + 1

let bits = typed_bytes_check(bytes, types, lens)
let data_bits = concat! bits, calloc(8 * (bpe * chunks - n))
let packed_bits = loop_flat_map! chunks, j {
  concat! slice((clone! data_bits), 8 * bpe * j, 8 * bpe), calloc(e - 8 * bpe)
}
let state = typed_lattice_digest_bits((concat! lattice_bits_static(n), packed_bits), a_absorb)

static kappa = lattice_kappa()
static out_len = lattice_output_len()
static t = lattice_t()
let perm = lattice_permutation(concat! calloc(out_len), state, [count], calloc(t - out_len - kappa - 1))
wtns! slice_from_start(perm, out_len)