From 08cee4325d557d5198e2079d4e02163fd2b8021d Mon Sep 17 00:00:00 2001 From: czurnieden Date: Mon, 21 May 2018 22:54:55 +0200 Subject: [PATCH] Moved mp_mul_si() into bn_mp_prime_strong_lucas_selfridge.c as a local function --- bn_mp_mul_si.c | 50 ---------------------------- bn_mp_prime_frobenius_underwood.c | 1 - bn_mp_prime_strong_lucas_selfridge.c | 41 +++++++++++++++++++++++ tommath.h | 4 --- 4 files changed, 41 insertions(+), 55 deletions(-) delete mode 100644 bn_mp_mul_si.c diff --git a/bn_mp_mul_si.c b/bn_mp_mul_si.c deleted file mode 100644 index 4fc17be..0000000 --- a/bn_mp_mul_si.c +++ /dev/null @@ -1,50 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_MUL_SI_C - -/* LibTomMath, multiple-precision integer library -- Tom St Denis - * - * LibTomMath is a library that provides multiple-precision - * integer arithmetic as well as number theoretic functionality. - * - * The library was designed directly after the MPI library by - * Michael Fromberger but has been written from scratch with - * additional optimizations in place. - * - * The library is free for all purposes without any express - * guarantee it works. - */ - -// multiply bigint a with int d and put the result in c -// Like mp_mul_d() but with a signed long as the small input -int mp_mul_si(const mp_int *a, long d, mp_int *c) -{ - mp_int t; - int err, neg = 0; - - if ((err = mp_init(&t)) != MP_OKAY) { - return err; - } - if (d < 0) { - neg = 1; - d = -d; - } - - // mp_digit might be smaller than a long, which excludes - // the use of mp_mul_d() here. - if ((err = mp_set_long(&t, (unsigned long) d)) != MP_OKAY) { - goto LBL_MPMULSI_ERR; - } - if ((err = mp_mul(a, &t, c)) != MP_OKAY) { - goto LBL_MPMULSI_ERR; - } - if (neg == 1) { - c->sign = (a->sign == MP_NEG) ? MP_ZPOS: MP_NEG; - } -LBL_MPMULSI_ERR: - mp_clear(&t); - return err; -} - - - -#endif diff --git a/bn_mp_prime_frobenius_underwood.c b/bn_mp_prime_frobenius_underwood.c index 28b5269..5be9d0d 100644 --- a/bn_mp_prime_frobenius_underwood.c +++ b/bn_mp_prime_frobenius_underwood.c @@ -14,7 +14,6 @@ * guarantee it works. */ - #ifdef MP_8BIT /* * floor of positive solution of diff --git a/bn_mp_prime_strong_lucas_selfridge.c b/bn_mp_prime_strong_lucas_selfridge.c index 6d0e3fe..1fcbbd5 100644 --- a/bn_mp_prime_strong_lucas_selfridge.c +++ b/bn_mp_prime_strong_lucas_selfridge.c @@ -14,8 +14,49 @@ * guarantee it works. */ +/* + * 8-bit is just too small. You can try the Frobenius test + * but that frobenius test can fail, too, for the same reason. + */ #ifndef MP_8BIT +/* + * multiply bigint a with int d and put the result in c + * Like mp_mul_d() but with a signed long as the small input + */ +static int mp_mul_si(const mp_int *a, long d, mp_int *c) +{ + mp_int t; + int err, neg = 0; + + if ((err = mp_init(&t)) != MP_OKAY) { + return err; + } + if (d < 0) { + neg = 1; + d = -d; + } + + /* + * mp_digit might be smaller than a long, which excludes + * the use of mp_mul_d() here. + */ + if ((err = mp_set_long(&t, (unsigned long) d)) != MP_OKAY) { + goto LBL_MPMULSI_ERR; + } + if ((err = mp_mul(a, &t, c)) != MP_OKAY) { + goto LBL_MPMULSI_ERR; + } + if (neg == 1) { + c->sign = (a->sign == MP_NEG) ? MP_ZPOS: MP_NEG; + } +LBL_MPMULSI_ERR: + mp_clear(&t); + return err; +} + + + /* Strong Lucas-Selfridge test. returns MP_YES if it is a strong L-S prime, MP_NO if it is composite diff --git a/tommath.h b/tommath.h index f9132a2..80ab7b9 100644 --- a/tommath.h +++ b/tommath.h @@ -364,10 +364,6 @@ int mp_sub_d(const mp_int *a, mp_digit b, mp_int *c); /* c = a * b */ int mp_mul_d(const mp_int *a, mp_digit b, mp_int *c); -/* multiply bigint a with int d and put the result in c - Like mp_mul_d() but with a signed long as the small input */ -int mp_mul_si(const mp_int *a, long d, mp_int *c); - /* a/b => cb + d == a */ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d);