make mp_div_3 private

This commit is contained in:
Daniel Mendler 2019-11-06 16:51:51 +01:00
parent 0fa802f24b
commit 4f00e75b8f
No known key found for this signature in database
GPG Key ID: D88ADB2A2693CA43
8 changed files with 33 additions and 43 deletions

View File

@ -1241,14 +1241,14 @@ LBL_ERR:
return EXIT_FAILURE; return EXIT_FAILURE;
} }
static int test_mp_div_3(void) static int test_s_mp_div_3(void)
{ {
int cnt; int cnt;
mp_int a, b, c, d, e; mp_int a, b, c, d, e;
DOR(mp_init_multi(&a, &b, &c, &d, &e, NULL)); DOR(mp_init_multi(&a, &b, &c, &d, &e, NULL));
/* test mp_div_3 */ /* test s_mp_div_3 */
mp_set(&d, 3u); mp_set(&d, 3u);
for (cnt = 0; cnt < 10000;) { for (cnt = 0; cnt < 10000;) {
mp_digit r2; mp_digit r2;
@ -1259,10 +1259,10 @@ static int test_mp_div_3(void)
} }
DO(mp_rand(&a, (abs(rand_int()) % 128) + 1)); DO(mp_rand(&a, (abs(rand_int()) % 128) + 1));
DO(mp_div(&a, &d, &b, &e)); DO(mp_div(&a, &d, &b, &e));
DO(mp_div_3(&a, &c, &r2)); DO(s_mp_div_3(&a, &c, &r2));
if (mp_cmp(&b, &c) || mp_cmp_d(&e, r2)) { if (mp_cmp(&b, &c) || mp_cmp_d(&e, r2)) {
printf("\nmp_div_3 => Failure\n"); printf("\ns_mp_div_3 => Failure\n");
goto LBL_ERR; goto LBL_ERR;
} }
} }
@ -2297,7 +2297,7 @@ static int unit_tests(int argc, char **argv)
T1(mp_cnt_lsb, MP_CNT_LSB), T1(mp_cnt_lsb, MP_CNT_LSB),
T1(mp_complement, MP_COMPLEMENT), T1(mp_complement, MP_COMPLEMENT),
T1(mp_decr, MP_SUB_D), T1(mp_decr, MP_SUB_D),
T1(mp_div_3, MP_DIV_3), T1(s_mp_div_3, S_MP_DIV_3),
T1(mp_dr_reduce, MP_DR_REDUCE), T1(mp_dr_reduce, MP_DR_REDUCE),
T2(mp_pack_unpack,MP_PACK, MP_UNPACK), T2(mp_pack_unpack,MP_PACK, MP_UNPACK),
T2(mp_fread_fwrite, MP_FREAD, MP_FWRITE), T2(mp_fread_fwrite, MP_FREAD, MP_FWRITE),

View File

@ -2605,14 +2605,6 @@ mp_err mp_incr(mp_int *a);
mp_err mp_decr(mp_int *a); mp_err mp_decr(mp_int *a);
\end{alltt} \end{alltt}
The division by three can be made faster by replacing the division with a multiplication by the
multiplicative inverse of three.
\index{mp\_div\_3}
\begin{alltt}
mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d);
\end{alltt}
\chapter{Little Helpers} \chapter{Little Helpers}
It is never wrong to have some useful little shortcuts at hand. It is never wrong to have some useful little shortcuts at hand.
\section{Function Macros} \section{Function Macros}

View File

@ -43,8 +43,8 @@ mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
} }
/* three? */ /* three? */
if (MP_HAS(MP_DIV_3) && (b == 3u)) { if (MP_HAS(S_MP_DIV_3) && (b == 3u)) {
return mp_div_3(a, c, d); return s_mp_div_3(a, c, d);
} }
/* no easy answer [c'est la vie]. Just division */ /* no easy answer [c'est la vie]. Just division */

View File

