-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathring.c
More file actions
677 lines (580 loc) · 16.2 KB
/
ring.c
File metadata and controls
677 lines (580 loc) · 16.2 KB
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
/*
Copyright (C) 2020 William Hart
Copyright (C) 2024 Fredrik Johansson
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 "fmpz.h"
#include "fmpz_mod.h"
#include "mpn_mod.h"
#include "gr.h"
#include "gr_generic.h"
/* todo: separate method tables, more fixed-width optimizations */
/* todo: fast polynomial multiplication */
/* todo: powering */
/* todo: special functions */
int
mpn_mod_ctx_write(gr_stream_t out, gr_ctx_t ctx)
{
gr_stream_write(out, "Integers mod ");
gr_stream_write_free(out, _flint_mpn_get_str(MPN_MOD_CTX_MODULUS(ctx), MPN_MOD_CTX_NLIMBS(ctx)));
gr_stream_write(out, " (mpn)");
return GR_SUCCESS;
}
void
mpn_mod_ctx_clear(gr_ctx_t ctx)
{
flint_free(MPN_MOD_CTX(ctx));
}
int
mpn_mod_set_ui(nn_ptr res, ulong x, gr_ctx_t ctx)
{
FLINT_ASSERT(MPN_MOD_CTX_NLIMBS(ctx) >= 2);
res[0] = x;
flint_mpn_zero(res + 1, MPN_MOD_CTX_NLIMBS(ctx) - 1);
return GR_SUCCESS;
}
int
mpn_mod_set_si(nn_ptr res, slong x, gr_ctx_t ctx)
{
slong n = MPN_MOD_CTX_NLIMBS(ctx);
FLINT_ASSERT(n >= 2);
if (x >= 0)
{
res[0] = x;
flint_mpn_zero(res + 1, n - 1);
}
else
{
mpn_sub_1(res, MPN_MOD_CTX_MODULUS(ctx), n, -(ulong) x);
}
return GR_SUCCESS;
}
int
mpn_mod_neg_one(nn_ptr res, gr_ctx_t ctx)
{
return mpn_mod_set_si(res, -1, ctx);
}
/* fixme: flint_mpn_mod_preinvn is misdocumented */
int
mpn_mod_set_mpn(nn_ptr res, nn_srcptr x, slong xn, gr_ctx_t ctx)
{
slong n = MPN_MOD_CTX_NLIMBS(ctx);
flint_bitcnt_t norm = MPN_MOD_CTX_NORM(ctx);
slong rn;
if (xn < n || (xn == n && mpn_cmp(x, MPN_MOD_CTX_MODULUS(ctx), n) < 0))
{
flint_mpn_copyi(res, x, xn);
flint_mpn_zero(res + xn, n - xn);
}
else
{
nn_ptr r;
TMP_INIT;
TMP_START;
r = TMP_ALLOC((xn + 1) * sizeof(ulong));
if (norm == 0)
{
flint_mpn_copyi(r, x, xn);
rn = xn;
}
else
{
r[xn] = mpn_lshift(r, x, xn, norm);
rn = xn + (r[xn] != 0);
}
/* todo: good preinv code for nx2 reduction */
if (n == 2)
{
mpn_divrem_2(r + 2, 0, r, rn, MPN_MOD_CTX_MODULUS_NORMED(ctx));
if (norm == 0)
{
res[0] = r[0];
res[1] = r[1];
}
else
{
res[0] = (r[0] >> norm) | (r[1] << (FLINT_BITS - norm));
res[1] = (r[1] >> norm);
}
}
else
{
if (norm == 0)
{
flint_mpn_mod_preinvn(r, x, rn, MPN_MOD_CTX_MODULUS(ctx), MPN_MOD_CTX_NLIMBS(ctx), MPN_MOD_CTX_MODULUS_PREINV(ctx));
flint_mpn_copyi(res, r, n);
}
else
{
flint_mpn_mod_preinvn(r, r, rn, MPN_MOD_CTX_MODULUS_NORMED(ctx), MPN_MOD_CTX_NLIMBS(ctx), MPN_MOD_CTX_MODULUS_PREINV(ctx));
mpn_rshift(res, r, n, norm);
}
}
TMP_END;
}
return GR_SUCCESS;
}
int
mpn_mod_set_fmpz(nn_ptr res, const fmpz_t x, gr_ctx_t ctx)
{
if (!COEFF_IS_MPZ(*x))
mpn_mod_set_si(res, *x, ctx);
else
{
slong nd = FLINT_ABS(COEFF_TO_PTR(*x)->_mp_size);
int neg = COEFF_TO_PTR(*x)->_mp_size < 0;
mpn_mod_set_mpn(res, COEFF_TO_PTR(*x)->_mp_d, nd, ctx);
if (neg)
flint_mpn_negmod_n(res, res, MPN_MOD_CTX_MODULUS(ctx), MPN_MOD_CTX_NLIMBS(ctx));
}
return GR_SUCCESS;
}
/* todo: public interface */
typedef struct
{
fmpz_mod_ctx_struct * ctx;
truth_t is_prime;
fmpz a; /* when used as finite field with defining polynomial x - a */
}
_gr_fmpz_mod_ctx_struct;
#define FMPZ_MOD_CTX(ring_ctx) ((((_gr_fmpz_mod_ctx_struct *)(ring_ctx))->ctx))
int
mpn_mod_set_other(nn_ptr res, gr_ptr v, gr_ctx_t v_ctx, gr_ctx_t ctx)
{
if (v_ctx == ctx)
return mpn_mod_set(res, v, ctx);
if (v_ctx->which_ring == GR_CTX_MPN_MOD)
{
slong n = MPN_MOD_CTX_NLIMBS(ctx);
if (MPN_MOD_CTX_NLIMBS(v_ctx) == n &&
flint_mpn_equal_p(MPN_MOD_CTX_MODULUS(v_ctx), MPN_MOD_CTX_MODULUS(ctx), n))
{
flint_mpn_copyi(res, v, n);
return GR_SUCCESS;
}
}
if (v_ctx->which_ring == GR_CTX_FMPZ_MOD)
{
slong n = MPN_MOD_CTX_NLIMBS(ctx);
if (fmpz_size(FMPZ_MOD_CTX(v_ctx)->n) == n)
{
nn_srcptr vd = COEFF_TO_PTR(*(FMPZ_MOD_CTX(v_ctx)->n))->_mp_d;
if (flint_mpn_equal_p(vd, MPN_MOD_CTX_MODULUS(ctx), n))
return mpn_mod_set_fmpz(res, v, ctx);
}
}
return gr_generic_set_other(res, v, v_ctx, ctx);
}
int
mpn_mod_rand(nn_ptr res, flint_rand_t state, gr_ctx_t ctx)
{
fmpz_t t;
fmpz_init(t);
fmpz_set_ui_array(t, MPN_MOD_CTX_MODULUS(ctx), MPN_MOD_CTX_NLIMBS(ctx));
fmpz_randm(t, state, t);
GR_IGNORE(mpn_mod_set_fmpz(res, t, ctx));
fmpz_clear(t);
return GR_SUCCESS;
}
int
mpn_mod_randtest(nn_ptr res, flint_rand_t state, gr_ctx_t ctx)
{
fmpz_t t;
fmpz_init(t);
fmpz_set_ui_array(t, MPN_MOD_CTX_MODULUS(ctx), MPN_MOD_CTX_NLIMBS(ctx));
fmpz_randtest_mod(t, state, t);
GR_IGNORE(mpn_mod_set_fmpz(res, t, ctx));
fmpz_clear(t);
return GR_SUCCESS;
}
int
mpn_mod_write(gr_stream_t out, nn_srcptr x, gr_ctx_t ctx)
{
gr_stream_write_free(out, _flint_mpn_get_str(x, MPN_MOD_CTX_NLIMBS(ctx)));
return GR_SUCCESS;
}
int
mpn_mod_get_fmpz(fmpz_t res, nn_srcptr x, gr_ctx_t ctx)
{
fmpz_set_ui_array(res, x, MPN_MOD_CTX_NLIMBS(ctx));
return GR_SUCCESS;
}
truth_t
mpn_mod_is_neg_one(gr_srcptr x, gr_ctx_t ctx)
{
ulong t[MPN_MOD_MAX_LIMBS];
mpn_mod_neg_one(t, ctx);
return flint_mpn_equal_p(x, t, MPN_MOD_CTX_NLIMBS(ctx)) ? T_TRUE : T_FALSE;
}
int
mpn_mod_neg(nn_ptr res, nn_srcptr x, gr_ctx_t ctx)
{
if (MPN_MOD_CTX_NLIMBS(ctx) == 2)
flint_mpn_negmod_2(res, x, MPN_MOD_CTX_MODULUS(ctx));
else
flint_mpn_negmod_n(res, x, MPN_MOD_CTX_MODULUS(ctx), MPN_MOD_CTX_NLIMBS(ctx));
return GR_SUCCESS;
}
int
mpn_mod_add(nn_ptr res, nn_srcptr x, nn_srcptr y, gr_ctx_t ctx)
{
if (MPN_MOD_CTX_NLIMBS(ctx) == 2)
flint_mpn_addmod_2(res, x, y, MPN_MOD_CTX_MODULUS(ctx));
else
flint_mpn_addmod_n(res, x, y, MPN_MOD_CTX_MODULUS(ctx), MPN_MOD_CTX_NLIMBS(ctx));
return GR_SUCCESS;
}
int
mpn_mod_sub(nn_ptr res, nn_srcptr x, nn_srcptr y, gr_ctx_t ctx)
{
if (MPN_MOD_CTX_NLIMBS(ctx) == 2)
flint_mpn_submod_2(res, x, y, MPN_MOD_CTX_MODULUS(ctx));
else
flint_mpn_submod_n(res, x, y, MPN_MOD_CTX_MODULUS(ctx), MPN_MOD_CTX_NLIMBS(ctx));
return GR_SUCCESS;
}
int
mpn_mod_add_ui(nn_ptr res, nn_srcptr x, ulong y, gr_ctx_t ctx)
{
if (MPN_MOD_CTX_NLIMBS(ctx) == 2)
{
ulong t[2];
t[0] = y;
t[1] = 0;
flint_mpn_addmod_2(res, x, t, MPN_MOD_CTX_MODULUS(ctx));
}
else
{
flint_mpn_addmod_n_m(res, x, &y, 1, MPN_MOD_CTX_MODULUS(ctx), MPN_MOD_CTX_NLIMBS(ctx));
}
return GR_SUCCESS;
}
int
mpn_mod_sub_ui(nn_ptr res, nn_srcptr x, ulong y, gr_ctx_t ctx)
{
if (MPN_MOD_CTX_NLIMBS(ctx) == 2)
{
ulong t[2];
t[0] = y;
t[1] = 0;
flint_mpn_submod_2(res, x, t, MPN_MOD_CTX_MODULUS(ctx));
}
else
{
flint_mpn_submod_n_m(res, x, &y, 1, MPN_MOD_CTX_MODULUS(ctx), MPN_MOD_CTX_NLIMBS(ctx));
}
return GR_SUCCESS;
}
int
mpn_mod_add_si(nn_ptr res, nn_srcptr x, slong y, gr_ctx_t ctx)
{
if (y >= 0)
mpn_mod_add_ui(res, x, (ulong) y, ctx);
else
mpn_mod_sub_ui(res, x, -(ulong) y, ctx);
return GR_SUCCESS;
}
int
mpn_mod_sub_si(nn_ptr res, nn_srcptr x, slong y, gr_ctx_t ctx)
{
if (y >= 0)
mpn_mod_sub_ui(res, x, (ulong) y, ctx);
else
mpn_mod_add_ui(res, x, -(ulong) y, ctx);
return GR_SUCCESS;
}
int
mpn_mod_add_fmpz(nn_ptr res, nn_srcptr x, const fmpz_t y, gr_ctx_t ctx)
{
if (!COEFF_IS_MPZ(*y))
{
mpn_mod_add_si(res, x, *y, ctx);
}
else
{
ulong t[MPN_MOD_MAX_LIMBS];
nn_srcptr m = MPN_MOD_CTX_MODULUS(ctx);
slong n = MPN_MOD_CTX_NLIMBS(ctx);
mpz_ptr z = COEFF_TO_PTR(*y);
slong ssize = z->_mp_size;
slong zn = FLINT_ABS(ssize);
nn_srcptr zd = z->_mp_d;
if (zn < n || (zn == n && mpn_cmp(zd, m, n) < 0))
{
if (ssize > 0)
flint_mpn_addmod_n_m(res, x, zd, zn, m, n);
else
flint_mpn_submod_n_m(res, x, zd, zn, m, n);
}
else
{
mpn_mod_set_mpn(t, zd, zn, ctx);
if (ssize > 0)
mpn_mod_add(res, x, t, ctx);
else
mpn_mod_sub(res, x, t, ctx);
}
}
return GR_SUCCESS;
}
int
mpn_mod_sub_fmpz(nn_ptr res, nn_srcptr x, const fmpz_t y, gr_ctx_t ctx)
{
if (!COEFF_IS_MPZ(*y))
{
mpn_mod_sub_si(res, x, *y, ctx);
}
else
{
ulong t[MPN_MOD_MAX_LIMBS];
nn_srcptr m = MPN_MOD_CTX_MODULUS(ctx);
slong n = MPN_MOD_CTX_NLIMBS(ctx);
mpz_ptr z = COEFF_TO_PTR(*y);
slong ssize = z->_mp_size;
slong zn = FLINT_ABS(ssize);
nn_srcptr zd = z->_mp_d;
if (zn < n || (zn == n && mpn_cmp(zd, m, n) < 0))
{
if (ssize > 0)
flint_mpn_submod_n_m(res, x, zd, zn, m, n);
else
flint_mpn_addmod_n_m(res, x, zd, zn, m, n);
}
else
{
mpn_mod_set_mpn(t, zd, zn, ctx);
if (ssize > 0)
mpn_mod_sub(res, x, t, ctx);
else
mpn_mod_add(res, x, t, ctx);
}
}
return GR_SUCCESS;
}
/* should mirror flint_mpn_mulmod_preinvn (backport any improvements) */
int
mpn_mod_mul(nn_ptr res, nn_srcptr x, nn_srcptr y, gr_ctx_t ctx)
{
slong n = MPN_MOD_CTX_NLIMBS(ctx);
if (n == 2)
{
flint_mpn_mulmod_preinvn_2(res, x, y, MPN_MOD_CTX_MODULUS_NORMED(ctx), MPN_MOD_CTX_MODULUS_PREINV(ctx), MPN_MOD_CTX_NORM(ctx));
}
else
{
ulong t[5 * MPN_MOD_MAX_LIMBS];
flint_bitcnt_t norm = MPN_MOD_CTX_NORM(ctx);
nn_ptr d = MPN_MOD_CTX_MODULUS_NORMED(ctx);
nn_ptr dinv = MPN_MOD_CTX_MODULUS_PREINV(ctx);
ulong cy;
if (norm)
{
if (x == y)
{
flint_mpn_sqr(t, x, n);
mpn_lshift(t, t, 2 * n, norm);
}
else
{
mpn_lshift(t + 2 * n, x, n, norm);
flint_mpn_mul_n(t, t + 2 * n, y, n);
}
}
else
{
if (x == y)
flint_mpn_sqr(t, x, n);
else
flint_mpn_mul_n(t, x, y, n);
}
flint_mpn_mul_or_mulhigh_n(t + 3 * n, t + n, dinv, n);
mpn_add_n(t + 4 * n, t + 4 * n, t + n, n);
/* note: we rely on the fact that mul_or_mullow_n actually
writes at least n + 1 limbs */
flint_mpn_mul_or_mullow_n(t + 2 * n, t + 4 * n, d, n);
cy = t[n] - t[3 * n] - mpn_sub_n(res, t, t + 2 * n, n);
while (cy > 0)
cy -= mpn_sub_n(res, res, d, n);
if (mpn_cmp(res, d, n) >= 0)
mpn_sub_n(res, res, d, n);
FLINT_ASSERT(mpn_cmp(res, d, n) < 0);
if (norm)
mpn_rshift(res, res, n, norm);
}
return GR_SUCCESS;
}
int
mpn_mod_fmma(nn_ptr res, nn_srcptr x, nn_srcptr y, nn_srcptr x2, nn_srcptr y2, gr_ctx_t ctx)
{
slong n = MPN_MOD_CTX_NLIMBS(ctx);
if (n == 2)
flint_mpn_fmmamod_preinvn_2(res, x, y, x2, y2, MPN_MOD_CTX_MODULUS_NORMED(ctx), MPN_MOD_CTX_MODULUS_PREINV(ctx), MPN_MOD_CTX_NORM(ctx));
else
flint_mpn_fmmamod_preinvn(res, x, y, x2, y2, n, MPN_MOD_CTX_MODULUS_NORMED(ctx), MPN_MOD_CTX_MODULUS_PREINV(ctx), MPN_MOD_CTX_NORM(ctx));
return GR_SUCCESS;
}
/* todo: check for 0? */
int
mpn_mod_mul_ui(nn_ptr res, nn_srcptr x, ulong y, gr_ctx_t ctx)
{
ulong t[MPN_MOD_MAX_LIMBS + 1];
slong n = MPN_MOD_CTX_NLIMBS(ctx);
slong tn;
t[n] = mpn_mul_1(t, x, n, y);
tn = n + (t[n] != 0);
mpn_mod_set_mpn(res, t, tn, ctx);
return GR_SUCCESS;
}
/* todo: check for 0? */
int
mpn_mod_mul_si(nn_ptr res, nn_srcptr x, slong y, gr_ctx_t ctx)
{
ulong t[MPN_MOD_MAX_LIMBS + 1];
slong n = MPN_MOD_CTX_NLIMBS(ctx);
slong tn;
t[n] = mpn_mul_1(t, x, n, FLINT_UABS(y));
tn = n + (t[n] != 0);
mpn_mod_set_mpn(res, t, tn, ctx);
if (y < 0)
mpn_mod_neg(res, res, ctx);
return GR_SUCCESS;
}
/* todo: check for 0? */
int
mpn_mod_mul_fmpz(nn_ptr res, nn_srcptr x, const fmpz_t y, gr_ctx_t ctx)
{
if (!COEFF_IS_MPZ(*y))
{
mpn_mod_mul_si(res, x, *y, ctx);
}
else
{
ulong t[2 * MPN_MOD_MAX_LIMBS], cy;
slong tn, n = MPN_MOD_CTX_NLIMBS(ctx);
mpz_ptr z = COEFF_TO_PTR(*y);
slong ssize = z->_mp_size;
slong zn = FLINT_ABS(ssize);
nn_srcptr zd = z->_mp_d;
if (zn <= n)
{
cy = flint_mpn_mul(t, x, n, zd, zn);
tn = n + zn - (cy == 0);
mpn_mod_set_mpn(res, t, tn, ctx);
}
else
{
mpn_mod_set_mpn(t, zd, zn, ctx);
mpn_mod_mul(res, x, t, ctx);
}
if (ssize < 0)
mpn_mod_neg(res, res, ctx);
}
return GR_SUCCESS;
}
int
mpn_mod_addmul(nn_ptr res, nn_srcptr x, nn_srcptr y, gr_ctx_t ctx)
{
ulong t[MPN_MOD_MAX_LIMBS];
mpn_mod_mul(t, x, y, ctx);
mpn_mod_add(res, res, t, ctx);
return GR_SUCCESS;
}
int
mpn_mod_addmul_ui(nn_ptr res, nn_srcptr x, ulong y, gr_ctx_t ctx)
{
ulong t[MPN_MOD_MAX_LIMBS];
mpn_mod_mul_ui(t, x, y, ctx);
mpn_mod_add(res, res, t, ctx);
return GR_SUCCESS;
}
int
mpn_mod_addmul_si(nn_ptr res, nn_srcptr x, slong y, gr_ctx_t ctx)
{
ulong t[MPN_MOD_MAX_LIMBS];
mpn_mod_mul_si(t, x, y, ctx);
mpn_mod_add(res, res, t, ctx);
return GR_SUCCESS;
}
int
mpn_mod_addmul_fmpz(nn_ptr res, nn_srcptr x, const fmpz_t y, gr_ctx_t ctx)
{
ulong t[MPN_MOD_MAX_LIMBS];
mpn_mod_mul_fmpz(t, x, y, ctx);
mpn_mod_add(res, res, t, ctx);
return GR_SUCCESS;
}
int
mpn_mod_submul(nn_ptr res, nn_srcptr x, nn_srcptr y, gr_ctx_t ctx)
{
ulong t[MPN_MOD_MAX_LIMBS];
mpn_mod_mul(t, x, y, ctx);
mpn_mod_sub(res, res, t, ctx);
return GR_SUCCESS;
}
int
mpn_mod_submul_ui(nn_ptr res, nn_srcptr x, ulong y, gr_ctx_t ctx)
{
ulong t[MPN_MOD_MAX_LIMBS];
mpn_mod_mul_ui(t, x, y, ctx);
mpn_mod_sub(res, res, t, ctx);
return GR_SUCCESS;
}
int
mpn_mod_submul_si(nn_ptr res, nn_srcptr x, slong y, gr_ctx_t ctx)
{
ulong t[MPN_MOD_MAX_LIMBS];
mpn_mod_mul_si(t, x, y, ctx);
mpn_mod_sub(res, res, t, ctx);
return GR_SUCCESS;
}
int
mpn_mod_submul_fmpz(nn_ptr res, nn_srcptr x, const fmpz_t y, gr_ctx_t ctx)
{
ulong t[MPN_MOD_MAX_LIMBS];
mpn_mod_mul_fmpz(t, x, y, ctx);
mpn_mod_sub(res, res, t, ctx);
return GR_SUCCESS;
}
int
mpn_mod_inv(nn_ptr res, nn_srcptr x, gr_ctx_t ctx)
{
slong n = MPN_MOD_CTX_NLIMBS(ctx);
nn_srcptr d = MPN_MOD_CTX_MODULUS(ctx);
ulong g[MPN_MOD_MAX_LIMBS];
ulong s[MPN_MOD_MAX_LIMBS];
ulong t[MPN_MOD_MAX_LIMBS];
ulong u[MPN_MOD_MAX_LIMBS];
mp_size_t gsize, ssize;
if (mpn_mod_is_one(x, ctx) == T_TRUE || mpn_mod_is_neg_one(x, ctx) == T_TRUE)
return mpn_mod_set(res, x, ctx);
flint_mpn_copyi(t, x, n);
flint_mpn_copyi(u, d, n);
/* todo: does mpn_gcdext allow aliasing? */
/* NOTE: ssize must be mp_size_t since it is strictly different from slong
* on Windows systems. */
gsize = mpn_gcdext(g, s, &ssize, t, n, u, n);
if (gsize != 1 || g[0] != 1)
return GR_DOMAIN;
flint_mpn_zero(s + FLINT_ABS(ssize), n - FLINT_ABS(ssize));
if (ssize >= 0)
flint_mpn_copyi(res, s, n);
else
flint_mpn_negmod_n(res, s, d, n);
return GR_SUCCESS;
}
int
mpn_mod_div(nn_ptr res, nn_srcptr x, nn_srcptr y, gr_ctx_t ctx)
{
int status;
ulong t[MPN_MOD_MAX_LIMBS];
status = mpn_mod_inv(t, y, ctx);
if (status == GR_SUCCESS)
mpn_mod_mul(res, x, t, ctx);
else
mpn_mod_zero(res, ctx);
return status;
}