polytobytes function

Uint8List polytobytes(
  1. Poly a
)

Serializes a polynomial into a byte array using 12-bit encoding per coefficient.

Implementation

Uint8List polytobytes(Poly a) {
  Uint8List r = Uint8List(KYBER_POLYBYTES);
  Poly t = polyreduce(Poly()..coeffs = List<int>.from(a.coeffs));
  for (int i = 0; i < KYBER_N ~/ 2; i++) {
    int t0 = t.coeffs[2 * i];
    int t1 = t.coeffs[2 * i + 1];
    r[3 * i + 0] = t0 & 0xFF;
    r[3 * i + 1] = ((t0 >> 8) & 0x0F) | ((t1 & 0x0F) << 4);
    r[3 * i + 2] = t1 >> 4;
  }
  return r;
}