@ -1,10 +1,10 @@
#include "tommath_private.h" #include "tommath_private.h"
#ifdef MP_DIV_3_C #ifdef S_MP_DIV_3_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */ /* SPDX-License-Identifier: Unlicense */
/* divide by three (based on routine from MPI and the GMP manual) */ /* divide by three (based on routine from MPI and the GMP manual) */
mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) mp_err s_mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
{ {
mp_int q; mp_int q;
mp_word w; mp_word w;

View File

@ -133,7 +133,7 @@ mp_err s_mp_mul_toom(const mp_int *a, const mp_int *b, mp_int *c)
if ((err = mp_sub(&S2, &a1, &S2)) != MP_OKAY) goto LBL_ERR; if ((err = mp_sub(&S2, &a1, &S2)) != MP_OKAY) goto LBL_ERR;
/** S2 = S2 / 3; \\ this is an exact division */ /** S2 = S2 / 3; \\ this is an exact division */
if ((err = mp_div_3(&S2, &S2, NULL)) != MP_OKAY) goto LBL_ERR; if ((err = s_mp_div_3(&S2, &S2, NULL)) != MP_OKAY) goto LBL_ERR;
/** a1 = S1 - a1; */ /** a1 = S1 - a1; */
if ((err = mp_sub(&S1, &a1, &a1)) != MP_OKAY) goto LBL_ERR; if ((err = mp_sub(&S1, &a1, &a1)) != MP_OKAY) goto LBL_ERR;

View File

@ -300,9 +300,6 @@ mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d) MP_WUR;
/* b = a/2 */ /* b = a/2 */
mp_err mp_div_2(const mp_int *a, mp_int *b) MP_WUR; mp_err mp_div_2(const mp_int *a, mp_int *b) MP_WUR;
/* a/3 => 3c + d == a */
mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) MP_WUR;
/* c = a * 2**b, implemented as c = a << b */ /* c = a * 2**b, implemented as c = a << b */
mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c) MP_WUR; mp_err mp_mul_2d(const mp_int *a, int b, mp_int *c) MP_WUR;

View File

