mpz_powm timing fluctuations

Chris Heneghan c.heneghan at rhul.ac.uk
Fri Jun 25 17:53:40 CEST 2004


Hi, I'm doing some tests and I've noticed that there can be some pretty
big fluctuations(over 20%) in timings in the mpz_powm method. Below is a
little test program I made to illustrate with sample results(It
basically chooses a random base,exponent and modulus and iterates 1000
times). Anyone have any ideas what could be causing the difference in
the timings? Is there a better way to time besides using clock()?

Any help much appreciated. 

Regards, 

Chris

#include <time.h>
#include <gmp.h>

gmp_randstate_t state;
int mod_size = 2048;
clock_t start = 0;
clock_t end = 0;
float avg = 0;
int iterations = 1000;

int main(int argc, char** argv)
{
  int i = 0;
  mpz_t N;
  mpz_t e;
  mpz_t m;
  mpz_t temp;

  mpz_init(N);
  mpz_init(e);
  mpz_init(m);
  mpz_init(temp);

  gmp_randinit_default(state);
  gmp_randseed_ui(state,1234);

  if(argc = 2)
    mod_size = atoi(argv[1]);

#ifdef FIXED_VALUES
  mpz_urandomb(N,state,mod_size);
  mpz_setbit(N,mod_size-1);
  
  mpz_urandomb(e,state,mod_size-1);
  mpz_setbit(e,mod_size-2);
  
  mpz_urandomb(m,state,mod_size);
  mpz_setbit(m,mod_size-1);
#endif //FIXED_VALUES

  for(i = 0; i < iterations; i++)
    { 
#ifndef FIXED_VALUES 
      mpz_urandomb(N,state,mod_size);
      mpz_setbit(N,mod_size-1);

      mpz_urandomb(e,state,mod_size-1);
      mpz_setbit(e,mod_size-2);
      
      mpz_urandomb(m,state,mod_size);
      mpz_setbit(m,mod_size-1);
#endif //FIXED_VALUES

      start = clock();
      mpz_powm(temp,m,e,N);
      end = clock();
      avg += (float)(end-start)/(float)iterations;
    }

  avg /= CLOCKS_PER_SEC;
  printf("avg = %f\n",avg);
  mpz_clear(N);
  mpz_clear(e);
  mpz_clear(m);
  mpz_clear(temp);
}

[chris at localhost TunableRSA]$ gcc  -o blah test.c -I/usr/local/include
-L/usr/local/lib -lgmp
[chris at localhost TunableRSA]$ ./blah 1024
avg = 0.010660
[chris at localhost TunableRSA]$ ./blah 2048
avg = 0.065380
[chris at localhost TunableRSA]$ gcc -DFIXED_VALUES -o blah test.c
-I/usr/local/include -L/usr/local/lib -lgmp
[chris at localhost TunableRSA]$ ./blah 1024
avg = 0.008650
[chris at localhost TunableRSA]$ ./blah 2048
avg = 0.056730




More information about the gmp-discuss mailing list