-
Notifications
You must be signed in to change notification settings - Fork 9
/
mpi.c
3156 lines (2280 loc) · 64.1 KB
/
mpi.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
/*
mpi.c
by Michael J. Fromberger <http://www.dartmouth.edu/~sting/>
Copyright (C) 1998 Michael J. Fromberger, All Rights Reserved
Arbitrary precision integer arithmetic library
$Id: mpi.c,v 1.1 2004/02/08 04:29:29 sting Exp $
*/
#include "mpi.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#if MP_DEBUG
#include <stdio.h>
#define DIAG(T,V) {fprintf(stderr,T);mp_print(V,stderr);fputc('\n',stderr);}
#else
#define DIAG(T,V)
#endif
/*
If MP_LOGTAB is not defined, use the math library to compute the
logarithms on the fly. Otherwise, use the static table below.
Pick which works best for your system.
*/
#if MP_LOGTAB
/* {{{ s_logv_2[] - log table for 2 in various bases */
/*
A table of the logs of 2 for various bases (the 0 and 1 entries of
this table are meaningless and should not be referenced).
This table is used to compute output lengths for the mp_toradix()
function. Since a number n in radix r takes up about log_r(n)
digits, we estimate the output size by taking the least integer
greater than log_r(n), where:
log_r(n) = log_2(n) * log_r(2)
This table, therefore, is a table of log_r(2) for 2 <= r <= 36,
which are the output bases supported.
*/
#include "logtab.h"
/* }}} */
#define LOG_V_2(R) s_logv_2[(R)]
#else
#include <math.h>
#define LOG_V_2(R) (log(2.0)/log(R))
#endif
/* Default precision for newly created mp_int's */
static unsigned int s_mp_defprec = MP_DEFPREC;
/* {{{ Digit arithmetic macros */
/*
When adding and multiplying digits, the results can be larger than
can be contained in an mp_digit. Thus, an mp_word is used. These
macros mask off the upper and lower digits of the mp_word (the
mp_word may be more than 2 mp_digits wide, but we only concern
ourselves with the low-order 2 mp_digits)
If your mp_word DOES have more than 2 mp_digits, you need to
uncomment the first line, and comment out the second.
*/
/* #define CARRYOUT(W) (((W)>>DIGIT_BIT)&MP_DIGIT_MAX) */
#define CARRYOUT(W) ((W)>>DIGIT_BIT)
#define ACCUM(W) ((W)&MP_DIGIT_MAX)
/* }}} */
/* {{{ Comparison constants */
#define MP_LT -1
#define MP_EQ 0
#define MP_GT 1
/* }}} */
/* {{{ Constant strings */
/* Constant strings returned by mp_strerror() */
static const char *mp_err_string[] = {
"unknown result code", /* say what? */
"boolean true", /* MP_OKAY, MP_YES */
"boolean false", /* MP_NO */
"out of memory", /* MP_MEM */
"argument out of range", /* MP_RANGE */
"invalid input parameter", /* MP_BADARG */
"result is undefined" /* MP_UNDEF */
};
/* Value to digit maps for radix conversion */
/* s_dmap_1 - standard digits and letters */
static const char *s_dmap_1 =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
#if 0
/* s_dmap_2 - base64 ordering for digits */
static const char *s_dmap_2 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#endif
/* }}} */
/* {{{ Static function declarations */
/*
If MP_MACRO is false, these will be defined as actual functions;
otherwise, suitable macro definitions will be used. This works
around the fact that ANSI C89 doesn't support an 'inline' keyword
(although I hear C9x will ... about bloody time). At present, the
macro definitions are identical to the function bodies, but they'll
expand in place, instead of generating a function call.
I chose these particular functions to be made into macros because
some profiling showed they are called a lot on a typical workload,
and yet they are primarily housekeeping.
*/
#if MP_MACRO == 0
void s_mp_setz(mp_digit *dp, mp_size count); /* zero digits */
void s_mp_copy(mp_digit *sp, mp_digit *dp, mp_size count); /* copy */
void *s_mp_alloc(size_t nb, size_t ni); /* general allocator */
void s_mp_free(void *ptr); /* general free function */
#else
/* Even if these are defined as macros, we need to respect the settings
of the MP_MEMSET and MP_MEMCPY configuration options...
*/
#if MP_MEMSET == 0
#define s_mp_setz(dp, count) \
{int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=0;}
#else
#define s_mp_setz(dp, count) memset(dp, 0, (count) * sizeof(mp_digit))
#endif /* MP_MEMSET */
#if MP_MEMCPY == 0
#define s_mp_copy(sp, dp, count) \
{int ix;for(ix=0;ix<(count);ix++)(dp)[ix]=(sp)[ix];}
#else
#define s_mp_copy(sp, dp, count) memcpy(dp, sp, (count) * sizeof(mp_digit))
#endif /* MP_MEMCPY */
#define s_mp_alloc(nb, ni) calloc(nb, ni)
#define s_mp_free(ptr) {if(ptr) free(ptr);}
#endif /* MP_MACRO */
mp_err s_mp_grow(mp_int *mp, mp_size min, heap_t *hp); /* increase allocated size */
mp_err s_mp_pad(mp_int *mp, mp_size min, heap_t *hp); /* left pad with zeroes */
void s_mp_clamp(mp_int *mp); /* clip leading zeroes */
void s_mp_exch(mp_int *a, mp_int *b); /* swap a and b in place */
mp_err s_mp_lshd(mp_int *mp, mp_size p, heap_t *hp); /* left-shift by p digits */
void s_mp_rshd(mp_int *mp, mp_size p); /* right-shift by p digits */
void s_mp_div_2d(mp_int *mp, mp_digit d); /* divide by 2^d in place */
void s_mp_mod_2d(mp_int *mp, mp_digit d); /* modulo 2^d in place */
mp_err s_mp_mul_2d(mp_int *mp, mp_digit d, heap_t *hp); /* multiply by 2^d in place*/
void s_mp_div_2(mp_int *mp); /* divide by 2 in place */
mp_err s_mp_mul_2(mp_int *mp, heap_t *hp); /* multiply by 2 in place */
mp_digit s_mp_norm(mp_int *a, mp_int *b, heap_t *hp); /* normalize for division */
mp_err s_mp_add_d(mp_int *mp, mp_digit d, heap_t *hp); /* unsigned digit addition */
mp_err s_mp_sub_d(mp_int *mp, mp_digit d); /* unsigned digit subtract */
mp_err s_mp_mul_d(mp_int *mp, mp_digit d, heap_t *hp); /* unsigned digit multiply */
mp_err s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r, heap_t *hp);
/* unsigned digit divide */
mp_err s_mp_add(mp_int *a, mp_int *b, heap_t *hp); /* magnitude addition */
mp_err s_mp_sub(mp_int *a, mp_int *b); /* magnitude subtract */
mp_err s_mp_mul(mp_int *a, mp_int *b, heap_t *hp); /* magnitude multiply */
#if 0
void s_mp_kmul(mp_digit *a, mp_digit *b, mp_digit *out, mp_size len);
/* multiply buffers in place */
#endif
#if MP_SQUARE
mp_err s_mp_sqr(mp_int *a); /* magnitude square */
#else
#define s_mp_sqr(a, hp) s_mp_mul(a, a, hp)
#endif
mp_err s_mp_div(mp_int *a, mp_int *b, heap_t *hp); /* magnitude divide */
mp_err s_mp_2expt(mp_int *a, mp_digit k, heap_t *hp); /* a = 2^k */
int s_mp_cmp(mp_int *a, mp_int *b); /* magnitude comparison */
int s_mp_cmp_d(mp_int *a, mp_digit d); /* magnitude digit compare */
int s_mp_ispow2(mp_int *v); /* is v a power of 2? */
int s_mp_ispow2d(mp_digit d); /* is d a power of 2? */
int s_mp_tovalue(char ch, int r); /* convert ch to value */
char s_mp_todigit(int val, int r, int low); /* convert val to digit */
int s_mp_outlen(int bits, int r); /* output length in bytes */
/* }}} */
/* mp_int representation as terms */
// result is_int or is_bignum
term_t mp_to_term(mp_int ma)
{
if (USED(&ma) <= 2)
{
int z = (int)mp_get_int(&ma);
if (fits_int(z))
return tag_int(z);
}
return tag_bignum(ma);
}
// t is_int or is_bignum
mp_int term_to_mp(term_t t, heap_t *hp)
{
mp_int mp;
assert(is_int(t) || is_bignum(t));
if (is_int(t))
{
mp_init_size(&mp, 2, hp);
mp_set_int(&mp, int_value(t), hp);
}
else
mp = (mp_int)peel(t);
return mp;
}
term_t int_to_term(long z, heap_t *hp)
{
if (fits_int(z))
return tag_int((int)z);
else
{
mp_int mp;
mp_init_size(&mp, 4, hp);
mp_set_int(&mp, z, hp);
return tag_bignum(mp);
}
}
mp_int bignum_to_mp(term_t b)
{
assert(is_bignum(b));
return (mp_int)peel(b);
}
double term_to_double(term_t t)
{
mp_int ma;
assert(is_int(t) || is_bignum(t) || is_float(t));
if (is_int(t))
return (double)int_value(t);
if (is_float(t))
return float_value(t);
//is_bignum
ma = bignum_to_mp(t);
return mp_get_double(&ma);
}
/* {{{ Default precision manipulation */
unsigned int mp_get_prec(void)
{
return s_mp_defprec;
} /* end mp_get_prec() */
void mp_set_prec(unsigned int prec)
{
if(prec == 0)
s_mp_defprec = MP_DEFPREC;
else
s_mp_defprec = prec;
} /* end mp_set_prec() */
/* }}} */
/*------------------------------------------------------------------------*/
/* {{{ mp_init_size(mp, prec) */
/*
mp_init_size(mp, prec)
Initialize a new zero-valued mp_int with at least the given
precision; returns MP_OKAY if successful, or MP_MEM if memory could
not be allocated for the structure.
*/
mp_err mp_init_size(mp_int *mp, mp_size prec, heap_t *hp)
{
ARGCHK(mp != NULL && prec > 0, MP_BADARG);
*mp = bignum_to_mp(heap_bignum0(hp, MP_ZPOS, prec));
return MP_OKAY;
} /* end mp_init_size() */
/* }}} */
/* {{{ mp_init_copy(mp, from) */
/*
mp_init_copy(mp, from)
Initialize mp as an exact copy of from. Returns MP_OKAY if
successful, MP_MEM if memory could not be allocated for the new
structure.
*/
mp_err mp_init_copy(mp_int *mp, mp_int *from, heap_t *hp)
{
ARGCHK(mp != NULL && from != NULL, MP_BADARG);
if(mp == from)
return MP_OKAY;
*mp = bignum_to_mp(heap_bignum(hp, SIGN(from), USED(from), DIGITS(from)));
return MP_OKAY;
} /* end mp_init_copy() */
/* }}} */
/* {{{ mp_copy(from, to) */
/*
mp_copy(from, to)
Copies the mp_int 'from' to the mp_int 'to'. It is presumed that
'to' has already been initialized (if not, use mp_init_copy()
instead). If 'from' and 'to' are identical, nothing happens.
*/
mp_err mp_copy(mp_int *from, mp_int *to, heap_t *hp)
{
ARGCHK(from != NULL && to != NULL, MP_BADARG);
if(from == to)
return MP_OKAY;
{ /* copy */
/*
If the allocated buffer in 'to' already has enough space to hold
all the used digits of 'from', we'll re-use it to avoid hitting
the memory allocater more than necessary; otherwise, we'd have
to grow anyway, so we just allocate a hunk and make the copy as
usual
*/
if(ALLOC(to) >= USED(from)) {
s_mp_setz(DIGITS(to) + USED(from), ALLOC(to) - USED(from));
s_mp_copy(DIGITS(from), DIGITS(to), USED(from));
/* Copy the precision and sign from the original */
USED(to) = USED(from);
SIGN(to) = SIGN(from);
} else {
*to = bignum_to_mp(heap_bignum(hp, SIGN(from), USED(from), DIGITS(from)));
}
} /* end copy */
return MP_OKAY;
} /* end mp_copy() */
/* }}} */
/* {{{ mp_exch(mp1, mp2) */
/*
mp_exch(mp1, mp2)
Exchange mp1 and mp2 without allocating any intermediate memory
(well, unless you count the stack space needed for this call and the
locals it creates...). This cannot fail.
*/
void mp_exch(mp_int *mp1, mp_int *mp2)
{
#if MP_ARGCHK == 2
assert(mp1 != NULL && mp2 != NULL);
#else
if(mp1 == NULL || mp2 == NULL)
return;
#endif
s_mp_exch(mp1, mp2);
} /* end mp_exch() */
/* }}} */
/* {{{ mp_zero(mp) */
/*
mp_zero(mp)
Set mp to zero. Does not change the allocated size of the structure,
and therefore cannot fail (except on a bad argument, which we ignore)
*/
void mp_zero(mp_int *mp)
{
if(mp == NULL)
return;
s_mp_setz(DIGITS(mp), ALLOC(mp));
USED(mp) = 1;
SIGN(mp) = MP_ZPOS;
} /* end mp_zero() */
/* }}} */
/* {{{ mp_set(mp, d) */
void mp_set(mp_int *mp, mp_digit d)
{
if(mp == NULL)
return;
mp_zero(mp);
DIGIT(mp, 0) = d;
} /* end mp_set() */
/* }}} */
/* {{{ mp_set_int(mp, z) */
mp_err mp_set_int(mp_int *mp, long z, heap_t *hp)
{
int ix;
unsigned long v = abs(z);
mp_err res;
ARGCHK(mp != NULL, MP_BADARG);
mp_zero(mp);
if(z == 0)
return MP_OKAY; /* shortcut for zero */
for(ix = sizeof(long) - 1; ix >= 0; ix--) {
if((res = s_mp_mul_2d(mp, CHAR_BIT, hp)) != MP_OKAY)
return res;
res = s_mp_add_d(mp,
(mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX), hp);
if(res != MP_OKAY)
return res;
}
if(z < 0)
SIGN(mp) = MP_NEG;
return MP_OKAY;
} /* end mp_set_int() */
/* }}} */
/*------------------------------------------------------------------------*/
/* {{{ Digit arithmetic */
/* {{{ mp_add_d(a, d, b) */
/*
mp_add_d(a, d, b)
Compute the sum b = a + d, for a single digit d. Respects the sign of
its primary addend (single digits are unsigned anyway).
*/
mp_err mp_add_d(mp_int *a, mp_digit d, mp_int *b, heap_t *hp)
{
mp_err res = MP_OKAY;
ARGCHK(a != NULL && b != NULL, MP_BADARG);
if((res = mp_copy(a, b, hp)) != MP_OKAY)
return res;
if(SIGN(b) == MP_ZPOS) {
res = s_mp_add_d(b, d, hp);
} else if(s_mp_cmp_d(b, d) >= 0) {
res = s_mp_sub_d(b, d);
} else {
SIGN(b) = MP_ZPOS;
DIGIT(b, 0) = d - DIGIT(b, 0);
}
return res;
} /* end mp_add_d() */
/* }}} */
/* {{{ mp_sub_d(a, d, b) */
/*
mp_sub_d(a, d, b)
Compute the difference b = a - d, for a single digit d. Respects the
sign of its subtrahend (single digits are unsigned anyway).
*/
mp_err mp_sub_d(mp_int *a, mp_digit d, mp_int *b, heap_t *hp)
{
mp_err res;
ARGCHK(a != NULL && b != NULL, MP_BADARG);
if((res = mp_copy(a, b, hp)) != MP_OKAY)
return res;
if(SIGN(b) == MP_NEG) {
if((res = s_mp_add_d(b, d, hp)) != MP_OKAY)
return res;
} else if(s_mp_cmp_d(b, d) >= 0) {
if((res = s_mp_sub_d(b, d)) != MP_OKAY)
return res;
} else {
mp_neg(b, b, hp);
DIGIT(b, 0) = d - DIGIT(b, 0);
SIGN(b) = MP_NEG;
}
if(s_mp_cmp_d(b, 0) == 0)
SIGN(b) = MP_ZPOS;
return MP_OKAY;
} /* end mp_sub_d() */
/* }}} */
/* {{{ mp_mul_d(a, d, b) */
/*
mp_mul_d(a, d, b)
Compute the product b = a * d, for a single digit d. Respects the sign
of its multiplicand (single digits are unsigned anyway)
*/
mp_err mp_mul_d(mp_int *a, mp_digit d, mp_int *b, heap_t *hp)
{
mp_err res;
ARGCHK(a != NULL && b != NULL, MP_BADARG);
if(d == 0) {
mp_zero(b);
return MP_OKAY;
}
if((res = mp_copy(a, b, hp)) != MP_OKAY)
return res;
res = s_mp_mul_d(b, d, hp);
return res;
} /* end mp_mul_d() */
/* }}} */
/* {{{ mp_mul_2(a, c) */
mp_err mp_mul_2(mp_int *a, mp_int *c, heap_t *hp)
{
mp_err res;
ARGCHK(a != NULL && c != NULL, MP_BADARG);
if((res = mp_copy(a, c, hp)) != MP_OKAY)
return res;
return s_mp_mul_2(c, hp);
} /* end mp_mul_2() */
/* }}} */
/* {{{ mp_div_d(a, d, q, r) */
/*
mp_div_d(a, d, q, r)
Compute the quotient q = a / d and remainder r = a mod d, for a
single digit d. Respects the sign of its divisor (single digits are
unsigned anyway).
*/
mp_err mp_div_d(mp_int *a, mp_digit d, mp_int *q, mp_digit *r, heap_t *hp)
{
mp_err res;
mp_digit rem;
int pow;
ARGCHK(a != NULL, MP_BADARG);
if(d == 0)
return MP_RANGE;
/* Shortcut for powers of two ... */
if((pow = s_mp_ispow2d(d)) >= 0) {
mp_digit mask;
mask = (1 << pow) - 1;
rem = DIGIT(a, 0) & mask;
if(q) {
mp_copy(a, q, hp);
s_mp_div_2d(q, pow);
}
if(r)
*r = rem;
return MP_OKAY;
}
/*
If the quotient is actually going to be returned, we'll try to
avoid hitting the memory allocator by copying the dividend into it
and doing the division there. This can't be any _worse_ than
always copying, and will sometimes be better (since it won't make
another copy)
If it's not going to be returned, we need to allocate a temporary
to hold the quotient, which will just be discarded.
*/
if(q) {
if((res = mp_copy(a, q, hp)) != MP_OKAY)
return res;
res = s_mp_div_d(q, d, &rem, hp);
if(s_mp_cmp_d(q, 0) == MP_EQ)
SIGN(q) = MP_ZPOS;
} else {
mp_int qp;
if((res = mp_init_copy(&qp, a, hp)) != MP_OKAY)
return res;
res = s_mp_div_d(&qp, d, &rem, hp);
if(s_mp_cmp_d(&qp, 0) == 0)
SIGN(&qp) = MP_ZPOS;
mp_clear(&qp);
}
if(r)
*r = rem;
return res;
} /* end mp_div_d() */
/* }}} */
/* {{{ mp_div_2(a, c) */
/*
mp_div_2(a, c)
Compute c = a / 2, disregarding the remainder.
*/
mp_err mp_div_2(mp_int *a, mp_int *c, heap_t *hp)
{
mp_err res;
ARGCHK(a != NULL && c != NULL, MP_BADARG);
if((res = mp_copy(a, c, hp)) != MP_OKAY)
return res;
s_mp_div_2(c);
return MP_OKAY;
} /* end mp_div_2() */
/* }}} */
/*------------------------------------------------------------------------*/
/* {{{ Full arithmetic */
/* {{{ mp_abs(a, b) */
/*
mp_abs(a, b)
Compute b = |a|. 'a' and 'b' may be identical.
*/
mp_err mp_abs(mp_int *a, mp_int *b, heap_t *hp)
{
mp_err res;
ARGCHK(a != NULL && b != NULL, MP_BADARG);
if((res = mp_copy(a, b, hp)) != MP_OKAY)
return res;
SIGN(b) = MP_ZPOS;
return MP_OKAY;
} /* end mp_abs() */
/* }}} */
/* {{{ mp_neg(a, b) */
/*
mp_neg(a, b)
Compute b = -a. 'a' and 'b' may be identical.
*/
mp_err mp_neg(mp_int *a, mp_int *b, heap_t *hp)
{
mp_err res;
ARGCHK(a != NULL && b != NULL, MP_BADARG);
if((res = mp_copy(a, b, hp)) != MP_OKAY)
return res;
if(s_mp_cmp_d(b, 0) == MP_EQ)
SIGN(b) = MP_ZPOS;
else
SIGN(b) = (SIGN(b) == MP_NEG) ? MP_ZPOS : MP_NEG;
return MP_OKAY;
} /* end mp_neg() */
/* }}} */
/* {{{ mp_add(a, b, c) */
/*
mp_add(a, b, c)
Compute c = a + b. All parameters may be identical.
*/
mp_err mp_add(mp_int *a, mp_int *b, mp_int *c, heap_t *hp)
{
mp_err res;
int cmp;
ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
if(SIGN(a) == SIGN(b)) { /* same sign: add values, keep sign */
/* Commutativity of addition lets us do this in either order,
so we avoid having to use a temporary even if the result
is supposed to replace the output
*/
if(c == b) {
if((res = s_mp_add(c, a, hp)) != MP_OKAY)
return res;
} else {
if(c != a && (res = mp_copy(a, c, hp)) != MP_OKAY)
return res;
if((res = s_mp_add(c, b, hp)) != MP_OKAY)
return res;
}
} else if((cmp = s_mp_cmp(a, b)) > 0) { /* different sign: a > b */
/* If the output is going to be clobbered, we will use a temporary
variable; otherwise, we'll do it without touching the memory
allocator at all, if possible
*/
if(c == b) {
mp_int tmp;
if((res = mp_init_copy(&tmp, a, hp)) != MP_OKAY)
return res;
if((res = s_mp_sub(&tmp, b)) != MP_OKAY) {
mp_clear(&tmp);
return res;
}
s_mp_exch(&tmp, c);
mp_clear(&tmp);
} else {
if(c != a && (res = mp_copy(a, c, hp)) != MP_OKAY)
return res;
if((res = s_mp_sub(c, b)) != MP_OKAY)
return res;
}
} else if(cmp == 0) { /* different sign, a == b */
mp_zero(c);
return MP_OKAY;
} else { /* different sign: a < b */
/* See above... */
if(c == a) {
mp_int tmp;
if((res = mp_init_copy(&tmp, b, hp)) != MP_OKAY)
return res;
if((res = s_mp_sub(&tmp, a)) != MP_OKAY) {
mp_clear(&tmp);
return res;
}
s_mp_exch(&tmp, c);
mp_clear(&tmp);
} else {
if(c != b && (res = mp_copy(b, c, hp)) != MP_OKAY)
return res;
if((res = s_mp_sub(c, a)) != MP_OKAY)
return res;
}
}
if(USED(c) == 1 && DIGIT(c, 0) == 0)
SIGN(c) = MP_ZPOS;
return MP_OKAY;
} /* end mp_add() */
/* }}} */
/* {{{ mp_sub(a, b, c) */
/*
mp_sub(a, b, c)
Compute c = a - b. All parameters may be identical.
*/
mp_err mp_sub(mp_int *a, mp_int *b, mp_int *c, heap_t *hp)
{
mp_err res;
int cmp;
ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
if(SIGN(a) != SIGN(b)) {
if(c == a) {
if((res = s_mp_add(c, b, hp)) != MP_OKAY)
return res;
} else {
if(c != b && ((res = mp_copy(b, c, hp)) != MP_OKAY))
return res;
if((res = s_mp_add(c, a, hp)) != MP_OKAY)
return res;
SIGN(c) = SIGN(a);
}
} else if((cmp = s_mp_cmp(a, b)) > 0) { /* Same sign, a > b */
if(c == b) {
mp_int tmp;
if((res = mp_init_copy(&tmp, a, hp)) != MP_OKAY)
return res;
if((res = s_mp_sub(&tmp, b)) != MP_OKAY) {
mp_clear(&tmp);
return res;
}
s_mp_exch(&tmp, c);
mp_clear(&tmp);
} else {
if(c != a && ((res = mp_copy(a, c, hp)) != MP_OKAY))
return res;
if((res = s_mp_sub(c, b)) != MP_OKAY)
return res;
}
} else if(cmp == 0) { /* Same sign, equal magnitude */
mp_zero(c);
return MP_OKAY;
} else { /* Same sign, b > a */
if(c == a) {
mp_int tmp;
if((res = mp_init_copy(&tmp, b, hp)) != MP_OKAY)
return res;
if((res = s_mp_sub(&tmp, a)) != MP_OKAY) {
mp_clear(&tmp);
return res;
}
s_mp_exch(&tmp, c);
mp_clear(&tmp);
} else {
if(c != b && ((res = mp_copy(b, c, hp)) != MP_OKAY))
return res;
if((res = s_mp_sub(c, a)) != MP_OKAY)
return res;
}
SIGN(c) = !SIGN(b);
}
if(USED(c) == 1 && DIGIT(c, 0) == 0)
SIGN(c) = MP_ZPOS;
return MP_OKAY;
} /* end mp_sub() */
/* }}} */
/* {{{ mp_mul(a, b, c) */
/*
mp_mul(a, b, c)
Compute c = a * b. All parameters may be identical.
*/
mp_err mp_mul(mp_int *a, mp_int *b, mp_int *c, heap_t *hp)
{
mp_err res;
mp_sign sgn;
ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
sgn = (SIGN(a) == SIGN(b)) ? MP_ZPOS : MP_NEG;
if(c == b) {
if((res = s_mp_mul(c, a, hp)) != MP_OKAY)
return res;
} else {
if((res = mp_copy(a, c, hp)) != MP_OKAY)
return res;
if((res = s_mp_mul(c, b, hp)) != MP_OKAY)
return res;
}
if(sgn == MP_ZPOS || s_mp_cmp_d(c, 0) == MP_EQ)
SIGN(c) = MP_ZPOS;
else
SIGN(c) = sgn;
return MP_OKAY;
} /* end mp_mul() */
/* }}} */
/* {{{ mp_mul_2d(a, d, c) */
/*
mp_mul_2d(a, d, c)
Compute c = a * 2^d. a may be the same as c.
*/
mp_err mp_mul_2d(mp_int *a, mp_digit d, mp_int *c, heap_t *hp)
{
mp_err res;
ARGCHK(a != NULL && c != NULL, MP_BADARG);
if((res = mp_copy(a, c, hp)) != MP_OKAY)