1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/freebl/mpi/tests/mptest-8.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,65 @@ 1.4 +/* 1.5 + * Simple test driver for MPI library 1.6 + * 1.7 + * Test 8: Probabilistic primality tester 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 +#include <ctype.h> 1.17 +#include <limits.h> 1.18 +#include <time.h> 1.19 + 1.20 +#define MP_IOFUNC 1 1.21 +#include "mpi.h" 1.22 + 1.23 +#include "mpprime.h" 1.24 + 1.25 +int main(int argc, char *argv[]) 1.26 +{ 1.27 + int ix; 1.28 + mp_digit num; 1.29 + mp_int a; 1.30 + 1.31 + srand(time(NULL)); 1.32 + 1.33 + if(argc < 2) { 1.34 + fprintf(stderr, "Usage: %s <a>\n", argv[0]); 1.35 + return 1; 1.36 + } 1.37 + 1.38 + printf("Test 8: Probabilistic primality testing\n\n"); 1.39 + 1.40 + mp_init(&a); 1.41 + 1.42 + mp_read_radix(&a, argv[1], 10); 1.43 + 1.44 + printf("a = "); mp_print(&a, stdout); fputc('\n', stdout); 1.45 + 1.46 + printf("\nChecking for divisibility by small primes ... \n"); 1.47 + num = 170; 1.48 + if(mpp_divis_primes(&a, &num) == MP_YES) { 1.49 + printf("it is not prime\n"); 1.50 + goto CLEANUP; 1.51 + } 1.52 + printf("Passed that test (not divisible by any small primes).\n"); 1.53 + 1.54 + for(ix = 0; ix < 10; ix++) { 1.55 + printf("\nPerforming Rabin-Miller test, iteration %d\n", ix + 1); 1.56 + 1.57 + if(mpp_pprime(&a, 5) == MP_NO) { 1.58 + printf("it is not prime\n"); 1.59 + goto CLEANUP; 1.60 + } 1.61 + } 1.62 + printf("All tests passed; a is probably prime\n"); 1.63 + 1.64 +CLEANUP: 1.65 + mp_clear(&a); 1.66 + 1.67 + return 0; 1.68 +}