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

move modulus function to source, namespace it

This commit is contained in:
Leon Botros 2019-10-25 13:14:25 +02:00 committed by Kris Kwiatkowski
parent 36b6142794
commit c95f1b4ebb
9 changed files with 27 additions and 18 deletions

View File

@ -1,6 +1,11 @@
/** Ring arithmetic implementation */ /** Ring arithmetic implementation */
#include "ring.h" #include "ring.h"
/** Return the i'th limb of the modulus */
limb_t PQCLEAN_BABYBEAR_CLEAN_modulus(size_t i) {
return (i == DIGITS / 2) ? LMASK - 1 : LMASK;
}
/** Multiply and accumulate c += a*b */ /** Multiply and accumulate c += a*b */
void PQCLEAN_BABYBEAR_CLEAN_mac(gf_t c, const gf_t a, const gf_t b) { void PQCLEAN_BABYBEAR_CLEAN_mac(gf_t c, const gf_t a, const gf_t b) {
/* Reference non-Karatsuba MAC */ /* Reference non-Karatsuba MAC */
@ -57,7 +62,7 @@ void PQCLEAN_BABYBEAR_CLEAN_canon(gf_t c) {
/* Strong reduce. First subtract modulus */ /* Strong reduce. First subtract modulus */
scarry = hi >> LGX; scarry = hi >> LGX;
for (size_t i = 0; i < DIGITS; i++) { for (size_t i = 0; i < DIGITS; i++) {
scarry = scarry + (slimb_t)c[i] - modulus(i); scarry = scarry + (slimb_t)c[i] - PQCLEAN_BABYBEAR_CLEAN_modulus(i);
c[i] = scarry & LMASK; c[i] = scarry & LMASK;
scarry >>= LGX; scarry >>= LGX;
} }
@ -65,7 +70,7 @@ void PQCLEAN_BABYBEAR_CLEAN_canon(gf_t c) {
/* add it back */ /* add it back */
carry = 0; carry = 0;
for (size_t i = 0; i < DIGITS; i++) { for (size_t i = 0; i < DIGITS; i++) {
carry = carry + c[i] + ((dlimb_t)scarry & modulus(i)); carry = carry + c[i] + ((dlimb_t)scarry & PQCLEAN_BABYBEAR_CLEAN_modulus(i));
c[i] = carry & LMASK; c[i] = carry & LMASK;
carry >>= LGX; carry >>= LGX;
} }

View File

@ -24,8 +24,6 @@ void PQCLEAN_BABYBEAR_CLEAN_mac(gf_t c, const gf_t a, const gf_t b);
void PQCLEAN_BABYBEAR_CLEAN_canon(gf_t c); void PQCLEAN_BABYBEAR_CLEAN_canon(gf_t c);
/** Return the i'th limb of the modulus */ /** Return the i'th limb of the modulus */
static inline limb_t modulus(size_t i) { limb_t PQCLEAN_BABYBEAR_CLEAN_modulus(size_t i);
return (i == DIGITS / 2) ? LMASK - 1 : LMASK;
}
#endif #endif

View File

@ -61,7 +61,7 @@ static void noise(gf_t x, const shake256incctx *ctx, uint8_t iv) {
cshake256_inc_finalize(&ctx2); cshake256_inc_finalize(&ctx2);
cshake256_inc_squeeze(c, DIGITS, &ctx2); cshake256_inc_squeeze(c, DIGITS, &ctx2);
for (size_t i = 0; i < DIGITS; i++) { for (size_t i = 0; i < DIGITS; i++) {
x[i] = (limb_t)(psi(c[i]) + modulus(i)); x[i] = (limb_t)(psi(c[i]) + PQCLEAN_BABYBEAR_CLEAN_modulus(i));
} }
} }

View File

@ -1,6 +1,11 @@
/** Ring arithmetic implementation */ /** Ring arithmetic implementation */
#include "ring.h" #include "ring.h"
/** Return the i'th limb of the modulus */
limb_t PQCLEAN_MAMABEAR_CLEAN_modulus(size_t i) {
return (i == DIGITS / 2) ? LMASK - 1 : LMASK;
}
/** Multiply and accumulate c += a*b */ /** Multiply and accumulate c += a*b */
void PQCLEAN_MAMABEAR_CLEAN_mac(gf_t c, const gf_t a, const gf_t b) { void PQCLEAN_MAMABEAR_CLEAN_mac(gf_t c, const gf_t a, const gf_t b) {
/* Reference non-Karatsuba MAC */ /* Reference non-Karatsuba MAC */
@ -57,7 +62,7 @@ void PQCLEAN_MAMABEAR_CLEAN_canon(gf_t c) {
/* Strong reduce. First subtract modulus */ /* Strong reduce. First subtract modulus */
scarry = hi >> LGX; scarry = hi >> LGX;
for (size_t i = 0; i < DIGITS; i++) { for (size_t i = 0; i < DIGITS; i++) {
scarry = scarry + (slimb_t)c[i] - modulus(i); scarry = scarry + (slimb_t)c[i] - PQCLEAN_MAMABEAR_CLEAN_modulus(i);
c[i] = scarry & LMASK; c[i] = scarry & LMASK;
scarry >>= LGX; scarry >>= LGX;
} }
@ -65,7 +70,7 @@ void PQCLEAN_MAMABEAR_CLEAN_canon(gf_t c) {
/* add it back */ /* add it back */
carry = 0; carry = 0;
for (size_t i = 0; i < DIGITS; i++) { for (size_t i = 0; i < DIGITS; i++) {
carry = carry + c[i] + ((dlimb_t)scarry & modulus(i)); carry = carry + c[i] + ((dlimb_t)scarry & PQCLEAN_MAMABEAR_CLEAN_modulus(i));
c[i] = carry & LMASK; c[i] = carry & LMASK;
carry >>= LGX; carry >>= LGX;
} }

View File

@ -24,8 +24,6 @@ void PQCLEAN_MAMABEAR_CLEAN_mac(gf_t c, const gf_t a, const gf_t b);
void PQCLEAN_MAMABEAR_CLEAN_canon(gf_t c); void PQCLEAN_MAMABEAR_CLEAN_canon(gf_t c);
/** Return the i'th limb of the modulus */ /** Return the i'th limb of the modulus */
static inline limb_t modulus(size_t i) { limb_t PQCLEAN_MAMABEAR_CLEAN_modulus(size_t i);
return (i == DIGITS / 2) ? LMASK - 1 : LMASK;
}
#endif #endif

View File

@ -61,7 +61,7 @@ static void noise(gf_t x, const shake256incctx *ctx, uint8_t iv) {
cshake256_inc_finalize(&ctx2); cshake256_inc_finalize(&ctx2);
cshake256_inc_squeeze(c, DIGITS, &ctx2); cshake256_inc_squeeze(c, DIGITS, &ctx2);
for (size_t i = 0; i < DIGITS; i++) { for (size_t i = 0; i < DIGITS; i++) {
x[i] = (limb_t)(psi(c[i]) + modulus(i)); x[i] = (limb_t)(psi(c[i]) + PQCLEAN_MAMABEAR_CLEAN_modulus(i));
} }
} }

View File

@ -1,6 +1,11 @@
/** Ring arithmetic implementation */ /** Ring arithmetic implementation */
#include "ring.h" #include "ring.h"
/** Return the i'th limb of the modulus */
limb_t PQCLEAN_PAPABEAR_CLEAN_modulus(size_t i) {
return (i == DIGITS / 2) ? LMASK - 1 : LMASK;
}
/** Multiply and accumulate c += a*b */ /** Multiply and accumulate c += a*b */
void PQCLEAN_PAPABEAR_CLEAN_mac(gf_t c, const gf_t a, const gf_t b) { void PQCLEAN_PAPABEAR_CLEAN_mac(gf_t c, const gf_t a, const gf_t b) {
/* Reference non-Karatsuba MAC */ /* Reference non-Karatsuba MAC */
@ -57,7 +62,7 @@ void PQCLEAN_PAPABEAR_CLEAN_canon(gf_t c) {
/* Strong reduce. First subtract modulus */ /* Strong reduce. First subtract modulus */
scarry = hi >> LGX; scarry = hi >> LGX;
for (size_t i = 0; i < DIGITS; i++) { for (size_t i = 0; i < DIGITS; i++) {
scarry = scarry + (slimb_t)c[i] - modulus(i); scarry = scarry + (slimb_t)c[i] - PQCLEAN_PAPABEAR_CLEAN_modulus(i);
c[i] = scarry & LMASK; c[i] = scarry & LMASK;
scarry >>= LGX; scarry >>= LGX;
} }
@ -65,7 +70,7 @@ void PQCLEAN_PAPABEAR_CLEAN_canon(gf_t c) {
/* add it back */ /* add it back */
carry = 0; carry = 0;
for (size_t i = 0; i < DIGITS; i++) { for (size_t i = 0; i < DIGITS; i++) {
carry = carry + c[i] + ((dlimb_t)scarry & modulus(i)); carry = carry + c[i] + ((dlimb_t)scarry & PQCLEAN_PAPABEAR_CLEAN_modulus(i));
c[i] = carry & LMASK; c[i] = carry & LMASK;
carry >>= LGX; carry >>= LGX;
} }

View File

@ -24,8 +24,6 @@ void PQCLEAN_PAPABEAR_CLEAN_mac(gf_t c, const gf_t a, const gf_t b);
void PQCLEAN_PAPABEAR_CLEAN_canon(gf_t c); void PQCLEAN_PAPABEAR_CLEAN_canon(gf_t c);
/** Return the i'th limb of the modulus */ /** Return the i'th limb of the modulus */
static inline limb_t modulus(size_t i) { limb_t PQCLEAN_PAPABEAR_CLEAN_modulus(size_t i);
return (i == DIGITS / 2) ? LMASK - 1 : LMASK;
}
#endif #endif

View File

@ -61,7 +61,7 @@ static void noise(gf_t x, const shake256incctx *ctx, uint8_t iv) {
cshake256_inc_finalize(&ctx2); cshake256_inc_finalize(&ctx2);
cshake256_inc_squeeze(c, DIGITS, &ctx2); cshake256_inc_squeeze(c, DIGITS, &ctx2);
for (size_t i = 0; i < DIGITS; i++) { for (size_t i = 0; i < DIGITS; i++) {
x[i] = (limb_t)(psi(c[i]) + modulus(i)); x[i] = (limb_t)(psi(c[i]) + PQCLEAN_PAPABEAR_CLEAN_modulus(i));
} }
} }