-
Notifications
You must be signed in to change notification settings - Fork 1
/
copri.c
780 lines (667 loc) · 17 KB
/
copri.c
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
// copri, Attacking RSA by factoring coprimes
//
// License: GNU Lesser General Public License (LGPL), version 3 or later
// See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl>.
// Implementation of "Factoring into coprimes in essentially linear time".
// # Algorithm
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <gmp.h>
#include "copri.h"
#include "config.h"
#if USE_OPENMP
#include <omp.h>
#endif
// ###Compute a^2^n.
// The result is stored in the parameter `mpz_t rot`.
// This parameter has to be **initialized** on invocation.
// The function operates on a single (input & output) integer.
//
// Algorithm 10.1 [PDF page 13](http://cr.yp.to/lineartime/dcba-20040404.pdf)
//
// See [twopower test](test-twopower.html) for basic usage.
void two_power(mpz_t rot, unsigned long long n) {
while(n > 0) {
mpz_mul(rot, rot, rot);
n--;
}
}
// ### Compute gcd, ppi and ppo
// gcd = gcd(a,b) greatest common divisor
// ppi = ppi(a,b) powers in a of primes inside b
// ppo = ppo(a,b) powers in a of primes outside b
//
//
//
// Algorithm 11.3 [PDF page 14](http://cr.yp.to/lineartime/dcba-20040404.pdf)
//
// See [gcdppippo test](test-gcdppippo.html) for basic usage.
void gcd_ppi_ppo(mpz_pool *pool, mpz_t gcd, mpz_t ppi, mpz_t
ppo, const mpz_t a, const mpz_t b) {
mpz_t g;
pool_pop(pool, g);
mpz_gcd(ppi, a, b);
mpz_set(gcd, ppi);
mpz_fdiv_q(ppo, a, ppi);
while(1) {
mpz_gcd(g, ppi, ppo);
if (mpz_cmp_ui(g, 1) == 0) {
pool_push(pool, g);
return;
}
mpz_mul(ppi, ppi, g);
mpz_fdiv_q(ppo, ppo, g);
}
}
// #### Shortcuts
// Compute ppi and ppo. Ingore gcd.
void ppi_ppo(mpz_pool *pool, mpz_t ppi, mpz_t ppo,
const mpz_t a, const mpz_t c) {
mpz_t gcd;
pool_pop(pool, gcd);
gcd_ppi_ppo(pool, gcd, ppi, ppo, a, c);
pool_push(pool, gcd);
}
// Compute ppi. Ingore gcd and ppo.
void ppi(mpz_pool *pool, mpz_t ppi, const mpz_t a,
const mpz_t c) {
mpz_t gcd;
mpz_t ppo;
pool_pop(pool, gcd);
pool_pop(pool, ppo);
gcd_ppi_ppo(pool, gcd, ppi, ppo, a, c);
pool_push(pool, gcd);
pool_push(pool, ppo);
}
// ### Compute gcd, ppg and pple
// gcd = gcd(a,b) greatest common divisor
// ppg = ppg(a,b) prime powers in a greater than those in b
// pple = pple(a,b) prime powers in a less than or equal to those in b
//
//
//
// Algorithm 11.4 [PDF page 14](http://cr.yp.to/lineartime/dcba-20040404.pdf)
//
// See [gcdppgpple test](test-gcdppgpple.html) for basic usage.
void gcd_ppg_pple(mpz_pool *pool, mpz_t gcd, mpz_t ppg,
mpz_t pple, const mpz_t a, const mpz_t b) {
mpz_t g;
pool_pop(pool, g);
mpz_gcd(pple, a, b);
mpz_set(gcd, pple);
mpz_fdiv_q(ppg, a, pple);
while(1) {
mpz_gcd(g, ppg, pple);
if (mpz_cmp_ui(g, 1) == 0) {
pool_push(pool, g);
return;
}
mpz_mul(ppg, ppg, g);
mpz_fdiv_q(pple, pple, g);
}
}
// ### Adds cb{a,b} to the array.
// Algorithm 13.2 [PDF page 17](http://cr.yp.to/lineartime/dcba-20040404.pdf)
//
// See [appendcb test](test-appendcb.html) for basic usage.
void append_cb(mpz_pool *pool, mpz_array *out, const mpz_t a,
const mpz_t b) {
mpz_t r, g, h, c, c0, x, y, d, b1, b2, a1;
unsigned long long n;
/* gmp_printf("enter append_cb(%Zd, %Zd)\n", a, b); */
// **Step 1**
//
// If `b == 1` and `a == 1` add `a` to the array. If `b == 1` return.
// This is the break condition of the recursive function.
if (mpz_cmp_ui(b, 1) == 0) {
if (mpz_cmp_ui(a, 1) != 0) {
array_add(out, a);
}
return;
}
pool_pop(pool, r);
pool_pop(pool, g);
pool_pop(pool, a1);
// **Step 2**
//
// Store ppi in `a1` and ppo in `r`. Use `a1` and not `a` to keep the input `const`
// and handle the case that one parameter is used twice.
ppi_ppo(pool, a1, r, a, b);
// **Step 3**
//
// If `r` (ppo) is **not** one add it to the array.
if (mpz_cmp_ui(r, 1) != 0) {
array_add(out, r);
}
pool_pop(pool, h);
pool_pop(pool, c);
// **Step 4**
//
// Caluclate the gcd, ppg and pple.
gcd_ppg_pple(pool, g, h, c, a1, b);
// **Step 5**
//
// Store pple in `c0` and `x`.
pool_pop(pool, c0);
mpz_set(c0, c);
pool_pop(pool, x);
mpz_set(x, c0);
// **Step 6**
//
// Set `n` to one.
n = 1;
pool_pop(pool, b1);
pool_pop(pool, b2);
pool_pop(pool, d);
pool_pop(pool, y);
// Start while loop to be able to return to step 7.
while(1) {
// **Step 7**
//
// Compute (g, h, c) ← (gcd,ppg,pple)(h,g^2)
mpz_mul(b1, g, g);
mpz_set(b2, h);
gcd_ppg_pple(pool, g, h, c, b2, b1);
// **Step 8**
//
// Compute d ← gcd(c, b)
mpz_gcd(d, c, b);
// **Step 9**
//
// Set x ← xd
mpz_mul(x, x, d);
// **Sep 10**
//
// Compute y ← d^2^ n−1.
mpz_set(y, d);
two_power(y, n - 1);
// **Sep 11**
//
// Recursively apply (c/y,d).
mpz_fdiv_q(b1, c, y);
/* gmp_printf("rec call append_cb(%Zd, %Zd)\n", b1, d); */
append_cb(pool, out, b1, d);
// **Sep 12**
//
// If h is not 1: Set n ← n+1. Return to Step 7.
if (mpz_cmp_ui(h, 1) == 0) break;
n = n + 1;
}
// **Step 13**
//
// Recursively apply (b/x, c0).
mpz_fdiv_q(b1, b, x);
append_cb(pool, out, b1, c0);
// Free the memory.
pool_push(pool, r);
pool_push(pool, g);
pool_push(pool, h);
pool_push(pool, c);
pool_push(pool, c0);
pool_push(pool, x);
pool_push(pool, y);
pool_push(pool, d);
pool_push(pool, b1);
pool_push(pool, b2);
pool_push(pool, a1);
}
// ###Compute the product of an array.
// Compute the product of the values in `mpz_t array` and store it in `mpz_t rot`.
// `size_t from` is the start index and `size_t to` is the end index.
//
// This function expects initialized mpz integers in all array fields between `from` and `to`.
//
// Algorithm 14.1 [PDF page 19](http://cr.yp.to/lineartime/dcba-20040404.pdf)
//
// See [prod test](test-prod.html) for basic usage.
void prod(mpz_pool *pool, mpz_t rot, mpz_t * array,
size_t from, size_t to) {
size_t n = to - from;
mpz_t x, y;
// If #S = 1: Find a ∈ S. Print a. Stop.
if (n == 0) {
mpz_set(rot, array[from]);
return;
}
// Select T ⊆ S with #T = b#S/2c.
//
// Compute X ← prod(T).
pool_pop(pool, x);
prod(pool, x, array, from, to - n/2 - 1);
// Compute Y ← prod(S−T).
pool_pop(pool, y);
prod(pool, y, array, to - n/2, to);
// Print XY.
mpz_mul(rot, x, y);
// Free the memory.
pool_push(pool, x);
pool_push(pool, y);
}
// #### array verison
// Compute product of an `mpz_array` and store it in `mpz_t rot`.
void array_prod(mpz_pool *pool, mpz_array *a, mpz_t rot) {
if (a->used > 0)
prod(pool, rot, a->array, 0, a->used-1);
else {
mpz_set_ui(rot, 1);
}
}
// ### fast algorithm to compute split(a,P).
// This function expects initialized mpz integers in all array fields between `from` and `to`.
//
// Algorithm 15.3 [PDF page 20](http://cr.yp.to/lineartime/dcba-20040404.pdf)
//
// See [split test](test-split.html) for basic usage.
void split(mpz_pool *pool, mpz_array *ret, const mpz_t a,
mpz_t *p, size_t from, size_t to) {
mpz_t b, x;
size_t n = to - from;
// **Sep 2**
//
// Compute b ← ppi(a,prodP)
pool_pop(pool, x);
pool_pop(pool, b);
prod(pool, x, p, from, to);
ppi(pool, b, a, x);
pool_push(pool, x);
// **Sep 2**
//
// If #P = 1: find p ∈ P, print (p,b), and stop
if (n == 0) {
array_add(ret, b);
pool_push(pool, b);
return;
}
// **Sep 3**
//
// Select Q ⊆ P with #Q = b#P/2c.
split(pool, ret, b, p, from, to - n/2 - 1);
split(pool, ret, b, p, to - n/2, to);
// Free the memory.
pool_push(pool, b);
}
// #### array verison
void array_split(mpz_pool *pool, mpz_array *ret,
const mpz_t a, mpz_array *p) {
if (p->used > 0)
split(pool, ret, a, p->array, 0, p->used-1);
else
fprintf(stderr, "array_split on empty array\n");
}
// ### Extending a coprime base
// This algorithm finds cb(P∪{b}) when P is coprime.
//
// Algorithm 16.2 [PDF page 21](http://cr.yp.to/lineartime/dcba-20040404.pdf)
//
// See [cbextend test](test-cbextend.html) for basic usage.
void cbextend(mpz_pool *pool, mpz_array *ret, mpz_array *p,
const mpz_t b) {
size_t i;
mpz_t x, a, r;
mpz_array s;
// **Sep 1**
//
// If P = {}: Print b if b != 1. Stop.
if (!p->used) {
if (mpz_cmp_ui(b, 1) != 0) {
array_add(ret, b);
}
}
// **Sep 2**
//
// Compute x ← prod P
pool_pop(pool, x);
array_prod(pool, p, x);
// **Sep 3**
//
// Compute (a,r) ← (ppi,ppo)(b, x) b
pool_pop(pool, a);
pool_pop(pool, r);
ppi_ppo(pool, a, r, b, x);
// **Sep 4**
//
// Print r if r != 1.
if (mpz_cmp_ui(r, 1) != 0) {
array_add(ret, r);
}
// **Sep 5**
//
// Compute S ← split(a,P)
array_init(&s, p->used);
array_split(pool, &s, a, p);
// **Sep 6**
//
// For each (p, c) ∈ S: Apply append_cb(p, c).
if (p->used != s.used) {
fprintf(stderr, "logic error in cbextend: p.used != s.used");
} else {
for (i = 0; i < p->used; i++) {
append_cb(pool, ret, p->array[i], s.array[i]);
}
}
// Free the memory.
array_clear(&s);
pool_push(pool, a);
pool_push(pool, r);
pool_push(pool, x);
}
// #### bit test util
// see [PDF page 22](http://cr.yp.to/lineartime/dcba-20040404.pdf)
int bit(size_t i, size_t k) {
if (k & (1 << i)) return 1;
return 0;
}
// ### Merging coprime bases
// This algorithm finds cb(P ∪ Q) if P is coprime and Q is coprime
//
// Algorithm 17.3 [PDF page 23](http://cr.yp.to/lineartime/dcba-20040404.pdf)
//
// See [cbmerge test](test-cbmerge.html) for basic usage.
void cbmerge(mpz_pool *pool, mpz_array *s, mpz_array *p,
mpz_array *q) {
mpz_array t; // T
mpz_array r; // buffer for q_k : bit_i k = 0 and q_k : bit_i k = 1
size_t n = q->used;
size_t b = 0;
size_t i = 0;
size_t k = 0;
mpz_t x; // buffer
pool_pop(pool, x);
// Find the smallest b ≥ 1 with 2^b.
do {
b++;
mpz_ui_pow_ui(x, 2, b);
} while(mpz_cmp_ui(x, n) < 0);
// Set S ← P.
array_add_array(s, p);
while(1) {
// If i = b: Print S. Stop.
if (i == b) {
pool_push(pool, x);
return;
}
// Find R ← {qk : bit(k) = 1}
array_init(&r, n);
for(k=0; k<n; k++) {
if (!bit(i,k)) array_add(&r, q->array[k]);
}
// Compute x ← prod{R}
array_prod(pool, &r, x);
// Compute T ← cbextend(S ∪ {x})
array_init(&t, s->size);
cbextend(pool, &t, s, x);
// Find R ← {qk : bit(k) = 1}
array_clear(&r);
array_init(&r, n);
for(k=0; k<n; k++) {
if (bit(i,k)) array_add(&r, q->array[k]);
}
// Compute x ← prod{R}
array_prod(pool, &r, x);
// Compute S ← cbextend(T ∪ {x})
array_clear(s);
array_init(s, t.size);
cbextend(pool, s, &t, x);
// Free the memory.
array_clear(&r);
array_clear(&t);
i++;
}
}
// ### Computing a coprime base for a finite set
// This algorithm computes the natural coprime base for any finite subset of a free coid.
// It uses `cbmerge` to merge coprime bases for halves of the set.
//
// Algorithm 18.1 [PDF page 24](http://cr.yp.to/lineartime/dcba-20040404.pdf)
//
// See [cb test](test-cb.html) for basic usage.
void cb(mpz_pool *pool, mpz_array *ret, mpz_t *s,
size_t from, size_t to) {
size_t n = to - from;
mpz_array p, q;
#if USE_OPENMP
mpz_pool pool_p, pool_q;
#endif
// If #S = 1: Find a ∈ S. Print a if a != 1. Stop.
if (n == 0) {
if (mpz_cmp_ui(s[from], 0) == 0) {
fprintf(stderr, "warning adding 0 in cb\n");
} else {
if (mpz_cmp_ui(s[from], 1) != 0) {
array_add(ret, s[from]);
}
}
return;
}
// ## OpenMP multithreading
// Execute both recrusive `cb` calls in parallel.
//
// `export OMP_NUM_THREADS=4` to set the maximal thread number.
array_init(&p, n);
array_init(&q, n);
#if USE_OPENMP
const int parent = omp_get_thread_num();
#pragma omp parallel sections
{
#pragma omp section
{
const int id = omp_get_thread_num();
if (id != parent) {
/* printf("New thread\n"); */
pool_init(&pool_p, 0);
cb(&pool_p, &p, s, from, to - n/2 - 1);
pool_clear(&pool_p);
} else {
cb(pool, &p, s, from, to - n/2 - 1);
}
}
#pragma omp section
{
const int id = omp_get_thread_num();
if (id != parent) {
/* printf("New thread\n"); */
pool_init(&pool_q, 0);
cb(&pool_q, &q, s, to - n/2, to);
pool_clear(&pool_q);
} else {
cb(pool, &q, s, to - n/2, to);
}
}
}
#else
cb(pool, &p, s, from, to - n/2 - 1);
cb(pool, &q, s, to - n/2, to);
#endif
// Print cbmerge(P∪Q)
if (q.used && p.used) {
cbmerge(pool, ret, &p, &q);
} else if(!q.used && p.used) {
array_add_array(ret, &p);
fprintf(stderr, "warning: q is empty in cb\n");
} else if(q.used && !p.used) {
array_add_array(ret, &q);
fprintf(stderr, "warning: p is empty in cb\n");
} else {
fprintf(stderr, "warning: p an q are empty in cb\n");
}
// Free the memory.
array_clear(&p);
array_clear(&q);
}
// #### array verison
void array_cb(mpz_pool *pool, mpz_array *ret, mpz_array *s) {
if (s->used > 0)
cb(pool, ret, s->array, 0, s->used-1);
else
fprintf(stderr, "array_cb on empty array\n");
}
// ### The reduce function
// Algorithm 19.2 [PDF page 24](http://cr.yp.to/lineartime/dcba-20040404.pdf)
//
// See [reduce test](test-reduce.html) for basic usage.
void reduce(mpz_pool *pool, mpz_t i, mpz_t pai,
const mpz_t p, const mpz_t a) {
mpz_t r, j, b, p2, a2;
// **Sep 1**
//
// If p does not divide a: Print (0,a) and stop.
pool_pop(pool, r);
mpz_fdiv_r(r, a, p);
if (mpz_cmp_ui(r, 0) != 0) {
pool_push(pool, r);
mpz_set_ui(i, 0);
mpz_set(pai, a);
return;
}
// **Sep 2**
//
// Compute (j,b) ← reduce(p^2 ,a/p)
pool_pop(pool, j);
pool_pop(pool, b);
pool_pop(pool, p2);
pool_pop(pool, a2);
mpz_mul(p2, p, p);
mpz_fdiv_q(a2, a, p);
reduce(pool, j, b, p2, a2);
pool_push(pool, p2);
pool_push(pool, a2);
// **Sep 3**
//
// If p divides b: Print (2 j +2,b/p) and stop.
mpz_fdiv_r(r, b, p);
if (mpz_cmp_ui(r, 0) == 0) {
mpz_mul_ui(j, j, 2);
mpz_add_ui(j, j, 2);
mpz_set(i, j);
mpz_fdiv_q(b, b, p);
mpz_set(pai, b);
pool_push(pool, r);
pool_push(pool, b);
pool_push(pool, j);
return;
}
pool_push(pool, r);
// **Sep 4**
//
// Print (2 j +1,b).
mpz_mul_ui(j, j, 2);
mpz_add_ui(j, j, 1);
mpz_set(i, j);
mpz_set(pai, b);
// Free the memory.
pool_push(pool, b);
pool_push(pool, j);
}
// ### Factoring over a coprime base
// This algorithm factors a as a product of powers of elements of P if possible; otherwise it
// proclaims failure.
//
// Algorithm 20.1 [PDF page 25](http://cr.yp.to/lineartime/dcba-20040404.pdf)
//
// See [findfactor test](test-findfactor.html) for basic usage.
int find_factor(mpz_pool *pool, mpz_array *out, const mpz_t a0,
const mpz_t a, mpz_t *p, size_t from, size_t to) {
mpz_t m, c, y, b, c2;
size_t n = to - from;
unsigned int r = 1;
// If #P = 1: Find p ∈ P. Compute (n, c) ← reduce(p,a) by Algorithm 19.2. If
// c != 1, proclaim failure and stop. Otherwise print (p,n) and stop
if (n == 0) {
pool_pop(pool, m);
pool_pop(pool, c);
reduce(pool, m, c, p[from], a);
if (mpz_cmp_ui(c, 1) != 0) {
r = 0;
} else {
if (mpz_cmp(a0, p[from]) != 0) {
pool_pop(pool, y);
mpz_fdiv_q(y, a0, p[from]);
array_add(out, a0);
array_add(out, p[from]);
array_add(out, y);
pool_push(pool, y);
r = 0;
}
}
pool_push(pool, m);
pool_push(pool, c);
return r;
}
// Select Q ⊆ P with #Q = b#P/2c.
// Compute y ← prod Q
pool_pop(pool, y);
prod(pool, y, p, from, to - n/2 - 1);
// Compute (b, c) ← (ppi,ppo)(a, y)
pool_pop(pool, b);
pool_pop(pool, c2);
ppi_ppo(pool, b, c2, a, y);
// Apply Algorithm 20.1 to (b,Q) recursively. If Algorithm 20.1 fails, proclaim
// failure and stop.
if (!find_factor(pool, out, a0, b, p, from, to - n/2 - 1)) {
r = 0;
// Apply Algorithm 20.1 to (c,P−Q) recursively. If Algorithm 20.1 fails, proclaim
// failure and stop.
} else if (!find_factor(pool, out, a0, c2, p, to - n/2, to)) {
r = 0;
}
// Free the memory.
pool_push(pool, y);
pool_push(pool, b);
pool_push(pool, c2);
return r;
}
// #### array verison
int array_find_factor(mpz_pool *pool, mpz_array *out,
const mpz_t a, mpz_array *p) {
if (p->used > 0)
return find_factor(pool, out, a, a, p->array, 0, p->used-1);
else {
fprintf(stderr, "array_printfactors on empty array\n");
return 0;
}
}
// ### Factoring a set over a coprime base
// This algorithm factors each element a ∈ S over P if P is a base for S; otherwise it proclaims failure.
//
// Algorithm 21.2 [PDF page 27](http://cr.yp.to/lineartime/dcba-20040404.pdf)
//
// See [findfactors test](test-findfactors.html) for basic usage.
void find_factors(mpz_pool *pool, mpz_array *out, mpz_t *s,
size_t from, size_t to, mpz_array *p) {
mpz_t x, y, z;
mpz_array d, q;
size_t i, n = to - from;
pool_pop(pool, x);
array_prod(pool, p, x);
pool_pop(pool, y);
prod(pool, y, s, from, to);
pool_pop(pool, z);
ppi(pool, z, x, y);
array_init(&d, p->size);
array_split(pool, &d, z, p);
array_init(&q, p->size);
for (i = 0; i < p->used; i++) {
if (mpz_cmp(d.array[i], p->array[i]) == 0)
array_add(&q, p->array[i]);
}
if (n == 0) {
array_find_factor(pool, out, y, &q);
} else {
find_factors(pool, out, s, from, to - n/2 - 1, &q);
find_factors(pool, out, s, to - n/2, to, &q);
}
pool_push(pool, x);
pool_push(pool, y);
pool_push(pool, z);
array_clear(&d);
array_clear(&q);
}
// #### array verison
void array_find_factors(mpz_pool *pool, mpz_array *out,
mpz_array *s, mpz_array *p) {
if (s->used > 0)
find_factors(pool, out, s->array, 0, s->used-1, p);
else
fprintf(stderr, "array_printfactors_set on empty array\n");
}