Skip to content

Commit

Permalink
Document implementation-defined C behaviour in montgomery_reduce()
Browse files Browse the repository at this point in the history
See pq-crystals/kyber#77

Signed-off-by: Hanno Becker <[email protected]>
  • Loading branch information
hanno-becker committed Oct 11, 2024
1 parent 28894ca commit 12b0fc3
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions mlkem/reduce.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
* < q (C/2^16 + 1/2).
**************************************************/
int16_t montgomery_reduce(int32_t a) {
int16_t t;

// Bounds on paper
//
// - Case |a| < q * C, for some C
Expand All @@ -43,7 +41,16 @@ int16_t montgomery_reduce(int32_t a) {
// Replace C -> C * q in the above and estimate
// q / 2^17 < 0.0254.

t = (int16_t)a * QINV;
uint16_t u;
int16_t t;
// Compute a*q^{-1} mod 2^16 in unsigned representatives
u = (uint16_t)a * QINV;
// Lift to signed canonical representative mod 2^16.
// PORTABILITY: This relies on uint16_t -> int16_t
// being implemented as the inverse of int16_t -> uint16_t,
// which is not mandated by the standard.
t = (int16_t)u;
// By construction, the LHS is divisible by 2^16
t = (a - (int32_t)t * KYBER_Q) >> 16;
return t;
}
Expand Down

0 comments on commit 12b0fc3

Please sign in to comment.