| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: rabin.c,v 1.3 1999/12/22 15:50:29 mdw Exp $ |
| 4 | * |
| 5 | * Miller-Rabin primality test |
| 6 | * |
| 7 | * (c) 1999 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 14 | * Catacomb is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with Catacomb; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: rabin.c,v $ |
| 33 | * Revision 1.3 1999/12/22 15:50:29 mdw |
| 34 | * Reworking for new prime-search system. Add function for working out how |
| 35 | * many iterations to use for a particular number. |
| 36 | * |
| 37 | * Revision 1.2 1999/12/10 23:29:48 mdw |
| 38 | * Change header file guard names. |
| 39 | * |
| 40 | * Revision 1.1 1999/11/19 13:17:57 mdw |
| 41 | * Prime number generator and tester. |
| 42 | * |
| 43 | */ |
| 44 | |
| 45 | /*----- Header files ------------------------------------------------------*/ |
| 46 | |
| 47 | #include "mp.h" |
| 48 | #include "mpbarrett.h" |
| 49 | #include "mpmont.h" |
| 50 | #include "pgen.h" |
| 51 | #include "rabin.h" |
| 52 | |
| 53 | /*----- Main code ---------------------------------------------------------*/ |
| 54 | |
| 55 | /* --- @rabin_create@ --- * |
| 56 | * |
| 57 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
| 58 | * @mp *m@ = pointer to number to test |
| 59 | * |
| 60 | * Returns: --- |
| 61 | * |
| 62 | * Use: Precomputes some useful values for performing the |
| 63 | * Miller-Rabin probabilistic primality test. |
| 64 | */ |
| 65 | |
| 66 | void rabin_create(rabin *r, mp *m) |
| 67 | { |
| 68 | mp *m1 = mp_sub(MP_NEW, m, MP_ONE); |
| 69 | mpscan sc; |
| 70 | size_t s; |
| 71 | |
| 72 | /* --- Find @r@ and @s@ --- */ |
| 73 | |
| 74 | mpmont_create(&r->mm, m); |
| 75 | mp_scan(&sc, m1); |
| 76 | s = 0; |
| 77 | while (mp_step(&sc)) { |
| 78 | if (mp_bit(&sc)) |
| 79 | break; |
| 80 | s++; |
| 81 | } |
| 82 | r->s = s; |
| 83 | r->r = mp_lsr(MP_NEW, m1, s); |
| 84 | |
| 85 | /* --- Compute %$(m - 1)R \bmod m$% --- */ |
| 86 | |
| 87 | r->m1 = mp_sub(MP_NEW, m, r->mm.r); |
| 88 | mp_drop(m1); |
| 89 | } |
| 90 | |
| 91 | /* --- @rabin_destroy@ --- * |
| 92 | * |
| 93 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
| 94 | * |
| 95 | * Returns: --- |
| 96 | * |
| 97 | * Use: Disposes of a Rabin-Miller context when it's no longer |
| 98 | * needed. |
| 99 | */ |
| 100 | |
| 101 | void rabin_destroy(rabin *r) |
| 102 | { |
| 103 | mp_drop(r->r); |
| 104 | mp_drop(r->m1); |
| 105 | mpmont_destroy(&r->mm); |
| 106 | } |
| 107 | |
| 108 | /* --- @rabin_test@ --- * |
| 109 | * |
| 110 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
| 111 | * @mp *g@ = base to test the number against |
| 112 | * |
| 113 | * Returns: Either @PGEN_FAIL@ if the test failed, or @PGEN_PASS@ |
| 114 | * if it succeeded. |
| 115 | * |
| 116 | * Use: Performs a single iteration of the Rabin-Miller primality |
| 117 | * test. |
| 118 | */ |
| 119 | |
| 120 | int rabin_test(rabin *r, mp *g) |
| 121 | { |
| 122 | mp *y; |
| 123 | mp *dd, *spare = MP_NEW; |
| 124 | size_t j; |
| 125 | int rc = PGEN_FAIL; |
| 126 | |
| 127 | /* --- Calculate %$y R = g^r R \bmod m$% --- * |
| 128 | * |
| 129 | * If %$y = 1$% or %$y = m - 1$% then %$m$% is prime. If course, note that |
| 130 | * @y@ here has an extra factor of %$R$%. |
| 131 | */ |
| 132 | |
| 133 | y = mpmont_expr(&r->mm, MP_NEW, g, r->r); |
| 134 | if (MP_CMP(y, ==, r->mm.r) || MP_CMP(y, ==, r->m1)) { |
| 135 | rc = PGEN_PASS; |
| 136 | goto done; |
| 137 | } |
| 138 | |
| 139 | /* --- Now for the main loop --- * |
| 140 | * |
| 141 | * If %$y^{2^j} \ne m - 1$% for any %$0 \le j < s$% then %$m$% is |
| 142 | * composite. Of course, %$j = 0$% has already been tested. |
| 143 | */ |
| 144 | |
| 145 | for (j = 1; j < r->s; j++) { |
| 146 | dd = mp_sqr(spare, y); |
| 147 | dd = mpmont_reduce(&r->mm, dd, dd); |
| 148 | spare = y; y = dd; |
| 149 | if (MP_CMP(y, ==, r->mm.r)) |
| 150 | break; |
| 151 | if (MP_CMP(y, ==, r->m1)) { |
| 152 | rc = PGEN_PASS; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* --- Done --- */ |
| 158 | |
| 159 | done: |
| 160 | if (spare != MP_NEW) |
| 161 | MP_DROP(spare); |
| 162 | MP_DROP(y); |
| 163 | return (rc); |
| 164 | } |
| 165 | |
| 166 | /* --- @rabin_iters@ --- * |
| 167 | * |
| 168 | * Arguments: @unsigned len@ = number of bits in value |
| 169 | * |
| 170 | * Returns: Number of iterations recommended. |
| 171 | * |
| 172 | * Use: Returns the recommended number of iterations to ensure that a |
| 173 | * number with @len@ bits is really prime. |
| 174 | */ |
| 175 | |
| 176 | int rabin_iters(unsigned len) |
| 177 | { |
| 178 | static struct { |
| 179 | unsigned b; |
| 180 | int i; |
| 181 | } *p, *q, tab[] = { |
| 182 | { 100, 27 }, |
| 183 | { 150, 18 }, |
| 184 | { 200, 15 }, |
| 185 | { 250, 12 }, |
| 186 | { 300, 9 }, |
| 187 | { 350, 8 }, |
| 188 | { 400, 7 }, |
| 189 | { 450, 6 }, |
| 190 | { 550, 5 }, |
| 191 | { 650, 4 }, |
| 192 | { 850, 3 }, |
| 193 | { 1300, 2 } |
| 194 | }; |
| 195 | |
| 196 | unsigned i; |
| 197 | |
| 198 | /* --- Binary search through the table --- */ |
| 199 | |
| 200 | p = tab; |
| 201 | q = tab + (sizeof(tab)/sizeof(tab[0])); |
| 202 | for (;;) { |
| 203 | i = (q - p) / 2; |
| 204 | if (!i) |
| 205 | break; |
| 206 | if (len >= p[i].b && len < p[i + 1].b) |
| 207 | break; |
| 208 | if (len > p[i].b) |
| 209 | p = p + i; |
| 210 | else |
| 211 | q = p + i; |
| 212 | } |
| 213 | return (p[i].i); |
| 214 | } |
| 215 | |
| 216 | /*----- That's all, folks -------------------------------------------------*/ |