4 * Arbitrary precision integer arithmetic library
6 * ***** BEGIN LICENSE BLOCK *****
7 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
19 * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
21 * The Initial Developer of the Original Code is
22 * Michael J. Fromberger.
23 * Portions created by the Initial Developer are Copyright (C) 1998
24 * the Initial Developer. All Rights Reserved.
27 * Netscape Communications Corporation
28 * Douglas Stebila <douglas@stebila.ca> of Sun Laboratories.
30 * Alternatively, the contents of this file may be used under the terms of
31 * either the GNU General Public License Version 2 or later (the "GPL"), or
32 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
33 * in which case the provisions of the GPL or the LGPL are applicable instead
34 * of those above. If you wish to allow use of your version of this file only
35 * under the terms of either the GPL or the LGPL, and not to allow others to
36 * use your version of this file under the terms of the MPL, indicate your
37 * decision by deleting the provisions above and replace them with the notice
38 * and other provisions required by the GPL or the LGPL. If you do not delete
39 * the provisions above, a recipient may use your version of this file under
40 * the terms of any one of the MPL, the GPL or the LGPL.
42 * ***** END LICENSE BLOCK ***** */
43 /* $Id: mpi.c,v 1.45 2006/09/29 20:12:21 alexei.volkov.bugs%sun.com Exp $ */
52 A table of the logs of 2 for various bases (the 0 and 1 entries of
53 this table are meaningless and should not be referenced).
55 This table is used to compute output lengths for the mp_toradix()
56 function. Since a number n in radix r takes up about log_r(n)
57 digits, we estimate the output size by taking the least integer
58 greater than log_r(n), where:
60 log_r(n) = log_2(n) * log_r(2)
62 This table, therefore, is a table of log_r(2) for 2 <= r <= 36,
63 which are the output bases supported.
68 /* {{{ Constant strings */
70 /* Constant strings returned by mp_strerror() */
71 static const char *mp_err_string
[] = {
72 "unknown result code", /* say what? */
73 "boolean true", /* MP_OKAY, MP_YES */
74 "boolean false", /* MP_NO */
75 "out of memory", /* MP_MEM */
76 "argument out of range", /* MP_RANGE */
77 "invalid input parameter", /* MP_BADARG */
78 "result is undefined" /* MP_UNDEF */
81 /* Value to digit maps for radix conversion */
83 /* s_dmap_1 - standard digits and letters */
84 static const char *s_dmap_1
=
85 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
89 unsigned long mp_allocs
;
90 unsigned long mp_frees
;
91 unsigned long mp_copies
;
93 /* {{{ Default precision manipulation */
95 /* Default precision for newly created mp_int's */
96 static mp_size s_mp_defprec
= MP_DEFPREC
;
98 mp_size
mp_get_prec(void)
102 } /* end mp_get_prec() */
104 void mp_set_prec(mp_size prec
)
107 s_mp_defprec
= MP_DEFPREC
;
111 } /* end mp_set_prec() */
115 /*------------------------------------------------------------------------*/
116 /* {{{ mp_init(mp) */
121 Initialize a new zero-valued mp_int. Returns MP_OKAY if successful,
122 MP_MEM if memory could not be allocated for the structure.
125 mp_err
mp_init(mp_int
*mp
)
127 return mp_init_size(mp
, s_mp_defprec
);
129 } /* end mp_init() */
133 /* {{{ mp_init_size(mp, prec) */
136 mp_init_size(mp, prec)
138 Initialize a new zero-valued mp_int with at least the given
139 precision; returns MP_OKAY if successful, or MP_MEM if memory could
140 not be allocated for the structure.
143 mp_err
mp_init_size(mp_int
*mp
, mp_size prec
)
145 ARGCHK(mp
!= NULL
&& prec
> 0, MP_BADARG
);
147 prec
= MP_ROUNDUP(prec
, s_mp_defprec
);
148 if((DIGITS(mp
) = s_mp_alloc(prec
, sizeof(mp_digit
))) == NULL
)
157 } /* end mp_init_size() */
161 /* {{{ mp_init_copy(mp, from) */
164 mp_init_copy(mp, from)
166 Initialize mp as an exact copy of from. Returns MP_OKAY if
167 successful, MP_MEM if memory could not be allocated for the new
171 mp_err
mp_init_copy(mp_int
*mp
, const mp_int
*from
)
173 ARGCHK(mp
!= NULL
&& from
!= NULL
, MP_BADARG
);
178 if((DIGITS(mp
) = s_mp_alloc(ALLOC(from
), sizeof(mp_digit
))) == NULL
)
181 s_mp_copy(DIGITS(from
), DIGITS(mp
), USED(from
));
182 USED(mp
) = USED(from
);
183 ALLOC(mp
) = ALLOC(from
);
184 SIGN(mp
) = SIGN(from
);
188 } /* end mp_init_copy() */
192 /* {{{ mp_copy(from, to) */
197 Copies the mp_int 'from' to the mp_int 'to'. It is presumed that
198 'to' has already been initialized (if not, use mp_init_copy()
199 instead). If 'from' and 'to' are identical, nothing happens.
202 mp_err
mp_copy(const mp_int
*from
, mp_int
*to
)
204 ARGCHK(from
!= NULL
&& to
!= NULL
, MP_BADARG
);
214 If the allocated buffer in 'to' already has enough space to hold
215 all the used digits of 'from', we'll re-use it to avoid hitting
216 the memory allocater more than necessary; otherwise, we'd have
217 to grow anyway, so we just allocate a hunk and make the copy as
220 if(ALLOC(to
) >= USED(from
)) {
221 s_mp_setz(DIGITS(to
) + USED(from
), ALLOC(to
) - USED(from
));
222 s_mp_copy(DIGITS(from
), DIGITS(to
), USED(from
));
225 if((tmp
= s_mp_alloc(ALLOC(from
), sizeof(mp_digit
))) == NULL
)
228 s_mp_copy(DIGITS(from
), tmp
, USED(from
));
230 if(DIGITS(to
) != NULL
) {
232 s_mp_setz(DIGITS(to
), ALLOC(to
));
234 s_mp_free(DIGITS(to
));
238 ALLOC(to
) = ALLOC(from
);
241 /* Copy the precision and sign from the original */
242 USED(to
) = USED(from
);
243 SIGN(to
) = SIGN(from
);
248 } /* end mp_copy() */
252 /* {{{ mp_exch(mp1, mp2) */
257 Exchange mp1 and mp2 without allocating any intermediate memory
258 (well, unless you count the stack space needed for this call and the
259 locals it creates...). This cannot fail.
262 void mp_exch(mp_int
*mp1
, mp_int
*mp2
)
265 assert(mp1
!= NULL
&& mp2
!= NULL
);
267 if(mp1
== NULL
|| mp2
== NULL
)
273 } /* end mp_exch() */
277 /* {{{ mp_clear(mp) */
282 Release the storage used by an mp_int, and void its fields so that
283 if someone calls mp_clear() again for the same int later, we won't
287 void mp_clear(mp_int
*mp
)
292 if(DIGITS(mp
) != NULL
) {
294 s_mp_setz(DIGITS(mp
), ALLOC(mp
));
296 s_mp_free(DIGITS(mp
));
303 } /* end mp_clear() */
307 /* {{{ mp_zero(mp) */
312 Set mp to zero. Does not change the allocated size of the structure,
313 and therefore cannot fail (except on a bad argument, which we ignore)
315 void mp_zero(mp_int
*mp
)
320 s_mp_setz(DIGITS(mp
), ALLOC(mp
));
324 } /* end mp_zero() */
328 /* {{{ mp_set(mp, d) */
330 void mp_set(mp_int
*mp
, mp_digit d
)
342 /* {{{ mp_set_int(mp, z) */
344 mp_err
mp_set_int(mp_int
*mp
, long z
)
347 unsigned long v
= labs(z
);
350 ARGCHK(mp
!= NULL
, MP_BADARG
);
354 return MP_OKAY
; /* shortcut for zero */
356 if (sizeof v
<= sizeof(mp_digit
)) {
359 for (ix
= sizeof(long) - 1; ix
>= 0; ix
--) {
360 if ((res
= s_mp_mul_d(mp
, (UCHAR_MAX
+ 1))) != MP_OKAY
)
363 res
= s_mp_add_d(mp
, (mp_digit
)((v
>> (ix
* CHAR_BIT
)) & UCHAR_MAX
));
373 } /* end mp_set_int() */
377 /* {{{ mp_set_ulong(mp, z) */
379 mp_err
mp_set_ulong(mp_int
*mp
, unsigned long z
)
384 ARGCHK(mp
!= NULL
, MP_BADARG
);
388 return MP_OKAY
; /* shortcut for zero */
390 if (sizeof z
<= sizeof(mp_digit
)) {
393 for (ix
= sizeof(long) - 1; ix
>= 0; ix
--) {
394 if ((res
= s_mp_mul_d(mp
, (UCHAR_MAX
+ 1))) != MP_OKAY
)
397 res
= s_mp_add_d(mp
, (mp_digit
)((z
>> (ix
* CHAR_BIT
)) & UCHAR_MAX
));
403 } /* end mp_set_ulong() */
407 /*------------------------------------------------------------------------*/
408 /* {{{ Digit arithmetic */
410 /* {{{ mp_add_d(a, d, b) */
415 Compute the sum b = a + d, for a single digit d. Respects the sign of
416 its primary addend (single digits are unsigned anyway).
419 mp_err
mp_add_d(const mp_int
*a
, mp_digit d
, mp_int
*b
)
424 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_BADARG
);
426 if((res
= mp_init_copy(&tmp
, a
)) != MP_OKAY
)
429 if(SIGN(&tmp
) == ZPOS
) {
430 if((res
= s_mp_add_d(&tmp
, d
)) != MP_OKAY
)
432 } else if(s_mp_cmp_d(&tmp
, d
) >= 0) {
433 if((res
= s_mp_sub_d(&tmp
, d
)) != MP_OKAY
)
438 DIGIT(&tmp
, 0) = d
- DIGIT(&tmp
, 0);
441 if(s_mp_cmp_d(&tmp
, 0) == 0)
450 } /* end mp_add_d() */
454 /* {{{ mp_sub_d(a, d, b) */
459 Compute the difference b = a - d, for a single digit d. Respects the
460 sign of its subtrahend (single digits are unsigned anyway).
463 mp_err
mp_sub_d(const mp_int
*a
, mp_digit d
, mp_int
*b
)
468 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_BADARG
);
470 if((res
= mp_init_copy(&tmp
, a
)) != MP_OKAY
)
473 if(SIGN(&tmp
) == NEG
) {
474 if((res
= s_mp_add_d(&tmp
, d
)) != MP_OKAY
)
476 } else if(s_mp_cmp_d(&tmp
, d
) >= 0) {
477 if((res
= s_mp_sub_d(&tmp
, d
)) != MP_OKAY
)
482 DIGIT(&tmp
, 0) = d
- DIGIT(&tmp
, 0);
486 if(s_mp_cmp_d(&tmp
, 0) == 0)
495 } /* end mp_sub_d() */
499 /* {{{ mp_mul_d(a, d, b) */
504 Compute the product b = a * d, for a single digit d. Respects the sign
505 of its multiplicand (single digits are unsigned anyway)
508 mp_err
mp_mul_d(const mp_int
*a
, mp_digit d
, mp_int
*b
)
512 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_BADARG
);
519 if((res
= mp_copy(a
, b
)) != MP_OKAY
)
522 res
= s_mp_mul_d(b
, d
);
526 } /* end mp_mul_d() */
530 /* {{{ mp_mul_2(a, c) */
532 mp_err
mp_mul_2(const mp_int
*a
, mp_int
*c
)
536 ARGCHK(a
!= NULL
&& c
!= NULL
, MP_BADARG
);
538 if((res
= mp_copy(a
, c
)) != MP_OKAY
)
541 return s_mp_mul_2(c
);
543 } /* end mp_mul_2() */
547 /* {{{ mp_div_d(a, d, q, r) */
552 Compute the quotient q = a / d and remainder r = a mod d, for a
553 single digit d. Respects the sign of its divisor (single digits are
557 mp_err
mp_div_d(const mp_int
*a
, mp_digit d
, mp_int
*q
, mp_digit
*r
)
564 ARGCHK(a
!= NULL
, MP_BADARG
);
569 /* Shortcut for powers of two ... */
570 if((pow
= s_mp_ispow2d(d
)) >= 0) {
573 mask
= ((mp_digit
)1 << pow
) - 1;
574 rem
= DIGIT(a
, 0) & mask
;
587 if((res
= mp_init_copy(&qp
, a
)) != MP_OKAY
)
590 res
= s_mp_div_d(&qp
, d
, &rem
);
592 if(s_mp_cmp_d(&qp
, 0) == 0)
604 } /* end mp_div_d() */
608 /* {{{ mp_div_2(a, c) */
613 Compute c = a / 2, disregarding the remainder.
616 mp_err
mp_div_2(const mp_int
*a
, mp_int
*c
)
620 ARGCHK(a
!= NULL
&& c
!= NULL
, MP_BADARG
);
622 if((res
= mp_copy(a
, c
)) != MP_OKAY
)
629 } /* end mp_div_2() */
633 /* {{{ mp_expt_d(a, d, b) */
635 mp_err
mp_expt_d(const mp_int
*a
, mp_digit d
, mp_int
*c
)
640 ARGCHK(a
!= NULL
&& c
!= NULL
, MP_BADARG
);
642 if((res
= mp_init(&s
)) != MP_OKAY
)
644 if((res
= mp_init_copy(&x
, a
)) != MP_OKAY
)
651 if((res
= s_mp_mul(&s
, &x
)) != MP_OKAY
)
657 if((res
= s_mp_sqr(&x
)) != MP_OKAY
)
670 } /* end mp_expt_d() */
676 /*------------------------------------------------------------------------*/
677 /* {{{ Full arithmetic */
679 /* {{{ mp_abs(a, b) */
684 Compute b = |a|. 'a' and 'b' may be identical.
687 mp_err
mp_abs(const mp_int
*a
, mp_int
*b
)
691 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_BADARG
);
693 if((res
= mp_copy(a
, b
)) != MP_OKAY
)
704 /* {{{ mp_neg(a, b) */
709 Compute b = -a. 'a' and 'b' may be identical.
712 mp_err
mp_neg(const mp_int
*a
, mp_int
*b
)
716 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_BADARG
);
718 if((res
= mp_copy(a
, b
)) != MP_OKAY
)
721 if(s_mp_cmp_d(b
, 0) == MP_EQ
)
724 SIGN(b
) = (SIGN(b
) == NEG
) ? ZPOS
: NEG
;
732 /* {{{ mp_add(a, b, c) */
737 Compute c = a + b. All parameters may be identical.
740 mp_err
mp_add(const mp_int
*a
, const mp_int
*b
, mp_int
*c
)
744 ARGCHK(a
!= NULL
&& b
!= NULL
&& c
!= NULL
, MP_BADARG
);
746 if(SIGN(a
) == SIGN(b
)) { /* same sign: add values, keep sign */
747 MP_CHECKOK( s_mp_add_3arg(a
, b
, c
) );
748 } else if(s_mp_cmp(a
, b
) >= 0) { /* different sign: |a| >= |b| */
749 MP_CHECKOK( s_mp_sub_3arg(a
, b
, c
) );
750 } else { /* different sign: |a| < |b| */
751 MP_CHECKOK( s_mp_sub_3arg(b
, a
, c
) );
754 if (s_mp_cmp_d(c
, 0) == MP_EQ
)
764 /* {{{ mp_sub(a, b, c) */
769 Compute c = a - b. All parameters may be identical.
772 mp_err
mp_sub(const mp_int
*a
, const mp_int
*b
, mp_int
*c
)
777 ARGCHK(a
!= NULL
&& b
!= NULL
&& c
!= NULL
, MP_BADARG
);
784 if (MP_SIGN(a
) != MP_SIGN(b
)) {
785 MP_CHECKOK( s_mp_add_3arg(a
, b
, c
) );
786 } else if (!(magDiff
= s_mp_cmp(a
, b
))) {
789 } else if (magDiff
> 0) {
790 MP_CHECKOK( s_mp_sub_3arg(a
, b
, c
) );
792 MP_CHECKOK( s_mp_sub_3arg(b
, a
, c
) );
793 MP_SIGN(c
) = !MP_SIGN(a
);
796 if (s_mp_cmp_d(c
, 0) == MP_EQ
)
797 MP_SIGN(c
) = MP_ZPOS
;
806 /* {{{ mp_mul(a, b, c) */
811 Compute c = a * b. All parameters may be identical.
813 mp_err
mp_mul(const mp_int
*a
, const mp_int
*b
, mp_int
* c
)
819 mp_size useda
, usedb
;
821 ARGCHK(a
!= NULL
&& b
!= NULL
&& c
!= NULL
, MP_BADARG
);
824 if ((res
= mp_init_copy(&tmp
, a
)) != MP_OKAY
)
830 if ((res
= mp_init_copy(&tmp
, b
)) != MP_OKAY
)
837 if (MP_USED(a
) < MP_USED(b
)) {
838 const mp_int
*xch
= b
; /* switch a and b, to do fewer outer loops */
843 MP_USED(c
) = 1; MP_DIGIT(c
, 0) = 0;
844 if((res
= s_mp_pad(c
, USED(a
) + USED(b
))) != MP_OKAY
)
848 if ((MP_USED(a
) == MP_USED(b
)) && IS_POWER_OF_2(MP_USED(b
))) {
849 if (MP_USED(a
) == 4) {
850 s_mp_mul_comba_4(a
, b
, c
);
853 if (MP_USED(a
) == 8) {
854 s_mp_mul_comba_8(a
, b
, c
);
857 if (MP_USED(a
) == 16) {
858 s_mp_mul_comba_16(a
, b
, c
);
861 if (MP_USED(a
) == 32) {
862 s_mp_mul_comba_32(a
, b
, c
);
869 s_mpv_mul_d(MP_DIGITS(a
), MP_USED(a
), *pb
++, MP_DIGITS(c
));
871 /* Outer loop: Digits of b */
874 for (ib
= 1; ib
< usedb
; ib
++) {
875 mp_digit b_i
= *pb
++;
877 /* Inner product: Digits of a */
879 s_mpv_mul_d_add(MP_DIGITS(a
), useda
, b_i
, MP_DIGITS(c
) + ib
);
881 MP_DIGIT(c
, ib
+ useda
) = b_i
;
886 if(SIGN(a
) == SIGN(b
) || s_mp_cmp_d(c
, 0) == MP_EQ
)
898 /* {{{ mp_sqr(a, sqr) */
902 Computes the square of a. This can be done more
903 efficiently than a general multiplication, because many of the
904 computation steps are redundant when squaring. The inner product
905 step is a bit more complicated, but we save a fair number of
906 iterations of the multiplication loop.
909 /* sqr = a^2; Caller provides both a and tmp; */
910 mp_err
mp_sqr(const mp_int
*a
, mp_int
*sqr
)
919 ARGCHK(a
!= NULL
&& sqr
!= NULL
, MP_BADARG
);
922 if((res
= mp_init_copy(&tmp
, a
)) != MP_OKAY
)
931 if (ix
> MP_ALLOC(sqr
)) {
933 MP_CHECKOK( s_mp_grow(sqr
, ix
) );
936 MP_DIGIT(sqr
, 0) = 0;
939 if (IS_POWER_OF_2(MP_USED(a
))) {
940 if (MP_USED(a
) == 4) {
941 s_mp_sqr_comba_4(a
, sqr
);
944 if (MP_USED(a
) == 8) {
945 s_mp_sqr_comba_8(a
, sqr
);
948 if (MP_USED(a
) == 16) {
949 s_mp_sqr_comba_16(a
, sqr
);
952 if (MP_USED(a
) == 32) {
953 s_mp_sqr_comba_32(a
, sqr
);
960 count
= MP_USED(a
) - 1;
963 s_mpv_mul_d(pa
, count
, d
, MP_DIGITS(sqr
) + 1);
964 for (ix
= 3; --count
> 0; ix
+= 2) {
966 s_mpv_mul_d_add(pa
, count
, d
, MP_DIGITS(sqr
) + ix
);
968 MP_DIGIT(sqr
, MP_USED(sqr
)-1) = 0; /* above loop stopped short of this. */
973 MP_DIGIT(sqr
, 1) = 0;
976 /* now add the squares of the digits of a to sqr. */
977 s_mpv_sqr_add_prop(MP_DIGITS(a
), MP_USED(a
), MP_DIGITS(sqr
));
991 /* {{{ mp_div(a, b, q, r) */
996 Compute q = a / b and r = a mod b. Input parameters may be re-used
997 as output parameters. If q or r is NULL, that portion of the
998 computation will be discarded (although it will still be computed)
1000 mp_err
mp_div(const mp_int
*a
, const mp_int
*b
, mp_int
*q
, mp_int
*r
)
1004 mp_int qtmp
, rtmp
, btmp
;
1009 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_BADARG
);
1014 if(mp_cmp_z(b
) == MP_EQ
)
1021 /* Set up some temporaries... */
1022 if (!r
|| r
== a
|| r
== b
) {
1023 MP_CHECKOK( mp_init_copy(&rtmp
, a
) );
1026 MP_CHECKOK( mp_copy(a
, r
) );
1030 if (!q
|| q
== a
|| q
== b
) {
1031 MP_CHECKOK( mp_init_size(&qtmp
, MP_USED(a
)) );
1034 MP_CHECKOK( s_mp_pad(q
, MP_USED(a
)) );
1040 If |a| <= |b|, we can compute the solution without division;
1041 otherwise, we actually do the work required.
1043 if ((cmp
= s_mp_cmp(a
, b
)) <= 0) {
1045 /* r was set to a above. */
1052 MP_CHECKOK( mp_init_copy(&btmp
, b
) );
1053 MP_CHECKOK( s_mp_div(pR
, &btmp
, pQ
) );
1056 /* Compute the signs for the output */
1057 MP_SIGN(pR
) = signA
; /* Sr = Sa */
1058 /* Sq = ZPOS if Sa == Sb */ /* Sq = NEG if Sa != Sb */
1059 MP_SIGN(pQ
) = (signA
== signB
) ? ZPOS
: NEG
;
1061 if(s_mp_cmp_d(pQ
, 0) == MP_EQ
)
1063 if(s_mp_cmp_d(pR
, 0) == MP_EQ
)
1066 /* Copy output, if it is needed */
1080 } /* end mp_div() */
1084 /* {{{ mp_div_2d(a, d, q, r) */
1086 mp_err
mp_div_2d(const mp_int
*a
, mp_digit d
, mp_int
*q
, mp_int
*r
)
1090 ARGCHK(a
!= NULL
, MP_BADARG
);
1093 if((res
= mp_copy(a
, q
)) != MP_OKAY
)
1097 if((res
= mp_copy(a
, r
)) != MP_OKAY
)
1109 } /* end mp_div_2d() */
1113 /* {{{ mp_expt(a, b, c) */
1118 Compute c = a ** b, that is, raise a to the b power. Uses a
1119 standard iterative square-and-multiply technique.
1122 mp_err
mp_expt(mp_int
*a
, mp_int
*b
, mp_int
*c
)
1129 ARGCHK(a
!= NULL
&& b
!= NULL
&& c
!= NULL
, MP_BADARG
);
1134 if((res
= mp_init(&s
)) != MP_OKAY
)
1139 if((res
= mp_init_copy(&x
, a
)) != MP_OKAY
)
1142 /* Loop over low-order digits in ascending order */
1143 for(dig
= 0; dig
< (USED(b
) - 1); dig
++) {
1146 /* Loop over bits of each non-maximal digit */
1147 for(bit
= 0; bit
< DIGIT_BIT
; bit
++) {
1149 if((res
= s_mp_mul(&s
, &x
)) != MP_OKAY
)
1155 if((res
= s_mp_sqr(&x
)) != MP_OKAY
)
1160 /* Consider now the last digit... */
1165 if((res
= s_mp_mul(&s
, &x
)) != MP_OKAY
)
1171 if((res
= s_mp_sqr(&x
)) != MP_OKAY
)
1178 res
= mp_copy(&s
, c
);
1187 } /* end mp_expt() */
1191 /* {{{ mp_2expt(a, k) */
1193 /* Compute a = 2^k */
1195 mp_err
mp_2expt(mp_int
*a
, mp_digit k
)
1197 ARGCHK(a
!= NULL
, MP_BADARG
);
1199 return s_mp_2expt(a
, k
);
1201 } /* end mp_2expt() */
1205 /* {{{ mp_mod(a, m, c) */
1210 Compute c = a (mod m). Result will always be 0 <= c < m.
1213 mp_err
mp_mod(const mp_int
*a
, const mp_int
*m
, mp_int
*c
)
1218 ARGCHK(a
!= NULL
&& m
!= NULL
&& c
!= NULL
, MP_BADARG
);
1224 If |a| > m, we need to divide to get the remainder and take the
1227 If |a| < m, we don't need to do any division, just copy and adjust
1228 the sign (if a is negative).
1230 If |a| == m, we can simply set the result to zero.
1232 This order is intended to minimize the average path length of the
1233 comparison chain on common workloads -- the most frequent cases are
1234 that |a| != m, so we do those first.
1236 if((mag
= s_mp_cmp(a
, m
)) > 0) {
1237 if((res
= mp_div(a
, m
, NULL
, c
)) != MP_OKAY
)
1240 if(SIGN(c
) == NEG
) {
1241 if((res
= mp_add(c
, m
, c
)) != MP_OKAY
)
1245 } else if(mag
< 0) {
1246 if((res
= mp_copy(a
, c
)) != MP_OKAY
)
1249 if(mp_cmp_z(a
) < 0) {
1250 if((res
= mp_add(c
, m
, c
)) != MP_OKAY
)
1262 } /* end mp_mod() */
1266 /* {{{ mp_mod_d(a, d, c) */
1271 Compute c = a (mod d). Result will always be 0 <= c < d
1273 mp_err
mp_mod_d(const mp_int
*a
, mp_digit d
, mp_digit
*c
)
1278 ARGCHK(a
!= NULL
&& c
!= NULL
, MP_BADARG
);
1280 if(s_mp_cmp_d(a
, d
) > 0) {
1281 if((res
= mp_div_d(a
, d
, NULL
, &rem
)) != MP_OKAY
)
1286 rem
= d
- DIGIT(a
, 0);
1296 } /* end mp_mod_d() */
1300 /* {{{ mp_sqrt(a, b) */
1305 Compute the integer square root of a, and store the result in b.
1306 Uses an integer-arithmetic version of Newton's iterative linear
1307 approximation technique to determine this value; the result has the
1308 following two properties:
1313 It is a range error to pass a negative value.
1315 mp_err
mp_sqrt(const mp_int
*a
, mp_int
*b
)
1321 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_BADARG
);
1323 /* Cannot take square root of a negative value */
1327 /* Special cases for zero and one, trivial */
1328 if(mp_cmp_d(a
, 1) <= 0)
1329 return mp_copy(a
, b
);
1331 /* Initialize the temporaries we'll use below */
1332 if((res
= mp_init_size(&t
, USED(a
))) != MP_OKAY
)
1335 /* Compute an initial guess for the iteration as a itself */
1336 if((res
= mp_init_copy(&x
, a
)) != MP_OKAY
)
1341 s_mp_rshd(&x
, used
/ 2);
1345 /* t = (x * x) - a */
1346 mp_copy(&x
, &t
); /* can't fail, t is big enough for original x */
1347 if((res
= mp_sqr(&t
, &t
)) != MP_OKAY
||
1348 (res
= mp_sub(&t
, a
, &t
)) != MP_OKAY
)
1353 if((res
= mp_div(&t
, &x
, &t
, NULL
)) != MP_OKAY
)
1357 /* Terminate the loop, if the quotient is zero */
1358 if(mp_cmp_z(&t
) == MP_EQ
)
1362 if((res
= mp_sub(&x
, &t
, &x
)) != MP_OKAY
)
1367 /* Copy result to output parameter */
1368 mp_sub_d(&x
, 1, &x
);
1378 } /* end mp_sqrt() */
1384 /*------------------------------------------------------------------------*/
1385 /* {{{ Modular arithmetic */
1388 /* {{{ mp_addmod(a, b, m, c) */
1391 mp_addmod(a, b, m, c)
1393 Compute c = (a + b) mod m
1396 mp_err
mp_addmod(const mp_int
*a
, const mp_int
*b
, const mp_int
*m
, mp_int
*c
)
1400 ARGCHK(a
!= NULL
&& b
!= NULL
&& m
!= NULL
&& c
!= NULL
, MP_BADARG
);
1402 if((res
= mp_add(a
, b
, c
)) != MP_OKAY
)
1404 if((res
= mp_mod(c
, m
, c
)) != MP_OKAY
)
1413 /* {{{ mp_submod(a, b, m, c) */
1416 mp_submod(a, b, m, c)
1418 Compute c = (a - b) mod m
1421 mp_err
mp_submod(const mp_int
*a
, const mp_int
*b
, const mp_int
*m
, mp_int
*c
)
1425 ARGCHK(a
!= NULL
&& b
!= NULL
&& m
!= NULL
&& c
!= NULL
, MP_BADARG
);
1427 if((res
= mp_sub(a
, b
, c
)) != MP_OKAY
)
1429 if((res
= mp_mod(c
, m
, c
)) != MP_OKAY
)
1438 /* {{{ mp_mulmod(a, b, m, c) */
1441 mp_mulmod(a, b, m, c)
1443 Compute c = (a * b) mod m
1446 mp_err
mp_mulmod(const mp_int
*a
, const mp_int
*b
, const mp_int
*m
, mp_int
*c
)
1450 ARGCHK(a
!= NULL
&& b
!= NULL
&& m
!= NULL
&& c
!= NULL
, MP_BADARG
);
1452 if((res
= mp_mul(a
, b
, c
)) != MP_OKAY
)
1454 if((res
= mp_mod(c
, m
, c
)) != MP_OKAY
)
1463 /* {{{ mp_sqrmod(a, m, c) */
1466 mp_err
mp_sqrmod(const mp_int
*a
, const mp_int
*m
, mp_int
*c
)
1470 ARGCHK(a
!= NULL
&& m
!= NULL
&& c
!= NULL
, MP_BADARG
);
1472 if((res
= mp_sqr(a
, c
)) != MP_OKAY
)
1474 if((res
= mp_mod(c
, m
, c
)) != MP_OKAY
)
1479 } /* end mp_sqrmod() */
1484 /* {{{ s_mp_exptmod(a, b, m, c) */
1487 s_mp_exptmod(a, b, m, c)
1489 Compute c = (a ** b) mod m. Uses a standard square-and-multiply
1490 method with modular reductions at each step. (This is basically the
1491 same code as mp_expt(), except for the addition of the reductions)
1493 The modular reductions are done using Barrett's algorithm (see
1494 s_mp_reduce() below for details)
1497 mp_err
s_mp_exptmod(const mp_int
*a
, const mp_int
*b
, const mp_int
*m
, mp_int
*c
)
1504 ARGCHK(a
!= NULL
&& b
!= NULL
&& c
!= NULL
, MP_BADARG
);
1506 if(mp_cmp_z(b
) < 0 || mp_cmp_z(m
) <= 0)
1509 if((res
= mp_init(&s
)) != MP_OKAY
)
1511 if((res
= mp_init_copy(&x
, a
)) != MP_OKAY
||
1512 (res
= mp_mod(&x
, m
, &x
)) != MP_OKAY
)
1514 if((res
= mp_init(&mu
)) != MP_OKAY
)
1521 s_mp_lshd(&mu
, 2 * USED(m
));
1522 if((res
= mp_div(&mu
, m
, &mu
, NULL
)) != MP_OKAY
)
1525 /* Loop over digits of b in ascending order, except highest order */
1526 for(dig
= 0; dig
< (USED(b
) - 1); dig
++) {
1529 /* Loop over the bits of the lower-order digits */
1530 for(bit
= 0; bit
< DIGIT_BIT
; bit
++) {
1532 if((res
= s_mp_mul(&s
, &x
)) != MP_OKAY
)
1534 if((res
= s_mp_reduce(&s
, m
, &mu
)) != MP_OKAY
)
1540 if((res
= s_mp_sqr(&x
)) != MP_OKAY
)
1542 if((res
= s_mp_reduce(&x
, m
, &mu
)) != MP_OKAY
)
1547 /* Now do the last digit... */
1552 if((res
= s_mp_mul(&s
, &x
)) != MP_OKAY
)
1554 if((res
= s_mp_reduce(&s
, m
, &mu
)) != MP_OKAY
)
1560 if((res
= s_mp_sqr(&x
)) != MP_OKAY
)
1562 if((res
= s_mp_reduce(&x
, m
, &mu
)) != MP_OKAY
)
1577 } /* end s_mp_exptmod() */
1581 /* {{{ mp_exptmod_d(a, d, m, c) */
1583 mp_err
mp_exptmod_d(const mp_int
*a
, mp_digit d
, const mp_int
*m
, mp_int
*c
)
1588 ARGCHK(a
!= NULL
&& c
!= NULL
, MP_BADARG
);
1590 if((res
= mp_init(&s
)) != MP_OKAY
)
1592 if((res
= mp_init_copy(&x
, a
)) != MP_OKAY
)
1599 if((res
= s_mp_mul(&s
, &x
)) != MP_OKAY
||
1600 (res
= mp_mod(&s
, m
, &s
)) != MP_OKAY
)
1606 if((res
= s_mp_sqr(&x
)) != MP_OKAY
||
1607 (res
= mp_mod(&x
, m
, &x
)) != MP_OKAY
)
1620 } /* end mp_exptmod_d() */
1623 #endif /* if MP_MODARITH */
1627 /*------------------------------------------------------------------------*/
1628 /* {{{ Comparison functions */
1630 /* {{{ mp_cmp_z(a) */
1635 Compare a <=> 0. Returns <0 if a<0, 0 if a=0, >0 if a>0.
1638 int mp_cmp_z(const mp_int
*a
)
1642 else if(USED(a
) == 1 && DIGIT(a
, 0) == 0)
1647 } /* end mp_cmp_z() */
1651 /* {{{ mp_cmp_d(a, d) */
1656 Compare a <=> d. Returns <0 if a<d, 0 if a=d, >0 if a>d
1659 int mp_cmp_d(const mp_int
*a
, mp_digit d
)
1661 ARGCHK(a
!= NULL
, MP_EQ
);
1666 return s_mp_cmp_d(a
, d
);
1668 } /* end mp_cmp_d() */
1672 /* {{{ mp_cmp(a, b) */
1674 int mp_cmp(const mp_int
*a
, const mp_int
*b
)
1676 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_EQ
);
1678 if(SIGN(a
) == SIGN(b
)) {
1681 if((mag
= s_mp_cmp(a
, b
)) == MP_EQ
)
1689 } else if(SIGN(a
) == ZPOS
) {
1695 } /* end mp_cmp() */
1699 /* {{{ mp_cmp_mag(a, b) */
1704 Compares |a| <=> |b|, and returns an appropriate comparison result
1707 int mp_cmp_mag(mp_int
*a
, mp_int
*b
)
1709 ARGCHK(a
!= NULL
&& b
!= NULL
, MP_EQ
);
1711 return s_mp_cmp(a
, b
);
1713 } /* end mp_cmp_mag() */
1717 /* {{{ mp_cmp_int(a, z) */
1720 This just converts z to an mp_int, and uses the existing comparison
1721 routines. This is sort of inefficient, but it's not clear to me how
1722 frequently this wil get used anyway. For small positive constants,
1723 you can always use mp_cmp_d(), and for zero, there is mp_cmp_z().
1725 int mp_cmp_int(const mp_int
*a
, long z
)
1730 ARGCHK(a
!= NULL
, MP_EQ
);
1732 mp_init(&tmp
); mp_set_int(&tmp
, z
);
1733 out
= mp_cmp(a
, &tmp
);
1738 } /* end mp_cmp_int() */
1742 /* {{{ mp_isodd(a) */
1747 Returns a true (non-zero) value if a is odd, false (zero) otherwise.
1749 int mp_isodd(const mp_int
*a
)
1751 ARGCHK(a
!= NULL
, 0);
1753 return (int)(DIGIT(a
, 0) & 1);
1755 } /* end mp_isodd() */
1759 /* {{{ mp_iseven(a) */
1761 int mp_iseven(const mp_int
*a
)
1763 return !mp_isodd(a
);
1765 } /* end mp_iseven() */
1771 /*------------------------------------------------------------------------*/
1772 /* {{{ Number theoretic functions */
1775 /* {{{ mp_gcd(a, b, c) */
1778 Like the old mp_gcd() function, except computes the GCD using the
1779 binary algorithm due to Josef Stein in 1961 (via Knuth).
1781 mp_err
mp_gcd(mp_int
*a
, mp_int
*b
, mp_int
*c
)
1787 ARGCHK(a
!= NULL
&& b
!= NULL
&& c
!= NULL
, MP_BADARG
);
1789 if(mp_cmp_z(a
) == MP_EQ
&& mp_cmp_z(b
) == MP_EQ
)
1791 if(mp_cmp_z(a
) == MP_EQ
) {
1792 return mp_copy(b
, c
);
1793 } else if(mp_cmp_z(b
) == MP_EQ
) {
1794 return mp_copy(a
, c
);
1797 if((res
= mp_init(&t
)) != MP_OKAY
)
1799 if((res
= mp_init_copy(&u
, a
)) != MP_OKAY
)
1801 if((res
= mp_init_copy(&v
, b
)) != MP_OKAY
)
1807 /* Divide out common factors of 2 until at least 1 of a, b is even */
1808 while(mp_iseven(&u
) && mp_iseven(&v
)) {
1816 if((res
= mp_copy(&v
, &t
)) != MP_OKAY
)
1820 if(SIGN(&v
) == ZPOS
)
1826 if((res
= mp_copy(&u
, &t
)) != MP_OKAY
)
1832 while(mp_iseven(&t
)) {
1836 if(mp_cmp_z(&t
) == MP_GT
) {
1837 if((res
= mp_copy(&t
, &u
)) != MP_OKAY
)
1841 if((res
= mp_copy(&t
, &v
)) != MP_OKAY
)
1845 if(SIGN(&t
) == ZPOS
)
1851 if((res
= mp_sub(&u
, &v
, &t
)) != MP_OKAY
)
1854 if(s_mp_cmp_d(&t
, 0) == MP_EQ
)
1858 s_mp_2expt(&v
, k
); /* v = 2^k */
1859 res
= mp_mul(&u
, &v
, c
); /* c = u * v */
1870 } /* end mp_gcd() */
1874 /* {{{ mp_lcm(a, b, c) */
1876 /* We compute the least common multiple using the rule:
1880 ... by computing the product, and dividing out the gcd.
1883 mp_err
mp_lcm(mp_int
*a
, mp_int
*b
, mp_int
*c
)
1888 ARGCHK(a
!= NULL
&& b
!= NULL
&& c
!= NULL
, MP_BADARG
);
1890 /* Set up temporaries */
1891 if((res
= mp_init(&gcd
)) != MP_OKAY
)
1893 if((res
= mp_init(&prod
)) != MP_OKAY
)
1896 if((res
= mp_mul(a
, b
, &prod
)) != MP_OKAY
)
1898 if((res
= mp_gcd(a
, b
, &gcd
)) != MP_OKAY
)
1901 res
= mp_div(&prod
, &gcd
, c
, NULL
);
1910 } /* end mp_lcm() */
1914 /* {{{ mp_xgcd(a, b, g, x, y) */
1917 mp_xgcd(a, b, g, x, y)
1919 Compute g = (a, b) and values x and y satisfying Bezout's identity
1920 (that is, ax + by = g). This uses the binary extended GCD algorithm
1921 based on the Stein algorithm used for mp_gcd()
1922 See algorithm 14.61 in Handbook of Applied Cryptogrpahy.
1925 mp_err
mp_xgcd(const mp_int
*a
, const mp_int
*b
, mp_int
*g
, mp_int
*x
, mp_int
*y
)
1927 mp_int gx
, xc
, yc
, u
, v
, A
, B
, C
, D
;
1932 if(mp_cmp_z(b
) == 0)
1935 /* Initialize all these variables we need */
1936 MP_CHECKOK( mp_init(&u
) );
1938 MP_CHECKOK( mp_init(&v
) );
1940 MP_CHECKOK( mp_init(&gx
) );
1941 clean
[++last
] = &gx
;
1942 MP_CHECKOK( mp_init(&A
) );
1944 MP_CHECKOK( mp_init(&B
) );
1946 MP_CHECKOK( mp_init(&C
) );
1948 MP_CHECKOK( mp_init(&D
) );
1950 MP_CHECKOK( mp_init_copy(&xc
, a
) );
1951 clean
[++last
] = &xc
;
1953 MP_CHECKOK( mp_init_copy(&yc
, b
) );
1954 clean
[++last
] = &yc
;
1959 /* Divide by two until at least one of them is odd */
1960 while(mp_iseven(&xc
) && mp_iseven(&yc
)) {
1961 mp_size nx
= mp_trailing_zeros(&xc
);
1962 mp_size ny
= mp_trailing_zeros(&yc
);
1963 mp_size n
= MP_MIN(nx
, ny
);
1966 MP_CHECKOK( s_mp_mul_2d(&gx
,n
) );
1971 mp_set(&A
, 1); mp_set(&D
, 1);
1973 /* Loop through binary GCD algorithm */
1975 while(mp_iseven(&u
)) {
1978 if(mp_iseven(&A
) && mp_iseven(&B
)) {
1979 s_mp_div_2(&A
); s_mp_div_2(&B
);
1981 MP_CHECKOK( mp_add(&A
, &yc
, &A
) );
1983 MP_CHECKOK( mp_sub(&B
, &xc
, &B
) );
1988 while(mp_iseven(&v
)) {
1991 if(mp_iseven(&C
) && mp_iseven(&D
)) {
1992 s_mp_div_2(&C
); s_mp_div_2(&D
);
1994 MP_CHECKOK( mp_add(&C
, &yc
, &C
) );
1996 MP_CHECKOK( mp_sub(&D
, &xc
, &D
) );
2001 if(mp_cmp(&u
, &v
) >= 0) {
2002 MP_CHECKOK( mp_sub(&u
, &v
, &u
) );
2003 MP_CHECKOK( mp_sub(&A
, &C
, &A
) );
2004 MP_CHECKOK( mp_sub(&B
, &D
, &B
) );
2006 MP_CHECKOK( mp_sub(&v
, &u
, &v
) );
2007 MP_CHECKOK( mp_sub(&C
, &A
, &C
) );
2008 MP_CHECKOK( mp_sub(&D
, &B
, &D
) );
2010 } while (mp_cmp_z(&u
) != 0);
2012 /* copy results to output */
2014 MP_CHECKOK( mp_copy(&C
, x
) );
2017 MP_CHECKOK( mp_copy(&D
, y
) );
2020 MP_CHECKOK( mp_mul(&gx
, &v
, g
) );
2024 mp_clear(clean
[last
--]);
2028 } /* end mp_xgcd() */
2032 mp_size
mp_trailing_zeros(const mp_int
*mp
)
2038 if (!mp
|| !MP_DIGITS(mp
) || !mp_cmp_z(mp
))
2041 for (ix
= 0; !(d
= MP_DIGIT(mp
,ix
)) && (ix
< MP_USED(mp
)); ++ix
)
2044 return 0; /* shouldn't happen, but ... */
2045 #if !defined(MP_USE_UINT_DIGIT)
2046 if (!(d
& 0xffffffffU
)) {
2051 if (!(d
& 0xffffU
)) {
2072 assert(0 != (d
& 1));
2077 /* Given a and prime p, computes c and k such that a*c == 2**k (mod p).
2078 ** Returns k (positive) or error (negative).
2079 ** This technique from the paper "Fast Modular Reciprocals" (unpublished)
2080 ** by Richard Schroeppel (a.k.a. Captain Nemo).
2082 mp_err
s_mp_almost_inverse(const mp_int
*a
, const mp_int
*p
, mp_int
*c
)
2088 ARGCHK(a
&& p
&& c
, MP_BADARG
);
2093 MP_CHECKOK( mp_init(&d
) );
2094 MP_CHECKOK( mp_init_copy(&f
, a
) ); /* f = a */
2095 MP_CHECKOK( mp_init_copy(&g
, p
) ); /* g = p */
2100 if (mp_cmp_z(&f
) == 0) {
2105 while (mp_iseven(&f
)) {
2106 mp_size n
= mp_trailing_zeros(&f
);
2112 MP_CHECKOK( s_mp_mul_2d(&d
, n
) );
2115 if (mp_cmp_d(&f
, 1) == MP_EQ
) { /* f == 1 */
2119 diff_sign
= mp_cmp(&f
, &g
);
2120 if (diff_sign
< 0) { /* f < g */
2123 } else if (diff_sign
== 0) { /* f == g */
2124 res
= MP_UNDEF
; /* a and p are not relatively prime */
2127 if ((MP_DIGIT(&f
,0) % 4) == (MP_DIGIT(&g
,0) % 4)) {
2128 MP_CHECKOK( mp_sub(&f
, &g
, &f
) ); /* f = f - g */
2129 MP_CHECKOK( mp_sub(c
, &d
, c
) ); /* c = c - d */
2131 MP_CHECKOK( mp_add(&f
, &g
, &f
) ); /* f = f + g */
2132 MP_CHECKOK( mp_add(c
, &d
, c
) ); /* c = c + d */
2136 while (MP_SIGN(c
) != MP_ZPOS
) {
2137 MP_CHECKOK( mp_add(c
, p
, c
) );
2149 /* Compute T = (P ** -1) mod MP_RADIX. Also works for 16-bit mp_digits.
2150 ** This technique from the paper "Fast Modular Reciprocals" (unpublished)
2151 ** by Richard Schroeppel (a.k.a. Captain Nemo).
2153 mp_digit
s_mp_invmod_radix(mp_digit P
)
2160 #if !defined(MP_USE_UINT_DIGIT)
2167 /* Given c, k, and prime p, where a*c == 2**k (mod p),
2168 ** Compute x = (a ** -1) mod p. This is similar to Montgomery reduction.
2169 ** This technique from the paper "Fast Modular Reciprocals" (unpublished)
2170 ** by Richard Schroeppel (a.k.a. Captain Nemo).
2172 mp_err
s_mp_fixup_reciprocal(const mp_int
*c
, const mp_int
*p
, int k
, mp_int
*x
)
2179 if (mp_cmp_z(c
) < 0) { /* c < 0 */
2180 MP_CHECKOK( mp_add(c
, p
, x
) ); /* x = c + p */
2182 MP_CHECKOK( mp_copy(c
, x
) ); /* x = c */
2185 /* make sure x is large enough */
2186 ix
= MP_HOWMANY(k
, MP_DIGIT_BIT
) + MP_USED(p
) + 1;
2187 ix
= MP_MAX(ix
, MP_USED(x
));
2188 MP_CHECKOK( s_mp_pad(x
, ix
) );
2190 r
= 0 - s_mp_invmod_radix(MP_DIGIT(p
,0));
2192 for (ix
= 0; k
> 0; ix
++) {
2193 int j
= MP_MIN(k
, MP_DIGIT_BIT
);
2194 mp_digit v
= r
* MP_DIGIT(x
, ix
);
2195 if (j
< MP_DIGIT_BIT
) {
2196 v
&= ((mp_digit
)1 << j
) - 1; /* v = v mod (2 ** j) */
2198 s_mp_mul_d_add_offset(p
, v
, x
, ix
); /* x += p * v * (RADIX ** ix) */
2202 s_mp_div_2d(x
, k_orig
);
2209 /* compute mod inverse using Schroeppel's method, only if m is odd */
2210 mp_err
s_mp_invmod_odd_m(const mp_int
*a
, const mp_int
*m
, mp_int
*c
)
2216 ARGCHK(a
&& m
&& c
, MP_BADARG
);
2218 if(mp_cmp_z(a
) == 0 || mp_cmp_z(m
) == 0)
2226 if ((res
= mp_init_copy(&x
, a
)) != MP_OKAY
)
2231 } else if (m
== c
) {
2232 if ((res
= mp_init_copy(&x
, m
)) != MP_OKAY
)
2239 MP_CHECKOK( s_mp_almost_inverse(a
, m
, c
) );
2241 MP_CHECKOK( s_mp_fixup_reciprocal(c
, m
, k
, c
) );
2247 /* Known good algorithm for computing modular inverse. But slow. */
2248 mp_err
mp_invmod_xgcd(const mp_int
*a
, const mp_int
*m
, mp_int
*c
)
2253 ARGCHK(a
&& m
&& c
, MP_BADARG
);
2255 if(mp_cmp_z(a
) == 0 || mp_cmp_z(m
) == 0)
2260 MP_CHECKOK( mp_init(&x
) );
2261 MP_CHECKOK( mp_init(&g
) );
2263 MP_CHECKOK( mp_xgcd(a
, m
, &g
, &x
, NULL
) );
2265 if (mp_cmp_d(&g
, 1) != MP_EQ
) {
2270 res
= mp_mod(&x
, m
, c
);
2280 /* modular inverse where modulus is 2**k. */
2281 /* c = a**-1 mod 2**k */
2282 mp_err
s_mp_invmod_2d(const mp_int
*a
, mp_size k
, mp_int
*c
)
2286 mp_int t0
, t1
, val
, tmp
, two2k
;
2288 static const mp_digit d2
= 2;
2289 static const mp_int two
= { MP_ZPOS
, 1, 1, (mp_digit
*)&d2
};
2293 if (k
<= MP_DIGIT_BIT
) {
2294 mp_digit i
= s_mp_invmod_radix(MP_DIGIT(a
,0));
2295 if (k
< MP_DIGIT_BIT
)
2296 i
&= ((mp_digit
)1 << k
) - (mp_digit
)1;
2302 MP_DIGITS(&val
) = 0;
2303 MP_DIGITS(&tmp
) = 0;
2304 MP_DIGITS(&two2k
) = 0;
2305 MP_CHECKOK( mp_init_copy(&val
, a
) );
2306 s_mp_mod_2d(&val
, k
);
2307 MP_CHECKOK( mp_init_copy(&t0
, &val
) );
2308 MP_CHECKOK( mp_init_copy(&t1
, &t0
) );
2309 MP_CHECKOK( mp_init(&tmp
) );
2310 MP_CHECKOK( mp_init(&two2k
) );
2311 MP_CHECKOK( s_mp_2expt(&two2k
, k
) );
2313 MP_CHECKOK( mp_mul(&val
, &t1
, &tmp
) );
2314 MP_CHECKOK( mp_sub(&two
, &tmp
, &tmp
) );
2315 MP_CHECKOK( mp_mul(&t1
, &tmp
, &t1
) );
2316 s_mp_mod_2d(&t1
, k
);
2317 while (MP_SIGN(&t1
) != MP_ZPOS
) {
2318 MP_CHECKOK( mp_add(&t1
, &two2k
, &t1
) );
2320 if (mp_cmp(&t1
, &t0
) == MP_EQ
)
2322 MP_CHECKOK( mp_copy(&t1
, &t0
) );
2339 mp_err
s_mp_invmod_even_m(const mp_int
*a
, const mp_int
*m
, mp_int
*c
)
2343 mp_int oddFactor
, evenFactor
; /* factors of the modulus */
2344 mp_int oddPart
, evenPart
; /* parts to combine via CRT. */
2345 mp_int C2
, tmp1
, tmp2
;
2347 /*static const mp_digit d1 = 1; */
2348 /*static const mp_int one = { MP_ZPOS, 1, 1, (mp_digit *)&d1 }; */
2350 if ((res
= s_mp_ispow2(m
)) >= 0) {
2352 return s_mp_invmod_2d(a
, k
, c
);
2354 MP_DIGITS(&oddFactor
) = 0;
2355 MP_DIGITS(&evenFactor
) = 0;
2356 MP_DIGITS(&oddPart
) = 0;
2357 MP_DIGITS(&evenPart
) = 0;
2359 MP_DIGITS(&tmp1
) = 0;
2360 MP_DIGITS(&tmp2
) = 0;
2362 MP_CHECKOK( mp_init_copy(&oddFactor
, m
) ); /* oddFactor = m */
2363 MP_CHECKOK( mp_init(&evenFactor
) );
2364 MP_CHECKOK( mp_init(&oddPart
) );
2365 MP_CHECKOK( mp_init(&evenPart
) );
2366 MP_CHECKOK( mp_init(&C2
) );
2367 MP_CHECKOK( mp_init(&tmp1
) );
2368 MP_CHECKOK( mp_init(&tmp2
) );
2370 k
= mp_trailing_zeros(m
);
2371 s_mp_div_2d(&oddFactor
, k
);
2372 MP_CHECKOK( s_mp_2expt(&evenFactor
, k
) );
2374 /* compute a**-1 mod oddFactor. */
2375 MP_CHECKOK( s_mp_invmod_odd_m(a
, &oddFactor
, &oddPart
) );
2376 /* compute a**-1 mod evenFactor, where evenFactor == 2**k. */
2377 MP_CHECKOK( s_mp_invmod_2d( a
, k
, &evenPart
) );
2379 /* Use Chinese Remainer theorem to compute a**-1 mod m. */
2380 /* let m1 = oddFactor, v1 = oddPart,
2381 * let m2 = evenFactor, v2 = evenPart.
2384 /* Compute C2 = m1**-1 mod m2. */
2385 MP_CHECKOK( s_mp_invmod_2d(&oddFactor
, k
, &C2
) );
2387 /* compute u = (v2 - v1)*C2 mod m2 */
2388 MP_CHECKOK( mp_sub(&evenPart
, &oddPart
, &tmp1
) );
2389 MP_CHECKOK( mp_mul(&tmp1
, &C2
, &tmp2
) );
2390 s_mp_mod_2d(&tmp2
, k
);
2391 while (MP_SIGN(&tmp2
) != MP_ZPOS
) {
2392 MP_CHECKOK( mp_add(&tmp2
, &evenFactor
, &tmp2
) );
2395 /* compute answer = v1 + u*m1 */
2396 MP_CHECKOK( mp_mul(&tmp2
, &oddFactor
, c
) );
2397 MP_CHECKOK( mp_add(&oddPart
, c
, c
) );
2398 /* not sure this is necessary, but it's low cost if not. */
2399 MP_CHECKOK( mp_mod(c
, m
, c
) );
2402 mp_clear(&oddFactor
);
2403 mp_clear(&evenFactor
);
2405 mp_clear(&evenPart
);
2413 /* {{{ mp_invmod(a, m, c) */
2418 Compute c = a^-1 (mod m), if there is an inverse for a (mod m).
2419 This is equivalent to the question of whether (a, m) = 1. If not,
2420 MP_UNDEF is returned, and there is no inverse.
2423 mp_err
mp_invmod(const mp_int
*a
, const mp_int
*m
, mp_int
*c
)
2426 ARGCHK(a
&& m
&& c
, MP_BADARG
);
2428 if(mp_cmp_z(a
) == 0 || mp_cmp_z(m
) == 0)
2432 return s_mp_invmod_odd_m(a
, m
, c
);
2435 return MP_UNDEF
; /* not invertable */
2437 return s_mp_invmod_even_m(a
, m
, c
);
2439 } /* end mp_invmod() */
2442 #endif /* if MP_NUMTH */
2446 /*------------------------------------------------------------------------*/
2447 /* {{{ mp_print(mp, ofp) */
2453 Print a textual representation of the given mp_int on the output
2454 stream 'ofp'. Output is generated using the internal radix.
2457 void mp_print(mp_int
*mp
, FILE *ofp
)
2461 if(mp
== NULL
|| ofp
== NULL
)
2464 fputc((SIGN(mp
) == NEG
) ? '-' : '+', ofp
);
2466 for(ix
= USED(mp
) - 1; ix
>= 0; ix
--) {
2467 fprintf(ofp
, DIGIT_FMT
, DIGIT(mp
, ix
));
2470 } /* end mp_print() */
2472 #endif /* if MP_IOFUNC */
2476 /*------------------------------------------------------------------------*/
2477 /* {{{ More I/O Functions */
2479 /* {{{ mp_read_raw(mp, str, len) */
2482 mp_read_raw(mp, str, len)
2484 Read in a raw value (base 256) into the given mp_int
2487 mp_err
mp_read_raw(mp_int
*mp
, char *str
, int len
)
2491 unsigned char *ustr
= (unsigned char *)str
;
2493 ARGCHK(mp
!= NULL
&& str
!= NULL
&& len
> 0, MP_BADARG
);
2497 /* Get sign from first byte */
2503 /* Read the rest of the digits */
2504 for(ix
= 1; ix
< len
; ix
++) {
2505 if((res
= mp_mul_d(mp
, 256, mp
)) != MP_OKAY
)
2507 if((res
= mp_add_d(mp
, ustr
[ix
], mp
)) != MP_OKAY
)
2513 } /* end mp_read_raw() */
2517 /* {{{ mp_raw_size(mp) */
2519 int mp_raw_size(mp_int
*mp
)
2521 ARGCHK(mp
!= NULL
, 0);
2523 return (USED(mp
) * sizeof(mp_digit
)) + 1;
2525 } /* end mp_raw_size() */
2529 /* {{{ mp_toraw(mp, str) */
2531 mp_err
mp_toraw(mp_int
*mp
, char *str
)
2533 int ix
, jx
, pos
= 1;
2535 ARGCHK(mp
!= NULL
&& str
!= NULL
, MP_BADARG
);
2537 str
[0] = (char)SIGN(mp
);
2539 /* Iterate over each digit... */
2540 for(ix
= USED(mp
) - 1; ix
>= 0; ix
--) {
2541 mp_digit d
= DIGIT(mp
, ix
);
2543 /* Unpack digit bytes, high order first */
2544 for(jx
= sizeof(mp_digit
) - 1; jx
>= 0; jx
--) {
2545 str
[pos
++] = (char)(d
>> (jx
* CHAR_BIT
));
2551 } /* end mp_toraw() */
2555 /* {{{ mp_read_radix(mp, str, radix) */
2558 mp_read_radix(mp, str, radix)
2560 Read an integer from the given string, and set mp to the resulting
2561 value. The input is presumed to be in base 10. Leading non-digit
2562 characters are ignored, and the function reads until a non-digit
2563 character or the end of the string.
2566 mp_err
mp_read_radix(mp_int
*mp
, const char *str
, int radix
)
2568 int ix
= 0, val
= 0;
2572 ARGCHK(mp
!= NULL
&& str
!= NULL
&& radix
>= 2 && radix
<= MAX_RADIX
,
2577 /* Skip leading non-digit characters until a digit or '-' or '+' */
2579 (s_mp_tovalue(str
[ix
], radix
) < 0) &&
2585 if(str
[ix
] == '-') {
2588 } else if(str
[ix
] == '+') {
2589 sig
= ZPOS
; /* this is the default anyway... */
2593 while((val
= s_mp_tovalue(str
[ix
], radix
)) >= 0) {
2594 if((res
= s_mp_mul_d(mp
, radix
)) != MP_OKAY
)
2596 if((res
= s_mp_add_d(mp
, val
)) != MP_OKAY
)
2601 if(s_mp_cmp_d(mp
, 0) == MP_EQ
)
2608 } /* end mp_read_radix() */
2610 mp_err
mp_read_variable_radix(mp_int
*a
, const char * str
, int default_radix
)
2612 int radix
= default_radix
;
2617 /* Skip leading non-digit characters until a digit or '-' or '+' */
2618 while ((cx
= *str
) != 0 &&
2619 (s_mp_tovalue(cx
, radix
) < 0) &&
2628 } else if (cx
== '+') {
2629 sig
= ZPOS
; /* this is the default anyway... */
2633 if (str
[0] == '0') {
2634 if ((str
[1] | 0x20) == 'x') {
2642 res
= mp_read_radix(a
, str
, radix
);
2643 if (res
== MP_OKAY
) {
2644 MP_SIGN(a
) = (s_mp_cmp_d(a
, 0) == MP_EQ
) ? ZPOS
: sig
;
2651 /* {{{ mp_radix_size(mp, radix) */
2653 int mp_radix_size(mp_int
*mp
, int radix
)
2657 if(!mp
|| radix
< 2 || radix
> MAX_RADIX
)
2660 bits
= USED(mp
) * DIGIT_BIT
- 1;
2662 return s_mp_outlen(bits
, radix
);
2664 } /* end mp_radix_size() */
2668 /* {{{ mp_toradix(mp, str, radix) */
2670 mp_err
mp_toradix(mp_int
*mp
, char *str
, int radix
)
2674 ARGCHK(mp
!= NULL
&& str
!= NULL
, MP_BADARG
);
2675 ARGCHK(radix
> 1 && radix
<= MAX_RADIX
, MP_RANGE
);
2677 if(mp_cmp_z(mp
) == MP_EQ
) {
2684 mp_digit rem
, rdx
= (mp_digit
)radix
;
2687 if((res
= mp_init_copy(&tmp
, mp
)) != MP_OKAY
)
2690 /* Save sign for later, and take absolute value */
2691 sgn
= SIGN(&tmp
); SIGN(&tmp
) = ZPOS
;
2693 /* Generate output digits in reverse order */
2694 while(mp_cmp_z(&tmp
) != 0) {
2695 if((res
= mp_div_d(&tmp
, rdx
, &tmp
, &rem
)) != MP_OKAY
) {
2700 /* Generate digits, use capital letters */
2701 ch
= s_mp_todigit(rem
, radix
, 0);
2706 /* Add - sign if original value was negative */
2710 /* Add trailing NUL to end the string */
2713 /* Reverse the digits and sign indicator */
2729 } /* end mp_toradix() */
2733 /* {{{ mp_tovalue(ch, r) */
2735 int mp_tovalue(char ch
, int r
)
2737 return s_mp_tovalue(ch
, r
);
2739 } /* end mp_tovalue() */
2745 /* {{{ mp_strerror(ec) */
2750 Return a string describing the meaning of error code 'ec'. The
2751 string returned is allocated in static memory, so the caller should
2752 not attempt to modify or free the memory associated with this
2755 const char *mp_strerror(mp_err ec
)
2757 int aec
= (ec
< 0) ? -ec
: ec
;
2759 /* Code values are negative, so the senses of these comparisons
2761 if(ec
< MP_LAST_CODE
|| ec
> MP_OKAY
) {
2762 return mp_err_string
[0]; /* unknown error code */
2764 return mp_err_string
[aec
+ 1];
2767 } /* end mp_strerror() */
2771 /*========================================================================*/
2772 /*------------------------------------------------------------------------*/
2773 /* Static function definitions (internal use only) */
2775 /* {{{ Memory management */
2777 /* {{{ s_mp_grow(mp, min) */
2779 /* Make sure there are at least 'min' digits allocated to mp */
2780 mp_err
s_mp_grow(mp_int
*mp
, mp_size min
)
2782 if(min
> ALLOC(mp
)) {
2785 /* Set min to next nearest default precision block size */
2786 min
= MP_ROUNDUP(min
, s_mp_defprec
);
2788 if((tmp
= s_mp_alloc(min
, sizeof(mp_digit
))) == NULL
)
2791 s_mp_copy(DIGITS(mp
), tmp
, USED(mp
));
2794 s_mp_setz(DIGITS(mp
), ALLOC(mp
));
2796 s_mp_free(DIGITS(mp
));
2803 } /* end s_mp_grow() */
2807 /* {{{ s_mp_pad(mp, min) */
2809 /* Make sure the used size of mp is at least 'min', growing if needed */
2810 mp_err
s_mp_pad(mp_int
*mp
, mp_size min
)
2812 if(min
> USED(mp
)) {
2815 /* Make sure there is room to increase precision */
2816 if (min
> ALLOC(mp
)) {
2817 if ((res
= s_mp_grow(mp
, min
)) != MP_OKAY
)
2820 s_mp_setz(DIGITS(mp
) + USED(mp
), min
- USED(mp
));
2823 /* Increase precision; should already be 0-filled */
2829 } /* end s_mp_pad() */
2833 /* {{{ s_mp_setz(dp, count) */
2836 /* Set 'count' digits pointed to by dp to be zeroes */
2837 void s_mp_setz(mp_digit
*dp
, mp_size count
)
2842 for(ix
= 0; ix
< count
; ix
++)
2845 memset(dp
, 0, count
* sizeof(mp_digit
));
2848 } /* end s_mp_setz() */
2853 /* {{{ s_mp_copy(sp, dp, count) */
2856 /* Copy 'count' digits from sp to dp */
2857 void s_mp_copy(const mp_digit
*sp
, mp_digit
*dp
, mp_size count
)
2862 for(ix
= 0; ix
< count
; ix
++)
2865 memcpy(dp
, sp
, count
* sizeof(mp_digit
));
2868 } /* end s_mp_copy() */
2873 /* {{{ s_mp_alloc(nb, ni) */
2876 /* Allocate ni records of nb bytes each, and return a pointer to that */
2877 void *s_mp_alloc(size_t nb
, size_t ni
)
2880 return calloc(nb
, ni
);
2882 } /* end s_mp_alloc() */
2887 /* {{{ s_mp_free(ptr) */
2890 /* Free the memory pointed to by ptr */
2891 void s_mp_free(void *ptr
)
2897 } /* end s_mp_free() */
2902 /* {{{ s_mp_clamp(mp) */
2905 /* Remove leading zeroes from the given value */
2906 void s_mp_clamp(mp_int
*mp
)
2908 mp_size used
= MP_USED(mp
);
2909 while (used
> 1 && DIGIT(mp
, used
- 1) == 0)
2912 } /* end s_mp_clamp() */
2917 /* {{{ s_mp_exch(a, b) */
2919 /* Exchange the data for a and b; (b, a) = (a, b) */
2920 void s_mp_exch(mp_int
*a
, mp_int
*b
)
2928 } /* end s_mp_exch() */
2934 /* {{{ Arithmetic helpers */
2936 /* {{{ s_mp_lshd(mp, p) */
2939 Shift mp leftward by p digits, growing if needed, and zero-filling
2940 the in-shifted digits at the right end. This is a convenient
2941 alternative to multiplication by powers of the radix
2942 The value of USED(mp) must already have been set to the value for
2946 mp_err
s_mp_lshd(mp_int
*mp
, mp_size p
)
2955 if (MP_USED(mp
) == 1 && MP_DIGIT(mp
, 0) == 0)
2958 if((res
= s_mp_pad(mp
, USED(mp
) + p
)) != MP_OKAY
)
2963 /* Shift all the significant figures over as needed */
2964 for(ix
= pos
- p
; ix
>= 0; ix
--)
2965 DIGIT(mp
, ix
+ p
) = DIGIT(mp
, ix
);
2967 /* Fill the bottom digits with zeroes */
2968 for(ix
= 0; ix
< p
; ix
++)
2973 } /* end s_mp_lshd() */
2977 /* {{{ s_mp_mul_2d(mp, d) */
2980 Multiply the integer by 2^d, where d is a number of bits. This
2981 amounts to a bitwise shift of the value.
2983 mp_err
s_mp_mul_2d(mp_int
*mp
, mp_digit d
)
2986 mp_digit dshift
, bshift
;
2989 ARGCHK(mp
!= NULL
, MP_BADARG
);
2991 dshift
= d
/ MP_DIGIT_BIT
;
2992 bshift
= d
% MP_DIGIT_BIT
;
2993 /* bits to be shifted out of the top word */
2994 mask
= ((mp_digit
)~0 << (MP_DIGIT_BIT
- bshift
));
2995 mask
&= MP_DIGIT(mp
, MP_USED(mp
) - 1);
2997 if (MP_OKAY
!= (res
= s_mp_pad(mp
, MP_USED(mp
) + dshift
+ (mask
!= 0) )))
3000 if (dshift
&& MP_OKAY
!= (res
= s_mp_lshd(mp
, dshift
)))
3004 mp_digit
*pa
= MP_DIGITS(mp
);
3005 mp_digit
*alim
= pa
+ MP_USED(mp
);
3008 for (pa
+= dshift
; pa
< alim
; ) {
3010 *pa
++ = (x
<< bshift
) | prev
;
3011 prev
= x
>> (DIGIT_BIT
- bshift
);
3017 } /* end s_mp_mul_2d() */
3019 /* {{{ s_mp_rshd(mp, p) */
3022 Shift mp rightward by p digits. Maintains the invariant that
3023 digits above the precision are all zero. Digits shifted off the
3024 end are lost. Cannot fail.
3027 void s_mp_rshd(mp_int
*mp
, mp_size p
)
3030 mp_digit
*src
, *dst
;
3035 /* Shortcut when all digits are to be shifted off */
3037 s_mp_setz(DIGITS(mp
), ALLOC(mp
));
3043 /* Shift all the significant figures over as needed */
3044 dst
= MP_DIGITS(mp
);
3046 for (ix
= USED(mp
) - p
; ix
> 0; ix
--)
3050 /* Fill the top digits with zeroes */
3055 /* Strip off any leading zeroes */
3059 } /* end s_mp_rshd() */
3063 /* {{{ s_mp_div_2(mp) */
3065 /* Divide by two -- take advantage of radix properties to do it fast */
3066 void s_mp_div_2(mp_int
*mp
)
3070 } /* end s_mp_div_2() */
3074 /* {{{ s_mp_mul_2(mp) */
3076 mp_err
s_mp_mul_2(mp_int
*mp
)
3082 /* Shift digits leftward by 1 bit */
3085 for (ix
= 0; ix
< used
; ix
++) {
3087 *pd
++ = (d
<< 1) | kin
;
3088 kin
= (d
>> (DIGIT_BIT
- 1));
3091 /* Deal with rollover from last digit */
3093 if (ix
>= ALLOC(mp
)) {
3095 if((res
= s_mp_grow(mp
, ALLOC(mp
) + 1)) != MP_OKAY
)
3099 DIGIT(mp
, ix
) = kin
;
3105 } /* end s_mp_mul_2() */
3109 /* {{{ s_mp_mod_2d(mp, d) */
3112 Remainder the integer by 2^d, where d is a number of bits. This
3113 amounts to a bitwise AND of the value, and does not require the full
3116 void s_mp_mod_2d(mp_int
*mp
, mp_digit d
)
3118 mp_size ndig
= (d
/ DIGIT_BIT
), nbit
= (d
% DIGIT_BIT
);
3122 if(ndig
>= USED(mp
))
3125 /* Flush all the bits above 2^d in its digit */
3126 dmask
= ((mp_digit
)1 << nbit
) - 1;
3127 DIGIT(mp
, ndig
) &= dmask
;
3129 /* Flush all digits above the one with 2^d in it */
3130 for(ix
= ndig
+ 1; ix
< USED(mp
); ix
++)
3135 } /* end s_mp_mod_2d() */
3139 /* {{{ s_mp_div_2d(mp, d) */
3142 Divide the integer by 2^d, where d is a number of bits. This
3143 amounts to a bitwise shift of the value, and does not require the
3144 full division code (used in Barrett reduction, see below)
3146 void s_mp_div_2d(mp_int
*mp
, mp_digit d
)
3149 mp_digit save
, next
, mask
;
3151 s_mp_rshd(mp
, d
/ DIGIT_BIT
);
3154 mask
= ((mp_digit
)1 << d
) - 1;
3156 for(ix
= USED(mp
) - 1; ix
>= 0; ix
--) {
3157 next
= DIGIT(mp
, ix
) & mask
;
3158 DIGIT(mp
, ix
) = (DIGIT(mp
, ix
) >> d
) | (save
<< (DIGIT_BIT
- d
));
3164 } /* end s_mp_div_2d() */
3168 /* {{{ s_mp_norm(a, b, *d) */
3173 Normalize a and b for division, where b is the divisor. In order
3174 that we might make good guesses for quotient digits, we want the
3175 leading digit of b to be at least half the radix, which we
3176 accomplish by multiplying a and b by a power of 2. The exponent
3177 (shift count) is placed in *pd, so that the remainder can be shifted
3178 back at the end of the division process.
3181 mp_err
s_mp_norm(mp_int
*a
, mp_int
*b
, mp_digit
*pd
)
3186 mp_err res
= MP_OKAY
;
3189 mask
= DIGIT_MAX
& ~(DIGIT_MAX
>> 1); /* mask is msb of digit */
3190 b_msd
= DIGIT(b
, USED(b
) - 1);
3191 while (!(b_msd
& mask
)) {
3197 MP_CHECKOK( s_mp_mul_2d(a
, d
) );
3198 MP_CHECKOK( s_mp_mul_2d(b
, d
) );
3205 } /* end s_mp_norm() */
3211 /* {{{ Primitive digit arithmetic */
3213 /* {{{ s_mp_add_d(mp, d) */
3215 /* Add d to |mp| in place */
3216 mp_err
s_mp_add_d(mp_int
*mp
, mp_digit d
) /* unsigned digit addition */
3218 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3222 w
= (mp_word
)DIGIT(mp
, 0) + d
;
3223 DIGIT(mp
, 0) = ACCUM(w
);
3226 while(ix
< USED(mp
) && k
) {
3227 w
= (mp_word
)DIGIT(mp
, ix
) + k
;
3228 DIGIT(mp
, ix
) = ACCUM(w
);
3236 if((res
= s_mp_pad(mp
, USED(mp
) + 1)) != MP_OKAY
)
3239 DIGIT(mp
, ix
) = (mp_digit
)k
;
3244 mp_digit
* pmp
= MP_DIGITS(mp
);
3245 mp_digit sum
, mp_i
, carry
= 0;
3246 mp_err res
= MP_OKAY
;
3247 int used
= (int)MP_USED(mp
);
3250 *pmp
++ = sum
= d
+ mp_i
;
3252 while (carry
&& --used
> 0) {
3254 *pmp
++ = sum
= carry
+ mp_i
;
3257 if (carry
&& !used
) {
3260 MP_CHECKOK( s_mp_pad(mp
, used
+ 1) );
3261 MP_DIGIT(mp
, used
) = carry
;
3266 } /* end s_mp_add_d() */
3270 /* {{{ s_mp_sub_d(mp, d) */
3272 /* Subtract d from |mp| in place, assumes |mp| > d */
3273 mp_err
s_mp_sub_d(mp_int
*mp
, mp_digit d
) /* unsigned digit subtract */
3275 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3279 /* Compute initial subtraction */
3280 w
= (RADIX
+ (mp_word
)DIGIT(mp
, 0)) - d
;
3281 b
= CARRYOUT(w
) ? 0 : 1;
3282 DIGIT(mp
, 0) = ACCUM(w
);
3284 /* Propagate borrows leftward */
3285 while(b
&& ix
< USED(mp
)) {
3286 w
= (RADIX
+ (mp_word
)DIGIT(mp
, ix
)) - b
;
3287 b
= CARRYOUT(w
) ? 0 : 1;
3288 DIGIT(mp
, ix
) = ACCUM(w
);
3292 /* Remove leading zeroes */
3295 /* If we have a borrow out, it's a violation of the input invariant */
3301 mp_digit
*pmp
= MP_DIGITS(mp
);
3302 mp_digit mp_i
, diff
, borrow
;
3303 mp_size used
= MP_USED(mp
);
3306 *pmp
++ = diff
= mp_i
- d
;
3307 borrow
= (diff
> mp_i
);
3308 while (borrow
&& --used
) {
3310 *pmp
++ = diff
= mp_i
- borrow
;
3311 borrow
= (diff
> mp_i
);
3314 return (borrow
&& !used
) ? MP_RANGE
: MP_OKAY
;
3316 } /* end s_mp_sub_d() */
3320 /* {{{ s_mp_mul_d(a, d) */
3322 /* Compute a = a * d, single digit multiplication */
3323 mp_err
s_mp_mul_d(mp_int
*a
, mp_digit d
)
3335 if (0 <= (pow
= s_mp_ispow2d(d
))) {
3336 return s_mp_mul_2d(a
, (mp_digit
)pow
);
3340 MP_CHECKOK( s_mp_pad(a
, used
+ 1) );
3342 s_mpv_mul_d(MP_DIGITS(a
), used
, d
, MP_DIGITS(a
));
3349 } /* end s_mp_mul_d() */
3353 /* {{{ s_mp_div_d(mp, d, r) */
3356 s_mp_div_d(mp, d, r)
3358 Compute the quotient mp = mp / d and remainder r = mp mod d, for a
3359 single digit d. If r is null, the remainder will be discarded.
3362 mp_err
s_mp_div_d(mp_int
*mp
, mp_digit d
, mp_digit
*r
)
3364 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
3381 /* could check for power of 2 here, but mp_div_d does that. */
3382 if (MP_USED(mp
) == 1) {
3383 mp_digit n
= MP_DIGIT(mp
,0);
3394 MP_DIGITS(&rem
) = 0;
3395 MP_DIGITS("
) = 0;
3396 /* Make room for the quotient */
3397 MP_CHECKOK( mp_init_size("
, USED(mp
)) );
3399 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
3400 for(ix
= USED(mp
) - 1; ix
>= 0; ix
--) {
3401 w
= (w
<< DIGIT_BIT
) | DIGIT(mp
, ix
);
3410 s_mp_lshd("
, 1);
3411 DIGIT("
, 0) = (mp_digit
)q
;
3416 #if !defined(MP_ASSEMBLY_DIV_2DX1D)
3420 MP_CHECKOK( mp_init_copy(&rem
, mp
) );
3422 #if !defined(MP_ASSEMBLY_DIV_2DX1D)
3423 MP_DIGIT("
, 0) = d
;
3424 MP_CHECKOK( s_mp_norm(&rem
, "
, &norm
) );
3427 MP_DIGIT("
, 0) = 0;
3431 for (ix
= USED(&rem
) - 1; ix
>= 0; ix
--) {
3432 w
= DIGIT(&rem
, ix
);
3435 MP_CHECKOK( s_mpv_div_2dx1d(p
, w
, d
, &q
, &w
) );
3436 } else if (w
>= d
) {
3443 MP_CHECKOK( s_mp_lshd("
, 1) );
3444 DIGIT("
, 0) = q
;
3447 #if !defined(MP_ASSEMBLY_DIV_2DX1D)
3454 /* Deliver the remainder, if desired */
3465 } /* end s_mp_div_d() */
3472 /* {{{ Primitive full arithmetic */
3474 /* {{{ s_mp_add(a, b) */
3476 /* Compute a = |a| + |b| */
3477 mp_err
s_mp_add(mp_int
*a
, const mp_int
*b
) /* magnitude addition */
3479 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3482 mp_digit d
, sum
, carry
= 0;
3489 /* Make sure a has enough precision for the output value */
3490 if((USED(b
) > USED(a
)) && (res
= s_mp_pad(a
, USED(b
))) != MP_OKAY
)
3494 Add up all digits up to the precision of b. If b had initially
3495 the same precision as a, or greater, we took care of it by the
3496 padding step above, so there is no problem. If b had initially
3497 less precision, we'll have to make sure the carry out is duly
3498 propagated upward among the higher-order digits of the sum.
3503 for(ix
= 0; ix
< used
; ix
++) {
3504 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3505 w
= w
+ *pa
+ *pb
++;
3511 d
= (sum
< d
); /* detect overflow */
3512 *pa
++ = sum
+= carry
;
3513 carry
= d
+ (sum
< carry
); /* detect overflow */
3517 /* If we run out of 'b' digits before we're actually done, make
3518 sure the carries get propagated upward...
3521 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3522 while (w
&& ix
< used
) {
3529 while (carry
&& ix
< used
) {
3537 /* If there's an overall carry out, increase precision and include
3538 it. We could have done this initially, but why touch the memory
3539 allocator unless we're sure we have to?
3541 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3543 if((res
= s_mp_pad(a
, used
+ 1)) != MP_OKAY
)
3546 DIGIT(a
, ix
) = (mp_digit
)w
;
3550 if((res
= s_mp_pad(a
, used
+ 1)) != MP_OKAY
)
3553 DIGIT(a
, used
) = carry
;
3558 } /* end s_mp_add() */
3562 /* Compute c = |a| + |b| */ /* magnitude addition */
3563 mp_err
s_mp_add_3arg(const mp_int
*a
, const mp_int
*b
, mp_int
*c
)
3565 mp_digit
*pa
, *pb
, *pc
;
3566 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3569 mp_digit sum
, carry
= 0, d
;
3575 MP_SIGN(c
) = MP_SIGN(a
);
3576 if (MP_USED(a
) < MP_USED(b
)) {
3577 const mp_int
*xch
= a
;
3582 /* Make sure a has enough precision for the output value */
3583 if (MP_OKAY
!= (res
= s_mp_pad(c
, MP_USED(a
))))
3587 Add up all digits up to the precision of b. If b had initially
3588 the same precision as a, or greater, we took care of it by the
3589 exchange step above, so there is no problem. If b had initially
3590 less precision, we'll have to make sure the carry out is duly
3591 propagated upward among the higher-order digits of the sum.
3597 for (ix
= 0; ix
< used
; ix
++) {
3598 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3599 w
= w
+ *pa
++ + *pb
++;
3605 d
= (sum
< d
); /* detect overflow */
3606 *pc
++ = sum
+= carry
;
3607 carry
= d
+ (sum
< carry
); /* detect overflow */
3611 /* If we run out of 'b' digits before we're actually done, make
3612 sure the carries get propagated upward...
3614 for (used
= MP_USED(a
); ix
< used
; ++ix
) {
3615 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3620 *pc
++ = sum
= carry
+ *pa
++;
3621 carry
= (sum
< carry
);
3625 /* If there's an overall carry out, increase precision and include
3626 it. We could have done this initially, but why touch the memory
3627 allocator unless we're sure we have to?
3629 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3631 if((res
= s_mp_pad(c
, used
+ 1)) != MP_OKAY
)
3634 DIGIT(c
, used
) = (mp_digit
)w
;
3639 if((res
= s_mp_pad(c
, used
+ 1)) != MP_OKAY
)
3642 DIGIT(c
, used
) = carry
;
3649 /* {{{ s_mp_add_offset(a, b, offset) */
3651 /* Compute a = |a| + ( |b| * (RADIX ** offset) ) */
3652 mp_err
s_mp_add_offset(mp_int
*a
, mp_int
*b
, mp_size offset
)
3654 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3657 mp_digit d
, sum
, carry
= 0;
3664 /* Make sure a has enough precision for the output value */
3665 lim
= MP_USED(b
) + offset
;
3666 if((lim
> USED(a
)) && (res
= s_mp_pad(a
, lim
)) != MP_OKAY
)
3670 Add up all digits up to the precision of b. If b had initially
3671 the same precision as a, or greater, we took care of it by the
3672 padding step above, so there is no problem. If b had initially
3673 less precision, we'll have to make sure the carry out is duly
3674 propagated upward among the higher-order digits of the sum.
3677 for(ib
= 0, ia
= offset
; ib
< lim
; ib
++, ia
++) {
3678 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3679 w
= (mp_word
)DIGIT(a
, ia
) + DIGIT(b
, ib
) + k
;
3680 DIGIT(a
, ia
) = ACCUM(w
);
3683 d
= MP_DIGIT(a
, ia
);
3684 sum
= d
+ MP_DIGIT(b
, ib
);
3686 MP_DIGIT(a
,ia
) = sum
+= carry
;
3687 carry
= d
+ (sum
< carry
);
3691 /* If we run out of 'b' digits before we're actually done, make
3692 sure the carries get propagated upward...
3694 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3695 for (lim
= MP_USED(a
); k
&& (ia
< lim
); ++ia
) {
3696 w
= (mp_word
)DIGIT(a
, ia
) + k
;
3697 DIGIT(a
, ia
) = ACCUM(w
);
3701 for (lim
= MP_USED(a
); carry
&& (ia
< lim
); ++ia
) {
3702 d
= MP_DIGIT(a
, ia
);
3703 MP_DIGIT(a
,ia
) = sum
= d
+ carry
;
3708 /* If there's an overall carry out, increase precision and include
3709 it. We could have done this initially, but why touch the memory
3710 allocator unless we're sure we have to?
3712 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
3714 if((res
= s_mp_pad(a
, USED(a
) + 1)) != MP_OKAY
)
3717 DIGIT(a
, ia
) = (mp_digit
)k
;
3721 if((res
= s_mp_pad(a
, lim
+ 1)) != MP_OKAY
)
3724 DIGIT(a
, lim
) = carry
;
3731 } /* end s_mp_add_offset() */
3735 /* {{{ s_mp_sub(a, b) */
3737 /* Compute a = |a| - |b|, assumes |a| >= |b| */
3738 mp_err
s_mp_sub(mp_int
*a
, const mp_int
*b
) /* magnitude subtract */
3740 mp_digit
*pa
, *pb
, *limit
;
3741 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3744 mp_digit d
, diff
, borrow
= 0;
3748 Subtract and propagate borrow. Up to the precision of b, this
3749 accounts for the digits of b; after that, we just make sure the
3750 carries get to the right place. This saves having to pad b out to
3751 the precision of a just to make the loops work right...
3755 limit
= pb
+ MP_USED(b
);
3756 while (pb
< limit
) {
3757 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3758 w
= w
+ *pa
- *pb
++;
3764 d
= (diff
> d
); /* detect borrow */
3765 if (borrow
&& --diff
== MP_DIGIT_MAX
)
3771 limit
= MP_DIGITS(a
) + MP_USED(a
);
3772 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3773 while (w
&& pa
< limit
) {
3779 while (borrow
&& pa
< limit
) {
3781 *pa
++ = diff
= d
- borrow
;
3782 borrow
= (diff
> d
);
3786 /* Clobber any leading zeroes we created */
3790 If there was a borrow out, then |b| > |a| in violation
3791 of our input invariant. We've already done the work,
3792 but we'll at least complain about it...
3794 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3795 return w
? MP_RANGE
: MP_OKAY
;
3797 return borrow
? MP_RANGE
: MP_OKAY
;
3799 } /* end s_mp_sub() */
3803 /* Compute c = |a| - |b|, assumes |a| >= |b| */ /* magnitude subtract */
3804 mp_err
s_mp_sub_3arg(const mp_int
*a
, const mp_int
*b
, mp_int
*c
)
3806 mp_digit
*pa
, *pb
, *pc
;
3807 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3810 mp_digit d
, diff
, borrow
= 0;
3815 MP_SIGN(c
) = MP_SIGN(a
);
3817 /* Make sure a has enough precision for the output value */
3818 if (MP_OKAY
!= (res
= s_mp_pad(c
, MP_USED(a
))))
3822 Subtract and propagate borrow. Up to the precision of b, this
3823 accounts for the digits of b; after that, we just make sure the
3824 carries get to the right place. This saves having to pad b out to
3825 the precision of a just to make the loops work right...
3831 for (ix
= 0; ix
< limit
; ++ix
) {
3832 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3833 w
= w
+ *pa
++ - *pb
++;
3840 if (borrow
&& --diff
== MP_DIGIT_MAX
)
3846 for (limit
= MP_USED(a
); ix
< limit
; ++ix
) {
3847 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3853 *pc
++ = diff
= d
- borrow
;
3854 borrow
= (diff
> d
);
3858 /* Clobber any leading zeroes we created */
3863 If there was a borrow out, then |b| > |a| in violation
3864 of our input invariant. We've already done the work,
3865 but we'll at least complain about it...
3867 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
3868 return w
? MP_RANGE
: MP_OKAY
;
3870 return borrow
? MP_RANGE
: MP_OKAY
;
3873 /* {{{ s_mp_mul(a, b) */
3875 /* Compute a = |a| * |b| */
3876 mp_err
s_mp_mul(mp_int
*a
, const mp_int
*b
)
3878 return mp_mul(a
, b
, a
);
3879 } /* end s_mp_mul() */
3883 #if defined(MP_USE_UINT_DIGIT) && defined(MP_USE_LONG_LONG_MULTIPLY)
3884 /* This trick works on Sparc V8 CPUs with the Workshop compilers. */
3885 #define MP_MUL_DxD(a, b, Phi, Plo) \
3886 { unsigned long long product = (unsigned long long)a * b; \
3887 Plo = (mp_digit)product; \
3888 Phi = (mp_digit)(product >> MP_DIGIT_BIT); }
3890 #define MP_MUL_DxD(a, b, Phi, Plo) \
3891 { Plo = asm ("mulq %a0, %a1, %v0", a, b);\
3892 Phi = asm ("umulh %a0, %a1, %v0", a, b); }
3894 #define MP_MUL_DxD(a, b, Phi, Plo) \
3895 { mp_digit a0b1, a1b0; \
3896 Plo = (a & MP_HALF_DIGIT_MAX) * (b & MP_HALF_DIGIT_MAX); \
3897 Phi = (a >> MP_HALF_DIGIT_BIT) * (b >> MP_HALF_DIGIT_BIT); \
3898 a0b1 = (a & MP_HALF_DIGIT_MAX) * (b >> MP_HALF_DIGIT_BIT); \
3899 a1b0 = (a >> MP_HALF_DIGIT_BIT) * (b & MP_HALF_DIGIT_MAX); \
3901 Phi += a1b0 >> MP_HALF_DIGIT_BIT; \
3903 Phi += MP_HALF_RADIX; \
3904 a1b0 <<= MP_HALF_DIGIT_BIT; \
3911 #if !defined(MP_ASSEMBLY_MULTIPLY)
3913 void s_mpv_mul_d(const mp_digit
*a
, mp_size a_len
, mp_digit b
, mp_digit
*c
)
3915 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
3918 /* Inner product: Digits of a */
3920 mp_word w
= ((mp_word
)b
* *a
++) + d
;
3928 mp_digit a_i
= *a
++;
3929 mp_digit a0b0
, a1b1
;
3931 MP_MUL_DxD(a_i
, b
, a1b1
, a0b0
);
3944 void s_mpv_mul_d_add(const mp_digit
*a
, mp_size a_len
, mp_digit b
,
3947 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
3950 /* Inner product: Digits of a */
3952 mp_word w
= ((mp_word
)b
* *a
++) + *c
+ d
;
3960 mp_digit a_i
= *a
++;
3961 mp_digit a0b0
, a1b1
;
3963 MP_MUL_DxD(a_i
, b
, a1b1
, a0b0
);
3978 /* Presently, this is only used by the Montgomery arithmetic code. */
3980 void s_mpv_mul_d_add_prop(const mp_digit
*a
, mp_size a_len
, mp_digit b
, mp_digit
*c
)
3982 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
3985 /* Inner product: Digits of a */
3987 mp_word w
= ((mp_word
)b
* *a
++) + *c
+ d
;
3993 mp_word w
= (mp_word
)*c
+ d
;
4000 mp_digit a_i
= *a
++;
4001 mp_digit a0b0
, a1b1
;
4003 MP_MUL_DxD(a_i
, b
, a1b1
, a0b0
);
4020 carry
= carry
< c_i
;
4026 #if defined(MP_USE_UINT_DIGIT) && defined(MP_USE_LONG_LONG_MULTIPLY)
4027 /* This trick works on Sparc V8 CPUs with the Workshop compilers. */
4028 #define MP_SQR_D(a, Phi, Plo) \
4029 { unsigned long long square = (unsigned long long)a * a; \
4030 Plo = (mp_digit)square; \
4031 Phi = (mp_digit)(square >> MP_DIGIT_BIT); }
4033 #define MP_SQR_D(a, Phi, Plo) \
4034 { Plo = asm ("mulq %a0, %a0, %v0", a);\
4035 Phi = asm ("umulh %a0, %a0, %v0", a); }
4037 #define MP_SQR_D(a, Phi, Plo) \
4039 Plo = (a & MP_HALF_DIGIT_MAX) * (a & MP_HALF_DIGIT_MAX); \
4040 Phi = (a >> MP_HALF_DIGIT_BIT) * (a >> MP_HALF_DIGIT_BIT); \
4041 Pmid = (a & MP_HALF_DIGIT_MAX) * (a >> MP_HALF_DIGIT_BIT); \
4042 Phi += Pmid >> (MP_HALF_DIGIT_BIT - 1); \
4043 Pmid <<= (MP_HALF_DIGIT_BIT + 1); \
4050 #if !defined(MP_ASSEMBLY_SQUARE)
4051 /* Add the squares of the digits of a to the digits of b. */
4052 void s_mpv_sqr_add_prop(const mp_digit
*pa
, mp_size a_len
, mp_digit
*ps
)
4054 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
4060 #define ADD_SQUARE(n) \
4062 w += (d * (mp_word)d) + ps[2*n]; \
4063 ps[2*n] = ACCUM(w); \
4064 w = (w >> DIGIT_BIT) + ps[2*n+1]; \
4065 ps[2*n+1] = ACCUM(w); \
4066 w = (w >> DIGIT_BIT)
4068 for (ix
= a_len
; ix
>= 4; ix
-= 4) {
4080 case 3: ADD_SQUARE(-3); /* FALLTHRU */
4081 case 2: ADD_SQUARE(-2); /* FALLTHRU */
4082 case 1: ADD_SQUARE(-1); /* FALLTHRU */
4089 w
= (w
>> DIGIT_BIT
);
4094 mp_digit a_i
= *pa
++;
4095 mp_digit a0a0
, a1a1
;
4097 MP_SQR_D(a_i
, a1a1
, a0a0
);
4099 /* here a1a1 and a0a0 constitute a_i ** 2 */
4110 carry
= (a1a1
< a_i
);
4117 carry
= carry
< s_i
;
4123 #if (defined(MP_NO_MP_WORD) || defined(MP_NO_DIV_WORD)) \
4124 && !defined(MP_ASSEMBLY_DIV_2DX1D)
4126 ** Divide 64-bit (Nhi,Nlo) by 32-bit divisor, which must be normalized
4127 ** so its high bit is 1. This code is from NSPR.
4129 mp_err
s_mpv_div_2dx1d(mp_digit Nhi
, mp_digit Nlo
, mp_digit divisor
,
4130 mp_digit
*qp
, mp_digit
*rp
)
4132 mp_digit d1
, d0
, q1
, q0
;
4135 d1
= divisor
>> MP_HALF_DIGIT_BIT
;
4136 d0
= divisor
& MP_HALF_DIGIT_MAX
;
4140 r1
= (r1
<< MP_HALF_DIGIT_BIT
) | (Nlo
>> MP_HALF_DIGIT_BIT
);
4142 q1
--, r1
+= divisor
;
4143 if (r1
>= divisor
&& r1
< m
) {
4144 q1
--, r1
+= divisor
;
4151 r0
= (r0
<< MP_HALF_DIGIT_BIT
) | (Nlo
& MP_HALF_DIGIT_MAX
);
4153 q0
--, r0
+= divisor
;
4154 if (r0
>= divisor
&& r0
< m
) {
4155 q0
--, r0
+= divisor
;
4159 *qp
= (q1
<< MP_HALF_DIGIT_BIT
) | q0
;
4167 /* {{{ s_mp_sqr(a) */
4169 mp_err
s_mp_sqr(mp_int
*a
)
4174 if((res
= mp_init_size(&tmp
, 2 * USED(a
))) != MP_OKAY
)
4176 res
= mp_sqr(a
, &tmp
);
4177 if (res
== MP_OKAY
) {
4187 /* {{{ s_mp_div(a, b) */
4192 Compute a = a / b and b = a mod b. Assumes b > a.
4195 mp_err
s_mp_div(mp_int
*rem
, /* i: dividend, o: remainder */
4196 mp_int
*div
, /* i: divisor */
4197 mp_int
*quot
) /* i: 0; o: quotient */
4200 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
4210 if(mp_cmp_z(div
) == 0)
4213 /* Shortcut if divisor is power of two */
4214 if((ix
= s_mp_ispow2(div
)) >= 0) {
4215 MP_CHECKOK( mp_copy(rem
, quot
) );
4216 s_mp_div_2d(quot
, (mp_digit
)ix
);
4217 s_mp_mod_2d(rem
, (mp_digit
)ix
);
4223 MP_SIGN(rem
) = ZPOS
;
4224 MP_SIGN(div
) = ZPOS
;
4226 /* A working temporary for division */
4227 MP_CHECKOK( mp_init_size(&t
, MP_ALLOC(rem
)));
4229 /* Normalize to optimize guessing */
4230 MP_CHECKOK( s_mp_norm(rem
, div
, &d
) );
4234 /* Perform the division itself...woo! */
4235 MP_USED(quot
) = MP_ALLOC(quot
);
4237 /* Find a partial substring of rem which is at least div */
4238 /* If we didn't find one, we're finished dividing */
4239 while (MP_USED(rem
) > MP_USED(div
) || s_mp_cmp(rem
, div
) >= 0) {
4243 unusedRem
= MP_USED(rem
) - MP_USED(div
);
4244 MP_DIGITS(&part
) = MP_DIGITS(rem
) + unusedRem
;
4245 MP_ALLOC(&part
) = MP_ALLOC(rem
) - unusedRem
;
4246 MP_USED(&part
) = MP_USED(div
);
4247 if (s_mp_cmp(&part
, div
) < 0) {
4250 assert(unusedRem
>= 0);
4252 -- MP_DIGITS(&part
);
4257 /* Compute a guess for the next quotient digit */
4258 q_msd
= MP_DIGIT(&part
, MP_USED(&part
) - 1);
4259 div_msd
= MP_DIGIT(div
, MP_USED(div
) - 1);
4260 if (q_msd
>= div_msd
) {
4262 } else if (MP_USED(&part
) > 1) {
4263 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
4264 q_msd
= (q_msd
<< MP_DIGIT_BIT
) | MP_DIGIT(&part
, MP_USED(&part
) - 2);
4270 MP_CHECKOK( s_mpv_div_2dx1d(q_msd
, MP_DIGIT(&part
, MP_USED(&part
) - 2),
4271 div_msd
, &q_msd
, &r
) );
4277 assert(q_msd
> 0); /* This case should never occur any more. */
4282 /* See what that multiplies out to */
4284 MP_CHECKOK( s_mp_mul_d(&t
, (mp_digit
)q_msd
) );
4287 If it's too big, back it off. We should not have to do this
4288 more than once, or, in rare cases, twice. Knuth describes a
4289 method by which this could be reduced to a maximum of once, but
4290 I didn't implement that here.
4291 * When using s_mpv_div_2dx1d, we may have to do this 3 times.
4293 for (i
= 4; s_mp_cmp(&t
, &part
) > 0 && i
> 0; --i
) {
4295 s_mp_sub(&t
, div
); /* t -= div */
4302 /* At this point, q_msd should be the right next digit */
4303 MP_CHECKOK( s_mp_sub(&part
, &t
) ); /* part -= t */
4307 Include the digit in the quotient. We allocated enough memory
4308 for any quotient we could ever possibly get, so we should not
4309 have to check for failures here
4311 MP_DIGIT(quot
, unusedRem
) = (mp_digit
)q_msd
;
4314 /* Denormalize remainder */
4316 s_mp_div_2d(rem
, d
);
4326 } /* end s_mp_div() */
4331 /* {{{ s_mp_2expt(a, k) */
4333 mp_err
s_mp_2expt(mp_int
*a
, mp_digit k
)
4338 dig
= k
/ DIGIT_BIT
;
4339 bit
= k
% DIGIT_BIT
;
4342 if((res
= s_mp_pad(a
, dig
+ 1)) != MP_OKAY
)
4345 DIGIT(a
, dig
) |= ((mp_digit
)1 << bit
);
4349 } /* end s_mp_2expt() */
4353 /* {{{ s_mp_reduce(x, m, mu) */
4356 Compute Barrett reduction, x (mod m), given a precomputed value for
4357 mu = b^2k / m, where b = RADIX and k = #digits(m). This should be
4358 faster than straight division, when many reductions by the same
4359 value of m are required (such as in modular exponentiation). This
4360 can nearly halve the time required to do modular exponentiation,
4361 as compared to using the full integer divide to reduce.
4363 This algorithm was derived from the _Handbook of Applied
4364 Cryptography_ by Menezes, Oorschot and VanStone, Ch. 14,
4368 mp_err
s_mp_reduce(mp_int
*x
, const mp_int
*m
, const mp_int
*mu
)
4373 if((res
= mp_init_copy(&q
, x
)) != MP_OKAY
)
4376 s_mp_rshd(&q
, USED(m
) - 1); /* q1 = x / b^(k-1) */
4377 s_mp_mul(&q
, mu
); /* q2 = q1 * mu */
4378 s_mp_rshd(&q
, USED(m
) + 1); /* q3 = q2 / b^(k+1) */
4380 /* x = x mod b^(k+1), quick (no division) */
4381 s_mp_mod_2d(x
, DIGIT_BIT
* (USED(m
) + 1));
4383 /* q = q * m mod b^(k+1), quick (no division) */
4385 s_mp_mod_2d(&q
, DIGIT_BIT
* (USED(m
) + 1));
4388 if((res
= mp_sub(x
, &q
, x
)) != MP_OKAY
)
4391 /* If x < 0, add b^(k+1) to it */
4392 if(mp_cmp_z(x
) < 0) {
4394 if((res
= s_mp_lshd(&q
, USED(m
) + 1)) != MP_OKAY
)
4396 if((res
= mp_add(x
, &q
, x
)) != MP_OKAY
)
4400 /* Back off if it's too big */
4401 while(mp_cmp(x
, m
) >= 0) {
4402 if((res
= s_mp_sub(x
, m
)) != MP_OKAY
)
4411 } /* end s_mp_reduce() */
4417 /* {{{ Primitive comparisons */
4419 /* {{{ s_mp_cmp(a, b) */
4421 /* Compare |a| <=> |b|, return 0 if equal, <0 if a<b, >0 if a>b */
4422 int s_mp_cmp(const mp_int
*a
, const mp_int
*b
)
4424 mp_size used_a
= MP_USED(a
);
4426 mp_size used_b
= MP_USED(b
);
4428 if (used_a
> used_b
)
4430 if (used_a
< used_b
)
4435 mp_digit da
= 0, db
= 0;
4437 #define CMP_AB(n) if ((da = pa[n]) != (db = pb[n])) goto done
4439 pa
= MP_DIGITS(a
) + used_a
;
4440 pb
= MP_DIGITS(b
) + used_a
;
4441 while (used_a
>= 4) {
4450 while (used_a
-- > 0 && ((da
= *--pa
) == (db
= *--pb
)))
4463 } /* end s_mp_cmp() */
4467 /* {{{ s_mp_cmp_d(a, d) */
4469 /* Compare |a| <=> d, return 0 if equal, <0 if a<d, >0 if a>d */
4470 int s_mp_cmp_d(const mp_int
*a
, mp_digit d
)
4477 else if(DIGIT(a
, 0) > d
)
4482 } /* end s_mp_cmp_d() */
4486 /* {{{ s_mp_ispow2(v) */
4489 Returns -1 if the value is not a power of two; otherwise, it returns
4490 k such that v = 2^k, i.e. lg(v).
4492 int s_mp_ispow2(const mp_int
*v
)
4497 ix
= MP_USED(v
) - 1;
4498 d
= MP_DIGIT(v
, ix
); /* most significant digit of v */
4500 extra
= s_mp_ispow2d(d
);
4501 if (extra
< 0 || ix
== 0)
4505 if (DIGIT(v
, ix
) != 0)
4506 return -1; /* not a power of two */
4507 extra
+= MP_DIGIT_BIT
;
4512 } /* end s_mp_ispow2() */
4516 /* {{{ s_mp_ispow2d(d) */
4518 int s_mp_ispow2d(mp_digit d
)
4520 if ((d
!= 0) && ((d
& (d
-1)) == 0)) { /* d is a power of 2 */
4522 #if defined (MP_USE_UINT_DIGIT)
4523 if (d
& 0xffff0000U
)
4525 if (d
& 0xff00ff00U
)
4527 if (d
& 0xf0f0f0f0U
)
4529 if (d
& 0xccccccccU
)
4531 if (d
& 0xaaaaaaaaU
)
4533 #elif defined(MP_USE_LONG_LONG_DIGIT)
4534 if (d
& 0xffffffff00000000ULL
)
4536 if (d
& 0xffff0000ffff0000ULL
)
4538 if (d
& 0xff00ff00ff00ff00ULL
)
4540 if (d
& 0xf0f0f0f0f0f0f0f0ULL
)
4542 if (d
& 0xccccccccccccccccULL
)
4544 if (d
& 0xaaaaaaaaaaaaaaaaULL
)
4546 #elif defined(MP_USE_LONG_DIGIT)
4547 if (d
& 0xffffffff00000000UL
)
4549 if (d
& 0xffff0000ffff0000UL
)
4551 if (d
& 0xff00ff00ff00ff00UL
)
4553 if (d
& 0xf0f0f0f0f0f0f0f0UL
)
4555 if (d
& 0xccccccccccccccccUL
)
4557 if (d
& 0xaaaaaaaaaaaaaaaaUL
)
4560 #error "unknown type for mp_digit"
4566 } /* end s_mp_ispow2d() */
4572 /* {{{ Primitive I/O helpers */
4574 /* {{{ s_mp_tovalue(ch, r) */
4577 Convert the given character to its digit value, in the given radix.
4578 If the given character is not understood in the given radix, -1 is
4579 returned. Otherwise the digit's numeric value is returned.
4581 The results will be odd if you use a radix < 2 or > 62, you are
4582 expected to know what you're up to.
4584 int s_mp_tovalue(char ch
, int r
)
4595 else if(isupper(xch
))
4596 val
= xch
- 'A' + 10;
4597 else if(islower(xch
))
4598 val
= xch
- 'a' + 36;
4606 if(val
< 0 || val
>= r
)
4611 } /* end s_mp_tovalue() */
4615 /* {{{ s_mp_todigit(val, r, low) */
4618 Convert val to a radix-r digit, if possible. If val is out of range
4619 for r, returns zero. Otherwise, returns an ASCII character denoting
4620 the value in the given radix.
4622 The results may be odd if you use a radix < 2 or > 64, you are
4623 expected to know what you're doing.
4626 char s_mp_todigit(mp_digit val
, int r
, int low
)
4640 } /* end s_mp_todigit() */
4644 /* {{{ s_mp_outlen(bits, radix) */
4647 Return an estimate for how long a string is needed to hold a radix
4648 r representation of a number with 'bits' significant bits, plus an
4649 extra for a zero terminator (assuming C style strings here)
4651 int s_mp_outlen(int bits
, int r
)
4653 return (int)((double)bits
* LOG_V_2(r
) + 1.5) + 1;
4655 } /* end s_mp_outlen() */
4661 /* {{{ mp_read_unsigned_octets(mp, str, len) */
4662 /* mp_read_unsigned_octets(mp, str, len)
4663 Read in a raw value (base 256) into the given mp_int
4664 No sign bit, number is positive. Leading zeros ignored.
4668 mp_read_unsigned_octets(mp_int
*mp
, const unsigned char *str
, mp_size len
)
4674 ARGCHK(mp
!= NULL
&& str
!= NULL
&& len
> 0, MP_BADARG
);
4678 count
= len
% sizeof(mp_digit
);
4680 for (d
= 0; count
-- > 0; --len
) {
4681 d
= (d
<< 8) | *str
++;
4683 MP_DIGIT(mp
, 0) = d
;
4686 /* Read the rest of the digits */
4687 for(; len
> 0; len
-= sizeof(mp_digit
)) {
4688 for (d
= 0, count
= sizeof(mp_digit
); count
> 0; --count
) {
4689 d
= (d
<< 8) | *str
++;
4691 if (MP_EQ
== mp_cmp_z(mp
)) {
4695 if((res
= s_mp_lshd(mp
, 1)) != MP_OKAY
)
4698 MP_DIGIT(mp
, 0) = d
;
4701 } /* end mp_read_unsigned_octets() */
4704 /* {{{ mp_unsigned_octet_size(mp) */
4706 mp_unsigned_octet_size(const mp_int
*mp
)
4712 ARGCHK(mp
!= NULL
, MP_BADARG
);
4713 ARGCHK(MP_ZPOS
== SIGN(mp
), MP_BADARG
);
4715 bytes
= (USED(mp
) * sizeof(mp_digit
));
4717 /* subtract leading zeros. */
4718 /* Iterate over each digit... */
4719 for(ix
= USED(mp
) - 1; ix
>= 0; ix
--) {
4728 /* Have MSD, check digit bytes, high order first */
4729 for(ix
= sizeof(mp_digit
) - 1; ix
>= 0; ix
--) {
4730 unsigned char x
= (unsigned char)(d
>> (ix
* CHAR_BIT
));
4736 } /* end mp_unsigned_octet_size() */
4739 /* {{{ mp_to_unsigned_octets(mp, str) */
4740 /* output a buffer of big endian octets no longer than specified. */
4742 mp_to_unsigned_octets(const mp_int
*mp
, unsigned char *str
, mp_size maxlen
)
4747 ARGCHK(mp
!= NULL
&& str
!= NULL
&& !SIGN(mp
), MP_BADARG
);
4749 bytes
= mp_unsigned_octet_size(mp
);
4750 ARGCHK(bytes
<= maxlen
, MP_BADARG
);
4752 /* Iterate over each digit... */
4753 for(ix
= USED(mp
) - 1; ix
>= 0; ix
--) {
4754 mp_digit d
= DIGIT(mp
, ix
);
4757 /* Unpack digit bytes, high order first */
4758 for(jx
= sizeof(mp_digit
) - 1; jx
>= 0; jx
--) {
4759 unsigned char x
= (unsigned char)(d
>> (jx
* CHAR_BIT
));
4760 if (!pos
&& !x
) /* suppress leading zeros */
4768 } /* end mp_to_unsigned_octets() */
4771 /* {{{ mp_to_signed_octets(mp, str) */
4772 /* output a buffer of big endian octets no longer than specified. */
4774 mp_to_signed_octets(const mp_int
*mp
, unsigned char *str
, mp_size maxlen
)
4779 ARGCHK(mp
!= NULL
&& str
!= NULL
&& !SIGN(mp
), MP_BADARG
);
4781 bytes
= mp_unsigned_octet_size(mp
);
4782 ARGCHK(bytes
<= maxlen
, MP_BADARG
);
4784 /* Iterate over each digit... */
4785 for(ix
= USED(mp
) - 1; ix
>= 0; ix
--) {
4786 mp_digit d
= DIGIT(mp
, ix
);
4789 /* Unpack digit bytes, high order first */
4790 for(jx
= sizeof(mp_digit
) - 1; jx
>= 0; jx
--) {
4791 unsigned char x
= (unsigned char)(d
>> (jx
* CHAR_BIT
));
4793 if (!x
) /* suppress leading zeros */
4795 if (x
& 0x80) { /* add one leading zero to make output positive. */
4796 ARGCHK(bytes
+ 1 <= maxlen
, MP_BADARG
);
4797 if (bytes
+ 1 > maxlen
)
4808 } /* end mp_to_signed_octets() */
4811 /* {{{ mp_to_fixlen_octets(mp, str) */
4812 /* output a buffer of big endian octets exactly as long as requested. */
4814 mp_to_fixlen_octets(const mp_int
*mp
, unsigned char *str
, mp_size length
)
4819 ARGCHK(mp
!= NULL
&& str
!= NULL
&& !SIGN(mp
), MP_BADARG
);
4821 bytes
= mp_unsigned_octet_size(mp
);
4822 ARGCHK(bytes
<= length
, MP_BADARG
);
4824 /* place any needed leading zeros */
4825 for (;length
> bytes
; --length
) {
4829 /* Iterate over each digit... */
4830 for(ix
= USED(mp
) - 1; ix
>= 0; ix
--) {
4831 mp_digit d
= DIGIT(mp
, ix
);
4834 /* Unpack digit bytes, high order first */
4835 for(jx
= sizeof(mp_digit
) - 1; jx
>= 0; jx
--) {
4836 unsigned char x
= (unsigned char)(d
>> (jx
* CHAR_BIT
));
4837 if (!pos
&& !x
) /* suppress leading zeros */
4845 } /* end mp_to_fixlen_octets() */
4849 /*------------------------------------------------------------------------*/
4850 /* HERE THERE BE DRAGONS */