3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
12 * The library is free for all purposes without any express
15 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
18 /* single digit subtraction */
20 mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
22 mp_digit *tmpa, *tmpc, mu;
25 /* grow c as required */
26 if (c->alloc < a->used + 1) {
27 if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) {
32 /* if a is negative just do an unsigned
33 * addition [with fudged signs]
35 if (a->sign == MP_NEG) {
37 res = mp_add_d(a, b, c);
38 a->sign = c->sign = MP_NEG;
47 /* if a <= b simply fix the single digit */
48 if ((a->used == 1 && a->dp[0] <= b) || a->used == 0) {
64 /* subtract first digit */
66 mu = *tmpc >> (sizeof(mp_digit) * CHAR_BIT - 1);
69 /* handle rest of the digits */
70 for (ix = 1; ix < a->used; ix++) {
72 mu = *tmpc >> (sizeof(mp_digit) * CHAR_BIT - 1);
77 /* zero excess digits */
78 while (ix++ < oldused) {