Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | static u_int32_t *TinySieve, tinybits; | - |
13 | | - |
14 | | - |
15 | static u_int32_t *SmallSieve, smallbits, smallbase; | - |
16 | | - |
17 | | - |
18 | static u_int32_t *LargeSieve, largewords, largetries, largenumbers; | - |
19 | static u_int32_t largebits, largememory; | - |
20 | static BIGNUM *largebase; | - |
21 | | - |
22 | int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *); | - |
23 | int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *, unsigned long, | - |
24 | unsigned long); | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | static int | - |
30 | qfileout(FILE * ofile, u_int32_t otype, u_int32_t otests, u_int32_t otries, | - |
31 | u_int32_t osize, u_int32_t ogenerator, BIGNUM * omodulus) | - |
32 | { | - |
33 | struct tm *gtm; | - |
34 | time_t time_now; | - |
35 | int res; | - |
36 | | - |
37 | time(&time_now); | - |
38 | gtm = gmtime(&time_now); | - |
39 | | - |
40 | res = fprintf(ofile, "%04d%02d%02d%02d%02d%02d %u %u %u %u %x ", | - |
41 | gtm->tm_year + 1900, gtm->tm_mon + 1, gtm->tm_mday, | - |
42 | gtm->tm_hour, gtm->tm_min, gtm->tm_sec, | - |
43 | otype, otests, otries, osize, ogenerator); | - |
44 | | - |
45 | if (res < 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
46 | return never executed: return (-1); (-1);never executed: return (-1); | 0 |
47 | | - |
48 | if (BN_print_fp(ofile, omodulus) < 1TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
49 | return never executed: return (-1); (-1);never executed: return (-1); | 0 |
50 | | - |
51 | res = fprintf(ofile, "\n"); | - |
52 | fflush(ofile); | - |
53 | | - |
54 | return never executed: return (res > 0 ? 0 : -1); (res > 0TRUE | never evaluated | FALSE | never evaluated |
? 0 : -1);never executed: return (res > 0 ? 0 : -1); | 0 |
55 | } | - |
56 | | - |
57 | | - |
58 | | - |
59 | | - |
60 | | - |
61 | static void | - |
62 | sieve_large(u_int32_t s) | - |
63 | { | - |
64 | u_int32_t r, u; | - |
65 | | - |
66 | debug3("sieve_large %u", s); | - |
67 | largetries++; | - |
68 | | - |
69 | r = BN_mod_word(largebase, s); | - |
70 | if (r == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
71 | u = 0; never executed: u = 0; | 0 |
72 | else | - |
73 | u = s - r; never executed: u = s - r; | 0 |
74 | | - |
75 | if (u < largebits * 2TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
76 | | - |
77 | | - |
78 | | - |
79 | | - |
80 | | - |
81 | if (u & 0x1TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
82 | u += s; never executed: u += s; | 0 |
83 | | - |
84 | | - |
85 | for (u /= 2; u < largebitsTRUE | never evaluated | FALSE | never evaluated |
; u += s) | 0 |
86 | (( never executed: ((LargeSieve)[(u)>>((3)+(2))] |= (1L << ((u) & 31))); LargeSieve)[(u)>>((3)+(2))] |= (1L << ((u) & 31)));never executed: ((LargeSieve)[(u)>>((3)+(2))] |= (1L << ((u) & 31))); | 0 |
87 | } never executed: end of block | 0 |
88 | | - |
89 | | - |
90 | r = (2 * r + 1) % s; | - |
91 | if (r == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
92 | u = 0; never executed: u = 0; | 0 |
93 | else | - |
94 | u = s - r; never executed: u = s - r; | 0 |
95 | | - |
96 | if (u < largebits * 4TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
97 | | - |
98 | | - |
99 | | - |
100 | | - |
101 | | - |
102 | while (u & 0x3TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
103 | if ((TRUE | never evaluated | FALSE | never evaluated |
0xffffffffUL) - u < sTRUE | never evaluated | FALSE | never evaluated |
) | 0 |
104 | return; never executed: return; | 0 |
105 | u += s; | - |
106 | } never executed: end of block | 0 |
107 | | - |
108 | | - |
109 | for (u /= 4; u < largebitsTRUE | never evaluated | FALSE | never evaluated |
; u += s) | 0 |
110 | (( never executed: ((LargeSieve)[(u)>>((3)+(2))] |= (1L << ((u) & 31))); LargeSieve)[(u)>>((3)+(2))] |= (1L << ((u) & 31)));never executed: ((LargeSieve)[(u)>>((3)+(2))] |= (1L << ((u) & 31))); | 0 |
111 | } never executed: end of block | 0 |
112 | } never executed: end of block | 0 |
113 | | - |
114 | | - |
115 | | - |
116 | | - |
117 | | - |
118 | | - |
119 | int | - |
120 | gen_candidates(FILE *out, u_int32_t memory, u_int32_t power, BIGNUM *start) | - |
121 | { | - |
122 | BIGNUM *q; | - |
123 | u_int32_t j, r, s, t; | - |
124 | u_int32_t smallwords = (1UL<<16) >> 6; | - |
125 | u_int32_t tinywords = (1UL<<16) >> 6; | - |
126 | time_t time_start, time_stop; | - |
127 | u_int32_t i; | - |
128 | int ret = 0; | - |
129 | | - |
130 | largememory = memory; | - |
131 | | - |
132 | if (memory != 0TRUE | never evaluated | FALSE | never evaluated |
&& | 0 |
133 | (memory < (8UL)TRUE | never evaluated | FALSE | never evaluated |
|| memory > (127UL)TRUE | never evaluated | FALSE | never evaluated |
)) { | 0 |
134 | error("Invalid memory amount (min %ld, max %ld)", | - |
135 | (8UL), (127UL)); | - |
136 | return never executed: return (-1); (-1);never executed: return (-1); | 0 |
137 | } | - |
138 | | - |
139 | | - |
140 | | - |
141 | | - |
142 | | - |
143 | if (power > (1UL<<16)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
144 | error("Too many bits: %u > %lu", power, (1UL<<16)); | - |
145 | return never executed: return (-1); (-1);never executed: return (-1); | 0 |
146 | } else if (power < ((511) + 1)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
147 | error("Too few bits: %u < %u", power, ((511) + 1)); | - |
148 | return never executed: return (-1); (-1);never executed: return (-1); | 0 |
149 | } | - |
150 | power--; | - |
151 | | - |
152 | | - |
153 | | - |
154 | | - |
155 | | - |
156 | | - |
157 | | - |
158 | largewords = ((power * power) >> (((3)+(2)) - (3))); | - |
159 | | - |
160 | | - |
161 | | - |
162 | | - |
163 | | - |
164 | if (largememory > (127UL)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
165 | logit("Limited memory: %u MB; limit %lu MB", | - |
166 | largememory, (127UL)); | - |
167 | largememory = (127UL); | - |
168 | } never executed: end of block | 0 |
169 | | - |
170 | if (largewords <= (largememory << ((20)-(2)))TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
171 | logit("Increased memory: %u MB; need %u bytes", | - |
172 | largememory, (largewords << (2))); | - |
173 | largewords = (largememory << ((20)-(2))); | - |
174 | } never executed: end of block else if (largememory > 0TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
175 | logit("Decreased memory: %u MB; want %u bytes", | - |
176 | largememory, (largewords << (2))); | - |
177 | largewords = (largememory << ((20)-(2))); | - |
178 | } never executed: end of block | 0 |
179 | | - |
180 | TinySieve = xcalloc(tinywords, sizeof(u_int32_t)); | - |
181 | tinybits = tinywords << ((3)+(2)); | - |
182 | | - |
183 | SmallSieve = xcalloc(smallwords, sizeof(u_int32_t)); | - |
184 | smallbits = smallwords << ((3)+(2)); | - |
185 | | - |
186 | | - |
187 | | - |
188 | | - |
189 | while ((TRUE | never evaluated | FALSE | never evaluated |
LargeSieve = calloc(largewords, sizeof(u_int32_t))) == TRUE | never evaluated | FALSE | never evaluated |
| 0 |
190 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
191 | ) | - |
192 | largewords -= (1L << (((20)-(2)) - 2)); never executed: largewords -= (1L << (((20)-(2)) - 2)); | 0 |
193 | | - |
194 | largebits = largewords << ((3)+(2)); | - |
195 | largenumbers = largebits * 2; | - |
196 | | - |
197 | | - |
198 | largetries = 0; | - |
199 | if ((TRUE | never evaluated | FALSE | never evaluated |
q = BN_new()) == TRUE | never evaluated | FALSE | never evaluated |
| 0 |
200 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
201 | ) | - |
202 | fatal("BN_new failed"); never executed: fatal("BN_new failed"); | 0 |
203 | | - |
204 | | - |
205 | | - |
206 | | - |
207 | | - |
208 | if ((TRUE | never evaluated | FALSE | never evaluated |
largebase = BN_new()) == TRUE | never evaluated | FALSE | never evaluated |
| 0 |
209 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
210 | ) | - |
211 | fatal("BN_new failed"); never executed: fatal("BN_new failed"); | 0 |
212 | if (start == TRUE | never evaluated | FALSE | never evaluated |
| 0 |
213 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
214 | ) { | - |
215 | if (BN_rand(largebase, power, 1, 1) == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
216 | fatal("BN_rand failed"); never executed: fatal("BN_rand failed"); | 0 |
217 | } never executed: end of block else { | 0 |
218 | if (BN_copy(largebase, start) == TRUE | never evaluated | FALSE | never evaluated |
| 0 |
219 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
220 | ) | - |
221 | fatal("BN_copy: failed"); never executed: fatal("BN_copy: failed"); | 0 |
222 | } never executed: end of block | 0 |
223 | | - |
224 | | - |
225 | if (BN_set_bit(largebase, 0) == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
226 | fatal("BN_set_bit: failed"); never executed: fatal("BN_set_bit: failed"); | 0 |
227 | | - |
228 | time(&time_start); | - |
229 | | - |
230 | logit("%.24s Sieve next %u plus %u-bit", ctime(&time_start), | - |
231 | largenumbers, power); | - |
232 | debug2("start point: 0x%s", BN_bn2hex(largebase)); | - |
233 | | - |
234 | | - |
235 | | - |
236 | | - |
237 | for (i = 0; i < tinybitsTRUE | never evaluated | FALSE | never evaluated |
; i++) { | 0 |
238 | if (((TRUE | never evaluated | FALSE | never evaluated |
TinySieve)[(i)>>((3)+(2))] & (1L << ((i) & 31)))TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
239 | continue; never executed: continue; | 0 |
240 | | - |
241 | | - |
242 | t = 2 * i + 3; | - |
243 | | - |
244 | | - |
245 | for (j = i + t; j < tinybitsTRUE | never evaluated | FALSE | never evaluated |
; j += t) | 0 |
246 | (( never executed: ((TinySieve)[(j)>>((3)+(2))] |= (1L << ((j) & 31))); TinySieve)[(j)>>((3)+(2))] |= (1L << ((j) & 31)));never executed: ((TinySieve)[(j)>>((3)+(2))] |= (1L << ((j) & 31))); | 0 |
247 | | - |
248 | sieve_large(t); | - |
249 | } never executed: end of block | 0 |
250 | | - |
251 | | - |
252 | | - |
253 | | - |
254 | | - |
255 | for (smallbase = (1UL<<16) + 3; | - |
256 | smallbase < ((0xffffffffUL) - (1UL<<16))TRUE | never evaluated | FALSE | never evaluated |
; | 0 |
257 | smallbase += (1UL<<16)) { | - |
258 | for (i = 0; i < tinybitsTRUE | never evaluated | FALSE | never evaluated |
; i++) { | 0 |
259 | if (((TRUE | never evaluated | FALSE | never evaluated |
TinySieve)[(i)>>((3)+(2))] & (1L << ((i) & 31)))TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
260 | continue; never executed: continue; | 0 |
261 | | - |
262 | | - |
263 | t = 2 * i + 3; | - |
264 | r = smallbase % t; | - |
265 | | - |
266 | if (r == 0TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
267 | s = 0; | - |
268 | } never executed: end of block else { | 0 |
269 | | - |
270 | s = t - r; | - |
271 | } never executed: end of block | 0 |
272 | | - |
273 | | - |
274 | | - |
275 | | - |
276 | | - |
277 | | - |
278 | if (s & 1TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
279 | s += t; never executed: s += t; | 0 |
280 | | - |
281 | | - |
282 | for (s /= 2; s < smallbitsTRUE | never evaluated | FALSE | never evaluated |
; s += t) | 0 |
283 | (( never executed: ((SmallSieve)[(s)>>((3)+(2))] |= (1L << ((s) & 31))); SmallSieve)[(s)>>((3)+(2))] |= (1L << ((s) & 31)));never executed: ((SmallSieve)[(s)>>((3)+(2))] |= (1L << ((s) & 31))); | 0 |
284 | } never executed: end of block | 0 |
285 | | - |
286 | | - |
287 | | - |
288 | | - |
289 | for (i = 0; i < smallbitsTRUE | never evaluated | FALSE | never evaluated |
; i++) { | 0 |
290 | if (((TRUE | never evaluated | FALSE | never evaluated |
SmallSieve)[(i)>>((3)+(2))] & (1L << ((i) & 31)))TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
291 | continue; never executed: continue; | 0 |
292 | | - |
293 | | - |
294 | sieve_large((2 * i) + smallbase); | - |
295 | } never executed: end of block | 0 |
296 | | - |
297 | memset(SmallSieve, 0, smallwords << (2)); | - |
298 | } never executed: end of block | 0 |
299 | | - |
300 | time(&time_stop); | - |
301 | | - |
302 | logit("%.24s Sieved with %u small primes in %lld seconds", | - |
303 | ctime(&time_stop), largetries, (long long)(time_stop - time_start)); | - |
304 | | - |
305 | for (j = r = 0; j < largebitsTRUE | never evaluated | FALSE | never evaluated |
; j++) { | 0 |
306 | if (((TRUE | never evaluated | FALSE | never evaluated |
LargeSieve)[(j)>>((3)+(2))] & (1L << ((j) & 31)))TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
307 | continue; never executed: continue; | 0 |
308 | | - |
309 | debug2("test q = largebase+%u", 2 * j); | - |
310 | if (BN_set_word(q, 2 * j) == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
311 | fatal("BN_set_word failed"); never executed: fatal("BN_set_word failed"); | 0 |
312 | if (BN_add(q, q, largebase) == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
313 | fatal("BN_add failed"); never executed: fatal("BN_add failed"); | 0 |
314 | if (qfileout(out, (4),TRUE | never evaluated | FALSE | never evaluated |
| 0 |
315 | (0x02), largetries,TRUE | never evaluated | FALSE | never evaluated |
| 0 |
316 | (power - 1) , (0), q) == -1TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
317 | ret = -1; | - |
318 | break; never executed: break; | 0 |
319 | } | - |
320 | | - |
321 | r++; | - |
322 | } never executed: end of block | 0 |
323 | | - |
324 | time(&time_stop); | - |
325 | | - |
326 | free(LargeSieve); | - |
327 | free(SmallSieve); | - |
328 | free(TinySieve); | - |
329 | | - |
330 | logit("%.24s Found %u candidates", ctime(&time_stop), r); | - |
331 | | - |
332 | return never executed: return (ret); (ret);never executed: return (ret); | 0 |
333 | } | - |
334 | | - |
335 | static void | - |
336 | write_checkpoint(char *cpfile, u_int32_t lineno) | - |
337 | { | - |
338 | FILE *fp; | - |
339 | char tmp[ | - |
340 | 4096 | - |
341 | ]; | - |
342 | int r; | - |
343 | | - |
344 | r = snprintf(tmp, sizeof(tmp), "%s.XXXXXXXXXX", cpfile); | - |
345 | if (r == -1TRUE | never evaluated | FALSE | never evaluated |
|| r >= TRUE | never evaluated | FALSE | never evaluated |
| 0 |
346 | 4096TRUE | never evaluated | FALSE | never evaluated |
| 0 |
347 | ) { | - |
348 | logit("write_checkpoint: temp pathname too long"); | - |
349 | return; never executed: return; | 0 |
350 | } | - |
351 | if ((TRUE | never evaluated | FALSE | never evaluated |
r = mkstemp(tmp)) == -1TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
352 | logit("mkstemp(%s): %s", tmp, strerror( | - |
353 | (*__errno_location ()) | - |
354 | )); | - |
355 | return; never executed: return; | 0 |
356 | } | - |
357 | if ((TRUE | never evaluated | FALSE | never evaluated |
fp = fdopen(r, "w")) == TRUE | never evaluated | FALSE | never evaluated |
| 0 |
358 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
359 | ) { | - |
360 | logit("write_checkpoint: fdopen: %s", strerror( | - |
361 | (*__errno_location ()) | - |
362 | )); | - |
363 | unlink(tmp); | - |
364 | close(r); | - |
365 | return; never executed: return; | 0 |
366 | } | - |
367 | if (fprintf(fp, "%lu\n", (unsigned long)lineno) > 0TRUE | never evaluated | FALSE | never evaluated |
&& fclose(fp) == 0TRUE | never evaluated | FALSE | never evaluated |
| 0 |
368 | && rename(tmp, cpfile) == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
369 | debug3("wrote checkpoint line %lu to '%s'", never executed: debug3("wrote checkpoint line %lu to '%s'", (unsigned long)lineno, cpfile); | 0 |
370 | (unsigned long)lineno, cpfile); never executed: debug3("wrote checkpoint line %lu to '%s'", (unsigned long)lineno, cpfile); | 0 |
371 | else | - |
372 | logit("failed to write to checkpoint file '%s': %s", cpfile, never executed: logit("failed to write to checkpoint file '%s': %s", cpfile, strerror( (*__errno_location ()) )); | 0 |
373 | strerror( never executed: logit("failed to write to checkpoint file '%s': %s", cpfile, strerror( (*__errno_location ()) )); | 0 |
374 | (*__errno_location ()) never executed: logit("failed to write to checkpoint file '%s': %s", cpfile, strerror( (*__errno_location ()) )); | 0 |
375 | )); never executed: logit("failed to write to checkpoint file '%s': %s", cpfile, strerror( (*__errno_location ()) )); | 0 |
376 | } | - |
377 | | - |
378 | static unsigned long | - |
379 | read_checkpoint(char *cpfile) | - |
380 | { | - |
381 | FILE *fp; | - |
382 | unsigned long lineno = 0; | - |
383 | | - |
384 | if ((TRUE | never evaluated | FALSE | never evaluated |
fp = fopen(cpfile, "r")) == TRUE | never evaluated | FALSE | never evaluated |
| 0 |
385 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
386 | ) | - |
387 | return never executed: return 0; 0;never executed: return 0; | 0 |
388 | if (fscanf(fp, "%lu\n", &lineno) < 1TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
389 | logit("Failed to load checkpoint from '%s'", cpfile); never executed: logit("Failed to load checkpoint from '%s'", cpfile); | 0 |
390 | else | - |
391 | logit("Loaded checkpoint from '%s' line %lu", cpfile, lineno); never executed: logit("Loaded checkpoint from '%s' line %lu", cpfile, lineno); | 0 |
392 | fclose(fp); | - |
393 | return never executed: return lineno; lineno;never executed: return lineno; | 0 |
394 | } | - |
395 | | - |
396 | static unsigned long | - |
397 | count_lines(FILE *f) | - |
398 | { | - |
399 | unsigned long count = 0; | - |
400 | char lp[(100+8192) + 1]; | - |
401 | | - |
402 | if (fseek(f, 0, TRUE | never evaluated | FALSE | never evaluated |
| 0 |
403 | 0TRUE | never evaluated | FALSE | never evaluated |
| 0 |
404 | ) != 0TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
405 | debug("input file is not seekable"); | - |
406 | return never executed: return (0x7fffffffffffffffL * 2UL + 1UL) ; never executed: return (0x7fffffffffffffffL * 2UL + 1UL) ; | 0 |
407 | (0x7fffffffffffffffL * 2UL + 1UL) never executed: return (0x7fffffffffffffffL * 2UL + 1UL) ; | 0 |
408 | ; never executed: return (0x7fffffffffffffffL * 2UL + 1UL) ; | 0 |
409 | } | - |
410 | while (fgets(lp, (100+8192) + 1, f) != TRUE | never evaluated | FALSE | never evaluated |
| 0 |
411 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
412 | ) | - |
413 | count++; never executed: count++; | 0 |
414 | rewind(f); | - |
415 | debug("input file has %lu lines", count); | - |
416 | return never executed: return count; count;never executed: return count; | 0 |
417 | } | - |
418 | | - |
419 | static char * | - |
420 | fmt_time(time_t seconds) | - |
421 | { | - |
422 | int day, hr, min; | - |
423 | static char buf[128]; | - |
424 | | - |
425 | min = (seconds / 60) % 60; | - |
426 | hr = (seconds / 60 / 60) % 24; | - |
427 | day = seconds / 60 / 60 / 24; | - |
428 | if (day > 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
429 | snprintf(buf, sizeof buf, "%dd %d:%02d", day, hr, min); never executed: snprintf(buf, sizeof buf, "%dd %d:%02d", day, hr, min); | 0 |
430 | else | - |
431 | snprintf(buf, sizeof buf, "%d:%02d", hr, min); never executed: snprintf(buf, sizeof buf, "%d:%02d", hr, min); | 0 |
432 | return never executed: return buf; buf;never executed: return buf; | 0 |
433 | } | - |
434 | | - |
435 | static void | - |
436 | print_progress(unsigned long start_lineno, unsigned long current_lineno, | - |
437 | unsigned long end_lineno) | - |
438 | { | - |
439 | static time_t time_start, time_prev; | - |
440 | time_t time_now, elapsed; | - |
441 | unsigned long num_to_process, processed, remaining, percent, eta; | - |
442 | double time_per_line; | - |
443 | char *eta_str; | - |
444 | | - |
445 | time_now = monotime(); | - |
446 | if (time_start == 0TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
447 | time_start = time_prev = time_now; | - |
448 | return; never executed: return; | 0 |
449 | } | - |
450 | | - |
451 | if (time_now - time_prev < 5 * 60TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
452 | return; never executed: return; | 0 |
453 | time_prev = time_now; | - |
454 | elapsed = time_now - time_start; | - |
455 | processed = current_lineno - start_lineno; | - |
456 | remaining = end_lineno - current_lineno; | - |
457 | num_to_process = end_lineno - start_lineno; | - |
458 | time_per_line = (double)elapsed / processed; | - |
459 | | - |
460 | time(&time_now); | - |
461 | if (end_lineno == TRUE | never evaluated | FALSE | never evaluated |
| 0 |
462 | (0x7fffffffffffffffL * 2UL + 1UL)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
463 | ) { | - |
464 | logit("%.24s processed %lu in %s", ctime(&time_now), | - |
465 | processed, fmt_time(elapsed)); | - |
466 | return; never executed: return; | 0 |
467 | } | - |
468 | percent = 100 * processed / num_to_process; | - |
469 | eta = time_per_line * remaining; | - |
470 | eta_str = xstrdup(fmt_time(eta)); | - |
471 | logit("%.24s processed %lu of %lu (%lu%%) in %s, ETA %s", | - |
472 | ctime(&time_now), processed, num_to_process, percent, | - |
473 | fmt_time(elapsed), eta_str); | - |
474 | free(eta_str); | - |
475 | } never executed: end of block | 0 |
476 | | - |
477 | | - |
478 | | - |
479 | | - |
480 | | - |
481 | | - |
482 | | - |
483 | int | - |
484 | prime_test(FILE *in, FILE *out, u_int32_t trials, u_int32_t generator_wanted, | - |
485 | char *checkpoint_file, unsigned long start_lineno, unsigned long num_lines) | - |
486 | { | - |
487 | BIGNUM *q, *p, *a; | - |
488 | BN_CTX *ctx; | - |
489 | char *cp, *lp; | - |
490 | u_int32_t count_in = 0, count_out = 0, count_possible = 0; | - |
491 | u_int32_t generator_known, in_tests, in_tries, in_type, in_size; | - |
492 | unsigned long last_processed = 0, end_lineno; | - |
493 | time_t time_start, time_stop; | - |
494 | int res; | - |
495 | | - |
496 | if (trials < (4)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
497 | error("Minimum primality trials is %d", (4)); | - |
498 | return never executed: return (-1); (-1);never executed: return (-1); | 0 |
499 | } | - |
500 | | - |
501 | if (num_lines == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
502 | end_lineno = count_lines(in); never executed: end_lineno = count_lines(in); | 0 |
503 | else | - |
504 | end_lineno = start_lineno + num_lines; never executed: end_lineno = start_lineno + num_lines; | 0 |
505 | | - |
506 | time(&time_start); | - |
507 | | - |
508 | if ((TRUE | never evaluated | FALSE | never evaluated |
p = BN_new()) == TRUE | never evaluated | FALSE | never evaluated |
| 0 |
509 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
510 | ) | - |
511 | fatal("BN_new failed"); never executed: fatal("BN_new failed"); | 0 |
512 | if ((TRUE | never evaluated | FALSE | never evaluated |
q = BN_new()) == TRUE | never evaluated | FALSE | never evaluated |
| 0 |
513 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
514 | ) | - |
515 | fatal("BN_new failed"); never executed: fatal("BN_new failed"); | 0 |
516 | if ((TRUE | never evaluated | FALSE | never evaluated |
ctx = BN_CTX_new()) == TRUE | never evaluated | FALSE | never evaluated |
| 0 |
517 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
518 | ) | - |
519 | fatal("BN_CTX_new failed"); never executed: fatal("BN_CTX_new failed"); | 0 |
520 | | - |
521 | debug2("%.24s Final %u Miller-Rabin trials (%x generator)", | - |
522 | ctime(&time_start), trials, generator_wanted); | - |
523 | | - |
524 | if (checkpoint_file != TRUE | never evaluated | FALSE | never evaluated |
| 0 |
525 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
526 | ) | - |
527 | last_processed = read_checkpoint(checkpoint_file); never executed: last_processed = read_checkpoint(checkpoint_file); | 0 |
528 | last_processed = start_lineno = (((TRUE | never evaluated | FALSE | never evaluated |
last_processed) > (start_lineno))TRUE | never evaluated | FALSE | never evaluated |
? (last_processed) : (start_lineno)); | 0 |
529 | if (end_lineno == TRUE | never evaluated | FALSE | never evaluated |
| 0 |
530 | (0x7fffffffffffffffL * 2UL + 1UL)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
531 | ) | - |
532 | debug("process from line %lu from pipe", last_processed); never executed: debug("process from line %lu from pipe", last_processed); | 0 |
533 | else | - |
534 | debug("process from line %lu to line %lu", last_processed, never executed: debug("process from line %lu to line %lu", last_processed, end_lineno); | 0 |
535 | end_lineno); never executed: debug("process from line %lu to line %lu", last_processed, end_lineno); | 0 |
536 | | - |
537 | res = 0; | - |
538 | lp = xmalloc((100+8192) + 1); | - |
539 | while (fgets(lp, (100+8192) + 1, in) != TRUE | never evaluated | FALSE | never evaluated |
| 0 |
540 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
541 | && count_in < end_linenoTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
542 | count_in++; | - |
543 | if (count_in <= last_processedTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
544 | debug3("skipping line %u, before checkpoint or " | - |
545 | "specified start line", count_in); | - |
546 | continue; never executed: continue; | 0 |
547 | } | - |
548 | if (checkpoint_file != TRUE | never evaluated | FALSE | never evaluated |
| 0 |
549 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
550 | ) | - |
551 | write_checkpoint(checkpoint_file, count_in); never executed: write_checkpoint(checkpoint_file, count_in); | 0 |
552 | print_progress(start_lineno, count_in, end_lineno); | - |
553 | if (strlen(lp) < 14TRUE | never evaluated | FALSE | never evaluated |
|| *TRUE | never evaluated | FALSE | never evaluated |
lp == '!'TRUE | never evaluated | FALSE | never evaluated |
|| *TRUE | never evaluated | FALSE | never evaluated |
lp == '#'TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
554 | debug2("%10u: comment or short line", count_in); | - |
555 | continue; never executed: continue; | 0 |
556 | } | - |
557 | | - |
558 | | - |
559 | | - |
560 | cp = &lp[14]; | - |
561 | | - |
562 | | - |
563 | in_type = strtoul(cp, &cp, 10); | - |
564 | | - |
565 | | - |
566 | in_tests = strtoul(cp, &cp, 10); | - |
567 | | - |
568 | if (in_tests & (0x01)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
569 | debug2("%10u: known composite", count_in); | - |
570 | continue; never executed: continue; | 0 |
571 | } | - |
572 | | - |
573 | | - |
574 | in_tries = strtoul(cp, &cp, 10); | - |
575 | | - |
576 | | - |
577 | in_size = strtoul(cp, &cp, 10); | - |
578 | | - |
579 | | - |
580 | generator_known = strtoul(cp, &cp, 16); | - |
581 | | - |
582 | | - |
583 | cp += | - |
584 | __builtin_strspn ( | - |
585 | cp | - |
586 | , | - |
587 | " " | - |
588 | ) | - |
589 | ; | - |
590 | | - |
591 | | - |
592 | switch (in_type) { | - |
593 | case never executed: case (4): (4):never executed: case (4): | 0 |
594 | debug2("%10u: (%u) Sophie-Germain", count_in, in_type); | - |
595 | a = q; | - |
596 | if (BN_hex2bn(&a, cp) == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
597 | fatal("BN_hex2bn failed"); never executed: fatal("BN_hex2bn failed"); | 0 |
598 | | - |
599 | if (BN_lshift(p, q, 1) == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
600 | fatal("BN_lshift failed"); never executed: fatal("BN_lshift failed"); | 0 |
601 | if (BN_add_word(p, 1) == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
602 | fatal("BN_add_word failed"); never executed: fatal("BN_add_word failed"); | 0 |
603 | in_size += 1; | - |
604 | generator_known = 0; | - |
605 | break; never executed: break; | 0 |
606 | case never executed: case (1): (1):never executed: case (1): | 0 |
607 | case never executed: case (2): (2):never executed: case (2): | 0 |
608 | case never executed: case (3): (3):never executed: case (3): | 0 |
609 | case never executed: case (5): (5):never executed: case (5): | 0 |
610 | case never executed: case (0): (0):never executed: case (0): | 0 |
611 | debug2("%10u: (%u)", count_in, in_type); | - |
612 | a = p; | - |
613 | if (BN_hex2bn(&a, cp) == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
614 | fatal("BN_hex2bn failed"); never executed: fatal("BN_hex2bn failed"); | 0 |
615 | | - |
616 | if (BN_rshift(q, p, 1) == 0TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
617 | fatal("BN_rshift failed"); never executed: fatal("BN_rshift failed"); | 0 |
618 | break; never executed: break; | 0 |
619 | default never executed: default: :never executed: default: | 0 |
620 | debug2("Unknown prime type"); | - |
621 | break; never executed: break; | 0 |
622 | } | - |
623 | | - |
624 | | - |
625 | | - |
626 | | - |
627 | | - |
628 | if ((TRUE | never evaluated | FALSE | never evaluated |
u_int32_t)BN_num_bits(p) != (in_size + 1)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
629 | debug2("%10u: bit size %u mismatch", count_in, in_size); | - |
630 | continue; never executed: continue; | 0 |
631 | } | - |
632 | if (in_size < (511)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
633 | debug2("%10u: bit size %u too short", count_in, in_size); | - |
634 | continue; never executed: continue; | 0 |
635 | } | - |
636 | | - |
637 | if (in_tests & (0x04)TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
638 | in_tries += trials; never executed: in_tries += trials; | 0 |
639 | else | - |
640 | in_tries = trials; never executed: in_tries = trials; | 0 |
641 | | - |
642 | | - |
643 | | - |
644 | | - |
645 | if (generator_known == 0TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
646 | if (BN_mod_word(p, 24) == 11TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
647 | generator_known = 2; never executed: generator_known = 2; | 0 |
648 | else if (BN_mod_word(p, 12) == 5TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
649 | generator_known = 3; never executed: generator_known = 3; | 0 |
650 | else { | - |
651 | u_int32_t r = BN_mod_word(p, 10); | - |
652 | | - |
653 | if (r == 3TRUE | never evaluated | FALSE | never evaluated |
|| r == 7TRUE | never evaluated | FALSE | never evaluated |
) | 0 |
654 | generator_known = 5; never executed: generator_known = 5; | 0 |
655 | } never executed: end of block | 0 |
656 | } | - |
657 | | - |
658 | | - |
659 | | - |
660 | if (generator_wanted > 0TRUE | never evaluated | FALSE | never evaluated |
&& | 0 |
661 | generator_wanted != generator_knownTRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
662 | debug2("%10u: generator %d != %d", | - |
663 | count_in, generator_known, generator_wanted); | - |
664 | continue; never executed: continue; | 0 |
665 | } | - |
666 | | - |
667 | | - |
668 | | - |
669 | | - |
670 | | - |
671 | if (generator_known == 0TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
672 | debug2("%10u: no known generator", count_in); | - |
673 | continue; never executed: continue; | 0 |
674 | } | - |
675 | | - |
676 | count_possible++; | - |
677 | if (BN_is_prime_ex(q, 1, ctx, TRUE | never evaluated | FALSE | never evaluated |
| 0 |
678 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
679 | ) <= 0TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
680 | debug("%10u: q failed first possible prime test", | - |
681 | count_in); | - |
682 | continue; never executed: continue; | 0 |
683 | } | - |
684 | if (!BN_is_prime_ex(p, trials, ctx, TRUE | never evaluated | FALSE | never evaluated |
| 0 |
685 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
686 | )TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
687 | debug("%10u: p is not prime", count_in); | - |
688 | continue; never executed: continue; | 0 |
689 | } | - |
690 | debug("%10u: p is almost certainly prime", count_in); | - |
691 | | - |
692 | | - |
693 | if (!BN_is_prime_ex(q, trials - 1, ctx, TRUE | never evaluated | FALSE | never evaluated |
| 0 |
694 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
695 | )TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
696 | debug("%10u: q is not prime", count_in); | - |
697 | continue; never executed: continue; | 0 |
698 | } | - |
699 | debug("%10u: q is almost certainly prime", count_in); | - |
700 | | - |
701 | if (qfileout(out, (2),TRUE | never evaluated | FALSE | never evaluated |
| 0 |
702 | in_tests | (0x04),TRUE | never evaluated | FALSE | never evaluated |
| 0 |
703 | in_tries, in_size, generator_known, p)TRUE | never evaluated | FALSE | never evaluated |
) { | 0 |
704 | res = -1; | - |
705 | break; never executed: break; | 0 |
706 | } | - |
707 | | - |
708 | count_out++; | - |
709 | } never executed: end of block | 0 |
710 | | - |
711 | time(&time_stop); | - |
712 | free(lp); | - |
713 | BN_free(p); | - |
714 | BN_free(q); | - |
715 | BN_CTX_free(ctx); | - |
716 | | - |
717 | if (checkpoint_file != TRUE | never evaluated | FALSE | never evaluated |
| 0 |
718 | ((void *)0)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
719 | ) | - |
720 | unlink(checkpoint_file); never executed: unlink(checkpoint_file); | 0 |
721 | | - |
722 | logit("%.24s Found %u safe primes of %u candidates in %ld seconds", | - |
723 | ctime(&time_stop), count_out, count_possible, | - |
724 | (long) (time_stop - time_start)); | - |
725 | | - |
726 | return never executed: return (res); (res);never executed: return (res); | 0 |
727 | } | - |
| | |