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
/* Generates provable primes
*
* See http://iahu.ca:8080/papers/pp.pdf for more info.
*
* Tom St Denis, tomstdenis@iahu.ca, http://tom.iahu.ca
*/
#include <time.h>
#include "bn.h"
/* fast square root */
static mp_digit i_sqrt(mp_word x)
{
mp_word x1, x2;
x2 = x;
do {
x1 = x2;
x2 = x1 - ((x1 * x1) - x)/(2*x1);
} while (x1 != x2);
if (x1*x1 > x) {
--x1;
}
return x1;
}
/* generates a prime digit */
static mp_digit prime_digit()
{
mp_digit r, x, y, next;
/* make a DIGIT_BIT-bit random number */
for (r = x = 0; x < DIGIT_BIT; x++) {
r = (r << 1) | (rand() & 1);
}
/* now force it odd */
r |= 1;
/* force it to be >30 */
if (r < 30) {
r += 30;
}
/* get square root, since if 'r' is composite its factors must be < than this */
y = i_sqrt(r);
next = (y+1)*(y+1);
do {
r += 2; /* next candidate */
/* update sqrt ? */
if (next <= r) {
++y;
next = (y+1)*(y+1);
}
/* loop if divisible by 3,5,7,11,13,17,19,23,29 */
if ((r % 3) == 0) { x = 0; continue; }
if ((r % 5) == 0) { x = 0; continue; }
if ((r % 7) == 0) { x = 0; continue; }
if ((r % 11) == 0) { x = 0; continue; }
if ((r % 13) == 0) { x = 0; continue; }
if ((r % 17) == 0) { x = 0; continue; }
if ((r % 19) == 0) { x = 0; continue; }
if ((r % 23) == 0) { x = 0; continue; }
if ((r % 29) == 0) { x = 0; continue; }
/* now check if r is divisible by x + k={1,7,11,13,17,19,23,29} */
for (x = 30; x <= y; x += 30) {
if ((r % (x+1)) == 0) { x = 0; break; }
if ((r % (x+7)) == 0) { x = 0; break; }
if ((r % (x+11)) == 0) { x = 0; break; }
if ((r % (x+13)) == 0) { x = 0; break; }
if ((r % (x+17)) == 0) { x = 0; break; }
if ((r % (x+19)) == 0) { x = 0; break; }
if ((r % (x+23)) == 0) { x = 0; break; }
if ((r % (x+29)) == 0) { x = 0; break; }
}
} while (x == 0);
return r;
}
/* makes a prime of at least k bits */
int pprime(int k, mp_int *p, mp_int *q)
{
mp_int a, b, c, n, x, y, z, v;
int res;
/* single digit ? */
if (k <= (int)DIGIT_BIT) {
mp_set(p, prime_digit());
return MP_OKAY;
}
if ((res = mp_init(&c)) != MP_OKAY) {
return res;
}
if ((res = mp_init(&v)) != MP_OKAY) {
goto __C;
}
/* product of first 50 primes */
if ((res = mp_read_radix(&v, "19078266889580195013601891820992757757219839668357012055907516904309700014933909014729740190", 10)) != MP_OKAY) {
goto __V;
}
if ((res = mp_init(&a)) != MP_OKAY) {
goto __V;
}
/* set the prime */
mp_set(&a, prime_digit());
if ((res = mp_init(&b)) != MP_OKAY) {
goto __A;
}
if ((res = mp_init(&n)) != MP_OKAY) {
goto __B;
}
if ((res = mp_init(&x)) != MP_OKAY) {
goto __N;
}
if ((res = mp_init(&y)) != MP_OKAY) {
goto __X;
}
if ((res = mp_init(&z)) != MP_OKAY) {
goto __Y;
}
/* now loop making the single digit */
while (mp_count_bits(&a) < k) {
printf("prime has %4d bits left\r", k - mp_count_bits(&a)); fflush(stdout);
top:
mp_set(&b, prime_digit());
/* now compute z = a * b * 2 */
if ((res = mp_mul(&a, &b, &z)) != MP_OKAY) { /* z = a * b */
goto __Z;
}
if ((res = mp_copy(&z, &c)) != MP_OKAY) { /* c = a * b */
goto __Z;
}
if ((res = mp_mul_2(&z, &z)) != MP_OKAY) { /* z = 2 * a * b */
goto __Z;
}
/* n = z + 1 */
if ((res = mp_add_d(&z, 1, &n)) != MP_OKAY) { /* n = z + 1 */
goto __Z;
}
/* check (n, v) == 1 */
if ((res = mp_gcd(&n, &v, &y)) != MP_OKAY) { /* y = (n, v) */
goto __Z;
}
if (mp_cmp_d(&y, 1) != MP_EQ) goto top;
/* now try base x=2 */
mp_set(&x, 2);
/* compute x^a mod n */
if ((res = mp_exptmod(&x, &a, &n, &y)) != MP_OKAY) { /* y = x^a mod n */
goto __Z;
}
/* if y == 1 loop */
if (mp_cmp_d(&y, 1) == MP_EQ) goto top;
/* now x^2a mod n */
if ((res = mp_sqrmod(&y, &n, &y)) != MP_OKAY) { /* y = x^2a mod n */
goto __Z;
}
if (mp_cmp_d(&y, 1) == MP_EQ) goto top;
/* compute x^b mod n */
if ((res = mp_exptmod(&x, &b, &n, &y)) != MP_OKAY) { /* y = x^b mod n */
goto __Z;
}
/* if y == 1 loop */
if (mp_cmp_d(&y, 1) == MP_EQ) goto top;
/* now x^2b mod n */
if ((res = mp_sqrmod(&y, &n, &y)) != MP_OKAY) { /* y = x^2b mod n */
goto __Z;
}
if (mp_cmp_d(&y, 1) == MP_EQ) goto top;
/* compute x^c mod n == x^ab mod n */
if ((res = mp_exptmod(&x, &c, &n, &y)) != MP_OKAY) { /* y = x^ab mod n */
goto __Z;
}
/* if y == 1 loop */
if (mp_cmp_d(&y, 1) == MP_EQ) goto top;
/* now compute (x^c mod n)^2 */
if ((res = mp_sqrmod(&y, &n, &y)) != MP_OKAY) { /* y = x^2ab mod n */
goto __Z;
}
/* y should be 1 */
if (mp_cmp_d(&y, 1) != MP_EQ) goto top;
/*
{
char buf[4096];
mp_toradix(&n, buf, 10);
printf("Certificate of primality for:\n%s\n\n", buf);
mp_toradix(&a, buf, 10);
printf("A == \n%s\n\n", buf);
mp_toradix(&b, buf, 10);
printf("B == \n%s\n", buf);
printf("----------------------------------------------------------------\n");
}
*/
/* a = n */
mp_copy(&n, &a);
}
mp_exch(&n, p);
mp_exch(&b, q);
res = MP_OKAY;
__Z: mp_clear(&z);
__Y: mp_clear(&y);
__X: mp_clear(&x);
__N: mp_clear(&n);
__B: mp_clear(&b);
__A: mp_clear(&a);
__V: mp_clear(&v);
__C: mp_clear(&c);
return res;
}
int main(void)
{
mp_int p, q;
char buf[4096];
int k;
clock_t t1;
srand(time(NULL));
printf("Enter # of bits: \n");
scanf("%d", &k);
mp_init(&p);
mp_init(&q);
t1 = clock();
pprime(k, &p, &q);
t1 = clock() - t1;
printf("\n\nTook %ld ticks, %d bits\n", t1, mp_count_bits(&p));
mp_toradix(&p, buf, 10);
printf("P == %s\n", buf);
mp_toradix(&q, buf, 10);
printf("Q == %s\n", buf);
return 0;
}