polytobytes function
Serializes a polynomial into a byte array representation.
This function takes a polynomial a
and maps each of its coefficients to two
bytes in the output array. The coefficients are processed in chunks of two
bytes, each being interpreted as a 16-bit unsigned integer. The resulting
Uint8List
object has the serialized coefficients.
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; i++) {
int val = t.coeffs[i];
r[2 * i] = val & 0xFF;
r[2 * i + 1] = (val >> 8) & 0xFF;
}
return r;
}