@ -158,36 +158,37 @@ MP_STATIC_ASSERT(prec_geq_min_prec, MP_PREC >= MP_MIN_PREC)
extern MP_PRIVATE mp_err(*s_mp_rand_source)(void *out, size_t size); extern MP_PRIVATE mp_err(*s_mp_rand_source)(void *out, size_t size);
/* lowlevel functions, do not call! */ /* lowlevel functions, do not call! */
MP_PRIVATE bool s_mp_get_bit(const mp_int *a, int b); MP_PRIVATE bool s_mp_get_bit(const mp_int *a, int b) MP_WUR;
MP_PRIVATE mp_digit s_mp_log_d(mp_digit base, mp_digit n) MP_WUR;
MP_PRIVATE mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; MP_PRIVATE mp_err s_mp_add(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
MP_PRIVATE mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; MP_PRIVATE mp_err s_mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) MP_WUR;
MP_PRIVATE mp_err s_mp_mul_comba(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR; MP_PRIVATE mp_err s_mp_div_recursive(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r) MP_WUR;
MP_PRIVATE mp_err s_mp_div_school(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) MP_WUR;
MP_PRIVATE mp_err s_mp_div_small(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) MP_WUR;
MP_PRIVATE mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
MP_PRIVATE mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
MP_PRIVATE mp_err s_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
MP_PRIVATE mp_err s_mp_invmod_odd(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
MP_PRIVATE mp_err s_mp_log(const mp_int *a, uint32_t base, uint32_t *c) MP_WUR;
MP_PRIVATE mp_err s_mp_montgomery_reduce_comba(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR;
MP_PRIVATE mp_err s_mp_mul(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR; MP_PRIVATE mp_err s_mp_mul(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
MP_PRIVATE mp_err s_mp_mul_high_comba(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
MP_PRIVATE mp_err s_mp_mul_high(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
MP_PRIVATE mp_err s_mp_sqr_comba(const mp_int *a, mp_int *b) MP_WUR;
MP_PRIVATE mp_err s_mp_sqr(const mp_int *a, mp_int *b) MP_WUR;
MP_PRIVATE mp_err s_mp_mul_balance(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; MP_PRIVATE mp_err s_mp_mul_balance(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
MP_PRIVATE mp_err s_mp_mul_comba(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
MP_PRIVATE mp_err s_mp_mul_high(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
MP_PRIVATE mp_err s_mp_mul_high_comba(const mp_int *a, const mp_int *b, mp_int *c, int digs) MP_WUR;
MP_PRIVATE mp_err s_mp_mul_karatsuba(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; MP_PRIVATE mp_err s_mp_mul_karatsuba(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
MP_PRIVATE mp_err s_mp_mul_toom(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; MP_PRIVATE mp_err s_mp_mul_toom(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, bool *result) MP_WUR;
MP_PRIVATE mp_err s_mp_rand_platform(void *p, size_t n) MP_WUR;
MP_PRIVATE mp_err s_mp_sqr(const mp_int *a, mp_int *b) MP_WUR;
MP_PRIVATE mp_err s_mp_sqr_comba(const mp_int *a, mp_int *b) MP_WUR;
MP_PRIVATE mp_err s_mp_sqr_karatsuba(const mp_int *a, mp_int *b) MP_WUR; MP_PRIVATE mp_err s_mp_sqr_karatsuba(const mp_int *a, mp_int *b) MP_WUR;
MP_PRIVATE mp_err s_mp_sqr_toom(const mp_int *a, mp_int *b) MP_WUR; MP_PRIVATE mp_err s_mp_sqr_toom(const mp_int *a, mp_int *b) MP_WUR;
MP_PRIVATE mp_err s_mp_invmod_odd(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; MP_PRIVATE mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR;
MP_PRIVATE mp_err s_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; MP_PRIVATE uint32_t s_mp_log_pow2(const mp_int *a, uint32_t base) MP_WUR;
MP_PRIVATE mp_err s_mp_montgomery_reduce_comba(mp_int *x, const mp_int *n, mp_digit rho) MP_WUR; MP_PRIVATE void s_mp_copy_digs(mp_digit *d, const mp_digit *s, int digits);
MP_PRIVATE mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
MP_PRIVATE mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR;
MP_PRIVATE mp_err s_mp_rand_platform(void *p, size_t n) MP_WUR;
MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, bool *result);
MP_PRIVATE mp_digit s_mp_log_d(mp_digit base, mp_digit n);
MP_PRIVATE mp_err s_mp_log(const mp_int *a, uint32_t base, uint32_t *c);
MP_PRIVATE uint32_t s_mp_log_pow2(const mp_int *a, uint32_t base);
MP_PRIVATE mp_err s_mp_div_recursive(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r);
MP_PRIVATE mp_err s_mp_div_school(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d);
MP_PRIVATE mp_err s_mp_div_small(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d);
MP_PRIVATE void s_mp_zero_buf(void *mem, size_t size); MP_PRIVATE void s_mp_zero_buf(void *mem, size_t size);
MP_PRIVATE void s_mp_zero_digs(mp_digit *d, int digits); MP_PRIVATE void s_mp_zero_digs(mp_digit *d, int digits);
MP_PRIVATE void s_mp_copy_digs(mp_digit *d, const mp_digit *s, int digits);
/* TODO: jenkins prng is not thread safe as of now */ /* TODO: jenkins prng is not thread safe as of now */
MP_PRIVATE mp_err s_mp_rand_jenkins(void *p, size_t n) MP_WUR; MP_PRIVATE mp_err s_mp_rand_jenkins(void *p, size_t n) MP_WUR;

View File

@ -75,7 +75,6 @@
* like removing support for even moduli, etc... * like removing support for even moduli, etc...
*/ */
# ifdef LTM_LAST # ifdef LTM_LAST
# undef MP_DIV_3_C
# undef MP_DR_IS_MODULUS_C # undef MP_DR_IS_MODULUS_C
# undef MP_DR_REDUCE_C # undef MP_DR_REDUCE_C
# undef MP_DR_SETUP_C # undef MP_DR_SETUP_C
@ -83,6 +82,7 @@
# undef MP_REDUCE_2K_SETUP_C # undef MP_REDUCE_2K_SETUP_C
# undef MP_REDUCE_IS_2K_C # undef MP_REDUCE_IS_2K_C
# undef MP_REDUCE_SETUP_C # undef MP_REDUCE_SETUP_C
# undef S_MP_DIV_3_C
# undef S_MP_EXPTMOD_C # undef S_MP_EXPTMOD_C
# undef S_MP_INVMOD_ODD_C # undef S_MP_INVMOD_ODD_C
# undef S_MP_MUL_BALANCE_C # undef S_MP_MUL_BALANCE_C