cbd function

void cbd(
  1. Poly r,
  2. Uint8List buf
)

Samples a polynomial from a uniform distribution using the Centered Binomial Distribution (CBD) as described in the Kyber specification.

The CBD samples a uniform distribution of polynomials with coefficients in the range -η, η by sampling a uniform distribution of two bytes, interpreting them as a 16-bit unsigned integer, and then extracting the bits of the integer to obtain the coefficients of the polynomial. The coefficients are then divided by 2 to obtain the final coefficients in the range -η/2, η/2.

Implementation

void cbd(Poly r, Uint8List buf) {
  // η=2 for Kyber512
  for (int i = 0; i < KYBER_N ~/ 8; i++) {
    int t = buf[2 * i] | (buf[2 * i + 1] << 8);
    for (int j = 0; j < 8; j++) {
      int aj = (t >> j) & 1;
      int bj = (t >> (j + 8)) & 1;
      r.coeffs[8 * i + j] = aj - bj;
    }
  }
}