-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigd.c
1256 lines (979 loc) · 25.3 KB
/
bigd.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
/* $Id: bigd.c $ */
/******************** SHORT COPYRIGHT NOTICE**************************
This source code is part of the BigDigits multiple-precision
arithmetic library Version 2.2 originally written by David Ireland,
copyright (c) 2001-8 D.I. Management Services Pty Limited, all rights
reserved. It is provided "as is" with no warranties. You may use
this software under the terms of the full copyright notice
"bigdigitsCopyright.txt" that should have been included with this
library or can be obtained from <www.di-mgt.com.au/bigdigits.html>.
This notice must always be retained in any copy.
******************* END OF COPYRIGHT NOTICE***************************/
/*
Last updated:
$Date: 2008-07-31 12:54:00 $
$Revision: 2.2.0 $
$Author: dai $
*/
/* BIGD "bd" wrapper functions around BigDigits "mp" functions */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "bigd.h"
#include "bigdigits.h"
/* Required for opaque pointers */
#define T BIGD
struct T
{
DIGIT_T *digits; /* Ptr to array of digits, least sig. first */
size_t ndigits; /* No of non-zero significant digits */
size_t maxdigits; /* Max size allocated */
/*int is_signed;*/ /* (for future use) */
};
/*
All these functions MUST make sure that there are always
enough digits before doing anything, and SHOULD reset <ndigits>
afterwards to reflect the final significant size.
-- <maxdigits> is the size allocated (at least one).
-- <ndigits> may be zero.
-- <ndigits> may be too long if MS digits compute to zero so
consider it an upper bound on significant digits, not gospel.
It is an error to pass a NULL BIGD parameter except to bdFree.
*/
#ifdef _DEBUG
static int debug = 0; /* <= change this to > 0 for console debugging */
#else
static int debug = 0; /* <= ALWAYS ZERO */
#endif
/* Useful definitions */
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
T bdNew(void)
{
struct T *p;
p = calloc(1, (long)sizeof(*p));
if (!p)
{
mpFail("bdNew: Failed to calloc memory.");
}
copyright_notice();
/* set up with single zero digit */
p->digits = mpAlloc(1);
p->digits[0] = 0;
p->ndigits = 0;
p->maxdigits = 1;
//p->sign = 0;
return p;
}
void bdFree(T *p)
/* Zeroise and free memory. Set ptr to NULL. */
{
T bd = *p;
if (*p)
{
/* Zeroise them all, just in case */
if (bd->digits)
{
mpSetZero(bd->digits, bd->maxdigits);
free(bd->digits);
}
bd->maxdigits = 0;
bd->ndigits = 0;
free(*p);
}
*p = NULL;
}
static int bd_resize(T b, size_t newsize)
{
/*
Internal fn to re-size a BIGD structure before a calc.
Use carefully!
1. If growing, it allocs more digits and increases maxdigits
2. If shrinking, it decreases ndigits and zeroises the excess.
3. It does not increase b->ndigits; that's up to you later.
4. It does not release excess digits; use bdFree.
In other words, it's like middle-aged spread:
you go from a 32" waist to a 38 but can never go backwards.
Be careful doing the following:-
n = new_size_we_expect;
bd_resize(b, n);
mpFunctionOfSorts(b->digits, n);
b->ndigits = mpSizeof(b->digits, b->ndigits); // NO!
b->ndigits may be set too short
Better:
n = new_size_we_expect;
bd_resize(b, n);
mpFunctionOfSorts(b->digits, n);
b->ndigits = mpSizeof(b->digits, n); // Yes.
*/
size_t i;
/* Check just in case NULL */
assert(b);
assert(b->digits);
/* If we are shrinking, clear high digits */
if (newsize < b->ndigits)
{
for (i = newsize; i < b->ndigits; i++)
b->digits[i] = 0;
b->ndigits = newsize;
return 0;
}
/* We need more room */
if (b->maxdigits < newsize)
{
DIGIT_T *newp, *oldp;
size_t oldsize = b->maxdigits;
/* Increase size of digit array */
//b->digits = (DIGIT_T *)realloc(b->digits, newsize * sizeof(DIGIT_T));
/* -- [v2.2] changed [2008-03-30] to avoid realloc */
newp = (DIGIT_T *)malloc(newsize * sizeof(DIGIT_T));
oldp = b->digits;
/* Check for failure */
if (!newp)
{
mpSetZero(oldp, oldsize);
free(oldp);
mpFail("bd_resize: Failed to realloc memory.");
}
memcpy(newp, oldp, oldsize * sizeof(DIGIT_T));
mpSetZero(oldp, oldsize);
free(oldp);
b->digits = newp;
b->maxdigits = newsize; /* Remember new allocated size */
}
/* Make sure new digits are zero */
for (i = b->ndigits; i < newsize; i++)
b->digits[i] = 0;
return 0;
}
size_t bdConvFromOctets(T b, const unsigned char *c, size_t nbytes)
/* Converts nbytes octets into big digit b, resizing if necessary */
{
size_t ndigits, n;
assert(b);
ndigits = (nbytes + OCTETS_PER_DIGIT - 1) / OCTETS_PER_DIGIT;
bd_resize(b, ndigits);
n = mpConvFromOctets(b->digits, ndigits, c, nbytes);
b->ndigits = mpSizeof(b->digits, n);
return n;
}
size_t bdConvToOctets(T b, unsigned char *c, size_t nbytes)
/* Convert big digit b into string of octets, in big-endian order,
padding to nbytes or truncating if necessary.
Returns # significant bytes.
If c is NULL or nbytes == 0 then just return required size.
*/
{
size_t noctets, nbits, n;
assert(b);
nbits = mpBitLength(b->digits, b->ndigits);
noctets = (nbits + 7) / 8;
/* [2008-05-23] always return at least 1 */
if (0 == noctets) noctets = 1;
if (!c || 0 == nbytes)
{
return noctets;
}
n = mpConvToOctets(b->digits, b->ndigits, c, nbytes);
return noctets;
}
size_t bdConvFromHex(T b, const char *s)
/* Converts a hex string into big digit b */
{
size_t ndigits, n;
assert(b);
/* Revision [2006-02-21] */
/* EDIT: ndigits = (strlen(s) / 2 + OCTETS_PER_DIGIT - 1) / OCTETS_PER_DIGIT; */
ndigits = ((strlen(s) + 1) / 2 + OCTETS_PER_DIGIT - 1) / OCTETS_PER_DIGIT;
bd_resize(b, ndigits);
n = mpConvFromHex(b->digits, ndigits, s);
b->ndigits = mpSizeof(b->digits, n);
return n;
}
size_t bdConvToHex(T b, char *s, size_t smax)
{
assert(b);
return mpConvToHex(b->digits, b->ndigits, s, smax);
}
size_t bdConvFromDecimal(BIGD b, const char *s)
{
size_t ndigits, n;
assert(b);
/* approx size but never too small */
ndigits = (strlen(s) / 2 + OCTETS_PER_DIGIT) / OCTETS_PER_DIGIT;
bd_resize(b, ndigits);
n = mpConvFromDecimal(b->digits, ndigits, s);
b->ndigits = n;
return n;
}
size_t bdConvToDecimal(BIGD b, char *s, size_t smax)
{
assert(b);
return mpConvToDecimal(b->digits, b->ndigits, s, smax);
}
int bdSetShort(T b, bdigit_t value)
/* Converts value into a (single-digit) big digit b */
{
assert(b);
bd_resize(b, 1);
b->digits[0] = (DIGIT_T)value;
b->ndigits = (value ? 1 : 0);
return 0;
}
size_t bdBitLength(T b)
/* Returns base-1 index to most significant bit in b */
{
assert(b);
return mpBitLength(b->digits, b->ndigits);
}
size_t bdSizeof(T b)
/* Returns number of significant non-zero bytes in b */
{
assert(b);
return mpSizeof(b->digits, b->ndigits);
}
/* Print function for bigdigit_t structures */
void bdPrint(T p, size_t flags)
{
size_t n;
assert(p);
n = p->ndigits;
if (n == 0) n = 1;
if (flags & BD_PRINT_TRIM) /* Trim leading zeroes */
{
if (flags & BD_PRINT_NL) /* add newlines */
mpPrintTrimNL(p->digits, n);
else
mpPrintTrim(p->digits, n);
}
else
{
if (flags & BD_PRINT_NL) /* add newlines */
mpPrintNL(p->digits, n);
else
mpPrint(p->digits, n);
}
}
int bdIsEqual(T a, T b)
{
/* Returns true if a == b, else false */
size_t n, na, nb;
assert(a && b);
/* We can't trust ndigits */
na = mpSizeof(a->digits, a->ndigits);
nb = mpSizeof(b->digits, b->ndigits);
if (na != nb)
return FALSE;
if (na == 0 && nb == 0)
return TRUE;
/* Otherwise we have equal lengths */
n = na;
while (n--)
{
if (a->digits[n] != b->digits[n])
return FALSE;
}
return TRUE;
}
int bdIsZero(T a)
/* Returns true if a == 0, else false */
{
assert(a);
return mpIsZero(a->digits, a->ndigits);
}
int bdShortCmp(T a, bdigit_t d)
/* Returns sign of (a-d) */
{
assert(a);
return mpShortCmp(a->digits, d, a->ndigits);
}
int bdCompare(T a, T b)
/* Returns sign of (a-b) */
{
size_t n, na, nb;
assert(a && b);
if (a->ndigits != b->ndigits)
{
na = mpSizeof(a->digits, a->ndigits);
nb = mpSizeof(b->digits, b->ndigits);
if (na > nb) return 1;
if (na < nb) return -1;
n = na;
}
else
n = a->ndigits;
return mpCompare(a->digits, b->digits, n);
}
int bdIsEven(T a)
{
assert(a);
return ISEVEN(a->digits[0]);
}
int bdIsOdd(T a)
{
assert(a);
return ISODD(a->digits[0]);
}
int bdSetEqual(T a, T b)
/* Sets a = b */
{
assert(a && b);
bd_resize(a, b->ndigits);
mpSetEqual(a->digits, b->digits, b->ndigits);
a->ndigits = b->ndigits;
return 0;
}
int bdSetZero(T a)
/* Sets a = 0 */
{
assert(a);
mpSetZero(a->digits, a->ndigits);
a->ndigits = 0;
return 0;
}
int bdShortAdd(T w, T u, bdigit_t d)
/* Compute w = u + d,
returns 1 if we had a carry */
{
DIGIT_T carry;
size_t dig_size = max(u->ndigits, 1);
assert(w && u);
bd_resize(w, dig_size + 1);
carry = mpShortAdd(w->digits, u->digits, d, dig_size);
/* Cope with overflow */
if (carry)
{
w->digits[dig_size] = carry;
w->ndigits = dig_size + 1;
}
else
w->ndigits = dig_size;
return carry;
}
int bdAdd(T w, T u, T v)
/* Compute w = u + v, w#v*/
{
size_t dig_size;
DIGIT_T carry;
assert(w && u && v);
/* Check for cheaper option */
if (v->ndigits == 1)
return bdShortAdd(w, u, v->digits[0]);
/* Make sure u and v are the same size */
dig_size = max(u->ndigits, v->ndigits);
bd_resize(v, dig_size);
bd_resize(u, dig_size);
/* Now make sure w is big enough for sum (incl carry) */
bd_resize(w, dig_size + 1);
/* Finally, do the business */
carry = mpAdd(w->digits, u->digits, v->digits, dig_size);
/* Make sure we've set the right size for w */
if (carry)
{
w->digits[dig_size] = carry;
w->ndigits = dig_size + 1;
}
else
w->ndigits = mpSizeof(w->digits, dig_size);
return carry;
}
int bdAdd_s(T w, T u, T v)
/* Compute w = u + v (safe) */
{
DIGIT_T carry;
T ww;
assert(w && u && v);
/* Use temp */
ww = bdNew();
bdSetEqual(ww, w);
carry = bdAdd(ww, u, v);
bdSetEqual(w, ww);
bdFree(&ww);
return carry;
}
int bdShortSub(T w, T u, bdigit_t d)
/* Compute w = u - d, return borrow */
{
DIGIT_T borrow;
size_t dig_size = max(u->ndigits, 1);
assert(w && u);
bd_resize(w, dig_size);
borrow = mpShortSub(w->digits, u->digits, d, dig_size);
w->ndigits = dig_size;
return borrow;
}
int bdSubtract(T w, T u, T v)
/* Compute w = u - v, return borrow, w#v */
{
size_t dig_size;
DIGIT_T borrow;
assert(w && u && v);
/* Check for cheaper option */
if (v->ndigits == 1)
return bdShortSub(w, u, v->digits[0]);
/* Make sure u and v are the same size */
dig_size = max(u->ndigits, v->ndigits);
bd_resize(v, dig_size);
bd_resize(u, dig_size);
bd_resize(w, dig_size);
/* Finally, do the business */
borrow = mpSubtract(w->digits, u->digits, v->digits, dig_size);
/* Make sure we've set the right size for w */
w->ndigits = mpSizeof(w->digits, dig_size);
return borrow;
}
int bdSubtract_s(T w, T u, T v)
/* Compute w = u - v (safe) */
{
DIGIT_T carry;
T ww;
assert(w && u && v);
/* Use temp */
ww = bdNew();
bdSetEqual(ww, w);
carry = bdSubtract(ww, u, v);
bdSetEqual(w, ww);
bdFree(&ww);
return carry;
}
int bdIncrement(T a)
/* Sets a = a + 1, returns carry */
{
assert(a);
return bdShortAdd(a, a, 1);
}
int bdDecrement(T a)
/* Sets a = a - 1, returns borrow */
{
assert(a);
return bdShortSub(a, a, 1);
}
int bdShortMult(T w, T u, bdigit_t d)
/* Compute w = u * d */
{
DIGIT_T overflow;
size_t dig_size = u->ndigits;
assert(w && u);
bd_resize(w, dig_size+1);
overflow = mpShortMult(w->digits, u->digits, d, dig_size);
/* Cope with overflow */
if (overflow)
{
w->digits[dig_size] = overflow;
w->ndigits = dig_size + 1;
}
else
w->ndigits = mpSizeof(w->digits, dig_size);
return 0;
}
int bdMultiply(T w, T u, T v)
/* Compute w = u * v
-- no overlap permitted
*/
{
size_t dig_size;
assert(w && u && v);
/* Check for cheaper option */
if (v->ndigits == 1)
return bdShortMult(w, u, v->digits[0]);
/* Make sure u and v are the same size */
dig_size = max(u->ndigits, v->ndigits);
bd_resize(v, dig_size);
bd_resize(u, dig_size);
/* Now make sure w is big enough for product */
bd_resize(w, 2 * dig_size);
/* Finally, do the business */
mpMultiply(w->digits, u->digits, v->digits, dig_size);
/* Make sure we've set the right size for w */
w->ndigits = mpSizeof(w->digits, 2 * dig_size);
return 0;
}
int bdMultiply_s(T w, T u, T v)
/* Compute w = u * v (safe) */
{
T ww;
assert(w && u && v);
/* Use temp */
ww = bdNew();
bdSetEqual(ww, w);
bdMultiply(ww, u, v);
bdSetEqual(w, ww);
bdFree(&ww);
return 0;
}
int bdSquare(T w, T x)
/* Computes w = x^2, w#x */
{
size_t dig_size;
assert(w && x);
dig_size = max(x->ndigits, 1);
/* Make sure w is big enough for product */
bd_resize(w, 2 * dig_size);
/* Finally, do the business */
mpSquare(w->digits, x->digits, dig_size);
/* Make sure we've set the right size for w */
w->ndigits = mpSizeof(w->digits, 2 * dig_size);
return 0;
}
int bdSquare_s(T w, T x)
/* Compute w = x^2 (safe) */
{
T ww;
assert(w && x);
/* Use temp */
ww = bdNew();
bdSetEqual(ww, w);
bdSquare(ww, x);
bdSetEqual(w, ww);
bdFree(&ww);
return 0;
}
int bdSqrt(BIGD s, BIGD x)
/* Computes integer square root s = floor(sqrt(x)) */
{
size_t dig_size;
int r;
assert(s && x);
dig_size = x->ndigits;
bd_resize(s, dig_size);
r = mpSqrt(s->digits, x->digits, dig_size);
s->ndigits = mpSizeof(s->digits, dig_size);
return r;
}
int bdShortDiv(T q, T r, T u, bdigit_t d)
/* Computes quotient q = u / d and remainder r = u mod d */
{
DIGIT_T rem;
size_t dig_size;
assert(q && r && u);
dig_size = u->ndigits;
bd_resize(q, dig_size);
rem = mpShortDiv(q->digits, u->digits, d, dig_size);
bdSetShort(r, rem);
q->ndigits = mpSizeof(q->digits, dig_size);
return 0;
}
int bdDivide(T q, T r, T u, T v)
/* Computes quotient q = u / v and remainder r = u mod v
trashes q and r first
*/
{
size_t dig_size;
assert(q && r && u && v);
dig_size = u->ndigits;
bd_resize(q, dig_size);
bd_resize(r, dig_size);
/* Do the business */
mpDivide(q->digits, r->digits, u->digits, dig_size, v->digits, v->ndigits);
/* Set final sizes */
q->ndigits = mpSizeof(q->digits, dig_size);
r->ndigits = mpSizeof(r->digits, dig_size);
return 0;
}
int bdDivide_s(T q, T r, T u, T v)
/* Computes quotient q = u / v and remainder r = u mod v (safe) */
{
size_t dig_size;
BIGD qq, rr;
assert(q && r && u && v);
/* Use temps because mpDivide trashes q and r */
qq = bdNew();
rr = bdNew();
dig_size = u->ndigits;
bd_resize(qq, dig_size);
bd_resize(rr, dig_size);
/* Do the business */
mpDivide(qq->digits, rr->digits, u->digits, dig_size, v->digits, v->ndigits);
/* Copy temps */
qq->ndigits = dig_size;
rr->ndigits = dig_size;
bdSetEqual(q, qq);
bdSetEqual(r, rr);
/* Set final sizes */
q->ndigits = mpSizeof(q->digits, dig_size);
r->ndigits = mpSizeof(r->digits, dig_size);
/* Free temps */
bdFree(&qq);
bdFree(&rr);
return 0;
}
bdigit_t bdShortMod(T r, T u, bdigit_t d)
/* Returns r = u mod d */
{
DIGIT_T rr;
assert(r && u);
rr = mpShortMod(u->digits, d, u->ndigits);
bdSetShort(r, rr);
return rr;
}
int bdModulo(T r, T u, T v)
/* Computes r = u mod v, r#u */
{
size_t nr;
assert(r && u && v);
/* NB r is only vdigits long at most */
nr = v->ndigits;
bd_resize(r, nr);
/* Do the business */
mpModulo(r->digits, u->digits, u->ndigits, v->digits, v->ndigits);
/* Set final size */
r->ndigits = mpSizeof(r->digits, nr);
return 0;
}
int bdModulo_s(T r, T u, T v)
/* Computes r = u mod v (safe) */
{
T rr;
assert(r && u && v);
/* Use temp */
rr = bdNew();
bdSetEqual(rr, r);
bdModulo(rr, u, v);
bdSetEqual(r, rr);
bdFree(&rr);
return 0;
}
int bdSetBit(T a, size_t ibit, int value)
/* Set bit ibit (0..nbits-1) with value 1 or 0
-- increases size if a too small but does not shrink */
{
size_t idigit;
assert(a);
/* Which digit? (0-based) */
idigit = ibit / BITS_PER_DIGIT;
/* Check size */
/* [EDIT v2.1:] change a->maxdigits to a->ndigits */
if (idigit >= a->ndigits)
{
bd_resize(a, idigit+1);
a->ndigits = idigit+1;
}
/* [v2.2] use mp function */
mpSetBit(a->digits, a->ndigits, ibit, value);
/* Set the right size */
a->ndigits = mpSizeof(a->digits, a->ndigits);
return 0;
}
int bdGetBit(T a, size_t ibit)
/* Returns value 1 or 0 of bit ibit (0..nbits-1) */
{
size_t idigit;
assert(a);
/* Which digit? (0-based) */
idigit = ibit / BITS_PER_DIGIT;
/* Check size */
if (idigit >= a->maxdigits)
return 0;
/* [v2.2] use mp function */
return mpGetBit(a->digits, a->ndigits, ibit);
}
void bdShiftLeft(T a, T b, size_t s)
/* Computes a = b << s */
{
/* Increases the size of a if necessary. */
/* [v2.1.0] modified to allow any size of shift */
size_t dig_size = b->ndigits;
assert(a && b);
if (s >= BITS_PER_DIGIT)
dig_size += (s / BITS_PER_DIGIT);
/* Assume overflow */
dig_size++;
/* Make sure both big enough */
bd_resize(a, dig_size);
bd_resize(b, dig_size);
/* Set the final size */
mpShiftLeft(a->digits, b->digits, s, dig_size);
a->ndigits = mpSizeof(a->digits, dig_size);
}
void bdShiftRight(T a, T b, size_t n)
/* Computes a = b >> n */
{
/* Throws away shifted bits */
/* [v2.1.0] modified to allow any size of shift */
size_t dig_size = b->ndigits;
assert(a && b);
bd_resize(a, dig_size);
mpShiftRight(a->digits, b->digits, n, dig_size);
/* Set the final size */
a->ndigits = mpSizeof(a->digits, dig_size);
}
void bdXorBits(T a, T b, T c)
/* Computes bitwise operation a = b XOR c */
{
size_t n;
assert(a && b && c);
/* Make sure all variables are the same size */
n = max(b->ndigits, c->ndigits);
bd_resize(a, n);
bd_resize(b, n);
bd_resize(c, n);
/* Do the business */
mpXorBits(a->digits, b->digits, c->digits, n);
/* Set the final size */
a->ndigits = mpSizeof(a->digits, n);
}
void bdOrBits(T a, T b, T c)
/* Computes bitwise operation a = b OR c */
{
size_t n;
assert(a && b && c);
/* Make sure all variables are the same size */
n = max(b->ndigits, c->ndigits);
bd_resize(a, n);
bd_resize(b, n);
bd_resize(c, n);
/* Do the business */
mpOrBits(a->digits, b->digits, c->digits, n);
/* Set the final size */
a->ndigits = mpSizeof(a->digits, n);
}
void bdAndBits(T a, T b, T c)
/* Computes bitwise operation a = b AND c */
{
size_t n;
assert(a && b && c);
/* Make sure all variables are the same size */
n = max(b->ndigits, c->ndigits);
bd_resize(a, n);
bd_resize(b, n);
bd_resize(c, n);
/* Do the business */
mpAndBits(a->digits, b->digits, c->digits, n);
/* Set the final size */
a->ndigits = mpSizeof(a->digits, n);
}
void bdNotBits(BIGD a, BIGD b)
/* Computes bitwise a = NOT b */
{
size_t n;
assert(a && b);
/* Make sure all variables are the same size */
n = b->ndigits;
bd_resize(a, n);
/* Do the business */
mpNotBits(a->digits, b->digits, n);
/* Set the final size */
a->ndigits = mpSizeof(a->digits, n);
}
void bdModPowerOf2(BIGD a, size_t L)
/* Computes a = a mod 2^L */
{
size_t n;
assert(a);
n = a->ndigits;
/* Do the business */
mpModPowerOf2(a->digits, n, L);
/* Set the final size */
a->ndigits = mpSizeof(a->digits, n);
}
int bdModExp(T y, T x, T e, T m)
{
/* Compute y = x^e mod m
x,e < m */
size_t n;