From eb757f1071719d2258bc5c62b7920a1563bc6684 Mon Sep 17 00:00:00 2001 From: czurnieden Date: Wed, 27 Nov 2019 18:02:08 +0100 Subject: [PATCH 1/3] Removed Fermat and frobenius_underwood tests; made mp_prime_strong_lucas_selfridge private --- demo/test.c | 12 +- libtommath_VS2008.vcproj | 16 +-- makefile | 12 +- makefile.mingw | 12 +- makefile.msvc | 12 +- makefile.shared | 12 +- makefile.unix | 12 +- mp_prime_fermat.c | 41 ------ mp_prime_frobenius_underwood.c | 127 ------------------ mp_prime_is_prime.c | 17 +-- ...e.c => s_mp_prime_strong_lucas_selfridge.c | 8 +- tommath.h | 16 +-- tommath_class.h | 87 ++++-------- tommath_private.h | 6 + tommath_superclass.h | 1 - 15 files changed, 81 insertions(+), 310 deletions(-) delete mode 100644 mp_prime_fermat.c delete mode 100644 mp_prime_frobenius_underwood.c rename mp_prime_strong_lucas_selfridge.c => s_mp_prime_strong_lucas_selfridge.c (98%) diff --git a/demo/test.c b/demo/test.c index e1b6f0fa6..0e4ca513c 100644 --- a/demo/test.c +++ b/demo/test.c @@ -862,7 +862,7 @@ static int test_mp_prime_is_prime(void) { int ix; mp_err e; - bool cnt, fu; + bool cnt; mp_int a, b; DOR(mp_init_multi(&a, &b, NULL)); @@ -923,10 +923,6 @@ static int test_mp_prime_is_prime(void) if (!cnt) { printf("\nsub is not prime!\n"); } - DO(mp_prime_frobenius_underwood(&b, &fu)); - if (!fu) { - printf("\nfrobenius-underwood says sub is not prime!\n"); - } if ((e != MP_OKAY) || !cnt) { printf("prime tested was: 0x"); DO(mp_fwrite(&a,16,stdout)); @@ -942,14 +938,14 @@ static int test_mp_prime_is_prime(void) DO(mp_read_radix(&a, "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF", 16)); - e = mp_prime_strong_lucas_selfridge(&a, &cnt); + e = s_mp_prime_strong_lucas_selfridge(&a, &cnt); /* small problem */ if (e != MP_OKAY) { - printf("\nmp_prime_strong_lucas_selfridge failed with error: %s\n", mp_error_to_string(e)); + printf("\ns_mp_prime_strong_lucas_selfridge failed with error: %s\n", mp_error_to_string(e)); } /* large problem */ if (!cnt) { - printf("\n\nissue #143 - mp_prime_strong_lucas_selfridge FAILED!\n"); + printf("\n\nissue #143 - s_mp_prime_strong_lucas_selfridge FAILED!\n"); } if ((e != MP_OKAY) || !cnt) { printf("prime tested was: 0x"); diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj index 6f1a42355..c999c2556 100644 --- a/libtommath_VS2008.vcproj +++ b/libtommath_VS2008.vcproj @@ -604,14 +604,6 @@ RelativePath="mp_pack_count.c" > - - - - @@ -632,10 +624,6 @@ RelativePath="mp_prime_rand.c" > - - @@ -876,6 +864,10 @@ RelativePath="s_mp_prime_is_divisible.c" > + + diff --git a/makefile b/makefile index 63930bc97..b17eaa8b4 100644 --- a/makefile +++ b/makefile @@ -35,10 +35,9 @@ mp_get_mag_u64.o mp_get_mag_ul.o mp_grow.o mp_init.o mp_init_copy.o mp_init_i32. mp_init_multi.o mp_init_set.o mp_init_size.o mp_init_u32.o mp_init_u64.o mp_init_ul.o mp_invmod.o \ mp_is_square.o mp_kronecker.o mp_lcm.o mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o \ mp_montgomery_calc_normalization.o mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o \ -mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o \ -mp_prime_frobenius_underwood.o mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o \ -mp_prime_rabin_miller_trials.o mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_radix_size.o \ -mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o \ +mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_is_prime.o \ +mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o \ +mp_radix_size.o mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o \ mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_n.o mp_rshd.o \ mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_u32.o mp_set_u64.o \ mp_set_ul.o mp_shrink.o mp_signed_rsh.o mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o \ @@ -47,8 +46,9 @@ s_mp_copy_digs.o s_mp_div_3.o s_mp_div_recursive.o s_mp_div_school.o s_mp_div_sm s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_2expt.o \ s_mp_log_d.o s_mp_montgomery_reduce_comba.o s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o \ s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o s_mp_mul_toom.o s_mp_prime_is_divisible.o \ -s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o \ -s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o s_mp_zero_buf.o s_mp_zero_digs.o +s_mp_prime_strong_lucas_selfridge.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o \ +s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \ +s_mp_zero_buf.o s_mp_zero_digs.o #END_INS diff --git a/makefile.mingw b/makefile.mingw index ae98a5c86..a774f70d1 100644 --- a/makefile.mingw +++ b/makefile.mingw @@ -37,10 +37,9 @@ mp_get_mag_u64.o mp_get_mag_ul.o mp_grow.o mp_init.o mp_init_copy.o mp_init_i32. mp_init_multi.o mp_init_set.o mp_init_size.o mp_init_u32.o mp_init_u64.o mp_init_ul.o mp_invmod.o \ mp_is_square.o mp_kronecker.o mp_lcm.o mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o \ mp_montgomery_calc_normalization.o mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o \ -mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o \ -mp_prime_frobenius_underwood.o mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o \ -mp_prime_rabin_miller_trials.o mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_radix_size.o \ -mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o \ +mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_is_prime.o \ +mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o \ +mp_radix_size.o mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o \ mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_n.o mp_rshd.o \ mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_u32.o mp_set_u64.o \ mp_set_ul.o mp_shrink.o mp_signed_rsh.o mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o \ @@ -49,8 +48,9 @@ s_mp_copy_digs.o s_mp_div_3.o s_mp_div_recursive.o s_mp_div_school.o s_mp_div_sm s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_2expt.o \ s_mp_log_d.o s_mp_montgomery_reduce_comba.o s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o \ s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o s_mp_mul_toom.o s_mp_prime_is_divisible.o \ -s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o \ -s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o s_mp_zero_buf.o s_mp_zero_digs.o +s_mp_prime_strong_lucas_selfridge.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o \ +s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \ +s_mp_zero_buf.o s_mp_zero_digs.o HEADERS_PUB=tommath.h HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB) diff --git a/makefile.msvc b/makefile.msvc index 7dcbf3df1..e43560dab 100644 --- a/makefile.msvc +++ b/makefile.msvc @@ -30,10 +30,9 @@ mp_get_mag_u64.obj mp_get_mag_ul.obj mp_grow.obj mp_init.obj mp_init_copy.obj mp mp_init_multi.obj mp_init_set.obj mp_init_size.obj mp_init_u32.obj mp_init_u64.obj mp_init_ul.obj mp_invmod.obj \ mp_is_square.obj mp_kronecker.obj mp_lcm.obj mp_log_n.obj mp_lshd.obj mp_mod.obj mp_mod_2d.obj \ mp_montgomery_calc_normalization.obj mp_montgomery_reduce.obj mp_montgomery_setup.obj mp_mul.obj mp_mul_2.obj \ -mp_mul_2d.obj mp_mul_d.obj mp_mulmod.obj mp_neg.obj mp_or.obj mp_pack.obj mp_pack_count.obj mp_prime_fermat.obj \ -mp_prime_frobenius_underwood.obj mp_prime_is_prime.obj mp_prime_miller_rabin.obj mp_prime_next_prime.obj \ -mp_prime_rabin_miller_trials.obj mp_prime_rand.obj mp_prime_strong_lucas_selfridge.obj mp_radix_size.obj \ -mp_rand.obj mp_read_radix.obj mp_reduce.obj mp_reduce_2k.obj mp_reduce_2k_l.obj mp_reduce_2k_setup.obj \ +mp_mul_2d.obj mp_mul_d.obj mp_mulmod.obj mp_neg.obj mp_or.obj mp_pack.obj mp_pack_count.obj mp_prime_is_prime.obj \ +mp_prime_miller_rabin.obj mp_prime_next_prime.obj mp_prime_rabin_miller_trials.obj mp_prime_rand.obj \ +mp_radix_size.obj mp_rand.obj mp_read_radix.obj mp_reduce.obj mp_reduce_2k.obj mp_reduce_2k_l.obj mp_reduce_2k_setup.obj \ mp_reduce_2k_setup_l.obj mp_reduce_is_2k.obj mp_reduce_is_2k_l.obj mp_reduce_setup.obj mp_root_n.obj mp_rshd.obj \ mp_sbin_size.obj mp_set.obj mp_set_double.obj mp_set_i32.obj mp_set_i64.obj mp_set_l.obj mp_set_u32.obj mp_set_u64.obj \ mp_set_ul.obj mp_shrink.obj mp_signed_rsh.obj mp_sqrmod.obj mp_sqrt.obj mp_sqrtmod_prime.obj mp_sub.obj mp_sub_d.obj \ @@ -42,8 +41,9 @@ s_mp_copy_digs.obj s_mp_div_3.obj s_mp_div_recursive.obj s_mp_div_school.obj s_m s_mp_exptmod_fast.obj s_mp_get_bit.obj s_mp_invmod.obj s_mp_invmod_odd.obj s_mp_log.obj s_mp_log_2expt.obj \ s_mp_log_d.obj s_mp_montgomery_reduce_comba.obj s_mp_mul.obj s_mp_mul_balance.obj s_mp_mul_comba.obj \ s_mp_mul_high.obj s_mp_mul_high_comba.obj s_mp_mul_karatsuba.obj s_mp_mul_toom.obj s_mp_prime_is_divisible.obj \ -s_mp_prime_tab.obj s_mp_radix_map.obj s_mp_rand_jenkins.obj s_mp_rand_platform.obj s_mp_sqr.obj s_mp_sqr_comba.obj \ -s_mp_sqr_karatsuba.obj s_mp_sqr_toom.obj s_mp_sub.obj s_mp_zero_buf.obj s_mp_zero_digs.obj +s_mp_prime_strong_lucas_selfridge.obj s_mp_prime_tab.obj s_mp_radix_map.obj s_mp_rand_jenkins.obj \ +s_mp_rand_platform.obj s_mp_sqr.obj s_mp_sqr_comba.obj s_mp_sqr_karatsuba.obj s_mp_sqr_toom.obj s_mp_sub.obj \ +s_mp_zero_buf.obj s_mp_zero_digs.obj HEADERS_PUB=tommath.h HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB) diff --git a/makefile.shared b/makefile.shared index 2e24a43fb..71e753243 100644 --- a/makefile.shared +++ b/makefile.shared @@ -32,10 +32,9 @@ mp_get_mag_u64.o mp_get_mag_ul.o mp_grow.o mp_init.o mp_init_copy.o mp_init_i32. mp_init_multi.o mp_init_set.o mp_init_size.o mp_init_u32.o mp_init_u64.o mp_init_ul.o mp_invmod.o \ mp_is_square.o mp_kronecker.o mp_lcm.o mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o \ mp_montgomery_calc_normalization.o mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o \ -mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o \ -mp_prime_frobenius_underwood.o mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o \ -mp_prime_rabin_miller_trials.o mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_radix_size.o \ -mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o \ +mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_is_prime.o \ +mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o \ +mp_radix_size.o mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o \ mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_n.o mp_rshd.o \ mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_u32.o mp_set_u64.o \ mp_set_ul.o mp_shrink.o mp_signed_rsh.o mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o \ @@ -44,8 +43,9 @@ s_mp_copy_digs.o s_mp_div_3.o s_mp_div_recursive.o s_mp_div_school.o s_mp_div_sm s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_2expt.o \ s_mp_log_d.o s_mp_montgomery_reduce_comba.o s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o \ s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o s_mp_mul_toom.o s_mp_prime_is_divisible.o \ -s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o \ -s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o s_mp_zero_buf.o s_mp_zero_digs.o +s_mp_prime_strong_lucas_selfridge.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o \ +s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \ +s_mp_zero_buf.o s_mp_zero_digs.o #END_INS diff --git a/makefile.unix b/makefile.unix index 2b2589c98..cf3fc902b 100644 --- a/makefile.unix +++ b/makefile.unix @@ -38,10 +38,9 @@ mp_get_mag_u64.o mp_get_mag_ul.o mp_grow.o mp_init.o mp_init_copy.o mp_init_i32. mp_init_multi.o mp_init_set.o mp_init_size.o mp_init_u32.o mp_init_u64.o mp_init_ul.o mp_invmod.o \ mp_is_square.o mp_kronecker.o mp_lcm.o mp_log_n.o mp_lshd.o mp_mod.o mp_mod_2d.o \ mp_montgomery_calc_normalization.o mp_montgomery_reduce.o mp_montgomery_setup.o mp_mul.o mp_mul_2.o \ -mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_fermat.o \ -mp_prime_frobenius_underwood.o mp_prime_is_prime.o mp_prime_miller_rabin.o mp_prime_next_prime.o \ -mp_prime_rabin_miller_trials.o mp_prime_rand.o mp_prime_strong_lucas_selfridge.o mp_radix_size.o \ -mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o \ +mp_mul_2d.o mp_mul_d.o mp_mulmod.o mp_neg.o mp_or.o mp_pack.o mp_pack_count.o mp_prime_is_prime.o \ +mp_prime_miller_rabin.o mp_prime_next_prime.o mp_prime_rabin_miller_trials.o mp_prime_rand.o \ +mp_radix_size.o mp_rand.o mp_read_radix.o mp_reduce.o mp_reduce_2k.o mp_reduce_2k_l.o mp_reduce_2k_setup.o \ mp_reduce_2k_setup_l.o mp_reduce_is_2k.o mp_reduce_is_2k_l.o mp_reduce_setup.o mp_root_n.o mp_rshd.o \ mp_sbin_size.o mp_set.o mp_set_double.o mp_set_i32.o mp_set_i64.o mp_set_l.o mp_set_u32.o mp_set_u64.o \ mp_set_ul.o mp_shrink.o mp_signed_rsh.o mp_sqrmod.o mp_sqrt.o mp_sqrtmod_prime.o mp_sub.o mp_sub_d.o \ @@ -50,8 +49,9 @@ s_mp_copy_digs.o s_mp_div_3.o s_mp_div_recursive.o s_mp_div_school.o s_mp_div_sm s_mp_exptmod_fast.o s_mp_get_bit.o s_mp_invmod.o s_mp_invmod_odd.o s_mp_log.o s_mp_log_2expt.o \ s_mp_log_d.o s_mp_montgomery_reduce_comba.o s_mp_mul.o s_mp_mul_balance.o s_mp_mul_comba.o \ s_mp_mul_high.o s_mp_mul_high_comba.o s_mp_mul_karatsuba.o s_mp_mul_toom.o s_mp_prime_is_divisible.o \ -s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o \ -s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o s_mp_zero_buf.o s_mp_zero_digs.o +s_mp_prime_strong_lucas_selfridge.o s_mp_prime_tab.o s_mp_radix_map.o s_mp_rand_jenkins.o \ +s_mp_rand_platform.o s_mp_sqr.o s_mp_sqr_comba.o s_mp_sqr_karatsuba.o s_mp_sqr_toom.o s_mp_sub.o \ +s_mp_zero_buf.o s_mp_zero_digs.o HEADERS_PUB=tommath.h HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB) diff --git a/mp_prime_fermat.c b/mp_prime_fermat.c deleted file mode 100644 index ac8116fef..000000000 --- a/mp_prime_fermat.c +++ /dev/null @@ -1,41 +0,0 @@ -#include "tommath_private.h" -#ifdef MP_PRIME_FERMAT_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* performs one Fermat test. - * - * If "a" were prime then b**a == b (mod a) since the order of - * the multiplicative sub-group would be phi(a) = a-1. That means - * it would be the same as b**(a mod (a-1)) == b**1 == b (mod a). - * - * Sets result to 1 if the congruence holds, or zero otherwise. - */ -mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, bool *result) -{ - mp_int t; - mp_err err; - - /* ensure b > 1 */ - if (mp_cmp_d(b, 1uL) != MP_GT) { - return MP_VAL; - } - - /* init t */ - if ((err = mp_init(&t)) != MP_OKAY) { - return err; - } - - /* compute t = b**a mod a */ - if ((err = mp_exptmod(b, a, a, &t)) != MP_OKAY) { - goto LBL_ERR; - } - - /* is it equal to b? */ - *result = mp_cmp(&t, b) == MP_EQ; - -LBL_ERR: - mp_clear(&t); - return err; -} -#endif diff --git a/mp_prime_frobenius_underwood.c b/mp_prime_frobenius_underwood.c deleted file mode 100644 index 62d3476a9..000000000 --- a/mp_prime_frobenius_underwood.c +++ /dev/null @@ -1,127 +0,0 @@ -#include "tommath_private.h" -#ifdef MP_PRIME_FROBENIUS_UNDERWOOD_C - -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* - * See file mp_prime_is_prime.c or the documentation in doc/bn.tex for the details - */ -#ifndef LTM_USE_ONLY_MR - -/* - * floor of positive solution of - * (2^16)-1 = (a+4)*(2*a+5) - * TODO: Both values are smaller than N^(1/4), would have to use a bigint - * for a instead but any a biger than about 120 are already so rare that - * it is possible to ignore them and still get enough pseudoprimes. - * But it is still a restriction of the set of available pseudoprimes - * which makes this implementation less secure if used stand-alone. - */ -#define LTM_FROBENIUS_UNDERWOOD_A 32764 - -mp_err mp_prime_frobenius_underwood(const mp_int *N, bool *result) -{ - mp_int T1z, T2z, Np1z, sz, tz; - int a, ap2, i; - mp_err err; - - if ((err = mp_init_multi(&T1z, &T2z, &Np1z, &sz, &tz, NULL)) != MP_OKAY) { - return err; - } - - for (a = 0; a < LTM_FROBENIUS_UNDERWOOD_A; a++) { - int j; - - /* TODO: That's ugly! No, really, it is! */ - if ((a==2) || (a==4) || (a==7) || (a==8) || (a==10) || - (a==14) || (a==18) || (a==23) || (a==26) || (a==28)) { - continue; - } - - mp_set_i32(&T1z, (int32_t)((a * a) - 4)); - - if ((err = mp_kronecker(&T1z, N, &j)) != MP_OKAY) goto LBL_END; - - if (j == -1) { - break; - } - - if (j == 0) { - /* composite */ - *result = false; - goto LBL_END; - } - } - /* Tell it a composite and set return value accordingly */ - if (a >= LTM_FROBENIUS_UNDERWOOD_A) { - err = MP_ITER; - goto LBL_END; - } - /* Composite if N and (a+4)*(2*a+5) are not coprime */ - mp_set_u32(&T1z, (uint32_t)((a+4)*((2*a)+5))); - - if ((err = mp_gcd(N, &T1z, &T1z)) != MP_OKAY) goto LBL_END; - - if (!((T1z.used == 1) && (T1z.dp[0] == 1u))) { - /* composite */ - *result = false; - goto LBL_END; - } - - ap2 = a + 2; - if ((err = mp_add_d(N, 1uL, &Np1z)) != MP_OKAY) goto LBL_END; - - mp_set(&sz, 1uL); - mp_set(&tz, 2uL); - - for (i = mp_count_bits(&Np1z) - 2; i >= 0; i--) { - /* - * temp = (sz*(a*sz+2*tz))%N; - * tz = ((tz-sz)*(tz+sz))%N; - * sz = temp; - */ - if ((err = mp_mul_2(&tz, &T2z)) != MP_OKAY) goto LBL_END; - - /* a = 0 at about 50% of the cases (non-square and odd input) */ - if (a != 0) { - if ((err = mp_mul_d(&sz, (mp_digit)a, &T1z)) != MP_OKAY) goto LBL_END; - if ((err = mp_add(&T1z, &T2z, &T2z)) != MP_OKAY) goto LBL_END; - } - - if ((err = mp_mul(&T2z, &sz, &T1z)) != MP_OKAY) goto LBL_END; - if ((err = mp_sub(&tz, &sz, &T2z)) != MP_OKAY) goto LBL_END; - if ((err = mp_add(&sz, &tz, &sz)) != MP_OKAY) goto LBL_END; - if ((err = mp_mul(&sz, &T2z, &tz)) != MP_OKAY) goto LBL_END; - if ((err = mp_mod(&tz, N, &tz)) != MP_OKAY) goto LBL_END; - if ((err = mp_mod(&T1z, N, &sz)) != MP_OKAY) goto LBL_END; - if (s_mp_get_bit(&Np1z, i)) { - /* - * temp = (a+2) * sz + tz - * tz = 2 * tz - sz - * sz = temp - */ - if (a == 0) { - if ((err = mp_mul_2(&sz, &T1z)) != MP_OKAY) goto LBL_END; - } else { - if ((err = mp_mul_d(&sz, (mp_digit)ap2, &T1z)) != MP_OKAY) goto LBL_END; - } - if ((err = mp_add(&T1z, &tz, &T1z)) != MP_OKAY) goto LBL_END; - if ((err = mp_mul_2(&tz, &T2z)) != MP_OKAY) goto LBL_END; - if ((err = mp_sub(&T2z, &sz, &tz)) != MP_OKAY) goto LBL_END; - mp_exch(&sz, &T1z); - } - } - - mp_set_u32(&T1z, (uint32_t)((2 * a) + 5)); - if ((err = mp_mod(&T1z, N, &T1z)) != MP_OKAY) goto LBL_END; - - *result = mp_iszero(&sz) && (mp_cmp(&tz, &T1z) == MP_EQ); - -LBL_END: - mp_clear_multi(&tz, &sz, &Np1z, &T2z, &T1z, NULL); - return err; -} - -#endif -#endif diff --git a/mp_prime_is_prime.c b/mp_prime_is_prime.c index 7d73864c7..addc5b2f3 100644 --- a/mp_prime_is_prime.c +++ b/mp_prime_is_prime.c @@ -92,28 +92,17 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result) } /* - * Both, the Frobenius-Underwood test and the the Lucas-Selfridge test are quite - * slow so if speed is an issue, define LTM_USE_ONLY_MR to use M-R tests with - * bases 2, 3 and t random bases. + * The Lucas-Selfridge test is quite slow so if speed is an issue, define LTM_USE_ONLY_MR + * to use M-R tests with bases 2, 3 and t random bases. */ #ifndef LTM_USE_ONLY_MR if (t >= 0) { -#ifdef LTM_USE_FROBENIUS_TEST - err = mp_prime_frobenius_underwood(a, &res); - if ((err != MP_OKAY) && (err != MP_ITER)) { + if ((err = s_mp_prime_strong_lucas_selfridge(a, &res)) != MP_OKAY) { goto LBL_B; } if (!res) { goto LBL_B; } -#else - if ((err = mp_prime_strong_lucas_selfridge(a, &res)) != MP_OKAY) { - goto LBL_B; - } - if (!res) { - goto LBL_B; - } -#endif } #endif diff --git a/mp_prime_strong_lucas_selfridge.c b/s_mp_prime_strong_lucas_selfridge.c similarity index 98% rename from mp_prime_strong_lucas_selfridge.c rename to s_mp_prime_strong_lucas_selfridge.c index ffbd9d34f..9c335869d 100644 --- a/mp_prime_strong_lucas_selfridge.c +++ b/s_mp_prime_strong_lucas_selfridge.c @@ -1,9 +1,10 @@ #include "tommath_private.h" -#ifdef MP_PRIME_STRONG_LUCAS_SELFRIDGE_C - +#ifdef S_MP_PRIME_STRONG_LUCAS_SELFRIDGE_C /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ + + /* * See file mp_prime_is_prime.c or the documentation in doc/bn.tex for the details */ @@ -48,7 +49,7 @@ static mp_err s_mul_si(const mp_int *a, int32_t d, mp_int *c) (If that name sounds familiar, he is the guy who found the fdiv bug in the Pentium (P5x, I think) Intel processor) */ -mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result) +mp_err s_mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result) { /* CZ TODO: choose better variable names! */ mp_int Dz, gcd, Np1, Uz, Vz, U2mz, V2mz, Qmz, Q2mz, Qkdz, T1z, T2z, T3z, T4z, Q2kdz; @@ -278,4 +279,5 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result) return err; } #endif + #endif diff --git a/tommath.h b/tommath.h index 95f7127b4..8b1f19734 100644 --- a/tommath.h +++ b/tommath.h @@ -1,4 +1,4 @@ -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +//* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ #ifndef TOMMATH_H_ @@ -489,11 +489,6 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) /* ---> Primes <--- */ -/* performs one Fermat test of "a" using base "b". - * Sets result to 0 if composite or 1 if probable prime - */ -mp_err mp_prime_fermat(const mp_int *a, const mp_int *b, bool *result) MP_WUR; - /* performs one Miller-Rabin test of "a" using base "b". * Sets result to 0 if composite or 1 if probable prime */ @@ -504,15 +499,6 @@ mp_err mp_prime_miller_rabin(const mp_int *a, const mp_int *b, bool *result) MP_ */ int mp_prime_rabin_miller_trials(int size) MP_WUR; -/* performs one strong Lucas-Selfridge test of "a". - * Sets result to 0 if composite or 1 if probable prime - */ -mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result) MP_WUR; - -/* performs one Frobenius test of "a" as described by Paul Underwood. - * Sets result to 0 if composite or 1 if probable prime - */ -mp_err mp_prime_frobenius_underwood(const mp_int *N, bool *result) MP_WUR; /* performs t random rounds of Miller-Rabin on "a" additional to * bases 2 and 3. Also performs an initial sieve of trial diff --git a/tommath_class.h b/tommath_class.h index 936a17e46..b7107f254 100644 --- a/tommath_class.h +++ b/tommath_class.h @@ -84,14 +84,11 @@ # define MP_OR_C # define MP_PACK_C # define MP_PACK_COUNT_C -# define MP_PRIME_FERMAT_C -# define MP_PRIME_FROBENIUS_UNDERWOOD_C # define MP_PRIME_IS_PRIME_C # define MP_PRIME_MILLER_RABIN_C # define MP_PRIME_NEXT_PRIME_C # define MP_PRIME_RABIN_MILLER_TRIALS_C # define MP_PRIME_RAND_C -# define MP_PRIME_STRONG_LUCAS_SELFRIDGE_C # define MP_RADIX_SIZE_C # define MP_RAND_C # define MP_READ_RADIX_C @@ -152,6 +149,7 @@ # define S_MP_MUL_KARATSUBA_C # define S_MP_MUL_TOOM_C # define S_MP_PRIME_IS_DIVISIBLE_C +# define S_MP_PRIME_STRONG_LUCAS_SELFRIDGE_C # define S_MP_PRIME_TAB_C # define S_MP_RADIX_MAP_C # define S_MP_RAND_JENKINS_C @@ -575,35 +573,6 @@ # define MP_COUNT_BITS_C #endif -#if defined(MP_PRIME_FERMAT_C) -# define MP_CLEAR_C -# define MP_CMP_C -# define MP_CMP_D_C -# define MP_EXPTMOD_C -# define MP_INIT_C -#endif - -#if defined(MP_PRIME_FROBENIUS_UNDERWOOD_C) -# define MP_ADD_C -# define MP_ADD_D_C -# define MP_CLEAR_MULTI_C -# define MP_CMP_C -# define MP_COUNT_BITS_C -# define MP_EXCH_C -# define MP_GCD_C -# define MP_INIT_MULTI_C -# define MP_KRONECKER_C -# define MP_MOD_C -# define MP_MUL_2_C -# define MP_MUL_C -# define MP_MUL_D_C -# define MP_SET_C -# define MP_SET_I32_C -# define MP_SET_U32_C -# define MP_SUB_C -# define S_MP_GET_BIT_C -#endif - #if defined(MP_PRIME_IS_PRIME_C) # define MP_CLEAR_C # define MP_CMP_C @@ -613,11 +582,11 @@ # define MP_INIT_SET_C # define MP_IS_SQUARE_C # define MP_PRIME_MILLER_RABIN_C -# define MP_PRIME_STRONG_LUCAS_SELFRIDGE_C # define MP_RAND_C # define MP_READ_RADIX_C # define MP_SET_C # define S_MP_PRIME_IS_DIVISIBLE_C +# define S_MP_PRIME_STRONG_LUCAS_SELFRIDGE_C #endif #if defined(MP_PRIME_MILLER_RABIN_C) @@ -658,32 +627,6 @@ # define S_MP_ZERO_BUF_C #endif -#if defined(MP_PRIME_STRONG_LUCAS_SELFRIDGE_C) -# define MP_ADD_C -# define MP_ADD_D_C -# define MP_CLEAR_C -# define MP_CLEAR_MULTI_C -# define MP_CMP_C -# define MP_CMP_D_C -# define MP_CNT_LSB_C -# define MP_COUNT_BITS_C -# define MP_DIV_2D_C -# define MP_DIV_2_C -# define MP_GCD_C -# define MP_INIT_C -# define MP_INIT_MULTI_C -# define MP_KRONECKER_C -# define MP_MOD_C -# define MP_MUL_2_C -# define MP_MUL_C -# define MP_SET_C -# define MP_SET_I32_C -# define MP_SET_U32_C -# define MP_SUB_C -# define MP_SUB_D_C -# define S_MP_GET_BIT_C -#endif - #if defined(MP_RADIX_SIZE_C) # define MP_LOG_N_C #endif @@ -1166,6 +1109,32 @@ # define MP_DIV_D_C #endif +#if defined(S_MP_PRIME_STRONG_LUCAS_SELFRIDGE_C) +# define MP_ADD_C +# define MP_ADD_D_C +# define MP_CLEAR_C +# define MP_CLEAR_MULTI_C +# define MP_CMP_C +# define MP_CMP_D_C +# define MP_CNT_LSB_C +# define MP_COUNT_BITS_C +# define MP_DIV_2D_C +# define MP_DIV_2_C +# define MP_GCD_C +# define MP_INIT_C +# define MP_INIT_MULTI_C +# define MP_KRONECKER_C +# define MP_MOD_C +# define MP_MUL_2_C +# define MP_MUL_C +# define MP_SET_C +# define MP_SET_I32_C +# define MP_SET_U32_C +# define MP_SUB_C +# define MP_SUB_D_C +# define S_MP_GET_BIT_C +#endif + #if defined(S_MP_PRIME_TAB_C) #endif diff --git a/tommath_private.h b/tommath_private.h index eb566dd58..df23f95c8 100644 --- a/tommath_private.h +++ b/tommath_private.h @@ -193,6 +193,12 @@ MP_PRIVATE void s_mp_copy_digs(mp_digit *d, const mp_digit *s, int digits); MP_PRIVATE void s_mp_zero_buf(void *mem, size_t size); MP_PRIVATE void s_mp_zero_digs(mp_digit *d, int digits); +/* performs one strong Lucas-Selfridge test of "a". + * Sets result to 0 if composite or 1 if probable prime + */ +mp_err s_mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result) MP_WUR; + + /* 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 void s_mp_rand_jenkins_init(uint64_t seed); diff --git a/tommath_superclass.h b/tommath_superclass.h index 9e85d9865..b466305ba 100644 --- a/tommath_superclass.h +++ b/tommath_superclass.h @@ -26,7 +26,6 @@ # define MP_ISEVEN_C # define MP_ISODD_C # define MP_NEG_C -# define MP_PRIME_FROBENIUS_UNDERWOOD_C # define MP_RADIX_SIZE_C # define MP_LOG_N_C # define MP_RAND_C From 14f2f8ebc0b5a5464bc25fc96f7179be39637671 Mon Sep 17 00:00:00 2001 From: czurnieden Date: Wed, 27 Nov 2019 18:06:07 +0100 Subject: [PATCH 2/3] finalized privatization --- tommath_private.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tommath_private.h b/tommath_private.h index df23f95c8..88c784094 100644 --- a/tommath_private.h +++ b/tommath_private.h @@ -196,7 +196,7 @@ MP_PRIVATE void s_mp_zero_digs(mp_digit *d, int digits); /* performs one strong Lucas-Selfridge test of "a". * Sets result to 0 if composite or 1 if probable prime */ -mp_err s_mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result) MP_WUR; +MP_PRIVATE mp_err s_mp_prime_strong_lucas_selfridge(const mp_int *a, bool *result) MP_WUR; /* TODO: jenkins prng is not thread safe as of now */ From 968b0e328356cdc7c63da32c62c28b8e9226bbe3 Mon Sep 17 00:00:00 2001 From: czurnieden Date: Wed, 27 Nov 2019 18:35:16 +0100 Subject: [PATCH 3/3] there was some extra formatting needed --- tommath.def | 3 --- tommath.h | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/tommath.def b/tommath.def index 88733ca2a..a7dcdfeac 100644 --- a/tommath.def +++ b/tommath.def @@ -78,14 +78,11 @@ EXPORTS mp_or mp_pack mp_pack_count - mp_prime_fermat - mp_prime_frobenius_underwood mp_prime_is_prime mp_prime_miller_rabin mp_prime_next_prime mp_prime_rabin_miller_trials mp_prime_rand - mp_prime_strong_lucas_selfridge mp_radix_size mp_rand mp_read_radix diff --git a/tommath.h b/tommath.h index 8b1f19734..f38beb7e1 100644 --- a/tommath.h +++ b/tommath.h @@ -1,4 +1,4 @@ -//* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ #ifndef TOMMATH_H_