polydecompress function

Poly polydecompress(
  1. Uint8List r
)

Decompresses a polynomial from a compressed byte representation.

This function takes a compressed byte array r and reconstructs the polynomial a by decompressing each sequence of bytes into four coefficients. The coefficients are extracted from the bit-packed format in r, where each group of five bytes represents four 10-bit coefficients. The decompressed coefficients are converted to the original polynomial domain by scaling them with KYBER_Q and normalizing.

  • Parameters:
    • r: A byte array containing the compressed representation of the polynomial.
  • Returns: A Poly object with coefficients decompressed from the input byte array.

Implementation

Poly polydecompress(Uint8List r) {
  Poly a = Poly();
  int pos = 0;
  for (int i = 0; i < KYBER_N; i += 4) {
    int t0 = r[pos] | (r[pos+1]<<8) | (r[pos+2]<<16) | (r[pos+3]<<24);
    int t1 = r[pos+4];
    pos += 5;
    int d0 = t0 & 0x3FF;
    int d1 = (t0 >> 10) & 0x3FF;
    int d2 = (t0 >> 20) & 0x3FF;
    int d3 = ((t0 >> 30) & 0x03F) | ((t1 & 0xFF)<<6);

    a.coeffs[i]   = (d0 * KYBER_Q + (1<<9)) >> 10;
    a.coeffs[i+1] = (d1 * KYBER_Q + (1<<9)) >> 10;
    a.coeffs[i+2] = (d2 * KYBER_Q + (1<<9)) >> 10;
    a.coeffs[i+3] = (d3 * KYBER_Q + (1<<9)) >> 10;
  }
  return a;
}