Reputation: 393
I am developing a bignum library: http://pastebin.com/nFgF3zjW
I implemented the Miller-Rabin algorithm (isprime()
), but it is extremely slow, compared to for example OpenSSL's BN_is_prime_fasttest.
I tried profiling and the functions that are executed the most are bn_shr_atomic
and bn_cmp
.
Any idea how I can make this faster?
Upvotes: 0
Views: 793
Reputation: 52304
Big guess: If you really want to use your own library, I'd first replace the division algorithm by the long division.
To validate my guess: you have cmp and shr in the inner loop of your divison, are those calls the major contributor in your profile or do they come from somewhere else? In general, when you profile you should first look at higher level functions which are big contributor, changing algorithms there is usually more benefical than tuning low level functions.
Upvotes: 0
Reputation: 5540
The GNU Multiple Precision Arithmetic library implements Miller-Rabin. It's documentation is located here:
http://gmplib.org/manual/Number-Theoretic-Functions.html#Number-Theoretic-Functions
I would suggest examining their implementation for pointers on speeding up your computation. However, arbitrary precision arithmetic is inherently going to be slower than working with numbers that fit in registers.
Edit:
There is also a trade-off between the algorithm used and the quality of the resulting probability. That said, I'm not sure what test OpenSSL uses.
Upvotes: 1