sampleBounded static method

Poly sampleBounded(
  1. List<int> seed,
  2. int eta
)

Sample a polynomial with coefficients in {-η … η} (Algorithm 31 / RejBoundedPoly).

Implementation

static Poly sampleBounded(List<int> seed, int eta) {
  return QuickCrypto.shake256((xof) {
    final p = Poly();
    xof.update(seed);
    var idx = 0;
    final stream = <int>[];

    while (idx < MLDSAParams.n) {
      if (stream.isEmpty) {
        stream.addAll(_xofDigest(xof, 136));
      }
      final byte = stream.removeAt(0);

      if (eta == 2) {
        // FIPS 204 Algorithm 15: CoeffFromHalfByte for η=2
        // Correct mapping: 2 - (b mod 5)  →  {-2,-1,0,1,2}
        for (var shift = 0; shift < 8 && idx < MLDSAParams.n; shift += 4) {
          final b = (byte >> shift) & 0x0f;
          if (b < 15) {
            p.coeffs[idx++] = 2 - (b % 5);
          }
        }
      } else {
        // η == 4
        for (var shift = 0; shift < 8 && idx < MLDSAParams.n; shift += 4) {
          final b = (byte >> shift) & 0x0f;
          if (b < 9) {
            p.coeffs[idx++] = 4 - b;
          }
        }
      }
    }
    return p;
  });
}