flint-sys 0.9.0

Bindings to the FLINT C library
Documentation
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
/*
    Copyright (C) 2012 William Hart

    This file is part of FLINT.

    FLINT is free software: you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.  See <https://www.gnu.org/licenses/>.
*/

#include <string.h>
#include "ulong_extras.h"
#include "mpn_extras.h"
#include "fmpz.h"
#include "fmpz_factor.h"
#include "fmpz_vec.h"
#include "fmpz_poly.h"
#include "fmpz_mod.h"
#include "fmpz_mod_poly.h"

#define DEBUG 0 /* turn on some trace information */

#define pp1_mulmod(rxx, axx, bxx, nnn, nxx, ninv, norm)             \
   flint_mpn_mulmod_preinvn(rxx, axx, bxx, nnn, nxx, ninv, norm)

#ifdef FLINT64
static
ulong pp1_primorial[15] =
{
    UWORD(2), UWORD(6), UWORD(30), UWORD(210), UWORD(2310), UWORD(30030), UWORD(510510), UWORD(9699690),
    UWORD(223092870), UWORD(6469693230), UWORD(200560490130), UWORD(7420738134810),
    UWORD(304250263527210), UWORD(13082761331670030), UWORD(614889782588491410)
};
#define num_primorials 15
#else
static
ulong pp1_primorial[9] =
{
    UWORD(2), UWORD(6), UWORD(30), UWORD(210), UWORD(2310), UWORD(30030), UWORD(510510), UWORD(9699690),
    UWORD(223092870)
};
#define num_primorials 9
#endif

static void pp1_set(nn_ptr x1, nn_ptr y1,
              nn_srcptr x2, nn_srcptr y2, slong nn)
{
    flint_mpn_copyi(x1, x2, nn);
    flint_mpn_copyi(y1, y2, nn);
}

static void pp1_set_ui(nn_ptr x, slong nn, ulong norm, ulong c)
{
    mpn_zero(x, nn);
    x[0] = (c << norm);
    if (nn > 1 && norm)
        x[1] = (c >> (FLINT_BITS - norm));
}

static void pp1_2k(nn_ptr x, nn_ptr y, slong nn, nn_srcptr n,
            nn_srcptr ninv, nn_srcptr x0, ulong norm)
{
    pp1_mulmod(y, y, x, nn, n, ninv, norm);
    if (mpn_sub_n(y, y, x0, nn))
        mpn_add_n(y, y, n, nn);

    pp1_mulmod(x, x, x, nn, n, ninv, norm);
    if (mpn_sub_1(x, x, nn, UWORD(2) << norm))
        mpn_add_n(x, x, n, nn);
}

static void pp1_2kp1(nn_ptr x, nn_ptr y, slong nn, nn_srcptr n,
              nn_srcptr ninv, nn_srcptr x0, ulong norm)
{
    pp1_mulmod(x, x, y, nn, n, ninv, norm);
    if (mpn_sub_n(x, x, x0, nn))
        mpn_add_n(x, x, n, nn);

    pp1_mulmod(y, y, y, nn, n, ninv, norm);
    if (mpn_sub_1(y, y, nn, UWORD(2) << norm))
        mpn_add_n(y, y, n, nn);
}

static void pp1_pow_ui(nn_ptr x, nn_ptr y, slong nn,
                ulong exp, nn_srcptr n, nn_srcptr ninv, ulong norm)
{
    ulong t[30];
    nn_ptr x0 = t;
    ulong bit = ((UWORD(1) << FLINT_BIT_COUNT(exp)) >> 2);

    if (nn > 30)
        x0 = flint_malloc(nn*sizeof(ulong));
    flint_mpn_copyi(x0, x, nn);

    pp1_mulmod(y, x, x, nn, n, ninv, norm);
    if (mpn_sub_1(y, y, nn, UWORD(2) << norm))
        mpn_add_n(y, y, n, nn);

    while (bit)
    {
        if (exp & bit)
            pp1_2kp1(x, y, nn, n, ninv, x0, norm);
        else
            pp1_2k(x, y, nn, n, ninv, x0, norm);

        bit >>= 1;
    }

    if (nn > 30)
        flint_free(x0);
}

static slong pp1_factor(nn_ptr factor, nn_srcptr n,
                     nn_srcptr x, slong nn, ulong norm)
{
    slong ret = 0, xn = nn;

    nn_ptr n2 = flint_malloc(nn*sizeof(ulong));
    nn_ptr x2 = flint_malloc(nn*sizeof(ulong));

    if (norm)
        mpn_rshift(n2, n, nn, norm);
    else
        flint_mpn_copyi(n2, n, nn);

    if (norm)
        mpn_rshift(x2, x, nn, norm);
    else
        flint_mpn_copyi(x2, x, nn);

    if (mpn_sub_1(x2, x2, nn, 2))
        mpn_add_n(x2, x2, n2, nn);

    MPN_NORM(x2, xn);

    if (xn == 0)
        goto cleanup;

    ret = flint_mpn_gcd_full(factor, n2, nn, x2, xn);

cleanup:

    flint_free(n2);
    flint_free(x2);

    return ret;
}

