2019-09-17 13:02:01 +01:00
|
|
|
#include "params.h"
|
2020-07-31 07:17:42 +01:00
|
|
|
#include "reduce.h"
|
2019-09-17 13:02:01 +01:00
|
|
|
#include <stdint.h>
|
2020-07-31 07:17:42 +01:00
|
|
|
|
2019-09-17 13:02:01 +01:00
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER76890S_CLEAN_montgomery_reduce
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
|
|
|
* Description: Montgomery reduction; given a 32-bit integer a, computes
|
2020-10-27 13:48:42 +00:00
|
|
|
* 16-bit integer congruent to a * R^-1 mod q, where R=2^16
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - int32_t a: input integer to be reduced;
|
|
|
|
* has to be in {-q2^15,...,q2^15-1}
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
|
|
|
* Returns: integer in {-q+1,...,q-1} congruent to a * R^-1 modulo q.
|
|
|
|
**************************************************/
|
|
|
|
int16_t PQCLEAN_KYBER76890S_CLEAN_montgomery_reduce(int32_t a) {
|
2020-10-27 00:05:07 +00:00
|
|
|
int32_t t;
|
|
|
|
int16_t u;
|
2019-09-17 13:02:01 +01:00
|
|
|
|
2020-03-09 21:57:43 +00:00
|
|
|
u = (int16_t)(a * (int64_t)QINV);
|
2019-09-17 13:02:01 +01:00
|
|
|
t = (int32_t)u * KYBER_Q;
|
|
|
|
t = a - t;
|
|
|
|
t >>= 16;
|
2020-09-17 09:23:24 +01:00
|
|
|
return (int16_t)t;
|
2019-09-17 13:02:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER76890S_CLEAN_barrett_reduce
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
|
|
|
* Description: Barrett reduction; given a 16-bit integer a, computes
|
2020-10-27 13:48:42 +00:00
|
|
|
* centered representative congruent to a mod q in {-(q-1)/2,...,(q-1)/2}
|
2019-09-17 13:02:01 +01:00
|
|
|
*
|
|
|
|
* Arguments: - int16_t a: input integer to be reduced
|
|
|
|
*
|
2020-10-27 13:48:42 +00:00
|
|
|
* Returns: integer in {-(q-1)/2,...,(q-1)/2} congruent to a modulo q.
|
2019-09-17 13:02:01 +01:00
|
|
|
**************************************************/
|
|
|
|
int16_t PQCLEAN_KYBER76890S_CLEAN_barrett_reduce(int16_t a) {
|
2020-10-27 00:05:07 +00:00
|
|
|
int16_t t;
|
2020-07-31 07:17:42 +01:00
|
|
|
const int16_t v = ((1U << 26) + KYBER_Q / 2) / KYBER_Q;
|
2019-09-17 13:02:01 +01:00
|
|
|
|
2020-10-27 13:48:42 +00:00
|
|
|
t = ((int32_t)v * a + (1 << 25)) >> 26;
|
2019-09-17 13:02:01 +01:00
|
|
|
t *= KYBER_Q;
|
2020-07-31 07:17:42 +01:00
|
|
|
return a - t;
|
2019-09-17 13:02:01 +01:00
|
|
|
}
|