andre@0: /* andre@0: * mpprime.c andre@0: * andre@0: * Utilities for finding and working with prime and pseudo-prime andre@0: * integers andre@0: * andre@0: * This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "mpi-priv.h" andre@0: #include "mpprime.h" andre@0: #include "mplogic.h" andre@0: #include andre@0: #include andre@0: andre@0: #define SMALL_TABLE 0 /* determines size of hard-wired prime table */ andre@0: andre@0: #define RANDOM() rand() andre@0: andre@0: #include "primes.c" /* pull in the prime digit table */ andre@0: andre@0: /* andre@0: Test if any of a given vector of digits divides a. If not, MP_NO andre@0: is returned; otherwise, MP_YES is returned and 'which' is set to andre@0: the index of the integer in the vector which divided a. andre@0: */ andre@0: mp_err s_mpp_divp(mp_int *a, const mp_digit *vec, int size, int *which); andre@0: andre@0: /* {{{ mpp_divis(a, b) */ andre@0: andre@0: /* andre@0: mpp_divis(a, b) andre@0: andre@0: Returns MP_YES if a is divisible by b, or MP_NO if it is not. andre@0: */ andre@0: andre@0: mp_err mpp_divis(mp_int *a, mp_int *b) andre@0: { andre@0: mp_err res; andre@0: mp_int rem; andre@0: andre@0: if((res = mp_init(&rem)) != MP_OKAY) andre@0: return res; andre@0: andre@0: if((res = mp_mod(a, b, &rem)) != MP_OKAY) andre@0: goto CLEANUP; andre@0: andre@0: if(mp_cmp_z(&rem) == 0) andre@0: res = MP_YES; andre@0: else andre@0: res = MP_NO; andre@0: andre@0: CLEANUP: andre@0: mp_clear(&rem); andre@0: return res; andre@0: andre@0: } /* end mpp_divis() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* {{{ mpp_divis_d(a, d) */ andre@0: andre@0: /* andre@0: mpp_divis_d(a, d) andre@0: andre@0: Return MP_YES if a is divisible by d, or MP_NO if it is not. andre@0: */ andre@0: andre@0: mp_err mpp_divis_d(mp_int *a, mp_digit d) andre@0: { andre@0: mp_err res; andre@0: mp_digit rem; andre@0: andre@0: ARGCHK(a != NULL, MP_BADARG); andre@0: andre@0: if(d == 0) andre@0: return MP_NO; andre@0: andre@0: if((res = mp_mod_d(a, d, &rem)) != MP_OKAY) andre@0: return res; andre@0: andre@0: if(rem == 0) andre@0: return MP_YES; andre@0: else andre@0: return MP_NO; andre@0: andre@0: } /* end mpp_divis_d() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* {{{ mpp_random(a) */ andre@0: andre@0: /* andre@0: mpp_random(a) andre@0: andre@0: Assigns a random value to a. This value is generated using the andre@0: standard C library's rand() function, so it should not be used for andre@0: cryptographic purposes, but it should be fine for primality testing, andre@0: since all we really care about there is good statistical properties. andre@0: andre@0: As many digits as a currently has are filled with random digits. andre@0: */ andre@0: andre@0: mp_err mpp_random(mp_int *a) andre@0: andre@0: { andre@0: mp_digit next = 0; andre@0: unsigned int ix, jx; andre@0: andre@0: ARGCHK(a != NULL, MP_BADARG); andre@0: andre@0: for(ix = 0; ix < USED(a); ix++) { andre@0: for(jx = 0; jx < sizeof(mp_digit); jx++) { andre@0: next = (next << CHAR_BIT) | (RANDOM() & UCHAR_MAX); andre@0: } andre@0: DIGIT(a, ix) = next; andre@0: } andre@0: andre@0: return MP_OKAY; andre@0: andre@0: } /* end mpp_random() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* {{{ mpp_random_size(a, prec) */ andre@0: andre@0: mp_err mpp_random_size(mp_int *a, mp_size prec) andre@0: { andre@0: mp_err res; andre@0: andre@0: ARGCHK(a != NULL && prec > 0, MP_BADARG); andre@0: andre@0: if((res = s_mp_pad(a, prec)) != MP_OKAY) andre@0: return res; andre@0: andre@0: return mpp_random(a); andre@0: andre@0: } /* end mpp_random_size() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* {{{ mpp_divis_vector(a, vec, size, which) */ andre@0: andre@0: /* andre@0: mpp_divis_vector(a, vec, size, which) andre@0: andre@0: Determines if a is divisible by any of the 'size' digits in vec. andre@0: Returns MP_YES and sets 'which' to the index of the offending digit, andre@0: if it is; returns MP_NO if it is not. andre@0: */ andre@0: andre@0: mp_err mpp_divis_vector(mp_int *a, const mp_digit *vec, int size, int *which) andre@0: { andre@0: ARGCHK(a != NULL && vec != NULL && size > 0, MP_BADARG); andre@0: andre@0: return s_mpp_divp(a, vec, size, which); andre@0: andre@0: } /* end mpp_divis_vector() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* {{{ mpp_divis_primes(a, np) */ andre@0: andre@0: /* andre@0: mpp_divis_primes(a, np) andre@0: andre@0: Test whether a is divisible by any of the first 'np' primes. If it andre@0: is, returns MP_YES and sets *np to the value of the digit that did andre@0: it. If not, returns MP_NO. andre@0: */ andre@0: mp_err mpp_divis_primes(mp_int *a, mp_digit *np) andre@0: { andre@0: int size, which; andre@0: mp_err res; andre@0: andre@0: ARGCHK(a != NULL && np != NULL, MP_BADARG); andre@0: andre@0: size = (int)*np; andre@0: if(size > prime_tab_size) andre@0: size = prime_tab_size; andre@0: andre@0: res = mpp_divis_vector(a, prime_tab, size, &which); andre@0: if(res == MP_YES) andre@0: *np = prime_tab[which]; andre@0: andre@0: return res; andre@0: andre@0: } /* end mpp_divis_primes() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* {{{ mpp_fermat(a, w) */ andre@0: andre@0: /* andre@0: Using w as a witness, try pseudo-primality testing based on Fermat's andre@0: little theorem. If a is prime, and (w, a) = 1, then w^a == w (mod andre@0: a). So, we compute z = w^a (mod a) and compare z to w; if they are andre@0: equal, the test passes and we return MP_YES. Otherwise, we return andre@0: MP_NO. andre@0: */ andre@0: mp_err mpp_fermat(mp_int *a, mp_digit w) andre@0: { andre@0: mp_int base, test; andre@0: mp_err res; andre@0: andre@0: if((res = mp_init(&base)) != MP_OKAY) andre@0: return res; andre@0: andre@0: mp_set(&base, w); andre@0: andre@0: if((res = mp_init(&test)) != MP_OKAY) andre@0: goto TEST; andre@0: andre@0: /* Compute test = base^a (mod a) */ andre@0: if((res = mp_exptmod(&base, a, a, &test)) != MP_OKAY) andre@0: goto CLEANUP; andre@0: andre@0: andre@0: if(mp_cmp(&base, &test) == 0) andre@0: res = MP_YES; andre@0: else andre@0: res = MP_NO; andre@0: andre@0: CLEANUP: andre@0: mp_clear(&test); andre@0: TEST: andre@0: mp_clear(&base); andre@0: andre@0: return res; andre@0: andre@0: } /* end mpp_fermat() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* andre@0: Perform the fermat test on each of the primes in a list until andre@0: a) one of them shows a is not prime, or andre@0: b) the list is exhausted. andre@0: Returns: MP_YES if it passes tests. andre@0: MP_NO if fermat test reveals it is composite andre@0: Some MP error code if some other error occurs. andre@0: */ andre@0: mp_err mpp_fermat_list(mp_int *a, const mp_digit *primes, mp_size nPrimes) andre@0: { andre@0: mp_err rv = MP_YES; andre@0: andre@0: while (nPrimes-- > 0 && rv == MP_YES) { andre@0: rv = mpp_fermat(a, *primes++); andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /* {{{ mpp_pprime(a, nt) */ andre@0: andre@0: /* andre@0: mpp_pprime(a, nt) andre@0: andre@0: Performs nt iteration of the Miller-Rabin probabilistic primality andre@0: test on a. Returns MP_YES if the tests pass, MP_NO if one fails. andre@0: If MP_NO is returned, the number is definitely composite. If MP_YES andre@0: is returned, it is probably prime (but that is not guaranteed). andre@0: */ andre@0: andre@0: mp_err mpp_pprime(mp_int *a, int nt) andre@0: { andre@0: mp_err res; andre@0: mp_int x, amo, m, z; /* "amo" = "a minus one" */ andre@0: int iter; andre@0: unsigned int jx; andre@0: mp_size b; andre@0: andre@0: ARGCHK(a != NULL, MP_BADARG); andre@0: andre@0: MP_DIGITS(&x) = 0; andre@0: MP_DIGITS(&amo) = 0; andre@0: MP_DIGITS(&m) = 0; andre@0: MP_DIGITS(&z) = 0; andre@0: andre@0: /* Initialize temporaries... */ andre@0: MP_CHECKOK( mp_init(&amo)); andre@0: /* Compute amo = a - 1 for what follows... */ andre@0: MP_CHECKOK( mp_sub_d(a, 1, &amo) ); andre@0: andre@0: b = mp_trailing_zeros(&amo); andre@0: if (!b) { /* a was even ? */ andre@0: res = MP_NO; andre@0: goto CLEANUP; andre@0: } andre@0: andre@0: MP_CHECKOK( mp_init_size(&x, MP_USED(a)) ); andre@0: MP_CHECKOK( mp_init(&z) ); andre@0: MP_CHECKOK( mp_init(&m) ); andre@0: MP_CHECKOK( mp_div_2d(&amo, b, &m, 0) ); andre@0: andre@0: /* Do the test nt times... */ andre@0: for(iter = 0; iter < nt; iter++) { andre@0: andre@0: /* Choose a random value for 1 < x < a */ andre@0: s_mp_pad(&x, USED(a)); andre@0: mpp_random(&x); andre@0: MP_CHECKOK( mp_mod(&x, a, &x) ); andre@0: if(mp_cmp_d(&x, 1) <= 0) { andre@0: iter--; /* don't count this iteration */ andre@0: continue; /* choose a new x */ andre@0: } andre@0: andre@0: /* Compute z = (x ** m) mod a */ andre@0: MP_CHECKOK( mp_exptmod(&x, &m, a, &z) ); andre@0: andre@0: if(mp_cmp_d(&z, 1) == 0 || mp_cmp(&z, &amo) == 0) { andre@0: res = MP_YES; andre@0: continue; andre@0: } andre@0: andre@0: res = MP_NO; /* just in case the following for loop never executes. */ andre@0: for (jx = 1; jx < b; jx++) { andre@0: /* z = z^2 (mod a) */ andre@0: MP_CHECKOK( mp_sqrmod(&z, a, &z) ); andre@0: res = MP_NO; /* previous line set res to MP_YES */ andre@0: andre@0: if(mp_cmp_d(&z, 1) == 0) { andre@0: break; andre@0: } andre@0: if(mp_cmp(&z, &amo) == 0) { andre@0: res = MP_YES; andre@0: break; andre@0: } andre@0: } /* end testing loop */ andre@0: andre@0: /* If the test passes, we will continue iterating, but a failed andre@0: test means the candidate is definitely NOT prime, so we will andre@0: immediately break out of this loop andre@0: */ andre@0: if(res == MP_NO) andre@0: break; andre@0: andre@0: } /* end iterations loop */ andre@0: andre@0: CLEANUP: andre@0: mp_clear(&m); andre@0: mp_clear(&z); andre@0: mp_clear(&x); andre@0: mp_clear(&amo); andre@0: return res; andre@0: andre@0: } /* end mpp_pprime() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /* Produce table of composites from list of primes and trial value. andre@0: ** trial must be odd. List of primes must not include 2. andre@0: ** sieve should have dimension >= MAXPRIME/2, where MAXPRIME is largest andre@0: ** prime in list of primes. After this function is finished, andre@0: ** if sieve[i] is non-zero, then (trial + 2*i) is composite. andre@0: ** Each prime used in the sieve costs one division of trial, and eliminates andre@0: ** one or more values from the search space. (3 eliminates 1/3 of the values andre@0: ** alone!) Each value left in the search space costs 1 or more modular andre@0: ** exponentations. So, these divisions are a bargain! andre@0: */ andre@0: mp_err mpp_sieve(mp_int *trial, const mp_digit *primes, mp_size nPrimes, andre@0: unsigned char *sieve, mp_size nSieve) andre@0: { andre@0: mp_err res; andre@0: mp_digit rem; andre@0: mp_size ix; andre@0: unsigned long offset; andre@0: andre@0: memset(sieve, 0, nSieve); andre@0: andre@0: for(ix = 0; ix < nPrimes; ix++) { andre@0: mp_digit prime = primes[ix]; andre@0: mp_size i; andre@0: if((res = mp_mod_d(trial, prime, &rem)) != MP_OKAY) andre@0: return res; andre@0: andre@0: if (rem == 0) { andre@0: offset = 0; andre@0: } else { andre@0: offset = prime - (rem / 2); andre@0: } andre@0: for (i = offset; i < nSieve ; i += prime) { andre@0: sieve[i] = 1; andre@0: } andre@0: } andre@0: andre@0: return MP_OKAY; andre@0: } andre@0: andre@0: #define SIEVE_SIZE 32*1024 andre@0: andre@0: mp_err mpp_make_prime(mp_int *start, mp_size nBits, mp_size strong, andre@0: unsigned long * nTries) andre@0: { andre@0: mp_digit np; andre@0: mp_err res; andre@0: int i = 0; andre@0: mp_int trial; andre@0: mp_int q; andre@0: mp_size num_tests; andre@0: unsigned char *sieve; andre@0: andre@0: ARGCHK(start != 0, MP_BADARG); andre@0: ARGCHK(nBits > 16, MP_RANGE); andre@0: andre@0: sieve = malloc(SIEVE_SIZE); andre@0: ARGCHK(sieve != NULL, MP_MEM); andre@0: andre@0: MP_DIGITS(&trial) = 0; andre@0: MP_DIGITS(&q) = 0; andre@0: MP_CHECKOK( mp_init(&trial) ); andre@0: MP_CHECKOK( mp_init(&q) ); andre@0: /* values taken from table 4.4, HandBook of Applied Cryptography */ andre@0: if (nBits >= 1300) { andre@0: num_tests = 2; andre@0: } else if (nBits >= 850) { andre@0: num_tests = 3; andre@0: } else if (nBits >= 650) { andre@0: num_tests = 4; andre@0: } else if (nBits >= 550) { andre@0: num_tests = 5; andre@0: } else if (nBits >= 450) { andre@0: num_tests = 6; andre@0: } else if (nBits >= 400) { andre@0: num_tests = 7; andre@0: } else if (nBits >= 350) { andre@0: num_tests = 8; andre@0: } else if (nBits >= 300) { andre@0: num_tests = 9; andre@0: } else if (nBits >= 250) { andre@0: num_tests = 12; andre@0: } else if (nBits >= 200) { andre@0: num_tests = 15; andre@0: } else if (nBits >= 150) { andre@0: num_tests = 18; andre@0: } else if (nBits >= 100) { andre@0: num_tests = 27; andre@0: } else andre@0: num_tests = 50; andre@0: andre@0: if (strong) andre@0: --nBits; andre@0: MP_CHECKOK( mpl_set_bit(start, nBits - 1, 1) ); andre@0: MP_CHECKOK( mpl_set_bit(start, 0, 1) ); andre@0: for (i = mpl_significant_bits(start) - 1; i >= nBits; --i) { andre@0: MP_CHECKOK( mpl_set_bit(start, i, 0) ); andre@0: } andre@0: /* start sieveing with prime value of 3. */ andre@0: MP_CHECKOK(mpp_sieve(start, prime_tab + 1, prime_tab_size - 1, andre@0: sieve, SIEVE_SIZE) ); andre@0: andre@0: #ifdef DEBUG_SIEVE andre@0: res = 0; andre@0: for (i = 0; i < SIEVE_SIZE; ++i) { andre@0: if (!sieve[i]) andre@0: ++res; andre@0: } andre@0: fprintf(stderr,"sieve found %d potential primes.\n", res); andre@0: #define FPUTC(x,y) fputc(x,y) andre@0: #else andre@0: #define FPUTC(x,y) andre@0: #endif andre@0: andre@0: res = MP_NO; andre@0: for(i = 0; i < SIEVE_SIZE; ++i) { andre@0: if (sieve[i]) /* this number is composite */ andre@0: continue; andre@0: MP_CHECKOK( mp_add_d(start, 2 * i, &trial) ); andre@0: FPUTC('.', stderr); andre@0: /* run a Fermat test */ andre@0: res = mpp_fermat(&trial, 2); andre@0: if (res != MP_OKAY) { andre@0: if (res == MP_NO) andre@0: continue; /* was composite */ andre@0: goto CLEANUP; andre@0: } andre@0: andre@0: FPUTC('+', stderr); andre@0: /* If that passed, run some Miller-Rabin tests */ andre@0: res = mpp_pprime(&trial, num_tests); andre@0: if (res != MP_OKAY) { andre@0: if (res == MP_NO) andre@0: continue; /* was composite */ andre@0: goto CLEANUP; andre@0: } andre@0: FPUTC('!', stderr); andre@0: andre@0: if (!strong) andre@0: break; /* success !! */ andre@0: andre@0: /* At this point, we have strong evidence that our candidate andre@0: is itself prime. If we want a strong prime, we need now andre@0: to test q = 2p + 1 for primality... andre@0: */ andre@0: MP_CHECKOK( mp_mul_2(&trial, &q) ); andre@0: MP_CHECKOK( mp_add_d(&q, 1, &q) ); andre@0: andre@0: /* Test q for small prime divisors ... */ andre@0: np = prime_tab_size; andre@0: res = mpp_divis_primes(&q, &np); andre@0: if (res == MP_YES) { /* is composite */ andre@0: mp_clear(&q); andre@0: continue; andre@0: } andre@0: if (res != MP_NO) andre@0: goto CLEANUP; andre@0: andre@0: /* And test with Fermat, as with its parent ... */ andre@0: res = mpp_fermat(&q, 2); andre@0: if (res != MP_YES) { andre@0: mp_clear(&q); andre@0: if (res == MP_NO) andre@0: continue; /* was composite */ andre@0: goto CLEANUP; andre@0: } andre@0: andre@0: /* And test with Miller-Rabin, as with its parent ... */ andre@0: res = mpp_pprime(&q, num_tests); andre@0: if (res != MP_YES) { andre@0: mp_clear(&q); andre@0: if (res == MP_NO) andre@0: continue; /* was composite */ andre@0: goto CLEANUP; andre@0: } andre@0: andre@0: /* If it passed, we've got a winner */ andre@0: mp_exch(&q, &trial); andre@0: mp_clear(&q); andre@0: break; andre@0: andre@0: } /* end of loop through sieved values */ andre@0: if (res == MP_YES) andre@0: mp_exch(&trial, start); andre@0: CLEANUP: andre@0: mp_clear(&trial); andre@0: mp_clear(&q); andre@0: if (nTries) andre@0: *nTries += i; andre@0: if (sieve != NULL) { andre@0: memset(sieve, 0, SIEVE_SIZE); andre@0: free (sieve); andre@0: } andre@0: return res; andre@0: } andre@0: andre@0: /*========================================================================*/ andre@0: /*------------------------------------------------------------------------*/ andre@0: /* Static functions visible only to the library internally */ andre@0: andre@0: /* {{{ s_mpp_divp(a, vec, size, which) */ andre@0: andre@0: /* andre@0: Test for divisibility by members of a vector of digits. Returns andre@0: MP_NO if a is not divisible by any of them; returns MP_YES and sets andre@0: 'which' to the index of the offender, if it is. Will stop on the andre@0: first digit against which a is divisible. andre@0: */ andre@0: andre@0: mp_err s_mpp_divp(mp_int *a, const mp_digit *vec, int size, int *which) andre@0: { andre@0: mp_err res; andre@0: mp_digit rem; andre@0: andre@0: int ix; andre@0: andre@0: for(ix = 0; ix < size; ix++) { andre@0: if((res = mp_mod_d(a, vec[ix], &rem)) != MP_OKAY) andre@0: return res; andre@0: andre@0: if(rem == 0) { andre@0: if(which) andre@0: *which = ix; andre@0: return MP_YES; andre@0: } andre@0: } andre@0: andre@0: return MP_NO; andre@0: andre@0: } /* end s_mpp_divp() */ andre@0: andre@0: /* }}} */ andre@0: andre@0: /*------------------------------------------------------------------------*/ andre@0: /* HERE THERE BE DRAGONS */