Minimize stack utilization in probable_prime.

This commit is contained in:
Andy Polyakov 2007-09-18 20:52:05 +00:00
parent 716b87a026
commit 75a8e30f4f
3 changed files with 8 additions and 4 deletions

View File

@ -377,14 +377,14 @@ static int witness(BIGNUM *w, const BIGNUM *a, const BIGNUM *a1,
static int probable_prime(BIGNUM *rnd, int bits) static int probable_prime(BIGNUM *rnd, int bits)
{ {
int i; int i;
BN_ULONG mods[NUMPRIMES]; prime_t mods[NUMPRIMES];
BN_ULONG delta,maxdelta; BN_ULONG delta,maxdelta;
again: again:
if (!BN_rand(rnd,bits,1,1)) return(0); if (!BN_rand(rnd,bits,1,1)) return(0);
/* we now have a random number 'rand' to test. */ /* we now have a random number 'rand' to test. */
for (i=1; i<NUMPRIMES; i++) for (i=1; i<NUMPRIMES; i++)
mods[i]=BN_mod_word(rnd,(BN_ULONG)primes[i]); mods[i]=(prime_t)BN_mod_word(rnd,(BN_ULONG)primes[i]);
maxdelta=BN_MASK2 - primes[NUMPRIMES-1]; maxdelta=BN_MASK2 - primes[NUMPRIMES-1];
delta=0; delta=0;
loop: for (i=1; i<NUMPRIMES; i++) loop: for (i=1; i<NUMPRIMES; i++)

View File

@ -58,10 +58,12 @@
#ifndef EIGHT_BIT #ifndef EIGHT_BIT
#define NUMPRIMES 2048 #define NUMPRIMES 2048
typedef unsigned short prime_t;
#else #else
#define NUMPRIMES 54 #define NUMPRIMES 54
typedef unsigned char prime_t;
#endif #endif
static const unsigned int primes[NUMPRIMES]= static const prime_t primes[NUMPRIMES]=
{ {
2, 3, 5, 7, 11, 13, 17, 19, 2, 3, 5, 7, 11, 13, 17, 19,
23, 29, 31, 37, 41, 43, 47, 53, 23, 29, 31, 37, 41, 43, 47, 53,

View File

@ -101,10 +101,12 @@ for ($i=0; $i <= $#primes; $i++)
printf "#ifndef EIGHT_BIT\n"; printf "#ifndef EIGHT_BIT\n";
printf "#define NUMPRIMES %d\n",$num; printf "#define NUMPRIMES %d\n",$num;
printf "typedef unsigned short prime_t;\n";
printf "#else\n"; printf "#else\n";
printf "#define NUMPRIMES %d\n",$eight; printf "#define NUMPRIMES %d\n",$eight;
printf "typedef unsigned char prime_t;\n";
printf "#endif\n"; printf "#endif\n";
print "static const unsigned int primes[NUMPRIMES]=\n\t{\n\t"; print "static const prime_t primes[NUMPRIMES]=\n\t{\n\t";
$init=0; $init=0;
for ($i=0; $i <= $#primes; $i++) for ($i=0; $i <= $#primes; $i++)
{ {