static slong pp1_find_power(nn_ptr factor, nn_ptr x, nn_ptr y, slong nn,
                          ulong p, nn_srcptr n, nn_srcptr ninv, ulong norm)
{
    slong ret;

    do
    {
        pp1_pow_ui(x, y, nn, p, n, ninv, norm);
        ret = pp1_factor(factor, n, x, nn, norm);
    } while (ret == 1 && factor[0] == 1);

    return ret;
}

int fmpz_factor_pp1(fmpz_t fac, const fmpz_t n_in, ulong B1, ulong B2sqrt, ulong c)
{
    slong i, j;
    int ret = 0;
    slong nn = fmpz_size(n_in), r;
    nn_ptr x, y, oldx, oldy, n, ninv, factor, ptr_0, ptr_1, ptr_2, ptr_k;
    ulong pr, oldpr, sqrt, bits0, norm;
    n_primes_t iter;

    if (fmpz_is_even(n_in))
    {
        fmpz_set_ui(fac, 2);
        return 1;
    }

#if DEBUG
    flint_printf("starting stage 1\n");
#endif

    n_primes_init(iter);

    sqrt = n_sqrt(B1);
    bits0 = FLINT_BIT_COUNT(B1);

    x      = flint_malloc(nn*sizeof(ulong));
    y      = flint_malloc(nn*sizeof(ulong));
    oldx   = flint_malloc(nn*sizeof(ulong));
    oldy   = flint_malloc(nn*sizeof(ulong));
    n      = flint_malloc(nn*sizeof(ulong));
    ninv   = flint_malloc(nn*sizeof(ulong));
    factor = flint_malloc(nn*sizeof(ulong));

    if (nn == 1)
    {
        n[0] = fmpz_get_ui(n_in);
        norm = flint_clz(n[0]);
        n[0] <<= norm;
    } else
    {
        nn_ptr np = COEFF_TO_PTR(*n_in)->_mp_d;
        norm = flint_clz(np[nn - 1]);
        if (norm)
            mpn_lshift(n, np, nn, norm);
        else
            flint_mpn_copyi(n, np, nn);
    }

    flint_mpn_preinvn(ninv, n, nn);

    pp1_set_ui(x, nn, norm, c);

    /* mul by various prime powers */
    pr = 0;
    oldpr = 0;

    for (i = 0; pr < B1; )
    {
        j = i + 1024;
        oldpr = pr;
        pp1_set(oldx, oldy, x, y, nn);
        for ( ; i < j; i++)
        {
            pr = n_primes_next(iter);
            if (pr < sqrt)
            {
                ulong bits = FLINT_BIT_COUNT(pr);
                ulong exp = bits0 / bits;
                pp1_pow_ui(x, y, nn, n_pow(pr, exp), n, ninv, norm);
            } else
                pp1_pow_ui(x, y, nn, pr, n, ninv, norm);
        }

        r = pp1_factor(factor, n, x, nn, norm);
        if (r == 0)
            break;
        if (r != 1 || factor[0] != 1)
        {
            ret = 1;
            goto cleanup;
        }
    }

    if (pr < B1) /* factor = 0 */
    {
        n_primes_jump_after(iter, oldpr);
        pp1_set(x, y, oldx, oldy, nn);

        do
        {
            pr = n_primes_next(iter);
            pp1_set(oldx, oldy, x, y, nn);
            if (pr < sqrt)
            {
                ulong bits = FLINT_BIT_COUNT(pr);
                ulong exp = bits0 / bits;
                pp1_pow_ui(x, y, nn, n_pow(pr, exp), n, ninv, norm);
            } else
                pp1_pow_ui(x, y, nn, pr, n, ninv, norm);

            r = pp1_factor(factor, n, x, nn, norm);
            if (r == 0)
                break;
            if (r != 1 || factor[0] != 1)
            {
                ret = 1;
                goto cleanup;
            }
        } while (1);

        /* factor is still 0 */
        ret = pp1_find_power(factor, oldx, oldy, nn, pr, n, ninv, norm);
    } else /* stage 2 */
    {
        double quot;
        int num;
        char * sieve = flint_malloc(32768);
        slong * sieve_index = flint_malloc(32768*sizeof(slong));
        nn_ptr diff = flint_malloc(16384*nn*sizeof(ulong));
        ulong offset[15], num_roots;
        ulong s;
        slong k, index = 0;
        fmpz * roots, * roots2, * evals;
        fmpz_poly_struct ** tree2;

#if DEBUG
        ulong primorial;
        flint_printf("starting stage 2\n");
#endif

        /* find primorial <= B2sqrt ... */
        for (num = 1; num < num_primorials; num++)
        {
            if (pp1_primorial[num] > B2sqrt)
                break;
        }
        num--;

        /* ... but not too big */
        quot = (double) B2sqrt / (double) pp1_primorial[num];
        if (quot < 1.1 && num > 0)
            num--;

#if DEBUG
        primorial = pp1_primorial[num];
        flint_printf("found primorial %wu\n", primorial);
#endif

        /* adjust B2sqrt to multiple of primorial */
        B2sqrt = (((B2sqrt - 1)/ pp1_primorial[num]) + 1) * pp1_primorial[num];

#if DEBUG
        flint_printf("adjusted B2sqrt %wu\n", B2sqrt);
#endif

        /* compute num roots */
        num++; /* number of primes is 1 more than primorial index */
        pr = 2;
        num_roots = B2sqrt;
        for (i = 0; i < num; i++)
        {
            num_roots = (num_roots*(pr - 1))/pr;
            pr = n_nextprime(pr, 0);
        }

#if DEBUG
        flint_printf("computed num_roots %wu\n", num_roots);
        flint_printf("B2 = %wu\n", num_roots * B2sqrt);
#endif

        /* construct roots */
        roots = _fmpz_vec_init(num_roots);
        for (i = 0; (ulong) i < num_roots; i++)
        {
            mpz_ptr m = _fmpz_promote(roots + i);
            FLINT_MPZ_REALLOC(m, nn);
        }

        roots2 = _fmpz_vec_init(num_roots);
        for (i = 0; (ulong) i < num_roots; i++)
        {
            mpz_ptr m = _fmpz_promote(roots2 + i);
            FLINT_MPZ_REALLOC(m, nn);
        }

        evals = _fmpz_vec_init(num_roots);

#if DEBUG
        flint_printf("constructed roots\n");
#endif

        /* compute differences table v0, ... */
        mpn_zero(diff, nn);
        diff[0] = (UWORD(2) << norm);

        /* ... v2, ... */
        pp1_mulmod(diff + nn, x, x, nn, n, ninv, norm);
        if (mpn_sub_1(diff + nn, diff + nn, nn, UWORD(2) << norm))
            mpn_add_n(diff + nn, diff + nn, n, nn);

        /* ... the rest ... v_{k+2} = v_k v_2 - v_{k-2} */
        k = 2*nn;
        for (i = 2; i < 16384; i++, k += nn)
        {
            pp1_mulmod(diff + k, diff + k - nn, diff + nn, nn, n, ninv, norm);
            if (mpn_sub_n(diff + k, diff + k, diff + k - 2*nn, nn))
                mpn_add_n(diff + k, diff + k, n, nn);
        }

#if DEBUG
        flint_printf("conputed differences table\n");
#endif

        /* initial positions */
        pr = 2;
        for (i = 0; i < num; i++)
        {
            offset[i] = pr/2;
            pr = n_nextprime(pr, 0);
        }

        s = 0;
        while (2*s + 1 < B2sqrt)
        {
            /* sieve */
            memset(sieve, 1, 32768);
            pr = 3;
            for (i = 1; i < num; i++)
            {
                j = offset[i];
                while (j < 32768)
                    sieve[j] = 0, j += pr;

                /* store offset for start of next sieve run */
                offset[i] = j - 32768;
                pr = n_nextprime(pr, 0);
            }

            /* compute roots */
            for (i = 0; i < 32768 && 2*(s + i) + 1 < B2sqrt; i++)
            {
                if (sieve[i])
                {
                    ptr_2 = COEFF_TO_PTR(roots[index])->_mp_d;
                    k = (i + 1)/2;
                    for (j = i - 1; j >= k; j--)
                    {
                        if (sieve[j] && sieve[2*j - i])
                        {
                            /* V_{n+k} = V_n V_k - V_{n-k} */
                            ptr_0 = COEFF_TO_PTR(roots[sieve_index[2*j - i]])->_mp_d;
                            ptr_1 = COEFF_TO_PTR(roots[sieve_index[j]])->_mp_d;
                            ptr_k = diff + (i - j)*nn;
                            pp1_mulmod(ptr_2, ptr_1, ptr_k, nn, n, ninv, norm);
                            if (mpn_sub_n(ptr_2, ptr_2, ptr_0, nn))
                                mpn_add_n(ptr_2, ptr_2, n, nn);
                            break;
                        }
                    }

                    if (j < k) /* pair not found, compute using pow_ui */
                    {
                        flint_mpn_copyi(ptr_2, x, nn);
                        pp1_pow_ui(ptr_2, y, nn, 2*(s + i) + 1, n, ninv, norm);
                    }

                    sieve_index[i] = index;
                    index++;
                }
            }

            s += 32768;
        }

#if DEBUG
        flint_printf("roots computed %wd\n", index);
#endif

        /* v_1 */
        flint_mpn_copyi(oldx, x, nn);
        pp1_pow_ui(oldx, y, nn, B2sqrt, n, ninv, norm);
        ptr_0 = COEFF_TO_PTR(roots2[0])->_mp_d;
        flint_mpn_copyi(ptr_0, oldx, nn);

        /* v_2 */
        ptr_1 = COEFF_TO_PTR(roots2[1])->_mp_d;
        pp1_mulmod(ptr_1, ptr_0, ptr_0, nn, n, ninv, norm);
        if (mpn_sub_1(ptr_1, ptr_1, nn, UWORD(2) << norm))
            mpn_add_n(ptr_1, ptr_1, n, nn);

        for (i = 2; (ulong) i < num_roots; i++)
        {
            /* V_{k+n} = V_k V_n - V_{k-n} */
            ptr_2 = COEFF_TO_PTR(roots2[i])->_mp_d;

            pp1_mulmod(ptr_2, ptr_1, oldx, nn, n, ninv, norm);
            if (mpn_sub_n(ptr_2, ptr_2, ptr_0, nn))
                mpn_add_n(ptr_2, ptr_2, n, nn);

            ptr_0 = ptr_1;
            ptr_1 = ptr_2;
        }

#if DEBUG
        flint_printf("roots2 computed %wu\n", num_roots);
#endif

        for (i = 0; (ulong) i < num_roots; i++)
        {
            slong sn;
            mpz_ptr m1 = COEFF_TO_PTR(roots[i]);
            mpz_ptr m2 = COEFF_TO_PTR(roots2[i]);

            ptr_1 = m1->_mp_d;
            ptr_2 = m2->_mp_d;

            if (norm)
            {
                mpn_rshift(ptr_1, ptr_1, nn, norm);
                mpn_rshift(ptr_2, ptr_2, nn, norm);
            }

            sn = nn;
            MPN_NORM(ptr_1, sn);
            m1->_mp_size = sn;

            sn = nn;
            MPN_NORM(ptr_2, sn);
            m2->_mp_size = sn;

            _fmpz_demote_val(roots + i);
            _fmpz_demote_val(roots2 + i);
        }

#if DEBUG
        flint_printf("normalised roots\n");
#endif

        fmpz_mod_ctx_t ctx;
        fmpz_mod_ctx_init(ctx, n_in);

        tree2 = _fmpz_mod_poly_tree_alloc(num_roots);
        _fmpz_mod_poly_tree_build(tree2, roots2, num_roots, ctx);

        /* todo: use fmpz_mod_poly_mul */
        fmpz_poly_mul(tree2[FLINT_CLOG2(num_roots)], tree2[FLINT_CLOG2(num_roots)-1], tree2[FLINT_CLOG2(num_roots)-1]+1);
        fmpz_poly_scalar_mod_fmpz(tree2[FLINT_CLOG2(num_roots)], tree2[FLINT_CLOG2(num_roots)], n_in);

#if DEBUG
        flint_printf("built trees\n");
#endif

        _fmpz_mod_poly_evaluate_fmpz_vec_fast(evals, tree2[FLINT_CLOG2(num_roots)]->coeffs, tree2[FLINT_CLOG2(num_roots)]->length, roots, num_roots, ctx);
        _fmpz_mod_poly_tree_free(tree2, num_roots);

        fmpz_mod_ctx_clear(ctx);

#if DEBUG
        flint_printf("evaluated at roots\n");
#endif

        for (i = 0; (ulong) i < num_roots; i++)
        {
            fmpz_gcd(fac, n_in, evals + i);
            if (!fmpz_is_zero(fac) && !fmpz_is_one(fac))
            {
                ret = 1;
                break;
            }
        }

        _fmpz_vec_clear(evals, num_roots);
        _fmpz_vec_clear(roots, num_roots);
        _fmpz_vec_clear(roots2, num_roots);
        flint_free(sieve);
        flint_free(sieve_index);
        flint_free(diff);

        if ((ulong) i < num_roots)
            goto cleanup2;
    }

#if DEBUG
    flint_printf("done stage2\n");
#endif

cleanup:

    if (ret)
    {
        mpz_ptr fm = _fmpz_promote(fac);
        mp_ptr fp = FLINT_MPZ_REALLOC(fm, r);
        flint_mpn_copyi(fp, factor, r);
        fm->_mp_size = r;
        _fmpz_demote_val(fac);
    }

cleanup2:

    flint_free(x);
    flint_free(y);
    flint_free(oldx);
    flint_free(oldy);
    flint_free(n);
    flint_free(ninv);
    flint_free(factor);

    n_primes_clear(iter);

    return ret;
}