1
1
mirror of https://github.com/henrydcase/pqc.git synced 2024-11-22 07:35:38 +00:00

remove duplication

This commit is contained in:
Henry Case 2021-05-25 22:16:57 +01:00
parent fd21b95a2d
commit 1120727660
15 changed files with 18 additions and 148 deletions

View File

@ -3,7 +3,7 @@
#include <stdint.h>
/*************************************************
* Name: PQCLEAN_KYBER768_CLEAN_montgomery_reduce
* Name: kyber_montgomery_reduce
*
* Description: Montgomery reduction; given a 32-bit integer a, computes
* 16-bit integer congruent to a * R^-1 mod q, where R=2^16
@ -13,7 +13,7 @@
*
* Returns: integer in {-q+1,...,q-1} congruent to a * R^-1 modulo q.
**************************************************/
int16_t PQCLEAN_KYBER768_CLEAN_montgomery_reduce(int32_t a) {
int16_t kyber_montgomery_reduce(int32_t a) {
int32_t t;
int16_t u;
@ -25,20 +25,19 @@ int16_t PQCLEAN_KYBER768_CLEAN_montgomery_reduce(int32_t a) {
}
/*************************************************
* Name: PQCLEAN_KYBER768_CLEAN_barrett_reduce
* Name: kyber_barrett_reduce
*
* Description: Barrett reduction; given a 16-bit integer a, computes
* centered representative congruent to a mod q in {-(q-1)/2,...,(q-1)/2}
* centered representative congruent to a mod q in {0,q}
*
* Arguments: - int16_t a: input integer to be reduced
*
* Returns: integer in {-(q-1)/2,...,(q-1)/2} congruent to a modulo q.
* Returns: integer in {0,q} congruent to a modulo q.
**************************************************/
int16_t PQCLEAN_KYBER768_CLEAN_barrett_reduce(int16_t a) {
int16_t t;
const int16_t v = ((1U << 26) + KYBER_Q / 2) / KYBER_Q;
t = ((int32_t)v * a + (1 << 25)) >> 26;
t *= KYBER_Q;
return a - t;
int16_t kyber_barrett_reduce(int16_t a) {
static const int32_t v = 20159;
int32_t t;
t = v*a;
t >>= 26;
return a - ((int16_t)t)*KYBER_Q;
}

View File

@ -6,7 +6,7 @@ set(
ntt.c
poly.c
polyvec.c
reduce.c
../../common/reduce.c
symmetric-shake.c
verify.c
)

View File

@ -1,6 +1,6 @@
#include "ntt.h"
#include "params.h"
#include "reduce.h"
#include "../../common/reduce.h"
#include <stdint.h>
/* Code to generate PQCLEAN_KYBER1024_CLEAN_zetas and zetas_inv used in the number-theoretic transform:

View File

@ -2,7 +2,7 @@
#include "ntt.h"
#include "params.h"
#include "poly.h"
#include "reduce.h"
#include "../../common/reduce.h"
#include "symmetric.h"
#include <stdint.h>

View File

@ -1,44 +0,0 @@
#include "params.h"
#include "reduce.h"
#include <stdint.h>
/*************************************************
* Name: PQCLEAN_KYBER1024_CLEAN_montgomery_reduce
*
* Description: Montgomery reduction; given a 32-bit integer a, computes
* 16-bit integer congruent to a * R^-1 mod q, where R=2^16
*
* Arguments: - int32_t a: input integer to be reduced;
* has to be in {-q2^15,...,q2^15-1}
*
* Returns: integer in {-q+1,...,q-1} congruent to a * R^-1 modulo q.
**************************************************/
int16_t PQCLEAN_KYBER1024_CLEAN_montgomery_reduce(int32_t a) {
int32_t t;
int16_t u;
u = (int16_t)(a * (int64_t)QINV);
t = (int32_t)u * KYBER_Q;
t = a - t;
t >>= 16;
return (int16_t)t;
}
/*************************************************
* Name: PQCLEAN_KYBER1024_CLEAN_barrett_reduce
*
* Description: Barrett reduction; given a 16-bit integer a, computes
* centered representative congruent to a mod q in {-(q-1)/2,...,(q-1)/2}
*
* Arguments: - int16_t a: input integer to be reduced
*
* Returns: integer in {-(q-1)/2,...,(q-1)/2} congruent to a modulo q.
**************************************************/
int16_t PQCLEAN_KYBER1024_CLEAN_barrett_reduce(int16_t a) {
int16_t t;
const int16_t v = ((1U << 26) + KYBER_Q / 2) / KYBER_Q;
t = ((int32_t)v * a + (1 << 25)) >> 26;
t *= KYBER_Q;
return a - t;
}

View File

@ -1,13 +0,0 @@
#ifndef PQCLEAN_KYBER1024_CLEAN_REDUCE_H
#define PQCLEAN_KYBER1024_CLEAN_REDUCE_H
#include "params.h"
#include <stdint.h>
#define MONT 2285 // 2^16 mod q
#define QINV 62209 // q^-1 mod 2^16
int16_t PQCLEAN_KYBER1024_CLEAN_montgomery_reduce(int32_t a);
int16_t PQCLEAN_KYBER1024_CLEAN_barrett_reduce(int16_t a);
#endif

View File

@ -6,7 +6,6 @@ set(
ntt.c
poly.c
polyvec.c
reduce.c
symmetric-shake.c
verify.c
)

View File

@ -1,6 +1,6 @@
#include "ntt.h"
#include "params.h"
#include "reduce.h"
#include "../../common/reduce.h"
#include <stdint.h>
/* Code to generate PQCLEAN_KYBER512_CLEAN_zetas and zetas_inv used in the number-theoretic transform:

View File

@ -2,7 +2,7 @@
#include "ntt.h"
#include "params.h"
#include "poly.h"
#include "reduce.h"
#include "../../common/reduce.h"
#include "symmetric.h"
#include <stdint.h>

View File

@ -1,44 +0,0 @@
#include "params.h"
#include "reduce.h"
#include <stdint.h>
/*************************************************
* Name: PQCLEAN_KYBER512_CLEAN_montgomery_reduce
*
* Description: Montgomery reduction; given a 32-bit integer a, computes
* 16-bit integer congruent to a * R^-1 mod q, where R=2^16
*
* Arguments: - int32_t a: input integer to be reduced;
* has to be in {-q2^15,...,q2^15-1}
*
* Returns: integer in {-q+1,...,q-1} congruent to a * R^-1 modulo q.
**************************************************/
int16_t PQCLEAN_KYBER512_CLEAN_montgomery_reduce(int32_t a) {
int32_t t;
int16_t u;
u = (int16_t)(a * (int64_t)QINV);
t = (int32_t)u * KYBER_Q;
t = a - t;
t >>= 16;
return (int16_t)t;
}
/*************************************************
* Name: PQCLEAN_KYBER512_CLEAN_barrett_reduce
*
* Description: Barrett reduction; given a 16-bit integer a, computes
* centered representative congruent to a mod q in {-(q-1)/2,...,(q-1)/2}
*
* Arguments: - int16_t a: input integer to be reduced
*
* Returns: integer in {-(q-1)/2,...,(q-1)/2} congruent to a modulo q.
**************************************************/
int16_t PQCLEAN_KYBER512_CLEAN_barrett_reduce(int16_t a) {
int16_t t;
const int16_t v = ((1U << 26) + KYBER_Q / 2) / KYBER_Q;
t = ((int32_t)v * a + (1 << 25)) >> 26;
t *= KYBER_Q;
return a - t;
}

View File

@ -1,13 +0,0 @@
#ifndef PQCLEAN_KYBER512_CLEAN_REDUCE_H
#define PQCLEAN_KYBER512_CLEAN_REDUCE_H
#include "params.h"
#include <stdint.h>
#define MONT 2285 // 2^16 mod q
#define QINV 62209 // q^-1 mod 2^16
int16_t PQCLEAN_KYBER512_CLEAN_montgomery_reduce(int32_t a);
int16_t PQCLEAN_KYBER512_CLEAN_barrett_reduce(int16_t a);
#endif

View File

@ -6,7 +6,6 @@ set(
ntt.c
poly.c
polyvec.c
reduce.c
symmetric-shake.c
verify.c
)

View File

@ -1,6 +1,6 @@
#include "ntt.h"
#include "params.h"
#include "reduce.h"
#include "../../common/reduce.h"
#include <stdint.h>
/* Code to generate PQCLEAN_KYBER768_CLEAN_zetas and zetas_inv used in the number-theoretic transform:

View File

@ -2,7 +2,7 @@
#include "ntt.h"
#include "params.h"
#include "poly.h"
#include "reduce.h"
#include "../../common/reduce.h"
#include "symmetric.h"
#include <stdint.h>

View File

@ -1,13 +0,0 @@
#ifndef PQCLEAN_KYBER768_CLEAN_REDUCE_H
#define PQCLEAN_KYBER768_CLEAN_REDUCE_H
#include "params.h"
#include <stdint.h>
#define MONT 2285 // 2^16 mod q
#define QINV 62209 // q^-1 mod 2^16
int16_t PQCLEAN_KYBER768_CLEAN_montgomery_reduce(int32_t a);
int16_t PQCLEAN_KYBER768_CLEAN_barrett_reduce(int16_t a);
#endif