change some parameters in the library to meet the needs of the website httpbin.org

Fork of MiniTLS-GPL by Donatien Garnier

Committer:
MiniTLS
Date:
Fri Jun 06 10:49:02 2014 +0000
Revision:
0:35aa5be3b78d
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MiniTLS 0:35aa5be3b78d 1 /* TomsFastMath, a fast ISO C bignum library.
MiniTLS 0:35aa5be3b78d 2 *
MiniTLS 0:35aa5be3b78d 3 * This project is meant to fill in where LibTomMath
MiniTLS 0:35aa5be3b78d 4 * falls short. That is speed ;-)
MiniTLS 0:35aa5be3b78d 5 *
MiniTLS 0:35aa5be3b78d 6 * This project is public domain and free for all purposes.
MiniTLS 0:35aa5be3b78d 7 *
MiniTLS 0:35aa5be3b78d 8 * Tom St Denis, tomstdenis@gmail.com
MiniTLS 0:35aa5be3b78d 9 */
MiniTLS 0:35aa5be3b78d 10 #include <tfm.h>
MiniTLS 0:35aa5be3b78d 11
MiniTLS 0:35aa5be3b78d 12 /* Miller-Rabin test of "a" to the base of "b" as described in
MiniTLS 0:35aa5be3b78d 13 * HAC pp. 139 Algorithm 4.24
MiniTLS 0:35aa5be3b78d 14 *
MiniTLS 0:35aa5be3b78d 15 * Sets result to 0 if definitely composite or 1 if probably prime.
MiniTLS 0:35aa5be3b78d 16 * Randomly the chance of error is no more than 1/4 and often
MiniTLS 0:35aa5be3b78d 17 * very much lower.
MiniTLS 0:35aa5be3b78d 18 */
MiniTLS 0:35aa5be3b78d 19 void fp_prime_miller_rabin (fp_int * a, fp_int * b, int *result)
MiniTLS 0:35aa5be3b78d 20 {
MiniTLS 0:35aa5be3b78d 21 fp_int n1, y, r;
MiniTLS 0:35aa5be3b78d 22 int s, j;
MiniTLS 0:35aa5be3b78d 23
MiniTLS 0:35aa5be3b78d 24 /* default */
MiniTLS 0:35aa5be3b78d 25 *result = FP_NO;
MiniTLS 0:35aa5be3b78d 26
MiniTLS 0:35aa5be3b78d 27 /* ensure b > 1 */
MiniTLS 0:35aa5be3b78d 28 if (fp_cmp_d(b, 1) != FP_GT) {
MiniTLS 0:35aa5be3b78d 29 return;
MiniTLS 0:35aa5be3b78d 30 }
MiniTLS 0:35aa5be3b78d 31
MiniTLS 0:35aa5be3b78d 32 /* get n1 = a - 1 */
MiniTLS 0:35aa5be3b78d 33 fp_init_copy(&n1, a);
MiniTLS 0:35aa5be3b78d 34 fp_sub_d(&n1, 1, &n1);
MiniTLS 0:35aa5be3b78d 35
MiniTLS 0:35aa5be3b78d 36 /* set 2**s * r = n1 */
MiniTLS 0:35aa5be3b78d 37 fp_init_copy(&r, &n1);
MiniTLS 0:35aa5be3b78d 38
MiniTLS 0:35aa5be3b78d 39 /* count the number of least significant bits
MiniTLS 0:35aa5be3b78d 40 * which are zero
MiniTLS 0:35aa5be3b78d 41 */
MiniTLS 0:35aa5be3b78d 42 s = fp_cnt_lsb(&r);
MiniTLS 0:35aa5be3b78d 43
MiniTLS 0:35aa5be3b78d 44 /* now divide n - 1 by 2**s */
MiniTLS 0:35aa5be3b78d 45 fp_div_2d (&r, s, &r, NULL);
MiniTLS 0:35aa5be3b78d 46
MiniTLS 0:35aa5be3b78d 47 /* compute y = b**r mod a */
MiniTLS 0:35aa5be3b78d 48 fp_init(&y);
MiniTLS 0:35aa5be3b78d 49 fp_exptmod(b, &r, a, &y);
MiniTLS 0:35aa5be3b78d 50
MiniTLS 0:35aa5be3b78d 51 /* if y != 1 and y != n1 do */
MiniTLS 0:35aa5be3b78d 52 if (fp_cmp_d (&y, 1) != FP_EQ && fp_cmp (&y, &n1) != FP_EQ) {
MiniTLS 0:35aa5be3b78d 53 j = 1;
MiniTLS 0:35aa5be3b78d 54 /* while j <= s-1 and y != n1 */
MiniTLS 0:35aa5be3b78d 55 while ((j <= (s - 1)) && fp_cmp (&y, &n1) != FP_EQ) {
MiniTLS 0:35aa5be3b78d 56 fp_sqrmod (&y, a, &y);
MiniTLS 0:35aa5be3b78d 57
MiniTLS 0:35aa5be3b78d 58 /* if y == 1 then composite */
MiniTLS 0:35aa5be3b78d 59 if (fp_cmp_d (&y, 1) == FP_EQ) {
MiniTLS 0:35aa5be3b78d 60 return;
MiniTLS 0:35aa5be3b78d 61 }
MiniTLS 0:35aa5be3b78d 62 ++j;
MiniTLS 0:35aa5be3b78d 63 }
MiniTLS 0:35aa5be3b78d 64
MiniTLS 0:35aa5be3b78d 65 /* if y != n1 then composite */
MiniTLS 0:35aa5be3b78d 66 if (fp_cmp (&y, &n1) != FP_EQ) {
MiniTLS 0:35aa5be3b78d 67 return;
MiniTLS 0:35aa5be3b78d 68 }
MiniTLS 0:35aa5be3b78d 69 }
MiniTLS 0:35aa5be3b78d 70
MiniTLS 0:35aa5be3b78d 71 /* probably prime now */
MiniTLS 0:35aa5be3b78d 72 *result = FP_YES;
MiniTLS 0:35aa5be3b78d 73 }
MiniTLS 0:35aa5be3b78d 74
MiniTLS 0:35aa5be3b78d 75 /* $Source: /cvs/libtom/tomsfastmath/src/numtheory/fp_prime_miller_rabin.c,v $ */
MiniTLS 0:35aa5be3b78d 76 /* $Revision: 1.1 $ */
MiniTLS 0:35aa5be3b78d 77 /* $Date: 2007/01/24 21:25:19 $ */