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 de4ae9e commit 761c26d
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions mlkem/reduce.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,17 @@ 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;
t = (a - (int32_t)t * KYBER_Q) >> 16;
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;

Check failure on line 54 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / Linting (ubuntu-latest)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 54 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / Graviton3, CBMC (c7g.xlarge) / Linting Graviton3, CBMC (c7g.xlarge)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 54 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / Intel Xeon 4th gen (t3,+AVX2) / Linting Intel Xeon 4th gen (t3,+AVX2)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 54 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / AMD EPYC 4th gen (t3a,+AVX2) / Linting AMD EPYC 4th gen (t3a,+AVX2)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 54 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / AMD EPYC 4th gen (t3a) / Linting AMD EPYC 4th gen (t3a)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 54 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / Intel Xeon 4th gen (t3) / Linting Intel Xeon 4th gen (t3)

Format error

mlkem/reduce.c require to be formatted
// By construction, the LHS is divisible by 2^16
t = (a - (int32_t)t*KYBER_Q) >> 16;

Check failure on line 56 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / Linting (ubuntu-latest)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 56 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / Linting (ubuntu-latest)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 56 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / Graviton3, CBMC (c7g.xlarge) / Linting Graviton3, CBMC (c7g.xlarge)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 56 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / Graviton3, CBMC (c7g.xlarge) / Linting Graviton3, CBMC (c7g.xlarge)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 56 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / Intel Xeon 4th gen (t3,+AVX2) / Linting Intel Xeon 4th gen (t3,+AVX2)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 56 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / Intel Xeon 4th gen (t3,+AVX2) / Linting Intel Xeon 4th gen (t3,+AVX2)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 56 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / AMD EPYC 4th gen (t3a,+AVX2) / Linting AMD EPYC 4th gen (t3a,+AVX2)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 56 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / AMD EPYC 4th gen (t3a,+AVX2) / Linting AMD EPYC 4th gen (t3a,+AVX2)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 56 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / AMD EPYC 4th gen (t3a) / Linting AMD EPYC 4th gen (t3a)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 56 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / AMD EPYC 4th gen (t3a) / Linting AMD EPYC 4th gen (t3a)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 56 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / Intel Xeon 4th gen (t3) / Linting Intel Xeon 4th gen (t3)

Format error

mlkem/reduce.c require to be formatted

Check failure on line 56 in mlkem/reduce.c

View workflow job for this annotation

GitHub Actions / Intel Xeon 4th gen (t3) / Linting Intel Xeon 4th gen (t3)

Format error

mlkem/reduce.c require to be formatted
return t;
}

Expand Down

0 comments on commit 761c26d

Please sign in to comment.