Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * isprime.c |
michael@0 | 3 | * |
michael@0 | 4 | * Probabilistic primality tester command-line tool |
michael@0 | 5 | * |
michael@0 | 6 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 7 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 8 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 9 | |
michael@0 | 10 | #include <stdio.h> |
michael@0 | 11 | #include <stdlib.h> |
michael@0 | 12 | #include <string.h> |
michael@0 | 13 | |
michael@0 | 14 | #include "mpi.h" |
michael@0 | 15 | #include "mpprime.h" |
michael@0 | 16 | |
michael@0 | 17 | #define RM_TESTS 15 /* how many iterations of Rabin-Miller? */ |
michael@0 | 18 | #define MINIMUM 1024 /* don't bother us with a < this */ |
michael@0 | 19 | |
michael@0 | 20 | int g_tests = RM_TESTS; |
michael@0 | 21 | char *g_prog = NULL; |
michael@0 | 22 | |
michael@0 | 23 | int main(int argc, char *argv[]) |
michael@0 | 24 | { |
michael@0 | 25 | mp_int a; |
michael@0 | 26 | mp_digit np = prime_tab_size; /* from mpprime.h */ |
michael@0 | 27 | int res = 0; |
michael@0 | 28 | |
michael@0 | 29 | g_prog = argv[0]; |
michael@0 | 30 | |
michael@0 | 31 | if(argc < 2) { |
michael@0 | 32 | fprintf(stderr, "Usage: %s <a>, where <a> is a decimal integer\n" |
michael@0 | 33 | "Use '0x' prefix for a hexadecimal value\n", g_prog); |
michael@0 | 34 | return 1; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | /* Read number of tests from environment, if present */ |
michael@0 | 38 | { |
michael@0 | 39 | char *tmp; |
michael@0 | 40 | |
michael@0 | 41 | if((tmp = getenv("RM_TESTS")) != NULL) { |
michael@0 | 42 | if((g_tests = atoi(tmp)) <= 0) |
michael@0 | 43 | g_tests = RM_TESTS; |
michael@0 | 44 | } |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | mp_init(&a); |
michael@0 | 48 | if(argv[1][0] == '0' && argv[1][1] == 'x') |
michael@0 | 49 | mp_read_radix(&a, argv[1] + 2, 16); |
michael@0 | 50 | else |
michael@0 | 51 | mp_read_radix(&a, argv[1], 10); |
michael@0 | 52 | |
michael@0 | 53 | if(mp_cmp_d(&a, MINIMUM) <= 0) { |
michael@0 | 54 | fprintf(stderr, "%s: please use a value greater than %d\n", |
michael@0 | 55 | g_prog, MINIMUM); |
michael@0 | 56 | mp_clear(&a); |
michael@0 | 57 | return 1; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | /* Test for divisibility by small primes */ |
michael@0 | 61 | if(mpp_divis_primes(&a, &np) != MP_NO) { |
michael@0 | 62 | printf("Not prime (divisible by small prime %d)\n", np); |
michael@0 | 63 | res = 2; |
michael@0 | 64 | goto CLEANUP; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | /* Test with Fermat's test, using 2 as a witness */ |
michael@0 | 68 | if(mpp_fermat(&a, 2) != MP_YES) { |
michael@0 | 69 | printf("Not prime (failed Fermat test)\n"); |
michael@0 | 70 | res = 2; |
michael@0 | 71 | goto CLEANUP; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | /* Test with Rabin-Miller probabilistic test */ |
michael@0 | 75 | if(mpp_pprime(&a, g_tests) == MP_NO) { |
michael@0 | 76 | printf("Not prime (failed pseudoprime test)\n"); |
michael@0 | 77 | res = 2; |
michael@0 | 78 | goto CLEANUP; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | printf("Probably prime, 1 in 4^%d chance of false positive\n", g_tests); |
michael@0 | 82 | |
michael@0 | 83 | CLEANUP: |
michael@0 | 84 | mp_clear(&a); |
michael@0 | 85 | |
michael@0 | 86 | return res; |
michael@0 | 87 | |
michael@0 | 88 | } |