Index Mathematics Conjectures nbv lettuce Widgets

one_hot_bits

stdlib/bits/one_hot_bits.nbv

one_hot_bits(bits, scale)

One-hot encoding of the value of little-endian bits, scaled by scale. Returns a vector of length 2^(len! bits) with scale at the index equal to the value of the bits and 0 elsewhere. scale is usually 1 or a bit that gates the encoding. Costs 2^(len! bits) - 1 multiplications.

Calls
bits/and_bits
Precompiles
len!, loop!
Used by
mldsa/mldsa_hint_unpack, mldsa/mldsa_sample_in_ball

Source

# One-hot encoding of the value of little-endian `bits`, scaled by `scale`.
# Returns a vector of length 2^(len! bits) with `scale` at the index equal to
# the value of the bits and 0 elsewhere. `scale` is usually 1 or a bit that
# gates the encoding. Costs 2^(len! bits) - 1 multiplications.
(bits, scale)

static n = len! bits
static mut total = 1
loop! n {
  total = total * 2
}
let mut out[total]
out[0] = scale
static mut size = 1
loop! n, b {
  loop! size, j {
    let hi = and_bits(out[j], bits[b])
    out[j + size] = hi
    out[j] = out[j] - hi
  }
  size = size * 2
}
out