简体   繁体   中英

Bignum library, slow prime generator

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM