Possible to optimize for base 2 fermat primality test using shifts?
thehans at gmail.com
Fri Apr 12 02:44:25 UTC 2019
I've been learning to use GMP library lately and doing some experimenting
with searching for various specific types of prime numbers.
I am currently interested in any ways of increasing throughput over plainly
One technique I've been using to quickly filter out composites and sort of
balance load between pieces of code is to pipe data between various small
utilities using mpz_out_raw/mpz_inp_raw , each of which filter
progressively more in stages, beginning with some trial division, then
moving to a fermat test, etc. If an input doesn't pass test in one
utility, then it omits it from outputting further down the pipeline.
I noticed the mpz_probab_prime_p does a fermat primality test of base 210
before moving on to miller-rabin, with the following code:
/* Perform a Fermat test. */
mpz_set_ui (x, 210L);
mpz_powm (y, x, nm1, n);
if (mpz_cmp_ui (y, 1L) != 0)
Is base 210 significantly stronger than a base 2 test?
I'm wondering because it seems like a specialized function for base 2 might
be more optimizable to filter out weak candidates faster than generalized
mpz_powm used here.
I guess what I'm proposing is a new function signature such as:
void mpz_powm_2exp(mpz_t rop, const mpz_t exp, const mpz_t mod)
Set rop to (2 raised to exp) modulo mod.
I've only just read a little bit about how k-ary window exponentiation
works, the math behind it is still pretty new and difficult to me. So, I'm
not really clear if its directly applicable to drop in lshifts as
replacements for multiplications. But it seems that with k <=
log2(bits_per_limb), e.g. 6 for a 64bit machine, that mpn_lshift might be
applicable. And for higher values, I think the additional bits could be
still be applied by calling mpn_lshift and offsetting the destination limb?
One last thing comment I have is regarding the code for setting k, which
looks up the bitcount in this array:
static mp_bitcnt_t x =
However, I noticed that wikipedia cites an equation for optimal setting of
k, which results in different values than these:
>From citation source:
"For optimal efficiency, k should be equal to the smallest integer
satisfying lg(n) <= k*(k+1)*2^(2k) / (2^(k+1)-k-2))"
Cohen, H.; Frey, G., eds. (2006). *Handbook of Elliptic and Hyperelliptic
I calculated the right hand side for each k up to 53 (k=54 would exceed
64bit unsigned limits), and these are the values I ended up with:
static mp_bitcnt_t x =
Also I used ceil on the result, not sure if that's optimal vs floor or
rounding, but you could also argue that it doesn't make 1 bit of difference
So anyways, I don't know if the rest of the code actually would even work
with k values beyond the existing list, but I thought I would mention that
these might be more optimal.
Any thoughts, comments on this?
More information about the gmp-discuss