forked from primecoin/primecoin
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mpz.h
755 lines (628 loc) · 20.4 KB
/
mpz.h
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
/**
* @file mpz.c
*
* @brief Multiple Precision arithmetic code.
*
* @author David Matlack (dmatlack)
*/
#ifndef __418_MPZ_H__
#define __418_MPZ_H__
#include "compile.h"
#include "cuda_string.h"
#include "digit.h"
#define MPZ_NEGATIVE 1
#define MPZ_NONNEGATIVE 0
/*struct mpz_cuda_t __align__(8) {
unsigned int capacity;
digit_t digits[DIGITS_CAPACITY];
char sign;
};*/
__device__ __host__ inline char* mpz_get_str(mpz_cuda_t *mpz, char *str, int bufsize);
/**
* @brief Check that the mpz_cuda_t struct has enough memory to store __capacity
* digits.
*/
#ifndef __CUDACC__
#define CHECK_MEM(__mpz, __capacity) \
do { \
if ((__mpz)->capacity < (__capacity)) { \
printf("MPZ memory error at %s:%d.\n", __func__, __LINE__); \
printf("\tmpz capacity: %u, requested capacity %u\n", \
(__mpz)->capacity, (__capacity)); \
assert(0); \
} \
} while (0)
#else
#define CHECK_MEM(__mpz, __capacity)
#endif
/**
* @brief Sanity check the sign.
*/
#ifndef __CUDACC__
#define CHECK_SIGN(__mpz) \
do { \
if (digits_is_zero((__mpz)->digits, (__mpz)->capacity) && \
(__mpz)->sign != MPZ_NONNEGATIVE) { \
printf("MPZ Sign Error at %s:%d: Value is 0 but sign is %d.\n", \
__func__, __LINE__, (__mpz)->sign); \
assert(0);\
} \
if ((__mpz)->sign != MPZ_NEGATIVE && \
(__mpz)->sign != MPZ_NONNEGATIVE) { \
printf("MPZ Sign Error at %s:%d: Invalid sign %d.\n", \
__func__, __LINE__, (__mpz)->sign); \
assert(0);\
} \
} while (0)
#else
#define CHECK_SIGN(__mpz)
#endif
/**
* @brief Do some sanity checking on the mpz_cuda_t sign field.
*/
#ifndef __CUDACC__
#define CHECK_STRS(s1, s2) \
do { \
if (strcmp(s1, s2)) { \
printf("Input string %s became %s!\n", s1, s2); \
} \
} while (0)
#else
#define CHECK_STRS(s1, s2)
#endif
__device__ __host__ inline int mpz_is_negative(mpz_cuda_t *mpz) {
return (mpz->sign == MPZ_NEGATIVE);
}
__device__ __host__ inline void mpz_negate(mpz_cuda_t *mpz) {
mpz->sign = (mpz->sign == MPZ_NEGATIVE) ? MPZ_NONNEGATIVE : MPZ_NEGATIVE;
if (digits_is_zero(mpz->digits, mpz->capacity)) {
mpz->sign = MPZ_NONNEGATIVE;
}
}
/**
* @brief Initialize an mpz struct to 0.
*/
__device__ __host__ void mpz_init(mpz_cuda_t *mpz) {
mpz->capacity = DIGITS_CAPACITY;
digits_set_zero(mpz->digits);
mpz->sign = MPZ_NONNEGATIVE;
}
__device__ __host__ void mpz_init_wide(mpz_cuda_t *mpz) {
mpz->capacity = DIGITS_CAPACITY*2;
digits_set_zero(mpz->digits);
mpz->sign = MPZ_NONNEGATIVE;
}
/**
* @brief Assign an mpz_cuda_t struct to the value of another mpz_cuda_t struct.
*/
__device__ __host__ inline void mpz_set(mpz_cuda_t *to, mpz_cuda_t *from) {
unsigned int i;
for (i = 0; i < to->capacity; i++) {
digit_t d = (i < from->capacity) ? from->digits[i] : 0;
to->digits[i] = d;
}
to->sign = from->sign;
CHECK_SIGN(to);
CHECK_SIGN(from);
}
/**
* @brief Set the mpz integer to the provided integer.
*/
__device__ __host__ inline void mpz_set_i(mpz_cuda_t *mpz, int z) {
mpz->sign = (z < 0) ? MPZ_NEGATIVE : MPZ_NONNEGATIVE;
digits_set_lui(mpz->digits, abs(z));
}
/**
* @brief Set the mpz integer to the provided integer.
*/
__device__ __host__ inline void mpz_set_lui(mpz_cuda_t *mpz, unsigned long z) {
mpz->sign = MPZ_NONNEGATIVE;
digits_set_lui(mpz->digits, z);
}
/**
* @brief Set the mpz integer to the provided integer.
*/
__device__ __host__ inline void mpz_set_ui(mpz_cuda_t *mpz, unsigned int z) {
mpz->sign = MPZ_NONNEGATIVE;
digits_set_lui(mpz->digits, z);
}
/**
* @brief Set the mpz integer based on the provided (hex) string.
*/
__device__ __host__ void mpz_set_str(mpz_cuda_t *mpz, char *user_str, unsigned int index) {
int num_digits;
int i;
int is_zero;
for (i = 0; i < mpz->capacity; i++) mpz->digits[i] = 0;
char *str = user_str;
/* Check if the provided number is negative */
if (str[0] == '-') {
mpz->sign = MPZ_NEGATIVE;
str ++; // the number starts at the next character
}
else {
mpz->sign = MPZ_NONNEGATIVE;
}
int len = cuda_strlen(str);
int char_per_digit = LOG2_DIGIT_BASE / 4;
num_digits = ((len - 1) / char_per_digit) +1;
//if(index==0)
// printf("set_str: numdigits ==%i\n", num_digits );
CHECK_MEM(mpz, num_digits);
digits_set_zero(mpz->digits);
is_zero = true;
for (i = 0; i < num_digits; i ++) {
str[len - i * char_per_digit] = (char) 0;
int offset = (int) max(len - (i + 1) * char_per_digit, 0);
char *start = str + offset;
digit_t d = cuda_strtol(start, NULL, 16);
//if(index==0)
// printf("[0] digit %x from string %s offset %i\n", d, start,offset);
/* keep track of whether or not every digit is zero */
is_zero = is_zero && (d == 0);
/* parse the string backwards (little endian order) */
mpz->digits[i] = d;
}
/* Just in case the user gives us -0 as input */
if (is_zero) mpz->sign = MPZ_NONNEGATIVE;
#if 0
mpz_get_str(mpz, buf, bufsize);
CHECK_STRS(user_str, buf);
#endif
}
__device__ __host__ inline void mpz_get_binary_str(mpz_cuda_t *mpz, char *str, unsigned int s) {
(void) mpz;
(void) str;
(void) s;
}
/**
* @brief Destroy the mpz_cuda_t struct.
*
* @deprecated
*/
__device__ __host__ inline void mpz_destroy(mpz_cuda_t *mpz) {
(void) mpz;
}
/**
* @brief Add two multiple precision integers.
*
* dst := op1 + op2
*
* @warning It is assumed that all mpz_cuda_t parameters have been initialized.
* @warning Assumes dst != op1 != op2
*/
__device__ __host__ inline void mpz_add(mpz_cuda_t *dst, mpz_cuda_t *op1, mpz_cuda_t *op2) {
#ifdef __CUDACC__
unsigned int op1_digit_count = digits_count(op1->digits);
unsigned int op2_digit_count = digits_count(op2->digits);
/* In addition, if the operand with the most digits has D digits, then
* the result of the addition will have at most D + 1 digits. */
unsigned int capacity = max(op1_digit_count, op2_digit_count) + 1;
/* Make sure all of the mpz structs have enough memory to hold all of
* the digits. We will be doing 10's complement so everyone needs to
* have enough digits. */
CHECK_MEM(dst, capacity);
CHECK_MEM(op1, capacity);
CHECK_MEM(op2, capacity);
#endif
digits_set_zero(dst->digits);
/* If both are negative, treate them as positive and negate the result */
if (mpz_is_negative(op1) && mpz_is_negative(op2)) {
digits_add(dst->digits, dst->capacity,
op1->digits, op1->capacity,
op2->digits, op2->capacity);
dst->sign = MPZ_NEGATIVE;
}
/* one or neither are negative */
else {
digit_t carry_out;
/* Perform 10's complement on negative numbers before adding */
if (mpz_is_negative(op1)) digits_complement(op1->digits, op1->capacity);
if (mpz_is_negative(op2)) digits_complement(op2->digits, op2->capacity);
carry_out = digits_add(dst->digits, dst->capacity,
op1->digits, op1->capacity,
op2->digits, op2->capacity);
/* If there is no carryout, the result is negative */
if (carry_out == 0 && (mpz_is_negative(op1) || mpz_is_negative(op2))) {
digits_complement(dst->digits, dst->capacity);
dst->sign = MPZ_NEGATIVE;
}
/* Otherwise, the result is non-negative */
else {
dst->sign = MPZ_NONNEGATIVE;
}
/* Undo the 10s complement after adding */
if (mpz_is_negative(op1)) digits_complement(op1->digits, op1->capacity);
if (mpz_is_negative(op2)) digits_complement(op2->digits, op2->capacity);
}
CHECK_SIGN(op1);
CHECK_SIGN(op2);
CHECK_SIGN(dst);
}
__device__ __host__ inline void mpz_addeq(mpz_cuda_t *op1, mpz_cuda_t *op2) {
/* If both are negative, treate them as positive and negate the result */
if (mpz_is_negative(op1) && mpz_is_negative(op2)) {
digits_addeq(op1->digits, op1->capacity,
op2->digits, op2->capacity);
op1->sign = MPZ_NEGATIVE;
}
/* one or neither are negative */
else {
digit_t carry_out;
/* Perform 10's complement on negative numbers before adding */
if (mpz_is_negative(op1)) digits_complement(op1->digits, op1->capacity);
if (mpz_is_negative(op2)) digits_complement(op2->digits, op2->capacity);
carry_out = digits_addeq(op1->digits, op1->capacity,
op2->digits, op2->capacity);
/* If there is no carryout, the result is negative */
if (carry_out == 0 && (mpz_is_negative(op1) || mpz_is_negative(op2))) {
digits_complement(op1->digits, op1->capacity);
op1->sign = MPZ_NEGATIVE;
}
/* Otherwise, the result is non-negative */
else {
op1->sign = MPZ_NONNEGATIVE;
}
/* Undo the 10s complement after adding */
if (mpz_is_negative(op2)) digits_complement(op2->digits, op2->capacity);
}
CHECK_SIGN(op1);
CHECK_SIGN(op2);
}
/**
* @brief Perform dst := op1 - op2.
*
* @warning Assumes that all mpz_cuda_t parameters have been initialized.
* @warning Assumes dst != op1 != op2
*/
__device__ __host__ inline void mpz_sub(mpz_cuda_t *dst, mpz_cuda_t *op1, mpz_cuda_t *op2) {
mpz_negate(op2);
mpz_add(dst, op1, op2);
mpz_negate(op2);
}
/**
* @brief Perform op1 -= op2.
*
* @warning Assumes that all mpz_cuda_t parameters have been initialized.
* @warning Assumes op1 != op2
*/
__device__ __host__ inline void mpz_subeq(mpz_cuda_t *op1, mpz_cuda_t *op2) {
mpz_negate(op2);
mpz_addeq(op1, op2);
mpz_negate(op2);
}
/**
* @brief Perform dst := op1 * op2.
*
* @warning Assumes that all mpz_cuda_t parameters have been initialized.
* @warning Assumes dst != op1 != op2
*/
__device__ __host__ inline void mpz_mult(mpz_cuda_t *dst, mpz_cuda_t *op1, mpz_cuda_t *op2) {
unsigned int op1_digit_count = digits_count(op1->digits);
unsigned int op2_digit_count = digits_count(op2->digits);
unsigned int capacity = max(op1_digit_count, op2_digit_count);
/* In multiplication, if the operand with the most digits has D digits,
* then the result of the addition will have at most 2D digits. */
CHECK_MEM(dst, 2*capacity);
CHECK_MEM(op1, capacity);
CHECK_MEM(op2, capacity);
/* Done by long_multiplication */
/* digits_set_zero(dst->digits); */
/* We pass in capacity as the number of digits rather that the actual
* number of digits in each mpz_cuda_t struct. This is done because the
* multiplication code has some assumptions and optimizations (e.g.
* op1 and op2 to have the same number of digits) */
digits_mult(dst->digits, op1->digits, op2->digits, capacity, dst->capacity);
/* Compute the sign of the product */
dst->sign = (op1->sign == op2->sign) ? MPZ_NONNEGATIVE : MPZ_NEGATIVE;
if (MPZ_NEGATIVE == dst->sign && digits_is_zero(dst->digits, dst->capacity)) {
dst->sign = MPZ_NONNEGATIVE;
}
CHECK_SIGN(op1);
CHECK_SIGN(op2);
CHECK_SIGN(dst);
}
/**
* @return
* < 0 if a < b
* = 0 if a = b
* > 0 if a > b
*
* @warning This function does not give any indication about the distance
* between a and b, just the relative distance (<, >, =).
*/
#define MPZ_LESS -1
#define MPZ_GREATER 1
#define MPZ_EQUAL 0
__device__ __host__ inline int mpz_compare(mpz_cuda_t *a, mpz_cuda_t *b) {
int cmp;
int negative;
if (MPZ_NEGATIVE == a->sign && MPZ_NONNEGATIVE == b->sign) return MPZ_LESS;
if (MPZ_NEGATIVE == b->sign && MPZ_NONNEGATIVE == a->sign) return MPZ_GREATER;
/* At this point we know they have the same sign */
cmp = digits_compare(a->digits, a->capacity, b->digits, b->capacity);
negative = mpz_is_negative(a);
if (cmp == 0) return MPZ_EQUAL;
if (negative) {
return (cmp > 0) ? MPZ_LESS : MPZ_GREATER;
}
else {
return (cmp < 0) ? MPZ_LESS : MPZ_GREATER;
}
}
/** @brief Return true if a == b */
__device__ __host__ inline int mpz_equal(mpz_cuda_t *a, mpz_cuda_t *b) {
return (mpz_compare(a, b) == 0);
}
/** @brief Return true if a == 1 */
__device__ __host__ inline int mpz_equal_one(mpz_cuda_t *a) {
if (MPZ_NEGATIVE == a->sign) {
return false;
}
return digits_equal_one(a->digits, a->capacity);
}
/** @brief Return true if a < b */
__device__ __host__ inline int mpz_lt(mpz_cuda_t *a, mpz_cuda_t *b) {
return (mpz_compare(a, b) < 0);
}
/** @brief Return true if a <= b */
__device__ __host__ inline int mpz_lte(mpz_cuda_t *a, mpz_cuda_t *b) {
return (mpz_compare(a, b) <= 0);
}
/** @brief Return true if a > b */
__device__ __host__ inline int mpz_gt(mpz_cuda_t *a, mpz_cuda_t *b) {
return (mpz_compare(a, b) > 0);
}
/** @brief Return true if a == 1 */
__device__ __host__ inline int mpz_gt_one(mpz_cuda_t *a) {
if (MPZ_NEGATIVE == a->sign) {
return false;
}
return digits_gt_one(a->digits, a->capacity);
}
/** @brief Return true if a >= b */
__device__ __host__ inline int mpz_gte(mpz_cuda_t *a, mpz_cuda_t *b) {
return (mpz_compare(a, b) >= 0);
}
/**
* @brief Return the string representation of the integer represented by the
* mpz_cuda_t struct.
*
* @warning If buf is NULL, the string is dynamically allocated and must
* therefore be freed by the user.
*/
__device__ __host__ inline char* mpz_get_str(mpz_cuda_t *mpz, char *str, int bufsize) {
int print_zeroes = 0; // don't print leading 0s
int i;
int str_index = 0;
if (mpz_is_negative(mpz)) {
str[0] = '-';
str_index = 1;
}
for (i = mpz->capacity - 1; i >= 0; i--) {
digit_t digit = mpz->digits[i];
if (digit != 0 || print_zeroes) {
if (bufsize < str_index + 8) {
return NULL;
}
if (!print_zeroes) {
str_index += sprintf(str + str_index, "%x", digit);
}
else {
str_index += sprintf(str + str_index, "%08x", digit);
}
print_zeroes = 1;
}
}
str[str_index] = (char) 0;
/* the number is zero */
if (print_zeroes == 0) {
str[0] = '0';
str[1] = (char) 0;
}
return str;
}
__device__ __host__ inline void mpz_print(mpz_cuda_t *mpz) {
bool print_zeroes = false; // don't print leading 0s
printf("mpz_capacity: %i ", mpz->capacity);
for (int i = mpz->capacity - 1; i >= 0; i--) {
if ( mpz->digits[i] != 0 || print_zeroes)
{
print_zeroes = true;
printf("%08x", mpz->digits[i]);
}
}
printf("\n");
//#ifndef __CUDACC__
//char str[1024];
//mpz_get_str(mpz, str, 1024);
//#endif
}
__device__ __host__ inline void mpz_set_bit(mpz_cuda_t *mpz, unsigned bit_offset,
unsigned int bit) {
digits_set_bit(mpz->digits, bit_offset, bit);
if (MPZ_NEGATIVE == mpz->sign && bit == 0 &&
digits_is_zero(mpz->digits, mpz->capacity)) {
mpz->sign = MPZ_NONNEGATIVE;
}
}
__device__ __host__ inline void mpz_bit_lshift(mpz_cuda_t *mpz) {
bits_lshift(mpz->digits, mpz->capacity);
if (MPZ_NEGATIVE == mpz->sign && digits_is_zero(mpz->digits, mpz->capacity)) {
mpz->sign = MPZ_NONNEGATIVE;
}
}
__device__ __host__ inline void mpz_div(mpz_cuda_t *q, mpz_cuda_t *r, mpz_cuda_t *n,
mpz_cuda_t *d) {
unsigned int n_digit_count = digits_count(n->digits);
unsigned int num_bits;
int i;
int nsign = n->sign;
int dsign = d->sign;
num_bits = n_digit_count * LOG2_DIGIT_BASE;
mpz_set_ui(q, 0);
mpz_set_ui(r, 0);
n->sign = MPZ_NONNEGATIVE;
d->sign = MPZ_NONNEGATIVE;
if (mpz_gt(n, d)) {
for (i = num_bits - 1; i >= 0; i--) {
unsigned int n_i;
// r = r << 1
mpz_bit_lshift(r);
// r(0) = n(i)
n_i = digits_bit_at(n->digits, i);
mpz_set_bit(r, 0, n_i);
// if (r >= d)
if (mpz_gte(r, d)) {
// r = r - d
mpz_subeq(r, d);
// q(i) = 1
//printf("Setting bit %d of q to 1\n", i);
//printf("\tBefore: "); mpz_print(q); printf("\n");
mpz_set_bit(q, i, 1);
//printf("\tAfter: "); mpz_print(q); printf("\n");
}
}
/* Compute the sign of the division */
q->sign = (nsign == dsign) ? MPZ_NONNEGATIVE : MPZ_NEGATIVE;
if (MPZ_NEGATIVE == q->sign && digits_is_zero(q->digits, q->capacity)) {
q->sign = MPZ_NONNEGATIVE;
}
}
else {
// quotient = 0
mpz_set_ui(q, 0);
// remainder = numerator
mpz_set(r, n);
}
n->sign = nsign;
d->sign = dsign;
CHECK_SIGN(q);
CHECK_SIGN(r);
CHECK_SIGN(n);
CHECK_SIGN(d);
}
/**
* @brief Compute the GCD of op1 and op2.
*
* Euclidean Algorithm:
*
* while (b != 0) {
* t := b
* b := a % b
* a := t
* }
* gcd = a
*/
__device__ __inline__ void mpz_gcd_tmp(mpz_cuda_t *gcd, mpz_cuda_t *op1, mpz_cuda_t *op2,
// tmps
mpz_cuda_t *tmp1, mpz_cuda_t *tmp2,
mpz_cuda_t *tmp3) {
mpz_cuda_t *a = gcd;
mpz_cuda_t *b = tmp1;
mpz_cuda_t *mod = tmp2;
mpz_cuda_t *quo = tmp3;
int compare = mpz_compare(op1, op2);
mpz_set(a, (compare > 0) ? op1 : op2);
mpz_set(b, (compare > 0) ? op2 : op1);
while (!digits_is_zero(b->digits, b->capacity)) {
mpz_div(quo, mod, a, b);
mpz_set(a, b);
mpz_set(b, mod);
}
}
__device__ __inline__ void mpz_gcd(mpz_cuda_t *gcd, mpz_cuda_t *op1, mpz_cuda_t *op2) {
mpz_cuda_t tmp1;
mpz_cuda_t tmp2;
mpz_cuda_t tmp3;
mpz_init(&tmp1);
mpz_init(&tmp2);
mpz_init(&tmp3);
mpz_gcd_tmp(gcd, op1, op2, &tmp1, &tmp2, &tmp3);
}
/**
* Using exponentiation by squaring algorithm:
*
* function modular_pow(base, exponent, modulus)
* result := 1
* while exponent > 0
* if (exponent mod 2 == 1):
* result := (result * base) mod modulus
* exponent := exponent >> 1
* base = (base * base) mod modulus
* return result
*/
__device__ __inline__ void mpz_powmod_tmp(mpz_cuda_t *result, mpz_cuda_t *base,
mpz_cuda_t *exp, mpz_cuda_t *mod,
// temps
mpz_cuda_t *tmp1, mpz_cuda_t *tmp2,
mpz_cuda_t *tmp3) {
unsigned int iteration;
mpz_cuda_t *b = tmp3;
// result = 1
mpz_set_ui(result, 1);
// _base = base % mod
mpz_set(tmp1, base);
mpz_div(tmp2, b, tmp1, mod);
iteration = 0;
while (!bits_is_zero(exp->digits, exp->capacity, iteration)) {
// if (binary_exp is odd)
if (digits_bit_at(exp->digits, iteration) == 1) {
// result = (result * base) % mod
mpz_mult(tmp1, result, b);
mpz_div(tmp2, result, tmp1, mod);
}
// binary_exp = binary_exp >> 1
iteration++;
// base = (base * base) % mod
mpz_set(tmp1, b);
mpz_mult(tmp2, b, tmp1);
mpz_div(tmp1, b, tmp2, mod);
}
}
__device__ __inline__ void mpz_powmod(mpz_cuda_t *result, mpz_cuda_t *base,
mpz_cuda_t *exp, mpz_cuda_t *mod) {
mpz_cuda_t tmp1;
mpz_cuda_t tmp2;
mpz_cuda_t tmp3;
mpz_init(&tmp1);
mpz_init(&tmp2);
mpz_init(&tmp3);
mpz_powmod_tmp(result, base, exp, mod, &tmp1, &tmp2, &tmp3);
}
__device__ __inline__ void mpz_pow(mpz_cuda_t *result, mpz_cuda_t *base, unsigned int exponent) {
mpz_cuda_t tmp;
unsigned int i;
mpz_init(&tmp);
// result = 1
mpz_set_ui(result, 1);
for (i = 0; i < exponent; i++) {
// result *= base
mpz_mult(&tmp, result, base);
mpz_set(result, &tmp);
}
}
/**
* @brief Compute a += i
*/
__device__ __inline__ void mpz_addeq_i(mpz_cuda_t *a, int i) {
if (0 == i) return;
if (i < 0) {
digits_complement(a->digits, a->capacity);
digits_add_across(a->digits, a->capacity, -i);
digits_complement(a->digits, a->capacity);
}
else {
digits_add_across(a->digits, a->capacity, i);
}
}
/**
* @brief Compute result = a * i
*/
__device__ __inline__ void mpz_mult_u(mpz_cuda_t *result, mpz_cuda_t *a, unsigned int i) {
digits_mult_u(result->digits, a->digits, i);
result->sign = a->sign;
if (0 == i) result->sign = MPZ_NONNEGATIVE;
CHECK_SIGN(result);
}
#endif /* __418_MPZ_H__ */