mpz_powm memory usage vs speed
16 Nov 2002 12:34:47 +0100
I needed to run a Miller-Rabin test for a number of about 150000
bits. That main computation for that is in mpz_powm with
exponent and mod argument both 150000 bits.
For this computation, mpz_powm allocates more than 70 MB of
temporary memory. This memory is used for a table of 2048
precomputed powers of my base.
The actual exponentiation then takes (at least) 12 bits from the
exponent per iteration, indexing the precomputed table for the
right value to multiply into the result. And then it performs 12
My question is:
How was the table size vs execution speed determined for this
code? If I understand correctly, for every extra bit we take
from the exponent per iteration, the table allocation doubles.
But once we take more than a few bits per iteration, the
dominating work will be squarings.
By my reckoning, going from 11 bits to 12 bits saves about 1% of
the execution time, while it needs 36 MB extra space (for this
Time savings table (assuming a squaring cost 2/3 of a full
1=>2 0% (only half of bits are set)
Is my analysis correct? If it is, I'd suggest that we change
this code to never take more than, say, 9 bits from the exponent.