polyuniform function

void polyuniform(
  1. Poly a,
  2. Uint8List seed,
  3. int nonce
)

Generates a uniform polynomial from a seed and nonce using SHAKE128.

Implementation

void polyuniform(Poly a, Uint8List seed, int nonce) {
  Uint8List extseed = Uint8List(KYBER_SYMBYTES + 2);
  for (int i = 0; i < KYBER_SYMBYTES; i++) {
    extseed[i] = seed[i];
  }
  extseed[KYBER_SYMBYTES] = nonce & 0xFF;
  extseed[KYBER_SYMBYTES + 1] = (nonce >> 8) & 0xFF;
  int ctr = 0;
  while (ctr < KYBER_N) {
    int needed = (KYBER_N - ctr) * 3;
    if (needed < 168) {
      needed = 168;
    }
    Uint8List buf = shake128(extseed, needed);
    int pos = 0;
    while (pos + 3 <= buf.length && ctr < KYBER_N) {
      int t = (buf[pos] | (buf[pos + 1] << 8) | (buf[pos + 2] << 16)) & 0xFFF;
      if (t < KYBER_Q) {
        a.coeffs[ctr++] = t;
      }
      pos += 3;
    }
  }
}