-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtinyqs.c
1905 lines (1577 loc) · 55.3 KB
/
tinyqs.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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "tinyqs.h"
/******************************************************************************/
typedef signed char s8;
typedef unsigned char u8;
typedef signed short s16;
typedef unsigned short u16;
typedef signed int s32;
typedef unsigned int u32;
#ifdef _MSC_VER
typedef signed __int64 s64;
typedef unsigned __int64 u64;
#else
typedef long long s64;
typedef unsigned long long u64;
#endif
/******************************************************************************/
#ifndef M_LN2
#define M_LN2 0.69314718055994530942
#endif
#ifndef M_SQRT2
#define M_SQRT2 1.41421356237309504880
#endif
/* High-throughput, low-overhead implementation of the self-
initializing multiple polynomial quadratic sieve, optimized
for small inputs (50-120 bits). Many of the ideas here are
extensions of the remarkable MPQS code of F. Bahr, used
in the lattice sievers by Jens Franke */
/* TODO: lots of static mpz_t is an issue for threading */
/* seeds for random numbers */
static u32 rand_seed1 = 11111111;
static u32 rand_seed2 = 22222222;
#define RAND_MULT 2131995753
static u32 get_rand(u32 *seed1, u32 *seed2)
{
/* A multiply-with-carry generator by George Marsaglia.
The period is about 2^63. */
u64 temp = (u64)(*seed1) * (u64)RAND_MULT + (u64)(*seed2);
*seed1 = (u32)temp;
*seed2 = (u32)(temp >> 32);
return (u32)temp;
}
/* masks for picking out individual bits of 64-bit
words, used for the linear algebra */
#define B(x) ((u64)(1) << (x))
static const u64 bitmask[] = {
B( 0), B( 1), B( 2), B( 3), B( 4), B( 5), B( 6), B( 7),
B( 8), B( 9), B(10), B(11), B(12), B(13), B(14), B(15),
B(16), B(17), B(18), B(19), B(20), B(21), B(22), B(23),
B(24), B(25), B(26), B(27), B(28), B(29), B(30), B(31),
B(32), B(33), B(34), B(35), B(36), B(37), B(38), B(39),
B(40), B(41), B(42), B(43), B(44), B(45), B(46), B(47),
B(48), B(49), B(50), B(51), B(52), B(53), B(54), B(55),
B(56), B(57), B(58), B(59), B(60), B(61), B(62), B(63),
};
/* maximum size pool of primes from which
factor base is constructed */
#define NUM_PRIMES_TINY 1024
/* the number of dependencies the linear algebra
will find */
#define NUM_EXTRA_RELATIONS_TINY 16
/* largest number of relations that can go into the
linear algebra (includes relations combined from
pairs of partial relations */
#define MAX_RELATIONS_TINY 512
/* the largest possible factor base */
#define MAX_FB_SIZE_TINY (MAX_RELATIONS_TINY - \
NUM_EXTRA_RELATIONS_TINY)
/* offset of the first valid factor base prime */
#define MIN_FB_OFFSET_TINY 1
/* offset of the first factor base prime
actually contributing to the sieving */
#define MIN_FB_OFFSET_TO_SIEVE_TINY 7
/* number of primes used when testing multipliers */
#define NUM_TEST_PRIMES_TINY 30
/* fudge factor to the target sieve value to account
for not sieving with the smallest factor base primes */
#define SMALL_PRIME_FUDGE_TINY 10
/* maximum number of MPQS polynomials to be computed */
#define MAX_POLY_TINY 256
/* maximum number of FB primes that contribute to
a single polynomial 'A' value */
#define MAX_POLY_FACTORS_TINY 5
/* the size of the sieve interval. Each polynomial will
sieve over this many positive and negative values */
#define SIEVE_SIZE_TINY 16384
/* value of the sieve root used when sieving is not
to be performed for a given FB prime. Since this is
larger than SIEVE_SIZE_TINY no special-case code
is needed in the core sieve code */
#define DO_NOT_SIEVE_TINY 65535
/* maximum number of factors a relation can have (the
large prime is stored separately) */
#define MAX_FACTORS_TINY 20
/* partial relations are listed in the order
in which they occur, and a hashtable matches
up partial relations with the same large prime. */
#define LOG2_PARTIAL_TABLE_SIZE 10
#define LARGE_PRIME_HASH(x) (((u32)(x) * ((u32)40499 * 65543)) >> \
(32 - LOG2_PARTIAL_TABLE_SIZE))
/* number of collisions allowed in one hashtable entry */
#define LP_HASH_DEPTH_TINY 3
/* scale factor for all log values */
#define LOGPRIME_SCALE_TINY 2
/* maximum number of relations to be saved for
resieving, used in place of trial factoring */
#define SIEVE_BATCH_SIZE_TINY 128
/* maximum size of the pool of FB primes that
can appear in a polynomial 'A' value */
#define POLY_SELECT_BITS_TINY 12
#define POSITIVE 0
#define NEGATIVE 1
/* structure describing a single relation */
typedef struct {
u32 large_prime; /* the large prime (may be 1) */
s16 sieve_offset; /* the sieve offset of the relation */
u8 poly_num; /* ID of the poly that produce the relation */
u8 num_factors; /* number of factors from the factor base
(duplicates count) */
u16 fb_offsets[MAX_FACTORS_TINY]; /* offsets into FB of primes that
divide this relation */
} tiny_relation;
/* structure describing a factor base entry */
typedef struct {
u16 prime; /* the factor base prime */
u16 modsqrt; /* x that solves x^2 = N mod p */
u32 recip; /* integer reciprocal of 'prime' */
u8 logprime; /* log value used in sieve */
u16 roots[2]; /* the two sieve roots for 'prime' */
} tiny_fb;
/* structure describing one SIQS polynomial */
typedef struct {
u16 a_fb_offsets[MAX_POLY_FACTORS_TINY]; /* factors of 'A' value */
mpz_t b; /* B value */
} tiny_poly;
/* main structure controlling the factorization */
typedef struct {
/* basic stuff */
mpz_t n; /* number to be factored */
u32 multiplier; /* small multiplier of n */
u16 multiplier_fb[2]; /* fb offsets of factors of multiplier */
/* polynomial selection stuff */
double target_a; /* the optimal size of poly A values */
s32 poly_num; /* ID of current polynomial */
s32 num_a_factors; /* # of factors in poly 'A' values */
s32 poly_select_idx; /* ID of the combination of primes
that will make current A value */
u16 poly_select_offsets[POLY_SELECT_BITS_TINY]; /* pool of primes for A */
mpz_t poly_b_aux[MAX_POLY_FACTORS_TINY]; /* scratch values for com-
puting poly B values */
tiny_poly poly_list[MAX_POLY_TINY]; /* list of SIQS polynomials */
/* sieve stuff */
double align_me;
u8 sieve_block[SIEVE_SIZE_TINY]; /* the sieve interval (8-byte aligned) */
/* factor base stuff */
s32 fb_size; /* number of FB primes */
u16 prime_list[NUM_PRIMES_TINY]; /* complete list of primes from which
factor base is generated */
float test_prime_contrib[NUM_TEST_PRIMES_TINY]; /* scratch space used in
multiplier selection */
tiny_fb factor_base[MAX_FB_SIZE_TINY]; /* the factor base */
u16 root_aux[MAX_POLY_FACTORS_TINY *
MAX_FB_SIZE_TINY]; /* scratch value for initializing
sieve roots */
/* relation stuff */
s32 num_full_relations; /* where next full relation will go */
s32 partial_idx; /* where next partial relation will go */
s32 large_prime_max; /* max value of a large prime */
s32 error_bits; /* value used for trial factoring cutoff */
tiny_relation sieve_batch[SIEVE_BATCH_SIZE_TINY]; /* resieved relations */
/* all relations that survive sieving are put in relation_list.
Full relations (and partial relations whose large prime has
occurred more than once) are stored in a list that grows up
from the beginning of the list, while partial relations that
have not been matched up yet are stored in a list growing down
from the end of relation_list. num_full_relations is the index
of the first free space for full relations, and partial_idx
does the same for unmatched partial relations. */
tiny_relation relation_list[4 * MAX_RELATIONS_TINY];
/* a hashtable is used to match up partial relations, using the
large prime as a hash key. The hashtable stores the index in
relation_list of the partial relation that connects up all the
other partial relations with the same large prime (those other
relations are treated as full relations) */
u16 partial_hash[1 << LOG2_PARTIAL_TABLE_SIZE][LP_HASH_DEPTH_TINY];
/* linear algebra stuff */
u16 null_vectors[MAX_RELATIONS_TINY];
u64 matrix[MAX_FB_SIZE_TINY][(MAX_RELATIONS_TINY+63) / 64];
} tiny_qs_params;
/* the following is reused across factorizations */
static tiny_qs_params *g_params = NULL;
/* The following utility routines are not really
a performance bottleneck, but since they always
deal with 16-bit data at most their input
datatypes should really by u16's. This will
make all the division and remainder operations
a lot faster */
/***********************************/
static s32 legendre_16(s32 a, s32 p)
/***********************************
Compute the Legendre symbol (a/p)
************************************/
{
s32 tmp;
s32 x = a;
s32 y = p;
s32 out = 1;
while (x) {
while ((x & 1) == 0) {
x = x / 2;
if ( (y & 7) == 3 || (y & 7) == 5 )
out = -out;
}
tmp = x;
x = y;
y = tmp;
if ( (x & 3) == 3 && (y & 3) == 3 )
out = -out;
x = x % y;
}
if (y == 1)
return out;
return 0;
}
/***********************************/
static s32 powm_16(s32 a, s32 b, s32 n)
/***********************************
Compute a^b mod n
************************************/
{
s32 res = 1;
while (b) {
if (b & 1)
res = res * a % n;
a = a * a % n;
b = b >> 1;
}
return res;
}
/***********************************/
static s32 modinv_16(s32 a, s32 p)
/***********************************
High-speed modular inverse of 'a' mod 'p'
Thanks to the folks at www.mersenneforum.com
for coming up with this
************************************/
{
s32 ps1, ps2, parity, dividend, divisor, rem, q, t;
q = 1;
rem = a;
dividend = p;
divisor = a;
ps1 = 1;
ps2 = 0;
parity = 0;
while (divisor > 1) {
rem = dividend - divisor;
t = rem - divisor;
if (t >= 0) {
q += ps1; rem = t; t -= divisor;
if (t >= 0) {
q += ps1; rem = t; t -= divisor;
if (t >= 0) {
q += ps1; rem = t; t -= divisor;
if (t >= 0) {
q += ps1; rem = t; t -= divisor;
if (t >= 0) {
q += ps1; rem = t; t -= divisor;
if (t >= 0) {
q += ps1; rem = t; t -= divisor;
if (t >= 0) {
q += ps1; rem = t; t -= divisor;
if (t >= 0) {
q += ps1; rem = t;
if (rem >= divisor) {
q = dividend / divisor;
rem = dividend % divisor;
q *= ps1;
} } } } } } } }
}
q += ps2;
parity = ~parity;
dividend = divisor;
divisor = rem;
ps2 = ps1;
ps1 = q;
}
if (parity == 0)
return ps1;
else
return p - ps1;
}
/***********************************/
static s32 sqrtModP_16(s32 a, s32 p)
/***********************************
Compute the square root of 'a' mod 'p'
This is Algorithm 2.3.8 from Crandall &
Pomerance, "Prime Numbers: A Computational
Perspective"
************************************/
{
if ( (p & 7) == 3 || (p & 7) == 7 ) {
return powm_16(a, (p+1)/4, p);
}
else if ( (p & 7) == 5 ) {
#if 0
s32 x, y;
x = powm_16(a, (p+3)/8, p);
if ((x * x % p) == a)
return x;
y = powm_16(2, (p-1)/4, p);
return (s32)x * y % p;
#else
#define mulm_16(a, b, n) (((a) * (b)) % (n))
u32 a2, alpha, beta, b;
a2 = (a+a) % p;
alpha = powm_16(a2, (p-5)>>3, p);
beta = mulm_16(a2, mulm_16(alpha,alpha,p), p);
b = mulm_16(alpha, mulm_16(a, (beta ? beta-1 : (u32)p-1), p), p);
return b;
}
else if ( (p & 15) == 9 ) {
s32 a2, alpha, beta, b, d = 1;
a2 = (a+a) % p;
alpha = powm_16(a2, (p-9)>>4, p);
beta = mulm_16(a2, mulm_16(alpha,alpha,p), p);
if (((beta*beta) % p) != p-1) {
do { d += 2; } while (legendre_16(d,p) != -1 && d < p);
alpha = mulm_16(alpha, powm_16(d, (p-9)>>3, p), p);
beta = mulm_16(a2, mulm_16(mulm_16(d,d,p),mulm_16(alpha,p,p),p), p);
}
b = mulm_16(alpha, mulm_16(a, mulm_16(d, (beta ? beta-1 : p-1), p), p), p);
return b;
#endif
}
else {
s32 i, d0, d1, a1, s, t, m;
d0 = get_rand(&rand_seed1, &rand_seed2) % p;
while (legendre_16(d0, p) != -1)
d0 = get_rand(&rand_seed1, &rand_seed2) % p;
t = p - 1;
s = 0;
while (!(t & 1)) {
s++;
t = t / 2;
}
a1 = powm_16(a, t, p);
d1 = powm_16(d0, t, p);
for (i = 0, m = 0; i < s; i++) {
s32 ad = powm_16(d1, m, p);
ad = ad * a1 % p;
ad = powm_16(ad, (u16)(1) << (s-1-i), p);
if (ad == (p - 1))
m += (1 << i);
}
a1 = powm_16(a, (t+1)/2, p);
d1 = powm_16(d1, m/2, p);
return a1 * d1 % p;
}
}
/***********************************/
static void init_tinyqs(void)
/***********************************/
{
s32 i, j, k, rem;
tiny_qs_params *p;
if (g_params)
return;
/* allocate the main structure */
p = g_params = (tiny_qs_params *)malloc(sizeof(tiny_qs_params));
mpz_init(p->n);
/* fill in the pool of primes */
p->prime_list[0] = 2;
p->prime_list[1] = 3;
for (i = 2, j = 5; i < NUM_PRIMES_TINY; j += 2) {
for (k = 1, rem = 0; k < i; k++) {
s32 prime = p->prime_list[k];
rem = j % prime;
if (prime * prime > j || rem == 0)
break;
}
if (rem != 0)
p->prime_list[i++] = j;
}
/* init the scratch values for polynomial 'B'
value computations */
for (i = 0; i < MAX_POLY_FACTORS_TINY; i++) {
mpz_init(p->poly_b_aux[i]);
}
/* set up the list of sieve polynomials */
for (i = 0; i < MAX_POLY_TINY; i++) {
mpz_init(p->poly_list[i].b);
}
/* see the next routine for an explanation of what
these quantities are */
for (i = 1; i < NUM_TEST_PRIMES_TINY; i++) {
p->test_prime_contrib[i] = 2 * log((double)p->prime_list[i]) /
(p->prime_list[i] - 1) / M_LN2;
}
}
/* Implementation of the modified Knuth-Schroeppel multiplier
algorithm. This borrows ideas from at least four different
sources, and seems to choose multipliers that are better on
average than many of the other methods available.
There are many misconceptions about what this algorithm is
supposed to do. We want to multiply the input number n by a
small odd squarefree constant k, chosen so that the factor base
for k * n contains as many small primes as possible. Since small primes
occur more often than big ones, this makes sieve values smaller
on average and so more likely to be smooth. We quantify this
by measuring the average contribution of the first NUM_TEST_PRIMES_TINY
primes to sieve values. There are two constraints: first, larger
multipliers mean a larger number to factor. Second, we can't spend
all day testing multipliers, so the set of multipliers to test should
be small.
The list of available multipliers depends on the value of n mod
8, 3, and 5; each row of the table below gives the multipliers
to try, pre-sorted by how well they approximately optimize sieving
(the routine below computes a better approximation). Note that a
multiplier of 1 (i.e. no multiplier) is always possible. Experiments
show that 90% of the time the optimal multiplier is in one of the
first four columns of the table */
#define MAX_MULTIPLIERS 13 /* for residue classes: */
static u8 mult_list[32][MAX_MULTIPLIERS] = { /* mod 8 mod 3 mod 5 */
{ 1, 19, 61, 31, 21, 13, 7, 3, 73, 41, 5, 33, 37 }, /* 1 1 1 */
{ 1, 13, 7, 3, 73, 33, 37, 17, 57, 43, 5, 19, 15 }, /* 1 1 2 */
{ 1, 13, 7, 3, 73, 33, 37, 17, 57, 43, 5, 19, 15 }, /* 1 1 3 */
{ 1, 19, 61, 31, 21, 13, 7, 3, 73, 41, 5, 33, 37 }, /* 1 1 4 */
{ 1, 41, 5, 17, 11, 89, 29, 65, 21, 3, 59, 33, 35 }, /* 1 2 1 */
{ 1, 17, 5, 3, 33, 65, 57, 23, 41, 53, 47, 11, 89 }, /* 1 2 2 */
{ 1, 17, 5, 3, 33, 65, 57, 23, 41, 53, 47, 11, 89 }, /* 1 2 3 */
{ 1, 41, 5, 17, 11, 89, 29, 65, 21, 3, 59, 33, 35 }, /* 1 2 4 */
{ 1, 19, 3, 11, 31, 7, 51, 43, 15, 39, 61, 55, 21 }, /* 3 1 1 */
{ 1, 3, 7, 43, 19, 13, 37, 15, 55, 11, 73, 31, 35 }, /* 3 1 2 */
{ 1, 3, 7, 43, 19, 13, 37, 15, 55, 11, 73, 31, 35 }, /* 3 1 3 */
{ 1, 19, 3, 11, 31, 7, 51, 43, 15, 39, 61, 55, 21 }, /* 3 1 4 */
{ 1, 11, 3, 59, 35, 5, 51, 19, 29, 41, 15, 23, 39 }, /* 3 2 1 */
{ 1, 3, 11, 35, 5, 23, 17, 47, 7, 59, 43, 15, 53 }, /* 3 2 2 */
{ 1, 3, 11, 35, 5, 23, 17, 47, 7, 59, 43, 15, 53 }, /* 3 2 3 */
{ 1, 11, 3, 59, 35, 5, 51, 19, 29, 41, 15, 23, 39 }, /* 3 2 4 */
{ 1, 61, 21, 13, 5, 19, 37, 31, 29, 7, 3, 11, 15 }, /* 5 1 1 */
{ 1, 13, 37, 7, 3, 5, 73, 61, 21, 43, 33, 53, 17 }, /* 5 1 2 */
{ 1, 13, 37, 7, 3, 5, 73, 61, 21, 43, 33, 53, 17 }, /* 5 1 3 */
{ 1, 61, 21, 13, 5, 19, 37, 31, 29, 7, 3, 11, 15 }, /* 5 1 4 */
{ 1, 5, 29, 21, 11, 41, 53, 17, 89, 3, 59, 61, 65 }, /* 5 2 1 */
{ 1, 5, 53, 17, 3, 13, 29, 23, 21, 37, 47, 33, 11 }, /* 5 2 2 */
{ 1, 5, 53, 17, 3, 13, 29, 23, 21, 37, 47, 33, 11 }, /* 5 2 3 */
{ 1, 5, 29, 21, 11, 41, 53, 17, 89, 3, 59, 61, 65 }, /* 5 2 4 */
{ 1, 31, 7, 19, 15, 39, 55, 3, 11, 61, 21, 13, 51 }, /* 7 1 1 */
{ 1, 7, 3, 15, 13, 55, 31, 43, 23, 37, 19, 47, 73 }, /* 7 1 2 */
{ 1, 7, 3, 15, 13, 55, 31, 43, 23, 37, 19, 47, 73 }, /* 7 1 3 */
{ 1, 31, 7, 19, 15, 39, 55, 3, 11, 61, 21, 13, 51 }, /* 7 1 4 */
{ 1, 11, 5, 15, 23, 39, 3, 29, 47, 59, 31, 35, 7 }, /* 7 2 1 */
{ 1, 23, 3, 47, 7, 5, 15, 17, 11, 35, 53, 39, 33 }, /* 7 2 2 */
{ 1, 23, 3, 47, 7, 5, 15, 17, 11, 35, 53, 39, 33 }, /* 7 2 3 */
{ 1, 11, 5, 15, 23, 39, 3, 29, 47, 59, 31, 35, 7 }, /* 7 2 4 */
};
/***********************************/
static void find_multiplier_tiny(void)
/***********************************/
{
tiny_qs_params *params = g_params;
s32 i, j;
u16 *prime_list = params->prime_list;
u16 test_nmodp[NUM_TEST_PRIMES_TINY];
s32 best_mult = 1;
s32 nmod8 = mpz_get_ui(params->n) % 8;
float best_score;
u8 *mult_row;
s32 num_tests;
/* precompute information that will be needed
for all multipliers */
for (i = 1; i < NUM_TEST_PRIMES_TINY; i++)
test_nmodp[i] = mpz_tdiv_ui(params->n, prime_list[i]);
/* find the row of the table that is approriate for this
value of n */
mult_row = mult_list[ test_nmodp[2] - 1 +
4*(test_nmodp[1] - 1) +
8*(nmod8 / 2) ];
/* test less than the whole row if n is small */
num_tests = mpz_sizeinbase(params->n, 2) / 10;
if (num_tests > MAX_MULTIPLIERS)
num_tests = MAX_MULTIPLIERS;
best_score = 1000.0;
for (i = 0; i < num_tests; i++) {
s32 curr_mult = mult_row[i];
s32 knmod8 = (nmod8 * curr_mult) % 8;
float score;
/* measure the contribution of 2 as a factor of sieve
values. The multiplier itself must also be taken into
account in the score. 'score' is the correction that
is implicitly applied to the size of sieve values; a
negative score makes sieve values smaller, and so is
better. */
if (knmod8 == 1)
score = 0.5 * log((double)curr_mult) / M_LN2 - 2;
else if (knmod8 == 5)
score = 0.5 * log((double)curr_mult) / M_LN2 - 1;
else
score = 0.5 * log((double)curr_mult) / M_LN2 - 0.5;
for (j = 1; j < NUM_TEST_PRIMES_TINY; j++) {
s32 prime = prime_list[j];
s32 knmodp = (s32)test_nmodp[j] * curr_mult % prime;
/* if prime j is actually in the factor base
for k * n ... */
if (legendre_16(knmodp, prime) != -1) {
/* ...add its contribution. A prime p con-
tributes log(p) to 1 in p sieve values, plus
log(p) to 1 in p^2 sieve values, etc. The
average contribution of all multiples of p
to a random sieve value is thus
log(p) * (1/p + 1/p^2 + 1/p^3 + ...)
= (log(p) / p) * 1 / (1 - (1/p))
= log(p) / (p-1)
This contribution occurs once for each
square root used for sieving. There are two
roots for each factor base prime, unless
the prime divides the multiplier. In that
case there is only one root. The scores are
premultiplied by 2.0, and logarithms are
in base 2 (though any base will do) */
if (knmodp == 0)
score -= 0.5 * params->test_prime_contrib[j];
else
score -= params->test_prime_contrib[j];
}
}
if (score < best_score) {
best_score = score;
best_mult = curr_mult;
}
}
/* from now on we will factor best_mult * n */
params->multiplier = best_mult;
mpz_mul_ui(params->n, params->n, best_mult);
}
/***********************************/
static s32 init_fb_tiny(s32 fb_size)
/***********************************/
{
tiny_qs_params *params = g_params;
u16 *prime_list = params->prime_list;
s32 i, j, mult_idx;
tiny_fb *factor_base = params->factor_base;
i = MIN_FB_OFFSET_TINY;
mult_idx = 0;
factor_base[i].prime = 2;
params->multiplier_fb[0] = 0;
params->multiplier_fb[1] = 0;
/* Keep setting up factor base primes until enough
are found or the pool of primes runs out */
for (i++, j = 1; i < fb_size && j < NUM_PRIMES_TINY; j++) {
tiny_fb *fbptr = factor_base + i;
s32 prime = prime_list[j];
s32 nmodp = mpz_tdiv_ui(params->n, prime);
if (legendre_16(nmodp, prime) != -1) {
fbptr->prime = prime;
fbptr->logprime = (u8)(LOGPRIME_SCALE_TINY *
log((double)prime) / M_LN2 + 0.5);
fbptr->recip = (u32)(B(32) / (u64)prime);
/* if the prime divides n, it is part of n's
multiplier and is treated separately */
if (nmodp != 0) {
fbptr->modsqrt = (u16)sqrtModP_16(nmodp, prime);
}
else {
fbptr->modsqrt = DO_NOT_SIEVE_TINY;
params->multiplier_fb[mult_idx++] = i;
}
i++;
}
}
params->fb_size = i;
return i;
}
/***********************************/
static void fill_sieve_block_tiny(void)
/***********************************
Core sieving routine
************************************/
{
tiny_qs_params *params = g_params;
s32 i;
s32 fb_size = params->fb_size;
u8 *sieve_block = params->sieve_block;
tiny_fb *factor_base = params->factor_base;
/* Note that since this code will only ever
factor small inputs, the sieve interval will
always be ridiculously small and does not
need to be broken up into chunks. Further,
the bottleneck with small inputs is the trial
factoring of relations and not the sieving,
so no crazy unrolling tricks are needed
here either */
for (i = MIN_FB_OFFSET_TO_SIEVE_TINY; i < fb_size; i++) {
tiny_fb *fbptr = factor_base + i;
s32 prime = fbptr->prime;
u8 logprime = fbptr->logprime;
s32 root = fbptr->roots[0];
while (root < SIEVE_SIZE_TINY) {
sieve_block[root] -= logprime;
root += prime;
}
root = fbptr->roots[1];
while (root < SIEVE_SIZE_TINY) {
sieve_block[root] -= logprime;
root += prime;
}
}
}
#define PACKED_SIEVE_MASK ((u64)0x80808080 << 32 | 0x80808080)
/***********************************/
static s32 mark_sieve_block_tiny(void)
/***********************************
Walk through a filled-in sieve block and find
the offsets correspodning to relations that
are probably useful
************************************/
{
tiny_qs_params *params = g_params;
s32 i, j, k;
u8 *sieve_block = params->sieve_block;
u64 *packed_sieve_block = (u64 *)params->sieve_block;
/* standard technique for testing sieve locations
in parallel: initialize each byte to the target
sieve value, and subtract logs of the factor base
primes instead of adding them. Sieve offsets that
accumulate enough log values become negative,
and it's easy to simultaneously test for the top
bit in several bytes being set */
for (i = j = 0; i < SIEVE_SIZE_TINY / 8; i += 4) {
/* handle 32 bytes at a time */
u64 accum = packed_sieve_block[i] |
packed_sieve_block[i+1] |
packed_sieve_block[i+2] |
packed_sieve_block[i+3];
if ((accum & PACKED_SIEVE_MASK) == (u64)(0))
continue;
/* at least one byte is a hit; go back and search
the list one at a time. We treat the sieve interval
as a hashtable, and associate entry j in the list
of relations to be resieved (params->sieve_batch[])
with a byte that is negative. The high-order bit of
the byte is set to indicate that the low-order bits
mean something */
for (k = 0; k < 32; k++) {
u32 val = sieve_block[8 * i + k];
if (val & 0x80) {
if (j < SIEVE_BATCH_SIZE_TINY) {
tiny_relation *r = params->sieve_batch + j;
r->sieve_offset = 8 * i + k;
r->num_factors = 0;
sieve_block[8 * i + k] = j | 0x80;
j++;
}
else {
sieve_block[8 * i + k] = 0;
}
}
}
}
return j;
}
/***********************************/
static void resieve_tiny(void)
/***********************************
Just like fill_sieve_block_tiny(), except
sieving is used to avoid trial division
on all the relations previously found
************************************/
{
tiny_qs_params *params = g_params;
s32 i;
s32 fb_size = params->fb_size;
u8 *sieve_block = params->sieve_block;
tiny_fb *factor_base = params->factor_base;
/* Note that even though this routine does only
a little more work than fill_sieve_block_tiny(),
it runs almost 3x slower */
for (i = MIN_FB_OFFSET_TO_SIEVE_TINY; i < fb_size; i++) {
tiny_fb *fbptr = factor_base + i;
s32 prime = fbptr->prime;
s32 root = fbptr->roots[0];
while (root < SIEVE_SIZE_TINY) {
s32 val = sieve_block[root];
if (val & 0x80) {
tiny_relation *r = params->sieve_batch + (val & 0x7f);
r->fb_offsets[r->num_factors++] = i;
}
root += prime;
}
root = fbptr->roots[1];
while (root < SIEVE_SIZE_TINY) {
s32 val = sieve_block[root];
if (val & 0x80) {
tiny_relation *r = params->sieve_batch + (val & 0x7f);
r->fb_offsets[r->num_factors++] = i;
}
root += prime;
}
}
}
/***********************************/
static s32 check_sieve_val_tiny(mpz_t a, mpz_t b, mpz_t c,
tiny_relation *r,
s32 sign_of_index)
/***********************************
Trial factor a relation that survived sieving
************************************/
{
tiny_qs_params *params = g_params;
s32 i, j;
tiny_fb *factor_base = params->factor_base;
s32 num_factors = 0;
s32 sieve_offset = r->sieve_offset;
tiny_relation *relation = params->relation_list +
params->num_full_relations;
u16 *fb_offsets = relation->fb_offsets;
static u8 initialized = 0;
static mpz_t res, res2;
if (initialized == 0) {
mpz_init(res);
mpz_init(res2);
initialized = 1;
}
/* form the polynomial value */
mpz_mul_ui(res, a, sieve_offset);
if (sign_of_index == POSITIVE)
mpz_add(res, res, b);
else
mpz_sub(res, res, b);
mpz_mul_ui(res, res, sieve_offset);
mpz_add(res, res, c);
if (mpz_sgn(res) < 0) {
mpz_abs(res, res);
fb_offsets[num_factors++] = 0;
}
/* extract powers of two */
i = mpz_scan1(res, 0);
if (i) {
mpz_tdiv_q_2exp(res, res, i);
do {
if (num_factors >= MAX_FACTORS_TINY)
return 0;
fb_offsets[num_factors++] = MIN_FB_OFFSET_TINY;
} while (--i);
}
/* divide out the unsieved factor base primes */
for (i = MIN_FB_OFFSET_TINY + 1;
i < MIN_FB_OFFSET_TO_SIEVE_TINY; i++) {
tiny_fb *fbptr = factor_base + i;
s32 prime = fbptr->prime;
s32 root1 = fbptr->roots[0];
s32 root2 = fbptr->roots[1];
u32 recip = fbptr->recip;
if (root1 == DO_NOT_SIEVE_TINY)
continue;
j = (s32)(((u64)sieve_offset * (u64)recip) >> 32);
j = sieve_offset - j * prime;
if (j >= prime)
j -= prime;
if (j == root1 || j == root2) {
while (mpz_tdiv_q_ui(res2, res, prime) == 0) {
if (num_factors >= MAX_FACTORS_TINY)
return 0;
fb_offsets[num_factors++] = i;
mpz_swap(res, res2);
}
}
}
/* divide out the factors of the multiplier,
if any */
for (i = 0; i < 2; i++) {
if (params->multiplier_fb[i]) {
s32 prime;
j = params->multiplier_fb[i];
prime = factor_base[j].prime;
while (mpz_tdiv_q_ui(res2, res, prime) == 0) {
if (num_factors >= MAX_FACTORS_TINY)
return 0;
fb_offsets[num_factors++] = j;
mpz_swap(res, res2);
}
}
}
/* We should probably have been adding log values
to the log of this relation in the previous loops,
and testing that the complete log value now
exceeds the trial factoring cutoff. However,
resieving has already found the remaining factors,
so we wouldn't save much time bailing out at
this point */
for (i = 0; i < r->num_factors; i++) {
s32 prime;
j = r->fb_offsets[i];
prime = factor_base[j].prime;
while (mpz_tdiv_q_ui(res2, res, prime) == 0) {
if (num_factors >= MAX_FACTORS_TINY)
return 0;
fb_offsets[num_factors++] = j;
mpz_swap(res, res2);
}
}
/* start filling in the final relation */
if (sign_of_index == NEGATIVE)
sieve_offset = -sieve_offset;
relation->sieve_offset = sieve_offset;
relation->num_factors = num_factors;
relation->poly_num = params->poly_num;
if (mpz_cmp_ui(res, 1) == 0) {
/* full relation; we're done */
relation->large_prime = 1;
params->num_full_relations++;
}
else if (mpz_cmp_ui(res, params->large_prime_max) < 0) {
u32 lp = mpz_get_ui(res);
u32 table_idx = LARGE_PRIME_HASH(lp);
s32 partial_idx;
/* partial relation; see if it has occurred already */
relation->large_prime = lp;
for (i = 0; i < LP_HASH_DEPTH_TINY; i++) {
partial_idx = params->partial_hash[table_idx][i];
if (partial_idx == 0xffff ||
lp == params->relation_list[partial_idx].large_prime)
break;
}
if (i == LP_HASH_DEPTH_TINY) {
/* not found, and no room to store it */