From c95f1b4ebb89e28d2914b584fedcfc83fe6a36c5 Mon Sep 17 00:00:00 2001 From: Leon Botros Date: Fri, 25 Oct 2019 13:14:25 +0200 Subject: [PATCH] move modulus function to source, namespace it --- crypto_kem/babybear/clean/ring.c | 9 +++++++-- crypto_kem/babybear/clean/ring.h | 4 +--- crypto_kem/babybear/clean/threebears.c | 2 +- crypto_kem/mamabear/clean/ring.c | 9 +++++++-- crypto_kem/mamabear/clean/ring.h | 4 +--- crypto_kem/mamabear/clean/threebears.c | 2 +- crypto_kem/papabear/clean/ring.c | 9 +++++++-- crypto_kem/papabear/clean/ring.h | 4 +--- crypto_kem/papabear/clean/threebears.c | 2 +- 9 files changed, 27 insertions(+), 18 deletions(-) diff --git a/crypto_kem/babybear/clean/ring.c b/crypto_kem/babybear/clean/ring.c index 03c4ad64..aa2c384c 100644 --- a/crypto_kem/babybear/clean/ring.c +++ b/crypto_kem/babybear/clean/ring.c @@ -1,6 +1,11 @@ /** Ring arithmetic implementation */ #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 */ void PQCLEAN_BABYBEAR_CLEAN_mac(gf_t c, const gf_t a, const gf_t b) { /* Reference non-Karatsuba MAC */ @@ -57,7 +62,7 @@ void PQCLEAN_BABYBEAR_CLEAN_canon(gf_t c) { /* Strong reduce. First subtract modulus */ scarry = hi >> LGX; 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; scarry >>= LGX; } @@ -65,7 +70,7 @@ void PQCLEAN_BABYBEAR_CLEAN_canon(gf_t c) { /* add it back */ carry = 0; 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; carry >>= LGX; } diff --git a/crypto_kem/babybear/clean/ring.h b/crypto_kem/babybear/clean/ring.h index 8cbc207c..bc6c1a52 100644 --- a/crypto_kem/babybear/clean/ring.h +++ b/crypto_kem/babybear/clean/ring.h @@ -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); /** Return the i'th limb of the modulus */ -static inline limb_t modulus(size_t i) { - return (i == DIGITS / 2) ? LMASK - 1 : LMASK; -} +limb_t PQCLEAN_BABYBEAR_CLEAN_modulus(size_t i); #endif diff --git a/crypto_kem/babybear/clean/threebears.c b/crypto_kem/babybear/clean/threebears.c index c0487409..b6d886b7 100644 --- a/crypto_kem/babybear/clean/threebears.c +++ b/crypto_kem/babybear/clean/threebears.c @@ -61,7 +61,7 @@ static void noise(gf_t x, const shake256incctx *ctx, uint8_t iv) { cshake256_inc_finalize(&ctx2); cshake256_inc_squeeze(c, DIGITS, &ctx2); 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)); } } diff --git a/crypto_kem/mamabear/clean/ring.c b/crypto_kem/mamabear/clean/ring.c index d744b09e..ac93daff 100644 --- a/crypto_kem/mamabear/clean/ring.c +++ b/crypto_kem/mamabear/clean/ring.c @@ -1,6 +1,11 @@ /** Ring arithmetic implementation */ #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 */ void PQCLEAN_MAMABEAR_CLEAN_mac(gf_t c, const gf_t a, const gf_t b) { /* Reference non-Karatsuba MAC */ @@ -57,7 +62,7 @@ void PQCLEAN_MAMABEAR_CLEAN_canon(gf_t c) { /* Strong reduce. First subtract modulus */ scarry = hi >> LGX; 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; scarry >>= LGX; } @@ -65,7 +70,7 @@ void PQCLEAN_MAMABEAR_CLEAN_canon(gf_t c) { /* add it back */ carry = 0; 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; carry >>= LGX; } diff --git a/crypto_kem/mamabear/clean/ring.h b/crypto_kem/mamabear/clean/ring.h index 3e8b1dd1..adb4a5bc 100644 --- a/crypto_kem/mamabear/clean/ring.h +++ b/crypto_kem/mamabear/clean/ring.h @@ -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); /** Return the i'th limb of the modulus */ -static inline limb_t modulus(size_t i) { - return (i == DIGITS / 2) ? LMASK - 1 : LMASK; -} +limb_t PQCLEAN_MAMABEAR_CLEAN_modulus(size_t i); #endif diff --git a/crypto_kem/mamabear/clean/threebears.c b/crypto_kem/mamabear/clean/threebears.c index 0db882c6..f3864de8 100644 --- a/crypto_kem/mamabear/clean/threebears.c +++ b/crypto_kem/mamabear/clean/threebears.c @@ -61,7 +61,7 @@ static void noise(gf_t x, const shake256incctx *ctx, uint8_t iv) { cshake256_inc_finalize(&ctx2); cshake256_inc_squeeze(c, DIGITS, &ctx2); 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)); } } diff --git a/crypto_kem/papabear/clean/ring.c b/crypto_kem/papabear/clean/ring.c index a770260b..1a1e2925 100644 --- a/crypto_kem/papabear/clean/ring.c +++ b/crypto_kem/papabear/clean/ring.c @@ -1,6 +1,11 @@ /** Ring arithmetic implementation */ #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 */ void PQCLEAN_PAPABEAR_CLEAN_mac(gf_t c, const gf_t a, const gf_t b) { /* Reference non-Karatsuba MAC */ @@ -57,7 +62,7 @@ void PQCLEAN_PAPABEAR_CLEAN_canon(gf_t c) { /* Strong reduce. First subtract modulus */ scarry = hi >> LGX; 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; scarry >>= LGX; } @@ -65,7 +70,7 @@ void PQCLEAN_PAPABEAR_CLEAN_canon(gf_t c) { /* add it back */ carry = 0; 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; carry >>= LGX; } diff --git a/crypto_kem/papabear/clean/ring.h b/crypto_kem/papabear/clean/ring.h index aac7f750..b51b4b4b 100644 --- a/crypto_kem/papabear/clean/ring.h +++ b/crypto_kem/papabear/clean/ring.h @@ -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); /** Return the i'th limb of the modulus */ -static inline limb_t modulus(size_t i) { - return (i == DIGITS / 2) ? LMASK - 1 : LMASK; -} +limb_t PQCLEAN_PAPABEAR_CLEAN_modulus(size_t i); #endif diff --git a/crypto_kem/papabear/clean/threebears.c b/crypto_kem/papabear/clean/threebears.c index e2932071..a14d59b4 100644 --- a/crypto_kem/papabear/clean/threebears.c +++ b/crypto_kem/papabear/clean/threebears.c @@ -61,7 +61,7 @@ static void noise(gf_t x, const shake256incctx *ctx, uint8_t iv) { cshake256_inc_finalize(&ctx2); cshake256_inc_squeeze(c, DIGITS, &ctx2); 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)); } }