瀏覽代碼

remove duplication

blog/frodo_constant_time_issue
Henry Case 3 年之前
父節點
當前提交
1120727660
共有 15 個檔案被更改,包括 18 行新增148 行删除
  1. +11
    -12
      src/kem/kyber/common/reduce.c
  2. +1
    -1
      src/kem/kyber/kyber1024/clean/CMakeLists.txt
  3. +1
    -1
      src/kem/kyber/kyber1024/clean/ntt.c
  4. +1
    -1
      src/kem/kyber/kyber1024/clean/poly.c
  5. +0
    -44
      src/kem/kyber/kyber1024/clean/reduce.c
  6. +0
    -13
      src/kem/kyber/kyber1024/clean/reduce.h
  7. +0
    -1
      src/kem/kyber/kyber512/clean/CMakeLists.txt
  8. +1
    -1
      src/kem/kyber/kyber512/clean/ntt.c
  9. +1
    -1
      src/kem/kyber/kyber512/clean/poly.c
  10. +0
    -44
      src/kem/kyber/kyber512/clean/reduce.c
  11. +0
    -13
      src/kem/kyber/kyber512/clean/reduce.h
  12. +0
    -1
      src/kem/kyber/kyber768/clean/CMakeLists.txt
  13. +1
    -1
      src/kem/kyber/kyber768/clean/ntt.c
  14. +1
    -1
      src/kem/kyber/kyber768/clean/poly.c
  15. +0
    -13
      src/kem/kyber/kyber768/clean/reduce.h

src/kem/kyber/kyber768/clean/reduce.c → src/kem/kyber/common/reduce.c 查看文件

@@ -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;
}

+ 1
- 1
src/kem/kyber/kyber1024/clean/CMakeLists.txt 查看文件

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


+ 1
- 1
src/kem/kyber/kyber1024/clean/ntt.c 查看文件

@@ -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:


+ 1
- 1
src/kem/kyber/kyber1024/clean/poly.c 查看文件

@@ -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>



+ 0
- 44
src/kem/kyber/kyber1024/clean/reduce.c 查看文件

@@ -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;
}

+ 0
- 13
src/kem/kyber/kyber1024/clean/reduce.h 查看文件

@@ -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

+ 0
- 1
src/kem/kyber/kyber512/clean/CMakeLists.txt 查看文件

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


+ 1
- 1
src/kem/kyber/kyber512/clean/ntt.c 查看文件

@@ -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:


+ 1
- 1
src/kem/kyber/kyber512/clean/poly.c 查看文件

@@ -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>



+ 0
- 44
src/kem/kyber/kyber512/clean/reduce.c 查看文件

@@ -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;
}

+ 0
- 13
src/kem/kyber/kyber512/clean/reduce.h 查看文件

@@ -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

+ 0
- 1
src/kem/kyber/kyber768/clean/CMakeLists.txt 查看文件

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


+ 1
- 1
src/kem/kyber/kyber768/clean/ntt.c 查看文件

@@ -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:


+ 1
- 1
src/kem/kyber/kyber768/clean/poly.c 查看文件

@@ -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>



+ 0
- 13
src/kem/kyber/kyber768/clean/reduce.h 查看文件

@@ -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

Loading…
取消
儲存