1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/*************************************************
* Number Theory Source File *
* (C) 1999-2005 The Botan Project *
*************************************************/
#include <botan/numthry.h>
#include <botan/ui.h>
namespace Botan {
namespace {
/*************************************************
* Miller-Rabin Iterations *
*************************************************/
u32bit miller_rabin_test_iterations(u32bit bits, bool verify)
{
struct mapping { u32bit bits; u32bit verify_iter; u32bit check_iter; };
static const mapping tests[] = {
{ 50, 55, 25 },
{ 100, 38, 22 },
{ 160, 32, 18 },
{ 163, 31, 17 },
{ 168, 30, 16 },
{ 177, 29, 16 },
{ 181, 28, 15 },
{ 185, 27, 15 },
{ 190, 26, 15 },
{ 195, 25, 14 },
{ 201, 24, 14 },
{ 208, 23, 14 },
{ 215, 22, 13 },
{ 222, 21, 13 },
{ 231, 20, 13 },
{ 241, 19, 12 },
{ 252, 18, 12 },
{ 264, 17, 12 },
{ 278, 16, 11 },
{ 294, 15, 10 },
{ 313, 14, 9 },
{ 334, 13, 8 },
{ 360, 12, 8 },
{ 392, 11, 7 },
{ 430, 10, 7 },
{ 479, 9, 6 },
{ 542, 8, 6 },
{ 626, 7, 5 },
{ 746, 6, 4 },
{ 926, 5, 3 },
{ 1232, 4, 2 },
{ 1853, 3, 2 },
{ 0, 0, 0 }
};
for(u32bit j = 0; tests[j].bits; j++)
{
if(bits <= tests[j].bits)
if(verify)
return tests[j].verify_iter;
else
return tests[j].check_iter;
}
return 2;
}
}
/*************************************************
* Return the number of 0 bits at the end of n *
*************************************************/
u32bit low_zero_bits(const BigInt& n)
{
if(n.is_zero()) return 0;
u32bit bits = 0, max_bits = n.bits();
while((n.get_bit(bits) == 0) && bits < max_bits) bits++;
return bits;
}
/*************************************************
* Calculate the GCD *
*************************************************/
BigInt gcd(const BigInt& a, const BigInt& b)
{
if(a.is_zero() || b.is_zero()) return 0;
if(a == 1 || b == 1) return 1;
BigInt x = a, y = b;
x.set_sign(BigInt::Positive);
y.set_sign(BigInt::Positive);
u32bit shift = std::min(low_zero_bits(x), low_zero_bits(y));
x >>= shift;
y >>= shift;
while(x.is_nonzero())
{
x >>= low_zero_bits(x);
y >>= low_zero_bits(y);
if(x >= y) { x -= y; x >>= 1; }
else { y -= x; y >>= 1; }
}
return (y << shift);
}
/*************************************************
* Calculate the LCM *
*************************************************/
BigInt lcm(const BigInt& a, const BigInt& b)
{
return ((a * b) / gcd(a, b));
}
/*************************************************
* Square a BigInt *
*************************************************/
BigInt square(const BigInt& a)
{
return (a * a);
}
/*************************************************
* Find the Modular Inverse *
*************************************************/
BigInt inverse_mod(const BigInt& n, const BigInt& mod)
{
if(mod.is_zero())
throw BigInt::DivideByZero();
if(mod.is_negative() || n.is_negative())
throw Invalid_Argument("inverse_mod: arguments must be non-negative");
if(n.is_zero() || (n.is_even() && mod.is_even()))
return 0;
BigInt x = mod, y = n, u = mod, v = n;
BigInt A = 1, B = 0, C = 0, D = 1;
while(u.is_nonzero())
{
u32bit zero_bits = low_zero_bits(u);
u >>= zero_bits;
for(u32bit j = 0; j != zero_bits; j++)
{
if(A.is_odd() || B.is_odd())
{ A += y; B -= x; }
A >>= 1; B >>= 1;
}
zero_bits = low_zero_bits(v);
v >>= zero_bits;
for(u32bit j = 0; j != zero_bits; j++)
{
if(C.is_odd() || D.is_odd())
{ C += y; D -= x; }
C >>= 1; D >>= 1;
}
if(u >= v) { u -= v; A -= C; B -= D; }
else { v -= u; C -= A; D -= B; }
}
if(v != 1)
return 0;
while(D.is_negative()) D += mod;
while(D >= mod) D -= mod;
return D;
}
/*************************************************
* Calculate the Jacobi symbol *
*************************************************/
s32bit jacobi(const BigInt& a, const BigInt& n)
{
if(a.is_negative())
throw Invalid_Argument("jacobi: first argument must be non-negative");
if(n.is_even() || n < 2)
throw Invalid_Argument("jacobi: second argument must be odd and > 1");
BigInt x = a, y = n;
s32bit J = 1;
while(y > 1)
{
x %= y;
if(x > y / 2)
{
x = y - x;
if(y % 4 == 3)
J = -J;
}
if(x.is_zero())
return 0;
while(x % 4 == 0)
x >>= 2;
if(x.is_even())
{
x >>= 1;
if(y % 8 == 3 || y % 8 == 5)
J = -J;
}
if(x % 4 == 3 && y % 4 == 3)
J = -J;
std::swap(x, y);
}
return J;
}
/*************************************************
* Exponentiation *
*************************************************/
BigInt power(const BigInt& base, u32bit exp)
{
BigInt x = 1, a = base;
while(exp)
{
if(exp % 2)
x *= a;
exp >>= 1;
if(exp)
a = square(a);
}
return x;
}
/*************************************************
* Do simple tests of primality *
*************************************************/
s32bit simple_primality_tests(const BigInt& n)
{
const s32bit NOT_PRIME = -1, UNKNOWN = 0, PRIME = 1;
if(n == 2)
return PRIME;
if(n <= 1 || n.is_even())
return NOT_PRIME;
if(n <= PRIMES[PRIME_TABLE_SIZE-1])
{
const word num = n.word_at(0);
for(u32bit j = 0; PRIMES[j]; j++)
{
if(num == PRIMES[j]) return PRIME;
if(num < PRIMES[j]) return NOT_PRIME;
}
return NOT_PRIME;
}
u32bit check_first = std::min(n.bits() / 32, PRIME_PRODUCTS_TABLE_SIZE);
for(u32bit j = 0; j != check_first; j++)
if(gcd(n, PRIME_PRODUCTS[j]) != 1)
return NOT_PRIME;
return UNKNOWN;
}
/*************************************************
* Fast check of primality *
*************************************************/
bool check_prime(const BigInt& n)
{
return run_primality_tests(n, 0);
}
/*************************************************
* Test for primality *
*************************************************/
bool is_prime(const BigInt& n)
{
return run_primality_tests(n, 1);
}
/*************************************************
* Verify primality *
*************************************************/
bool verify_prime(const BigInt& n)
{
return run_primality_tests(n, 2);
}
/*************************************************
* Verify primality *
*************************************************/
bool run_primality_tests(const BigInt& n, u32bit level)
{
s32bit simple_tests = simple_primality_tests(n);
if(simple_tests) return (simple_tests == 1) ? true : false;
return passes_mr_tests(n, level);
}
/*************************************************
* Test for primaility using Miller-Rabin *
*************************************************/
bool passes_mr_tests(const BigInt& n, u32bit level)
{
const u32bit PREF_NONCE_BITS = 40;
if(level > 2)
level = 2;
MillerRabin_Test mr(n);
if(!mr.passes_test(2))
return false;
if(level == 0)
return true;
const u32bit NONCE_BITS = std::min(n.bits() - 1, PREF_NONCE_BITS);
const bool verify = (level == 2);
u32bit tests = miller_rabin_test_iterations(n.bits(), verify);
BigInt nonce;
for(u32bit j = 0; j != tests; j++)
{
if(verify) nonce = random_integer(NONCE_BITS, Nonce);
else nonce = PRIMES[j];
if(!mr.passes_test(nonce))
return false;
}
return true;
}
/*************************************************
* Miller-Rabin Test *
*************************************************/
bool MillerRabin_Test::passes_test(const BigInt& a)
{
if(a < 2 || a >= n_minus_1)
throw Invalid_Argument("Bad size for nonce in Miller-Rabin test");
UI::pulse(UI::PRIME_TESTING);
BigInt y = power_mod(a, r, reducer);
if(y == 1 || y == n_minus_1)
return true;
for(u32bit j = 1; j != s; j++)
{
UI::pulse(UI::PRIME_TESTING);
y = reducer->square(y);
if(y == 1)
return false;
if(y == n_minus_1)
return true;
}
return false;
}
/*************************************************
* Miller-Rabin Constructor *
*************************************************/
MillerRabin_Test::MillerRabin_Test(const BigInt& num)
{
if(num.is_even() || num < 3)
throw Invalid_Argument("MillerRabin_Test: Invalid number for testing");
n = num;
n_minus_1 = n - 1;
s = low_zero_bits(n_minus_1);
r = n_minus_1 >> s;
reducer = get_reducer(n);
}
}
|