security/nss/lib/freebl/mpi/utils/isprime.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/mpi/utils/isprime.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,88 @@
     1.4 +/*
     1.5 + *  isprime.c
     1.6 + *
     1.7 + *  Probabilistic primality tester command-line tool
     1.8 + *
     1.9 + * This Source Code Form is subject to the terms of the Mozilla Public
    1.10 + * License, v. 2.0. If a copy of the MPL was not distributed with this
    1.11 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.12 +
    1.13 +#include <stdio.h>
    1.14 +#include <stdlib.h>
    1.15 +#include <string.h>
    1.16 +
    1.17 +#include "mpi.h"
    1.18 +#include "mpprime.h"
    1.19 +
    1.20 +#define  RM_TESTS       15   /* how many iterations of Rabin-Miller? */
    1.21 +#define  MINIMUM        1024 /* don't bother us with a < this        */
    1.22 +
    1.23 +int      g_tests = RM_TESTS;
    1.24 +char    *g_prog = NULL;
    1.25 +
    1.26 +int main(int argc, char *argv[])
    1.27 +{
    1.28 +  mp_int   a;
    1.29 +  mp_digit np = prime_tab_size; /* from mpprime.h */
    1.30 +  int      res = 0;
    1.31 +
    1.32 +  g_prog = argv[0];
    1.33 +
    1.34 +  if(argc < 2) {
    1.35 +    fprintf(stderr, "Usage: %s <a>, where <a> is a decimal integer\n"
    1.36 +	    "Use '0x' prefix for a hexadecimal value\n", g_prog);
    1.37 +    return 1;
    1.38 +  }
    1.39 +
    1.40 +  /* Read number of tests from environment, if present */
    1.41 +  {
    1.42 +    char *tmp;
    1.43 +
    1.44 +    if((tmp = getenv("RM_TESTS")) != NULL) {
    1.45 +      if((g_tests = atoi(tmp)) <= 0)
    1.46 +	g_tests = RM_TESTS;
    1.47 +    }
    1.48 +  }
    1.49 +
    1.50 +  mp_init(&a);
    1.51 +  if(argv[1][0] == '0' && argv[1][1] == 'x')
    1.52 +    mp_read_radix(&a, argv[1] + 2, 16);
    1.53 +  else
    1.54 +    mp_read_radix(&a, argv[1], 10);
    1.55 +
    1.56 +  if(mp_cmp_d(&a, MINIMUM) <= 0) {
    1.57 +    fprintf(stderr, "%s: please use a value greater than %d\n", 
    1.58 +	    g_prog, MINIMUM);
    1.59 +    mp_clear(&a);
    1.60 +    return 1;
    1.61 +  }
    1.62 +
    1.63 +  /* Test for divisibility by small primes */
    1.64 +  if(mpp_divis_primes(&a, &np) != MP_NO) {
    1.65 +    printf("Not prime (divisible by small prime %d)\n", np);
    1.66 +    res = 2;
    1.67 +    goto CLEANUP;
    1.68 +  }
    1.69 +
    1.70 +  /* Test with Fermat's test, using 2 as a witness */
    1.71 +  if(mpp_fermat(&a, 2) != MP_YES) {
    1.72 +    printf("Not prime (failed Fermat test)\n");
    1.73 +    res = 2;
    1.74 +    goto CLEANUP;
    1.75 +  }
    1.76 + 
    1.77 +  /* Test with Rabin-Miller probabilistic test */
    1.78 +  if(mpp_pprime(&a, g_tests) == MP_NO) {
    1.79 +      printf("Not prime (failed pseudoprime test)\n");
    1.80 +      res = 2;
    1.81 +      goto CLEANUP;
    1.82 +  }
    1.83 +
    1.84 +  printf("Probably prime, 1 in 4^%d chance of false positive\n", g_tests);
    1.85 +
    1.86 +CLEANUP:
    1.87 +  mp_clear(&a);
    1.88 +  
    1.89 +  return res;
    1.90 +
    1.91 +}

mercurial