int main(int argc, char *argv[])
{
  mp_digit  num;
  mp_int    a, b;

  srand(time(NULL));

  if(argc < 3) {
    fprintf(stderr, "Usage: %s <a> <b>\n", argv[0]);
    return 1;
  }

  printf("Test 7: Random & divisibility tests\n\n");

  mp_init(&a);
  mp_init(&b);

  mp_read_radix(&a, argv[1], 10);
  mp_read_radix(&b, argv[2], 10);

  printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
  printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);

  if(mpp_divis(&a, &b) == MP_YES)
    printf("a is divisible by b\n");
  else
    printf("a is not divisible by b\n");

  if(mpp_divis(&b, &a) == MP_YES)
    printf("b is divisible by a\n");
  else
    printf("b is not divisible by a\n");

  printf("\nb = mpp_random()\n");
  mpp_random(&b);
  printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
  mpp_random(&b);
  printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
  mpp_random(&b);
  printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);

  printf("\nTesting a for divisibility by first 170 primes\n");
  num = 170;
  if(mpp_divis_primes(&a, &num) == MP_YES)
    printf("It is divisible by at least one of them\n");
  else
    printf("It is not divisible by any of them\n");

  mp_clear(&b);
  mp_clear(&a);

  return 0;
}
Example #2
0
int main(int argc, char *argv[])
{
    instant_t    start, finish;
    mp_int       prime, gen, expt, res;
    unsigned int ix, diff;
    int          num;

    srand(time(NULL));

    if(argc < 2) {
        fprintf(stderr, "Usage: %s <num-tests>\n", argv[0]);
        return 1;
    }

    if((num = atoi(argv[1])) < 0)
        num = -num;

    if(num == 0)
        ++num;

    mp_init(&prime);
    mp_init(&gen);
    mp_init(&res);
    mp_read_radix(&prime, g_prime, 16);
    mp_read_radix(&gen, g_gen, 16);

    mp_init_size(&expt, USED(&prime) - 1);
    s_mp_pad(&expt, USED(&prime) - 1);

    printf("Testing %d modular exponentations ... \n", num);

    start = now();
    for(ix = 0; ix < num; ix++) {
        mpp_random(&expt);
        mp_exptmod(&gen, &expt, &prime, &res);
    }
    finish = now();

    diff = (finish.sec - start.sec) * 1000000;
    diff += finish.usec;
    diff -= start.usec;

    printf("%d operations took %u usec (%.3f sec)\n",
           num, diff, (double)diff / 1000000.0);
    printf("That is %.3f sec per operation.\n",
           ((double)diff / 1000000.0) / num);

    mp_clear(&expt);
    mp_clear(&res);
    mp_clear(&gen);
    mp_clear(&prime);

    return 0;
}
Example #3
0
mp_err  mpp_random_size(mp_int *a, mp_size prec)
{
  mp_err   res;

  ARGCHK(a != NULL && prec > 0, MP_BADARG);
  
  if((res = s_mp_pad(a, prec)) != MP_OKAY)
    return res;

  return mpp_random(a);

} /* end mpp_random_size() */
Example #4
0
int main(int argc, char *argv[])
{
  int        ix, num, prec = 8;
  unsigned   int d;
  instant_t  start, finish;
  time_t     seed;
  mp_int     a, m, c;

  seed = time(NULL);

  if(argc < 2) {
    fprintf(stderr, "Usage: %s <num-tests> [<precision>]\n", argv[0]);
    return 1;
  }

  if((num = atoi(argv[1])) < 0)
    num = -num;

  if(!num) {
    fprintf(stderr, "%s: must perform at least 1 test\n", argv[0]);
    return 1;
  }

  if(argc > 2) {
    if((prec = atoi(argv[2])) <= 0)
      prec = 8;
  }
  
  printf("Test 3a: Modular exponentiation timing test\n"
	 "Precision:  %d digits (%d bits)\n"
	 "# of tests: %d\n\n", prec, prec * DIGIT_BIT, num);

  mp_init_size(&a, prec);
  mp_init_size(&m, prec);
  mp_init_size(&c, prec);
  s_mp_pad(&a, prec);
  s_mp_pad(&m, prec);
  s_mp_pad(&c, prec);

  printf("Testing modular exponentiation ... \n");
  srand((unsigned int)seed);

  start = now();
  for(ix = 0; ix < num; ix++) {
    mpp_random(&a);
    mpp_random(&c);
    mpp_random(&m);
    mp_exptmod(&a, &c, &m, &c);
  }
  finish = now();

  d = (finish.sec - start.sec) * 1000000;
  d -= start.usec; d += finish.usec;

  printf("Total time elapsed:        %u usec\n", d);
  printf("Time per exponentiation:   %u usec (%.3f sec)\n", 
	 (d / num), (double)(d / num) / 1000000);

  mp_clear(&c);
  mp_clear(&a);
  mp_clear(&m);

  return 0;
}
Example #5
0
mp_err  mpp_pprime(mp_int *a, int nt)
{
  mp_err   res;
  mp_int   x, amo, m, z;	/* "amo" = "a minus one" */
  int      iter;
  unsigned int jx;
  mp_size  b;

  ARGCHK(a != NULL, MP_BADARG);

  MP_DIGITS(&x) = 0;
  MP_DIGITS(&amo) = 0;
  MP_DIGITS(&m) = 0;
  MP_DIGITS(&z) = 0;

  /* Initialize temporaries... */
  MP_CHECKOK( mp_init(&amo));
  /* Compute amo = a - 1 for what follows...    */
  MP_CHECKOK( mp_sub_d(a, 1, &amo) );

  b = mp_trailing_zeros(&amo);
  if (!b) { /* a was even ? */
    res = MP_NO;
    goto CLEANUP;
  }

  MP_CHECKOK( mp_init_size(&x, MP_USED(a)) );
  MP_CHECKOK( mp_init(&z) );
  MP_CHECKOK( mp_init(&m) );
  MP_CHECKOK( mp_div_2d(&amo, b, &m, 0) );

  /* Do the test nt times... */
  for(iter = 0; iter < nt; iter++) {

    /* Choose a random value for x < a          */
    s_mp_pad(&x, USED(a));
    mpp_random(&x);
    MP_CHECKOK( mp_mod(&x, a, &x) );

    /* Compute z = (x ** m) mod a               */
    MP_CHECKOK( mp_exptmod(&x, &m, a, &z) );
    
    if(mp_cmp_d(&z, 1) == 0 || mp_cmp(&z, &amo) == 0) {
      res = MP_YES;
      continue;
    }
    
    res = MP_NO;  /* just in case the following for loop never executes. */
    for (jx = 1; jx < b; jx++) {
      /* z = z^2 (mod a) */
      MP_CHECKOK( mp_sqrmod(&z, a, &z) );
      res = MP_NO;	/* previous line set res to MP_YES */

      if(mp_cmp_d(&z, 1) == 0) {
	break;
      }
      if(mp_cmp(&z, &amo) == 0) {
	res = MP_YES;
	break;
      } 
    } /* end testing loop */

    /* If the test passes, we will continue iterating, but a failed
       test means the candidate is definitely NOT prime, so we will
       immediately break out of this loop
     */
    if(res == MP_NO)
      break;

  } /* end iterations loop */
  
CLEANUP:
  mp_clear(&m);
  mp_clear(&z);
  mp_clear(&x);
  mp_clear(&amo);
  return res;

} /* end mpp_pprime() */