1 #include "tommath_private.h"
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
7 mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
18 if ((err = mp_grow(c, max)) != MP_OKAY) {
26 mp_digit u, *tmpa, *tmpb, *tmpc;
29 /* alias for digit pointers */
34 /* set carry to zero */
36 for (i = 0; i < min; i++) {
37 /* T[i] = A[i] - B[i] - U */
38 *tmpc = (*tmpa++ - *tmpb++) - u;
40 /* U = carry bit of T[i]
41 * Note this saves performing an AND operation since
42 * if a carry does occur it will propagate all the way to the
43 * MSB. As a result a single shift is enough to get the carry
45 u = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u);
47 /* Clear carry from T[i] */
51 /* now copy higher words if any, e.g. if A has more digits than B */
52 for (; i < max; i++) {
56 /* U = carry bit of T[i] */
57 u = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u);
59 /* Clear carry from T[i] */
63 /* clear digits above used (since we may not have grown result above) */
64 MP_ZERO_DIGITS(tmpc, olduse - c->used);