-
Notifications
You must be signed in to change notification settings - Fork 1
/
bignum.c
4338 lines (3637 loc) · 122 KB
/
bignum.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
/*
* Simplified BSD License
*
* Copyright (c) 1988-1989, Digital Equipment Corporation & INRIA.
* Copyright (c) 1992-2017, Eligis
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This file is an agglomeration of the 3 C files from the parent
* bigz distribution at https://sourceforge.net/projects/bigz/ :
*
* bign.c : the kernel written in pure C (it uses no C library)
* bigz.c : provides an implementation of "unlimited-precision"
* arithmetic for signed integers.
*
*/
#if !defined(_CRT_SECURE_NO_DEPRECATE)
# define _CRT_SECURE_NO_DEPRECATE 1
#endif
#if !defined(_CRT_NONSTDC_NO_DEPRECATE)
# define _CRT_NONSTDC_NO_DEPRECATE 1
#endif
#include <stdlib.h>
#if defined(_WIN64) || defined(HAVE_STDINT_H)
# include <stdint.h>
#endif
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "bignum.h"
#define MaxInt(a, b) (((a) < (b)) ? (b) : (a))
#define AbsInt(x) (((x) >= 0) ? (x) : -(x))
/* bign.c */
/*
* Description of types and constants.
*
* Several conventions are used in the commentary:
* A "BigNum" is the name for an infinite-precision number.
* Capital letters (e.g., "N") are used to refer to the value of BigNums.
* The word "digit" refers to a single BigNum digit.
* The notation "Size(N)" refers to the number of digits in N,
* which is typically passed to the subroutine as "nl".
* The notation "Length(N)" refers to the number of digits in N,
* not including any leading zeros.
* The word "Base" is used for the number 2 ** BN_DIGIT_SIZE, where
* BN_DIGIT_SIZE is the number of bits in a single BigNum digit.
* The expression "BBase(N)" is used for Base ** NumDigits(N).
* The term "leading zeros" refers to any zeros before the most
* significant digit of a number.
*
* In the code, we have:
*
* "nn" is a pointer to a big number,
* "nl" is the number of digits from nn,
* "d" is a digit.
*
*/
static void
BnnDivideHelper(BigNum nn, BigNumLength nl, BigNum dd, BigNumLength dl);
void
BnnSetToZero(BigNum nn, BigNumLength nl) {
/*
* Sets all the specified digits of the BigNum to BN_ZERO (0).
*/
BigNumLength d;
for (d = 0; d < nl; ++d) {
nn[d] = BN_ZERO;
}
}
void
BnnAssign(BigNum mm, const BigNum nn, BigNumLength nl) {
/*
* Copies N => M
*/
int d;
if ((mm < nn) || (mm > (nn + nl))) {
/*
* no memory overlap using classic loop
*/
for (d = 0; d < (int)nl; ++d) {
mm[d] = nn[d];
}
} else if (mm > nn) {
/*
* memory overlap, loop starting from most significant digit
*/
for (d = (int)(nl - 1); d >= 0; --d) {
mm[d] = nn[d];
}
}
}
void
BnnSetDigit(BigNum nn, BigNumDigit d) {
/*
* Sets a single digit of N to the passed value
*/
*nn = d;
}
BigNumDigit
BnnGetDigit(const BigNum nn) {
/*
* Returns the single digit pointed by N
*/
return (*nn);
}
BigNumLength
BnnNumDigits(const BigNum nn, BigNumLength nl) {
/*
* Returns the number of digits of N, not counting leading zeros
*/
int d;
/*
* loop starting from most significant digit
*/
for (d = (int)(nl - 1); d >= 0; --d) {
if (nn[d] != BN_ZERO) {
/*
* length = d+1
*/
return ((BigNumLength)(d + 1));
}
}
return ((BigNumLength)1);
}
BigNumLength
BnnNumLength(const BigNum nn, BigNumLength nl) {
/*
* Returns the number of bits of N, not counting leading zeros
*/
const BigNumDigit d = nn[nl - 1];
int i;
for (i = (int)(BN_DIGIT_SIZE - 1); i >= 0; --i) {
if ((d & (BN_ONE << (BigNumLength)i)) != 0) {
return ((BigNumLength)(((nl-1) * BN_DIGIT_SIZE) + i+1));
}
}
return (0);
}
BigNumLength
BnnNumCount(const BigNum nn, BigNumLength nl) {
/*
* Returns the count of bits set of N.
*/
BigNumLength count = 0;
int j;
for (j = 0; j < (int)nl; ++j) {
const BigNumDigit d = nn[j];
int i;
for (i = (int)(BN_DIGIT_SIZE - 1); i >= 0; --i) {
if ((d & (BN_ONE << (BigNumLength)i)) != 0) {
++count;
}
}
}
return (count);
}
BigNumLength
BnnNumLeadingZeroBitsInDigit(BigNumDigit d) {
/*
* Returns the number of leading zero bits in a digit
*/
BigNumDigit mask = (BigNumDigit)(BN_ONE << (BN_DIGIT_SIZE - 1));
BigNumLength p;
if (d == BN_ZERO) {
return ((BigNumLength)BN_DIGIT_SIZE);
}
for (p = 0; (d & mask) == 0; ++p) {
mask >>= 1;
}
return (p);
}
BigNumBool
BnnIsPower2(const BigNum nn, BigNumLength nl) {
/*
* Returns BN_TRUE iff nn is a power of 2.
*/
BigNumLength i;
BigNumLength nbits;
BigNumDigit d;
/*
* The n-1 digits must be 0
*/
for (i = 0; i < (nl - 1); ++i) {
if (nn[i] != BN_ZERO) {
return (BN_FALSE);
}
}
/*
* There must be only 1 bit set on the last Digit.
*/
d = nn[i];
nbits = 0;
for (i = 0; i < (BigNumLength)BN_DIGIT_SIZE; ++i) {
if ((d & (BN_ONE << i)) != 0) {
if (nbits++ > 0) {
/*
* More than two digits.
*/
return (BN_FALSE);
}
}
}
return (BN_TRUE);
}
BigNumBool
BnnIsDigitZero(BigNumDigit d) {
/*
* Returns BN_TRUE iff digit = 0
*/
return ((BigNumBool)(d == 0));
}
BigNumBool
BnnIsDigitNormalized(BigNumDigit d) {
/*
* Returns BN_TRUE iff Base/2 <= digit < Base
* i.e. if digit's leading bit is 1
*/
if ((d & (BN_ONE << (BN_DIGIT_SIZE - 1))) != 0) {
return (BN_TRUE);
} else {
return (BN_FALSE);
}
}
BigNumBool
BnnIsDigitOdd(BigNumDigit d) {
/*
* Returns BN_TRUE iff digit is odd
*/
if ((d & 1) != 0) {
return (BN_TRUE);
} else {
return (BN_FALSE);
}
}
BigNumBool
BnnIsDigitEven(BigNumDigit d) {
/*
* Returns BN_TRUE iff digit is even
*/
if ((d & 1) == 0) {
return (BN_TRUE);
} else {
return (BN_FALSE);
}
}
BigNumCmp
BnnCompareDigits(BigNumDigit d1, BigNumDigit d2) {
/*
* Returns BN_GT if digit1 > digit2
* BN_EQ if digit1 = digit2
* BN_LT if digit1 < digit2
*/
return ((BigNumCmp)((d1 > d2) ? BN_GT : (d1 == d2 ? BN_EQ : BN_LT)));
}
void
BnnComplement(BigNum nn, BigNumLength nl) {
/*
* Performs the computation BBase(N) - N - 1 => N
*/
BigNumLength d;
for (d = 0; d < nl; ++d) {
nn[d] ^= BN_COMPLEMENT;
}
}
void
BnnComplement2(BigNum nn, BigNumLength nl) {
/*
* Performs the computation neg(N) => N
*/
BigNumDigit one = BN_ONE;
/*
* Initialize constants
*/
BnnComplement(nn, nl);
(void)BnnAdd(nn, nl, &one, (BigNumLength)1, BN_NOCARRY);
}
void
BnnAndDigits(BigNum n, BigNumDigit d) {
/*
* Returns the logical computation n[0] AND d in n[0]
*/
*n &= d;
}
void
BnnOrDigits(BigNum n, BigNumDigit d) {
/*
* Returns the logical computation n[0] OR d2 in n[0].
*/
*n |= d;
}
void
BnnXorDigits(BigNum n, BigNumDigit d) {
/*
* Returns the logical computation n[0] XOR d in n[0].
*/
*n ^= d;
}
/*
* Shift operations
*/
BigNumDigit
BnnShiftLeft(BigNum mm, BigNumLength ml, BigNumLength nbits) {
/*
* Shifts M left by "nbits", filling with 0s. Returns the
* leftmost "nbits" of M in a digit. Assumes 0 <= nbits <
* BN_DIGIT_SIZE.
*/
BigNumDigit res = BN_ZERO;
if (nbits != 0) {
BigNumLength rnbits = (BigNumLength)(BN_DIGIT_SIZE - nbits);
BigNumLength evenlen = (ml & ~(BigNumLength)1);
BigNumLength d;
/*
* Loop is now unrooled two BigNumDigit at a time.
*/
for (d = 0; d < evenlen; ++d) {
BigNumDigit save0;
BigNumDigit save1;
save0 = mm[d];
mm[d] = (save0 << nbits) | res;
save1 = mm[++d];
mm[d] = (save1 << nbits) | (save0 >> rnbits);
res = save1 >> rnbits;
}
if (ml != evenlen) {
BigNumDigit save = mm[d];
mm[d] = (save << nbits) | res;
res = save >> rnbits;
}
}
return (res);
}
BigNumDigit
BnnShiftRight(BigNum mm, BigNumLength ml, BigNumLength nbits) {
/*
* Shifts M right by "nbits", filling with 0s. Returns the
* rightmost "nbits" of M in a digit. Assumes 0 <= nbits <
* BN_DIGIT_SIZE.
*/
BigNumDigit res = BN_ZERO;
if (nbits != 0) {
const BigNumLength lnbits = (BigNumLength)BN_DIGIT_SIZE - nbits;
/*
* loop starting from most significant digit
*/
if ((ml & (BigNumLength)1) != (BigNumLength)0) {
/*
* Odd number of digits, start with most significant
* digit, then loop on even number of digits.
*/
BigNumDigit save = mm[--ml];
mm[ml] = (save >> nbits); /* res==0, no need to | res */
res = save << lnbits;
}
/*
* Loop is now unrooled two digits at a time.
*/
while (ml != (BigNumLength)0) {
BigNumDigit save0;
BigNumDigit save1;
save0 = mm[--ml];
mm[ml] = (save0 >> nbits) | res;
save1 = mm[--ml];
mm[ml] = (save1 >> nbits) | (save0 << lnbits);
res = save1 << lnbits;
}
}
return (res);
}
/*
* Additions
*/
BigNumCarry
BnnAddCarry(BigNum nn, BigNumLength nl, BigNumCarry carryin) {
/*
* Performs the sum N + CarryIn => N. Returns the CarryOut.
*/
if (carryin == BN_NOCARRY) {
return (BN_NOCARRY);
} else if (nl == 0) {
return (BN_CARRY);
} else {
BigNumLength d;
for (d = 0; d < nl; ++d) {
if (++nn[d] != 0) {
return (BN_NOCARRY);
}
}
return (BN_CARRY);
}
}
BigNumCarry
BnnAdd(BigNum mm,
BigNumLength ml,
const BigNum nn,
BigNumLength nl,
BigNumCarry carryin) {
/*
* Performs the sum M + N + CarryIn => M. Returns the CarryOut.
* Assumes Size(M) >= Size(N).
*/
BigNumProduct c = (BigNumProduct)carryin;
BigNumLength i;
for (i = 0; i < nl; ++i) {
BigNumProduct save = (BigNumProduct)*mm;
c += save;
if (c < save) {
*(mm++) = nn[i];
c = (BigNumProduct)1;
} else {
save = (BigNumProduct)nn[i];
c += save;
*(mm++) = (BigNumDigit)c;
c = (BigNumProduct)((c < save) ? 1 : 0);
}
}
return (BnnAddCarry(mm, ml-nl, ((c == 0) ? BN_NOCARRY : BN_CARRY)));
}
/*
* Subtraction
*/
BigNumCarry
BnnSubtractBorrow(BigNum nn, BigNumLength nl, BigNumCarry carryin) {
/*
* Performs the difference N + CarryIn - 1 => N.
* Returns the CarryOut.
*/
if (carryin == BN_CARRY) {
return (BN_CARRY);
} else if (nl == 0) {
return (BN_NOCARRY);
} else {
BigNumLength d;
for (d = 0; d < nl; ++d) {
if (nn[d]-- != 0) {
return (BN_CARRY);
}
}
return (BN_NOCARRY);
}
}
BigNumCarry
BnnSubtract(BigNum mm,
BigNumLength ml,
const BigNum nn,
BigNumLength nl,
BigNumCarry carryin) {
/*
* Performs the difference M - N + CarryIn - 1 => M.
* Returns the CarryOut. Assumes Size(M) >= Size(N).
*/
BigNumProduct c = (BigNumProduct)((carryin == BN_CARRY) ? 1 : 0);
BigNumDigit invn;
BigNumProduct save;
BigNumLength i;
for (i = 0; i < nl; ++i) {
save = (BigNumProduct)*mm;
invn = nn[i] ^ BN_COMPLEMENT;
c += save;
if (c < save) {
*(mm++) = invn;
c = (BigNumProduct)1;
} else {
c += invn;
*(mm++) = (BigNumDigit)c;
c = (BigNumProduct)((c < invn) ? 1 : 0);
}
}
if (c == 0) {
return (BnnSubtractBorrow(mm, ml-nl, BN_NOCARRY));
} else {
return (BnnSubtractBorrow(mm, ml-nl, BN_CARRY));
}
}
/*
* Multiplication
*/
#define LOW(x) (BigNumDigit)(x & ((BN_ONE << (BN_DIGIT_SIZE / 2)) - 1))
#define HIGH(x) (BigNumDigit)(x >> (BN_DIGIT_SIZE / 2))
#define L2H(x) (BigNumDigit)(x << (BN_DIGIT_SIZE / 2))
#define UPDATE_S(c, V, X3) c += V; X3 += (int)(c < V);
BigNumCarry
BnnMultiplyDigit(BigNum pp,
BigNumLength pl,
const BigNum mm,
BigNumLength ml,
BigNumDigit d) {
/*
* Performs the product:
* Q = P + M * d
* BB = BBase(P)
* Q mod BB => P
* Q div BB => CarryOut
* Returns the CarryOut.
* Assumes Size(P) >= Size(M) + 1.
*/
BigNumLength i;
BigNumProduct c = 0;
BigNumDigit save;
if (d == BN_ZERO) {
return (BN_NOCARRY);
}
if (d == BN_ONE) {
return (BnnAdd(pp, pl, mm, ml, BN_NOCARRY));
}
for (i = 0; i < ml; ++i) {
BigNumDigit Lm;
BigNumDigit Hm;
BigNumDigit Ld;
BigNumDigit Hd;
BigNumDigit X0;
BigNumDigit X1;
BigNumDigit X2;
BigNumDigit X3;
Ld = LOW(d);
Hd = HIGH(d);
Lm = LOW(mm[i]);
Hm = HIGH(mm[i]);
X0 = Ld * Lm;
X1 = Ld * Hm;
X2 = Hd * Lm;
X3 = Hd * Hm;
UPDATE_S(c, X0, X3);
UPDATE_S(c, L2H(X1), X3);
UPDATE_S(c, L2H(X2), X3);
UPDATE_S(c, *pp, X3);
--pl;
*(pp++) = (BigNumDigit)c;
c = X3 + HIGH(X1) + HIGH(X2);
}
if (pl == 0) {
return (BN_NOCARRY);
}
save = *pp;
c += save;
*pp = (BigNumDigit)c;
if (c >= save) {
return (BN_NOCARRY);
}
++pp;
--pl;
while (pl != 0 && (++(*pp++)) == 0) {
pl--;
}
return ((pl != 0) ? BN_NOCARRY : BN_CARRY);
}
/*
* Division
*/
/* xh:xl -= yh:yl */
#define SUB(xh, xl, yh, yl) \
xh -= yh + (int)(yl > xl); \
xl -= yl;
BigNumDigit
BnnDivideDigit(BigNum qq, BigNum nn, BigNumLength nl, BigNumDigit d) {
/*
* Performs the quotient: N div d => Q
* Returns R = N mod d
* Assumes leading digit of N < d, and d > 0.
*/
BigNumLength k;
BigNumLength orig_nl;
BigNumDigit rh; /* Two halves of current remainder */
BigNumDigit rl; /* Correspond to quad above */
BigNumDigit ph;
BigNumDigit pl; /* product of c and qa */
BigNumDigit ch;
BigNumDigit cl;
BigNumDigit prev_qq;
/*
* Normalize divisor
*/
k = BnnNumLeadingZeroBitsInDigit(d);
if (k != 0) {
prev_qq = qq[-1];
orig_nl = nl;
d <<= k;
(void)BnnShiftLeft(nn, nl, k);
} else {
prev_qq = 0;
orig_nl = 0;
}
nn += nl;
nl--;
qq += nl;
ch = HIGH(d);
cl = LOW(d);
/*
* At this point ch can't be == 0; d has been shifted by k
* (the number of leading 0).
*/
rl = *(--nn);
while (nl-- != 0) {
BigNumDigit qa; /* Current appr. to quotient */
rh = rl;
rl = *(--nn);
qa = rh / ch; /* appr. quotient */
/*
* Compute ph, pl
*/
pl = cl * qa;
ph = ch * qa;
ph += HIGH(pl);
pl = L2H(pl);
/*
* While ph:pl > rh:rl, decrement qa, adjust qh:ql
*/
while ((ph > rh) || ((ph == rh) && (pl > rl))) {
qa--;
SUB(ph, pl, ch, L2H(cl));
}
SUB(rh, rl, ph, pl);
/*
* Top half of quotient is correct; save it
*/
*(--qq) = L2H(qa);
qa = (L2H(rh) | HIGH(rl)) / ch;
/*
* Approx low half of q. Compute ph, pl, again
*/
pl = cl * qa;
ph = ch * qa;
ph += HIGH(pl);
pl = LOW(pl) | L2H(LOW(ph));
ph = HIGH(ph);
/*
* While ph:pl > rh:rl, decrement qa, adjust qh:ql
*/
while ((ph > rh) || ((ph == rh) && (pl > rl))) {
qa--;
SUB(ph, pl, 0, d);
}
/*
* Subtract ph:pl from rh:rl; we know rh will be 0
*/
rl -= pl;
*qq |= qa;
}
/*
* Denormalize dividend
*/
if (k != 0) {
if ((qq > nn) && (qq < &nn[orig_nl])) {
/*
* Overlap between qq and nn. Care of *qq!
*/
orig_nl = (BigNumLength)(qq - nn);
(void)BnnShiftRight(nn, orig_nl, k);
nn[orig_nl - 1] = prev_qq;
} else if (qq == nn) {
(void)BnnShiftRight(&nn[orig_nl-1], (BigNumLength)1, k);
} else {
(void)BnnShiftRight(nn, orig_nl, k);
}
}
return (rl >> k);
}
BigNumBool
BnnIsZero(const BigNum nn, BigNumLength nl) {
/*
* Returns BN_TRUE iff N = 0
*/
if ((BnnNumDigits(nn, nl) == (BigNumLength)1)
&& (nl == 0 || BnnIsDigitZero(*nn) != BN_FALSE)) {
return (BN_TRUE);
} else {
return (BN_FALSE);
}
}
BigNumCarry
BnnMultiply(BigNum pp,
BigNumLength pl,
const BigNum mm,
BigNumLength ml,
const BigNum nn,
BigNumLength nl) {
/*
* Performs the product:
* Q = P + M * N
* BB = BBase(P)
* Q mod BB => P
* Q div BB => CarryOut
*
* Returns the CarryOut.
*
* Assumes:
* Size(P) >= Size(M) + Size(N),
* Size(M) >= Size(N).
*/
BigNumLength i;
BigNumCarry c = BN_NOCARRY;
/*
* Multiply one digit at a time
*/
for (i = 0; i < nl; ++i) {
if (BnnMultiplyDigit(&pp[i], pl--, mm, ml, nn[i]) == BN_CARRY) {
c = BN_CARRY;
}
}
return (c);
}
#define BNN_COMPARE_DIGITS(d1, d2) (d1 == d2)
static void
BnnDivideHelper(BigNum nn, BigNumLength nl, BigNum dd, BigNumLength dl) {
/*
* In-place division.
*
* Input (N has been EXTENDED by 1 PLACE; D is normalized):
* +-----------------------------------------------+----+
* | N EXT|
* +-----------------------------------------------+----+
*
* +-------------------------------+
* | D 1|
* +-------------------------------+
*
* Output (in place of N):
* +-------------------------------+---------------+----+
* | R | Q |
* +-------------------------------+---------------+----+
*
* Assumes:
* N > D
* Size(N) > Size(D)
* last digit of N < last digit of D
* D is normalized (Base/2 <= last digit of D < Base)
*/
BigNumDigit DDigit;
BigNumDigit BaseMinus1;
BigNumDigit QApp;
BigNumLength ni;
/*
* Initialize constants
*/
/*
* BaseMinus1 = BN_COMPLEMENT;
* ->
* BnnSetDigit(&BaseMinus1, BN_ZERO);
* BnnComplement(&BaseMinus1, (BigNumLength)1);
*/
BaseMinus1 = BN_COMPLEMENT;
/*
* Save the most significant digit of D
*/
DDigit = BN_ZERO;
BnnAssign(&DDigit, dd + dl - 1, (BigNumLength)1);
/*
* Replace D by Base - D
*/
BnnComplement(dd, dl);
(void)BnnAddCarry(dd, dl, BN_CARRY);
/*
* For each digit of the divisor, from most significant to least:
*/
QApp = BN_ZERO;
nl += 1;
ni = nl - dl;
while (ni != 0) {
/*
* Compute the approximate quotient
*/
ni--;
nl--;
/*
* If first digits of numerator and denominator are the same,
*/
if (BNN_COMPARE_DIGITS(*(nn + nl), DDigit)) {
/*
* Use "Base - 1" for the approximate quotient
*/
BnnAssign(&QApp, &BaseMinus1, (BigNumLength)1);
} else {
/*
* Divide the first 2 digits of N by the
* first digit of D
*/
(void)BnnDivideDigit(&QApp,
nn + nl - 1,
(BigNumLength)2,
DDigit);
}
/*
* Compute the remainder
*/
(void)BnnMultiplyDigit(nn + ni, dl + 1, dd, dl, QApp);
/*
* Correct the approximate quotient, in case it was too large
*/
while (!BNN_COMPARE_DIGITS(*(nn + nl), QApp)) {
/*
* Subtract D from N
*/
(void)BnnSubtract(nn+ni, dl + 1, dd, dl, BN_CARRY);
/*
* Q -= 1
*/
(void)BnnSubtractBorrow(&QApp,
(BigNumLength)1,