polytomsg function

void polytomsg(
  1. Uint8List msg,
  2. Poly p
)

Converts a polynomial p into a message byte array msg.

This function iterates over the coefficients of the polynomial p and reconstructs the original message by interpreting groups of 8 coefficients as the bits of a byte. Each coefficient contributes one bit to the message byte, determined by its proximity to KYBER_Q / 2.

The output message msg will have KYBER_SYMBYTES bytes, reflecting the polynomial's encoded message form.

Implementation

void polytomsg(Uint8List msg, Poly p) {
  for (int i = 0; i < KYBER_SYMBYTES; i++) {
    msg[i] = 0;
    for (int j = 0; j < 8; j++) {
      int t = p.coeffs[8*i+j];
      t = (t + (KYBER_Q ~/ 2)) % KYBER_Q;
      int bit = ( (2*t) ~/ KYBER_Q ) & 1;
      msg[i] |= (bit << j);
    }
  }
}