2019-05-06 13:50:27 +01:00
|
|
|
#include "params.h"
|
2020-07-31 07:17:42 +01:00
|
|
|
#include "reduce.h"
|
2019-05-06 13:50:27 +01:00
|
|
|
#include <stdint.h>
|
2020-07-31 07:17:42 +01:00
|
|
|
|
2019-05-06 13:50:27 +01:00
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER1024_CLEAN_montgomery_reduce
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Montgomery reduction; given a 32-bit integer a, computes
|
|
|
|
* 16-bit integer congruent to a * R^-1 mod q,
|
|
|
|
* where R=2^16
|
|
|
|
*
|
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-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Returns: integer in {-q+1,...,q-1} congruent to a * R^-1 modulo q.
|
|
|
|
**************************************************/
|
|
|
|
int16_t PQCLEAN_KYBER1024_CLEAN_montgomery_reduce(int32_t a) {
|
2020-10-27 00:05:07 +00:00
|
|
|
int32_t t;
|
|
|
|
int16_t u;
|
2019-05-06 13:50:27 +01:00
|
|
|
|
2020-03-09 21:57:43 +00:00
|
|
|
u = (int16_t)(a * (int64_t)QINV);
|
2019-05-06 13:50:27 +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-05-06 13:50:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER1024_CLEAN_barrett_reduce
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Barrett reduction; given a 16-bit integer a, computes
|
|
|
|
* 16-bit integer congruent to a mod q in {0,...,q}
|
|
|
|
*
|
|
|
|
* Arguments: - int16_t a: input integer to be reduced
|
|
|
|
*
|
|
|
|
* Returns: integer in {0,...,q} congruent to a modulo q.
|
|
|
|
**************************************************/
|
|
|
|
int16_t PQCLEAN_KYBER1024_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-05-06 13:50:27 +01:00
|
|
|
|
2020-07-31 07:17:42 +01:00
|
|
|
t = (int32_t)v * a >> 26;
|
2019-05-06 13:50:27 +01:00
|
|
|
t *= KYBER_Q;
|
2020-07-31 07:17:42 +01:00
|
|
|
return a - t;
|
2019-05-06 13:50:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2020-07-31 07:17:42 +01:00
|
|
|
* Name: PQCLEAN_KYBER1024_CLEAN_csubq
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Description: Conditionallly subtract q
|
|
|
|
*
|
2020-07-31 07:17:42 +01:00
|
|
|
* Arguments: - int16_t x: input integer
|
2019-05-06 13:50:27 +01:00
|
|
|
*
|
|
|
|
* Returns: a - q if a >= q, else a
|
|
|
|
**************************************************/
|
|
|
|
int16_t PQCLEAN_KYBER1024_CLEAN_csubq(int16_t a) {
|
|
|
|
a -= KYBER_Q;
|
|
|
|
a += (a >> 15) & KYBER_Q;
|
|
|
|
return a;
|
|
|
|
}
|