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@gmail.com, http://math.libtomcrypt.com
18 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
20 s_mp_sub (mp_int
* a
, mp_int
* b
, mp_int
* c
)
22 int olduse
, res
, min
, max
;
30 if ((res
= mp_grow (c
, max
)) != MP_OKAY
) {
38 register mp_digit u
, *tmpa
, *tmpb
, *tmpc
;
41 /* alias for digit pointers */
46 /* set carry to zero */
48 for (i
= 0; i
< min
; i
++) {
49 /* T[i] = A[i] - B[i] - U */
50 *tmpc
= *tmpa
++ - *tmpb
++ - u
;
52 /* U = carry bit of T[i]
53 * Note this saves performing an AND operation since
54 * if a carry does occur it will propagate all the way to the
55 * MSB. As a result a single shift is enough to get the carry
57 u
= *tmpc
>> ((mp_digit
)(CHAR_BIT
* sizeof (mp_digit
) - 1));
59 /* Clear carry from T[i] */
63 /* now copy higher words if any, e.g. if A has more digits than B */
64 for (; i
< max
; i
++) {
68 /* U = carry bit of T[i] */
69 u
= *tmpc
>> ((mp_digit
)(CHAR_BIT
* sizeof (mp_digit
) - 1));
71 /* Clear carry from T[i] */
75 /* clear digits above used (since we may not have grown result above) */
76 for (i
= c
->used
; i
< olduse
; i
++) {
87 /* $Source: /cvs/libtom/libtommath/bn_s_mp_sub.c,v $ */
88 /* $Revision: 1.3 $ */
89 /* $Date: 2006/03/31 14:18:44 $ */