montgomeryreduce function

int montgomeryreduce(
  1. int a
)

Montgomery reduce reduces a number a modulo q and reverses the Montgomery transform. It serves for efficient modular multiplications in the Montgomery domain.

Implementation

int montgomeryreduce(int a) {
  // According to PQClean:
  // u = a * QINV mod 2^16
  // t = (a - u*q) >> 16
  // returns t in [0,q-1] if a < q*2^16

  int u = (a * QINV) & 0xFFFF; // sólo tomar los 16 bits bajos
  int t = a - u * KYBER_Q;
  t >>= 16;
  return t;
}