1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
3 * LibTomMath is library that provides for multiple-precision
4 * integer arithmetic as well as number theoretic functionality.
6 * The library is designed directly after the MPI library by
7 * Michael Fromberger but has been written from scratch with
8 * additional optimizations in place.
10 * The library is free for all purposes without any express
13 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
17 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
19 s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
21 int olduse, res, min, max;
29 if ((res = mp_grow (c, max)) != MP_OKAY) {
37 register mp_digit u, *tmpa, *tmpb, *tmpc;
40 /* alias for digit pointers */
45 /* set carry to zero */
47 for (i = 0; i < min; i++) {
48 /* T[i] = A[i] - B[i] - U */
49 *tmpc = *tmpa++ - *tmpb++ - u;
51 /* U = carry bit of T[i]
52 * Note this saves performing an AND operation since
53 * if a carry does occur it will propagate all the way to the
54 * MSB. As a result a single shift is enough to get the carry
56 u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1));
58 /* Clear carry from T[i] */
62 /* now copy higher words if any, e.g. if A has more digits than B */
63 for (; i < max; i++) {
67 /* U = carry bit of T[i] */
68 u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1));
70 /* Clear carry from T[i] */
74 /* clear digits above used (since we may not have grown result above) */
75 for (i = c->used; i < olduse; i++) {