-
Notifications
You must be signed in to change notification settings - Fork 335
/
sbignum.c
1922 lines (1732 loc) · 45.2 KB
/
sbignum.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
/* antirez's arbitrary precision integer math library.
*
* $Id: sbignum.c,v 1.3 2003/10/02 08:21:42 antirez Exp $
*
* This library was implemented only to joke a bit with the bignum issues,
* don't expect this is very fast or well tested.
* Note that in many applications you should check that the arbitrary
* precision math implementation is very reliable.
*
* (news! actually I'm using it for hping3, so starting from
* now it is something like a real project.)
*
* NOTE: if you need a very good bignums implementation check-out GMP
* at http://swox.com/gmp/ it is very fast and reliable.
*
* This library API is almost GMP compatible for the subset of
* functions exported.
*
* COPYRIGHT NOTICE
* ----------------
*
* Copyright(C) 2002-2003 Salvatore Sanfilippo <antirez@invece.org>
* All rights reserved.
*
* This code and the documentation is released under the GPL license
* version 2 of the license. You can get a copy of the license at
* http://www.gnu.org/licenses/gpl.html
* A copy of the license is distributed with this code,
* see the file COPYING. */
/* History of important bugs:
*
* 28 Feb 2002: Bad casting in low-level subtraction generated bad results
* for particular pairs of numbers. It was a bit hard to
* discover the real origin of the bug since all started
* with a strange behaviour of the Fermat little theorem.
* This was since the modular reduction uses the low-level
* subtraction to perform its work. Of course now it's fixed.
*
* 12 Sep 2003: Fixed a memory leak in mpz_tostr().
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <ctype.h>
#include "sbignum.h"
#include "sbignum-tables.h"
/* All the function with the _raw suffix don't care about the sign
* and works if the last operand, that's specified as a mpz_atom_t pointer
* and a u_int32_t length is stored in statically allocated memory, while
* higher level functions expect operands declared as mpz_t and initialized
* with mpz_init(). */
/* Macros and functions starting with the '_' character are usually not
* exported faster versions of normal functions, that do some unsane assumption
* like there is enough memory to store the result and so on.
* They are used to build more complex functions */
/* --------------------------- Low level functions -------------------------- */
/* For the actual list of supported functions see sbignum.h */
/* inititialization/allocation */
static int mpz_zero_realloc(mpz_ptr z, u_int32_t i);
static void mpz_zero(mpz_ptr z);
/* shifting */
static int mpz_lshiftword(mpz_ptr r, u_int32_t i);
static int mpz_rshiftword(mpz_ptr r, u_int32_t i);
/* comparision */
static int32_t mpz_cmpabsi_raw(mpz_ptr a, mpz_atom_t *d, u_int32_t l);
static int32_t mpz_cmpabs(mpz_ptr a, mpz_ptr b);
/* addition */
static int mpz_addi_raw(mpz_ptr r, mpz_ptr z, mpz_atom_t *d, u_int32_t l);
/* subtraction */
static int mpz_subi_raw(mpz_ptr r, mpz_ptr z, mpz_atom_t *d, u_int32_t l);
/* multiplication */
static int mpz_muli_raw(mpz_ptr r, mpz_ptr z, mpz_atom_t *d, u_int32_t l);
/* division */
static int mpz_divi_qr_raw(mpz_ptr q, mpz_ptr r, mpz_ptr z, mpz_atom_t *d,
u_int32_t l);
static int mpz_divi_r_raw(mpz_ptr r, mpz_ptr z, mpz_atom_t *d, u_int32_t l);
/* number theoretic functions */
static int mpz_gcd_raw(mpz_ptr g, mpz_ptr a, mpz_atom_t *b, u_int32_t l);
/* to/from mpz conversions */
static int mpz_tostr(mpz_ptr z, u_int32_t b, void *s, size_t l);
/* random numbers */
static void sbn_rand_init(void);
/* ================================== MPZ =================================== */
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
/* 32bit integer to mpz conversion */
#if ATOMBYTES == 4
#define u32tompz(t,u,l) \
mpz_atom_t t[1]; \
u_int32_t l = 0; \
t[0] = u; \
if (t[0]) l = 1
#elif ATOMBYTES == 2
#define u32tompz(t,u,l) \
mpz_atom_t t[2]; \
u_int32_t l = 0; \
t[0] = u & MPZ_MASK; u >>= MPZ_SHIFT; \
t[1] = u & MPZ_MASK; u >>= MPZ_SHIFT; \
if (t[1]) l = 1; \
else if (t[0]) l = 2
#elif ATOMBYTES == 1
#define u32tompz(t,u,l) \
mpz_atom_t t[4]; \
u_int32_t l = 0; \
t[0] = u & MPZ_MASK; u >>= MPZ_SHIFT; \
t[1] = u & MPZ_MASK; u >>= MPZ_SHIFT; \
t[2] = u & MPZ_MASK; u >>= MPZ_SHIFT; \
t[3] = u & MPZ_MASK; u >>= MPZ_SHIFT; \
if (t[3]) l = 4; \
else if (t[2]) l = 3; \
else if (t[1]) l = 2; \
else if (t[0]) l = 1
#endif
/* shift/andmask needed to division and modulo operation for ATOMBITS:
* a / ATOMBITS == A >> DIVATOMBITS_SHIFT
* a % ATOMBITS == A & MODATOMBITS_MASK */
#if ATOMBYTES == 4
#define DIVATOMBITS_SHIFT 5
#elif ATOMBYTES == 2
#define DIVATOMBITS_SHIFT 4
#elif ATOMBYTES == 1
#define DIVATOMBITS_SHIFT 3
#endif
#define MODATOMBITS_MASK ((1<<DIVATOMBITS_SHIFT)-1)
#define u32pack(mpz,t,l) \
do { \
(mpz)->l = l; \
(mpz)->a = l; \
(mpz)->s = 0; \
(mpz)->d = t; \
} while(0)
/* Raw inizialization of mpz_t elements */
#define _mpz_raw_init(z, d, l, a, s) \
do { \
(z)->d = d; \
(z)->l = l; \
(z)->a = a; \
(z)->s = s; \
}
#define _mpz_neg(z) \
do { \
(z)->s ^= 1; \
} while(0)
/* ------------------------ debugging macros -------------------------------- */
#define debugprint(m,z) do { \
char *_s = mpz_get_str(NULL, 10, z); \
printf("[%d]%s\n", m, _s); \
free(_s); \
} while(0)
#define debugprint2(m,z) do { \
char *_s = mpz_get_str(NULL, 2, z); \
printf("[%d]%s\n", m, _s); \
free(_s); \
} while(0)
/* ---------------------- initialization/allocation ------------------------- */
/* Initialize a relative bignum.
* return values: none, can't fail */
void mpz_init(mpz_ptr z)
{
z->d = NULL;
z->a = z->l = z->s = 0;
}
/* This function is used every time we need to set the z->d[l] word in the
* z->d array of the mpz_t type. It performs the allocation when
* needed. So if you call it with l = 0, there is anyway at least
* one word allocated. Warning: the normalization inside some function
* relies on this behaviour.
*
* return values:
* SBN_OK on success
* SBN_MEM on out of memory
*
* On error the previous memory configuration and memory of 'z'
* is untouched.
*
* The new words are initialized to zero.
* Note that this function relies on an ANSI-C realloc() that
* acts like free if the 'size' = 0, and return NULL in such a case,
* and also acts like malloc if the ptr = NULL. */
int mpz_realloc(mpz_ptr z, u_int32_t i)
{
void *new;
u_int32_t j;
if (i < z->a)
return SBN_OK;
new = realloc(z->d, (i+1)*MPZ_ATOMSZ);
if (new == NULL)
return SBN_MEM;
z->d = new;
/* set the new words to zero */
for (j = z->a; j <= i; j++)
z->d[j] = 0;
z->a = j; /* j = i+1 here */
return SBN_OK;
}
/* Normalize the length of z, that's to set z->l accordly to the
* most non-zero significant digit. Assume that all the storage
* is initialized to zero (that's a global assuption). */
void mpz_normalize(mpz_ptr z)
{
int32_t j;
if (!z->a)
return;
j = z->a-1;
while(j >= 0) {
if (z->d[j])
break;
j--;
}
z->l = j+1;
if (z->l == 0)
z->s = 0;
}
/* If z == 0, make it positive */
void mpz_normalize_sign(mpz_ptr z)
{
if (z->l == 0)
z->s = 0;
}
/* inline version of mpz_normalize() that assumes z->a > 0 */
#define _mpz_normalize(z) \
do { \
int32_t j = (z)->a-1; \
while(j >=0 && !(z)->d[j]) \
j--; \
(z)->l = j+1; \
} while(0)
/* Free a bignum, can't fail */
void mpz_clear(mpz_ptr z)
{
free(z->d);
}
/* Free a bignum and prepare it to accept up to i+1 digits (base 256)
* Note: not GMP compatible. Don't alter the sign */
int mpz_zero_realloc(mpz_ptr z, u_int32_t i)
{
int err;
if ((err = mpz_realloc(z, i)) != SBN_OK)
return err;
mpz_zero(z);
return SBN_OK;
}
/* raw z = 0
* Note: not GMP compatible. Don't alter the sign */
void mpz_zero(mpz_ptr z)
{
if (!z->l)
return;
memset(z->d, 0, z->l*MPZ_ATOMSZ);
z->l = 0;
}
/* Create a stack-allocated clone of the bignum pointed by 'z' and make
* 'z' pointing to the clone. This is used when the different operators
* of some operations point to the same object. */
#define _mpz_clone_stack(z) \
do { \
mpz_ptr t = alloca(sizeof(mpz_t)); \
t->d = alloca((z)->a*MPZ_ATOMSZ); \
t->s = (z)->s; \
t->l = (z)->l; \
t->a = (z)->a; \
memcpy(t->d, (z)->d, (z)->a*MPZ_ATOMSZ); \
(z) = t; \
} while(0)
/* Clone 'z' using the 'L' atoms pointed by 'D' using stack-allocated memory */
#define _mpz_rawclone_stack(z, D, L) \
do { \
(z)->d = alloca((L)*MPZ_ATOMSZ); \
(z)->l = z->a = (L); \
(z)->s = 0; \
memcpy((z)->d, (D), (L)*MPZ_ATOMSZ); \
} while(0)
/* Create a stack-allocated copy of 'z' in 'r'. 'r' is an mpz_ptr type */
#define _mpz_copy_stack(r, z) \
do { \
r = alloca(sizeof(mpz_t)); \
(r)->d = alloca((z)->a*MPZ_ATOMSZ); \
(r)->s = (z)->s; \
(r)->l = (z)->l; \
(r)->a = (z)->a; \
memcpy((r)->d, (z)->d, (z)->a*MPZ_ATOMSZ); \
} while(0)
/* ----------------------- basic raw operations ----------------------------- */
/* clear the sign flag, so 'z' will be ABS(z) */
#define _mpz_abs(z) \
do { \
(z)->s = 0; \
} while(0)
/* ---------------------------- bits operations ----------------------------- */
/* compute the number of bits needed to rappresent the number 'z' */
u_int32_t mpz_bits(mpz_ptr z)
{
u_int32_t bits = (z->l-1) * ATOMBITS;
mpz_atom_t x = z->d[z->l-1];
while(x) {
bits++;
x >>= 1;
}
return bits;
}
/* Set the bit 'i' in 'z' */
int mpz_setbit(mpz_ptr z, u_int32_t i)
{
u_int32_t atom = i >> DIVATOMBITS_SHIFT;
u_int32_t bit = i & MODATOMBITS_MASK;
int err;
if ((err = mpz_realloc(z, atom)) != SBN_OK)
return err;
z->d[atom] |= (mpz_atom_t) 1 << bit;
if (z->l < atom+1)
z->l = atom+1;
return SBN_OK;
}
/* Inline bit pusher that expects the user know what is doing.
* Used in the division algorithm. */
#define _mpz_setbit(z, i) \
do { \
u_int32_t _atom = (i)>>DIVATOMBITS_SHIFT; \
(z)->d[_atom] |= (mpz_atom_t) 1<<((i)&MODATOMBITS_MASK);\
if ((z)->l < _atom+1) (z)->l = _atom+1; \
} while(0)
/* Faster version without normalization */
#define __mpz_setbit(z, i) \
do { \
u_int32_t _atom = (i)>>DIVATOMBITS_SHIFT; \
(z)->d[_atom] |= (mpz_atom_t) 1<<((i)&MODATOMBITS_MASK);\
} while(0)
/* Clear the bit 'i' in 'z' */
int mpz_clrbit(mpz_ptr z, u_int32_t i)
{
u_int32_t atom = i >> DIVATOMBITS_SHIFT;
u_int32_t bit = i & MODATOMBITS_MASK;
if (atom >= z->l)
return SBN_OK; /* nothing to clear */
z->d[atom] &= ~((mpz_atom_t) 1 << bit);
if (atom == z->l-1)
mpz_normalize(z);
return SBN_OK;
}
/* Fast clear-bit with normalization */
#define _mpz_clrbit(z, i) \
do { \
u_int32_t _atom = (i)>>DIVATOMBITS_SHIFT; \
(z)->d[_atom] &= ~((mpz_atom_t) 1<<((i)&MODATOMBITS_MASK)); \
if (_atom == z->l-1) \
_mpz_normalize(z); \
} while(0)
/* Fast clear-bit without normalization */
#define __mpz_clrbit(z, i) \
do { \
u_int32_t _atom = (i)>>DIVATOMBITS_SHIFT; \
(z)->d[_atom] &= ~((mpz_atom_t) 1<<((i)&MODATOMBITS_MASK));\
} while(0)
/* test the bit 'i' of 'z' and return:
* 0 if the bit 'i' is not set or out of range
* > 0 if the bit 'i' is set */
int mpz_testbit(mpz_ptr z, u_int32_t i)
{
u_int32_t atom = i >> DIVATOMBITS_SHIFT;
u_int32_t bit = i & MODATOMBITS_MASK;
if (atom >= z->l)
return 0;
return (z->d[atom] & ((mpz_atom_t) 1 << bit));
}
/* inline bit tester that expects the user know what is doing.
* It's used in the division algorithm. Return 0 if the bit is set,
* non zero if the bit isn't zet */
#define _mpz_testbit(z, i) \
((z)->d[(i)>>DIVATOMBITS_SHIFT] & ((mpz_atom_t)1<<((i)&MODATOMBITS_MASK)))
/* Return 1 if 'z' is odd, 0 if it's even. */
#define mpz_is_odd(z) (((z)->l) ? ((z)->d[0] & 1) : 0)
/* The same of mpz_odd() but assume there is at least an word allocated */
#define _mpz_is_odd(z) ((z)->d[0] & 1)
#define _mpz_is_even(z) (!_mpz_is_odd(z))
/* -------------------------------- shifting -------------------------------- */
/* Left shift of 'i' words */
int mpz_lshiftword(mpz_ptr r, u_int32_t i)
{
int err;
if (!i)
return SBN_OK;
if ((err = mpz_realloc(r, (r->l+i)-1)) != SBN_OK)
return err;
memmove(r->d+i, r->d, r->l*MPZ_ATOMSZ);
memset(r->d, 0, i*MPZ_ATOMSZ);
r->l += i;
return SBN_OK;
}
/* Right shift of 'i' words */
int mpz_rshiftword(mpz_ptr r, u_int32_t i)
{
if (!i)
return SBN_OK;
if (i >= r->l) {
mpz_zero(r);
return SBN_OK;
}
memmove(r->d, r->d+i, (r->l-i)*MPZ_ATOMSZ);
r->l -= i;
memset(r->d+r->l, 0, i);
return SBN_OK;
}
/* Left shift of 'i' bits */
int mpz_lshift(mpz_ptr r, mpz_ptr z, u_int32_t i)
{
u_int32_t rawshift = i >> DIVATOMBITS_SHIFT;
u_int32_t bitshift = i & MODATOMBITS_MASK;
int32_t j;
mpz_carry_t x;
int err;
/* clone 'z' in 'r' */
if (r != z && ((err = mpz_set(r, z)) != SBN_OK))
return err;
if (rawshift && ((err = mpz_lshiftword(r, rawshift)) != SBN_OK))
return err;
if (!bitshift)
return SBN_OK;
/* We need an additional word */
if ((err = mpz_realloc(r, r->l+1)) != SBN_OK)
return err;
/* note that here we are sure that 'bitshift' <= ATOMBITS */
if (r->l) {
for (j = r->l-1; j >= 0; j--) {
x = (mpz_carry_t) r->d[j] << bitshift;
r->d[j] = x & MPZ_MASK;
r->d[j+1] |= x >> ATOMBITS;
}
if (r->d[r->l])
r->l++;
}
return SBN_OK;
}
/* Fast 'z' 1 bit left shift. Assume there is allocated space for
* an additional atom. Handle normalization */
#define _mpz_self_lshift1(z) \
do { \
int32_t j; \
for (j = (z)->l-1; j >= 0; j--) { \
(z)->d[j+1] |= ((z)->d[j] & (1<<(ATOMBITS-1))) >> (ATOMBITS-1);\
(z)->d[j] <<= 1; \
} \
if ((z)->d[(z)->l]) \
(z)->l++; \
} while(0);
/* Fast 'z' 1 bit left shift + set bit 0 to 'b'. Assume there is allocated
* space for an additional atom. Handle normalization */
#define _mpz_self_lshift1_setbit0(z, b) \
do { \
int32_t j; \
for (j = (z)->l-1; j >= 0; j--) { \
(z)->d[j+1] |= ((z)->d[j] & (1<<(ATOMBITS-1))) >> (ATOMBITS-1);\
(z)->d[j] <<= 1; \
} \
(z)->d[0] |= b; \
if ((z)->d[(z)->l]) \
(z)->l++; \
} while(0);
/* Right shift of 'i' bits */
int mpz_rshift(mpz_ptr r, mpz_ptr z, u_int32_t i)
{
u_int32_t rawshift = i >> DIVATOMBITS_SHIFT;
u_int32_t bitshift = i & MODATOMBITS_MASK;
u_int32_t j;
mpz_carry_t x;
int err;
/* clone 'z' in 'r' */
if (r != z && ((err = mpz_set(r, z)) != SBN_OK))
return err;
if (rawshift && ((err = mpz_rshiftword(r, rawshift)) != SBN_OK))
return err;
if (!bitshift)
return SBN_OK;
/* note that here we are sure that 'bitshift' <= ATOMBITS */
if (r->l) {
r->d[0] >>= bitshift;
for (j = 1; j < r->l; j++) {
x = (mpz_carry_t) r->d[j] << (ATOMBITS-bitshift);
r->d[j] = x >> ATOMBITS;
r->d[j-1] |= x & MPZ_MASK;
}
if (!r->d[r->l-1])
r->l--;
}
return SBN_OK;
}
/* Fast 'z' 1 bit right shift. Handle normalization. Assume z->a != 0
* (so z->d != NULL), that's: don't call it without a reallocation. */
#define _mpz_self_rshift1(z) \
do { \
u_int32_t j; \
(z)->d[0] >>= 1; \
for (j = 1; j < (z)->l; j++) { \
(z)->d[j-1] |= ((z)->d[j] & 1) << (ATOMBITS-1); \
(z)->d[j] >>= 1; \
} \
if (!(z)->d[(z)->l-1]) \
(z)->l--; \
} while(0);
/* -------------------------- bitwise AND OR XOR NOT ------------------------ */
/* 'r' = 'z' bit-AND 'm' */
int mpz_and(mpz_ptr r, mpz_ptr z, mpz_ptr m)
{
int err;
u_int32_t j;
u_int32_t len;
if (z == m) { /* A AND A = A */
mpz_set(r, z);
return SBN_OK;
}
len = MIN(z->l, m->l);
if ((err = mpz_realloc(r, len)) != SBN_OK)
return err;
for (j = 0; j < len; j++)
r->d[j] = z->d[j] & m->d[j];
memset(r->d+j, 0, r->a - j); /* clear not-used words before normalize */
mpz_normalize(r);
return SBN_OK;
}
/* -------------------------------- compare --------------------------------- */
/* The same as mpz_cmpabs() for immediate.
* Relies on the fact that mpz_cmpabs() don't perform any allocation-related
* operation on the second operand. */
int32_t mpz_cmpabsi_raw(mpz_ptr a, mpz_atom_t *d, u_int32_t l)
{
mpz_t b;
b->d = d;
b->l = b->a = l;
b->s = 0;
return mpz_cmpabs(a, b);
}
/* compare ABS('a') and ABS('b'), return values:
* >0 if a > b
* 0 if a == b
* <0 if a < b
*
* 'a->d' and 'b->d' can point to statically allocated memory.
*
* Note that we can't use subtraction to return >0 or <0 if a-b != 0
* since the type for length and atom is unsigned so it may overflow.
*/
int32_t mpz_cmpabs(mpz_ptr a, mpz_ptr b)
{
int32_t i;
if (a->l > b->l) return 1;
if (a->l < b->l) return -1;
i = a->l;
while(i--) {
if (a->d[i] > b->d[i]) return 1;
if (a->d[i] < b->d[i]) return -1;
}
return 0;
}
/* the same as mpz_cmpabs() but 'b' is a 32bit unsigned immediate */
int32_t mpz_cmpabs_ui(mpz_ptr a, u_int32_t u)
{
mpz_t mpz;
u32tompz(t,u,l);
u32pack(mpz,t,l);
return mpz_cmpabs(a, mpz);
}
/* compare 'a' and 'b'. Return values are the same as mpz_cmpabs() */
int32_t mpz_cmp(mpz_ptr a, mpz_ptr b)
{
if (!a->l && !b->l) /* 0 == 0 */
return 0;
if (a->s == b->s) { /* same sign */
if (a->s) return mpz_cmpabs(b,a); /* both negative */
return mpz_cmpabs(a,b); /* both positive */
}
/* one negative, one positive */
if (a->s)
return -1;
return 1;
}
/* The same as mpz_cmp() with unsigned 32bit immediate */
int32_t mpz_cmp_ui(mpz_ptr a, u_int32_t u)
{
mpz_t mpz;
u32tompz(t,u,l);
u32pack(mpz,t,l);
return mpz_cmp(a, mpz);
}
/* signed integer version */
int32_t mpz_cmp_si(mpz_ptr a, int32_t s)
{
mpz_t mpz;
u_int32_t u = (s > 0) ? s : -s;
u32tompz(t,u,l);
u32pack(mpz,t,l);
mpz->s = s < 0;
return mpz_cmp(a, mpz);
}
/* ---------------------------- addition ------------------------------------ */
/* Raw add of immediate, don't care about the sign since
* it's up to the caller */
int mpz_addi_raw(mpz_ptr r, mpz_ptr z, mpz_atom_t *d, u_int32_t l)
{
int err;
u_int32_t maxi = MAX(z->l, l);
mpz_atom_t car = 0;
mpz_carry_t sum;
u_int32_t j;
mpz_atom_t *t = NULL;
if (r->d == d) {
if ((t = malloc(l*MPZ_ATOMSZ)) == NULL)
return SBN_MEM;
memcpy(t, d, l*MPZ_ATOMSZ);
d = t;
}
/* two sum of a,b requires at max MAX(len(a),len(b))+1 bytes */
if (r != z && ((err = mpz_zero_realloc(r, maxi)) != SBN_OK))
return err;
if ((err = mpz_realloc(z, (r == z) ? maxi : l)) != SBN_OK)
return err;
for(j = 0; j < l; j++) {
sum = (mpz_carry_t) d[j] + z->d[j] + car;
car = sum >> MPZ_SHIFT;
sum &= MPZ_MASK;
r->d[j] = sum;
}
for (j = l; j < z->l; j++) {
sum = (mpz_carry_t) z->d[j] + car;
car = sum >> MPZ_SHIFT;
sum &= MPZ_MASK;
r->d[j] = sum;
}
if (car) {
r->d[j] = car;
j++;
}
r->l = j; /* mpz_normalize() not needed */
if (t)
free(t);
return SBN_OK;
}
/* Add 'z' and a 32bit unsigned integer 'u' and put the result in 'r'
* Relies on the ability of mpz_add() to accept the last operator
* statically allocated */
int mpz_add_ui(mpz_ptr r, mpz_ptr z, u_int32_t u)
{
mpz_t mpz;
u32tompz(t,u,l);
u32pack(mpz,t,l);
return mpz_add(r, z, mpz);
}
/* The same as mpz_add_ui but with signed integer */
int mpz_add_si(mpz_ptr r, mpz_ptr z, int32_t s)
{
mpz_t mpz;
u_int32_t u = (s > 0) ? s : -s;
u32tompz(t,u,l);
u32pack(mpz,t,l);
mpz->s = s < 0;
return mpz_add(r, z, mpz);
}
/* 'r' = 'a' + 'b'
* b->d can point to statically allocated data */
int mpz_add(mpz_ptr r, mpz_ptr a, mpz_ptr b)
{
int cmp = mpz_cmpabs(a, b);
int err;
/* both positive or negative */
if (a->s == b->s) {
err = mpz_addi_raw(r, a, b->d, b->l);
r->s = a->s;
return err;
}
/* different signs if we are here */
if (a->s) { /* a negative, b positive */
if (cmp >= 0) { /* a >= b */
err = mpz_subi_raw(r, a, b->d, b->l);
r->s = (r->l == 0) ? 0 : 1; /* negative */
return err;
} else { /* a < b */
err = mpz_subi_raw(r, b, a->d, a->l);
r->s = 0; /* positive */
return err;
}
} else { /* a positive, b negative */
if (cmp >= 0) { /* a >= b */
err = mpz_subi_raw(r, a, b->d, b->l);
r->s = 0; /* positive */
return err;
} else { /* a < b */
err = mpz_subi_raw(r, b, a->d, a->l);
r->s = (r->l == 0) ? 0 : 1; /* negative */
return err;
}
}
return SBN_OK; /* not reached */
}
/* ---------------------------- subtraction --------------------------------- */
/* WARNING: assume z > d */
int mpz_subi_raw(mpz_ptr r, mpz_ptr z, mpz_atom_t *d, u_int32_t l)
{
int err;
mpz_scarry_t sub;
mpz_atom_t car = 0;
u_int32_t j;
mpz_atom_t *t = NULL;
if (r->d == d) {
if ((t = malloc(l*MPZ_ATOMSZ)) == NULL)
return SBN_MEM;
memcpy(t, d, l*MPZ_ATOMSZ);
d = t;
}
if (r != z && ((err = mpz_set(r, z)) != SBN_OK))
return err;
for (j = 0; j < l; j++) {
sub = (mpz_scarry_t) z->d[j] - car - d[j];
car = 0;
if (sub < 0) {
sub += MPZ_BASE;
car = 1;
}
r->d[j] = sub;
}
for (j = l; j < z->l; j++) {
sub = (mpz_scarry_t) z->d[j] - car;
car = 0;
if (sub < 0) {
sub += MPZ_BASE;
car = 1;
}
r->d[j] = sub;
}
r->l = j;
mpz_normalize(r);
if (t)
free(t);
return SBN_OK;
}
/* 'r' = 'a' - 'b'
* b->d can be statically allocated data */
int mpz_sub(mpz_ptr r, mpz_ptr a, mpz_ptr b)
{
int cmp = mpz_cmpabs(a, b);
int err;
/* different signs? */
if (a->s != b->s) {
err = mpz_addi_raw(r, a, b->d, b->l);
r->s = a->s;
return err;
}
/* both positive or negative if we are here */
if (a->s) { /* both negative */
if (cmp >= 0) { /* a >= b */
err = mpz_subi_raw(r, a, b->d, b->l);
r->s = (r->l == 0) ? 0 : 1; /* negative */
return err;
} else { /* a < b */
err = mpz_subi_raw(r, b, a->d, a->l);
r->s = 0; /* positive */
return err;
}
} else { /* both positive */
if (cmp >= 0) { /* a >= b */
err = mpz_subi_raw(r, a, b->d, b->l);
r->s = 0; /* positive */
return err;
} else { /* a < b */
err = mpz_subi_raw(r, b, a->d, a->l);
r->s = (r->l == 0) ? 0 : 1; /* negative */
return err;
}
}
return SBN_OK; /* not reached */
}
/* mpz_sub() with immediate.
* Relies on the fact that mpz_sub() works if the last argument
* is statically allocated */
int mpz_sub_ui(mpz_ptr r, mpz_ptr z, u_int32_t u)
{
mpz_t mpz;
u32tompz(t,u,l);
u32pack(mpz,t,l);
return mpz_sub(r, z, mpz);
}
/* like mpz_sub_ui but with signed integer */
int mpz_sub_si(mpz_ptr r, mpz_ptr z, int32_t s)
{
mpz_t mpz;
u_int32_t u = (s > 0) ? s : -s;
u32tompz(t,u,l);
u32pack(mpz,t,l);
mpz->s = s < 0;
return mpz_sub(r, z, mpz);
}
/* ------------------------------- product ---------------------------------- */
/* Raw multiplication of immediate, don't care about the sign
* since it's up to the caller */
int mpz_muli_raw(mpz_ptr r, mpz_ptr z, mpz_atom_t *d, u_int32_t l)
{
int err;
u_int32_t maxi = z->l+l;
mpz_atom_t car;
mpz_carry_t mul;
u_int32_t j, i;
mpz_t t, rt;
mpz_ptr rbak = NULL;
int tmptarget = (r == z);
mpz_atom_t *x = NULL;
/* Make a copy of 'd' if it's == r */
if (r->d == d) {
if ((x = malloc(l*MPZ_ATOMSZ)) == NULL)
return SBN_MEM;
memcpy(x, d, l*MPZ_ATOMSZ);
d = x;
}
/* if r and z are the same we need a temp bignum target */
if (tmptarget) {
rbak = r;
r = rt;
mpz_init(r);
r->s = rbak->s; /* preserve the original sign */
}
/* two product of a,b requires at max len(a)+len(b) bytes */
if ((err = mpz_zero_realloc(r, maxi)) != SBN_OK)
goto error;
/* initialize the temp var */
mpz_init(t);
if ((err = mpz_realloc(t, maxi)) != SBN_OK)
goto error;
for(j = 0; j < l; j++) {
car = 0;
mpz_zero(t);
for (i = 0; i < z->l; i++) {
/* note that A = B * C + D + E
* with A of N*2 bits and C,D,E of N bits
* can't overflow since:
* (2^N-1)*(2^N-1)+(2^N-1)+(2^N-1) == 2^(2*N)-1 */
mul = (mpz_carry_t) d[j] * z->d[i] + car + r->d[i+j];
car = mul >> MPZ_SHIFT;
mul &= MPZ_MASK;
r->d[i+j] = mul;
}
if (car)
r->d[i+j] = car;
}
r->l = maxi;
mpz_normalize(r);
if (tmptarget && ((err = mpz_set(rbak, rt)) != SBN_OK))
goto error;
err = SBN_OK;
/* fall through */
error:
mpz_clear(t);
if (tmptarget)
mpz_clear(rt);
if (x)
free(x);
return err;
}
/* 'r' = 'z' * 'f' */
int mpz_mul(mpz_ptr r, mpz_ptr z, mpz_ptr f)
{
r->s = z->s^f->s; /* the sign is the xor of the two sings */
return mpz_muli_raw(r, z, f->d, f->l);
}
/* Mul 'z' and a 32bit unsigned integer 'u' and put the result in 'r'
* We don't need to touch the sign since the factor is >= 0 */
int mpz_mul_ui(mpz_ptr r, mpz_ptr z, u_int32_t u)
{
u32tompz(t,u,l);
r->s = z->s;
return mpz_muli_raw(r, z, t, l);
}
/* Like mpz_mul_ui but with signed integer */
int mpz_mul_si(mpz_ptr r, mpz_ptr z, int32_t s)
{
u_int32_t u = (s > 0) ? s : -s;
u32tompz(t,u,l);
r->s = z->s^(s<0);
return mpz_muli_raw(r, z, t, l);
}
/* 'r' = i! */
int mpz_fac_ui(mpz_ptr r, u_int32_t i)
{
u_int32_t j;
int err;
if (!i) {
mpz_setzero(r);
return SBN_OK;
}
if ((err = mpz_set_ui(r, 1)) != SBN_OK)
return err;
for (j = 2; j <= i; j++)
if ((err = mpz_mul_ui(r, r, j)) != SBN_OK)
return err;
return SBN_OK;
}
/* --------------------------- exponentialization --------------------------- */
/* compute b^e mod m.
* Note that there are much faster ways to do it.
* see www.nc.com for more information */
int mpz_powm(mpz_ptr r, mpz_ptr b, mpz_ptr e, mpz_ptr m)
{
int rs = 0, err;