comparison libtommath/bn_s_mp_sub.c @ 1436:60fc6476e044

Update to libtommath v1.0
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Jun 2017 22:37:14 +0800
parents 5ff8218bcee9
children 8bba51a55704
comparison
equal deleted inserted replaced
1435:f849a5ca2efc 1436:60fc6476e044
1 #include <tommath.h> 1 #include <tommath_private.h>
2 #ifdef BN_S_MP_SUB_C 2 #ifdef BN_S_MP_SUB_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 * 4 *
5 * LibTomMath is a library that provides multiple-precision 5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality. 6 * integer arithmetic as well as number theoretic functionality.
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * The library is free for all purposes without any express
13 * guarantee it works. 13 * guarantee it works.
14 * 14 *
15 * Tom St Denis, [email protected], http://math.libtomcrypt.com 15 * Tom St Denis, [email protected], http://libtom.org
16 */ 16 */
17 17
18 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */ 18 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
19 int 19 int
20 s_mp_sub (mp_int * a, mp_int * b, mp_int * c) 20 s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
33 } 33 }
34 olduse = c->used; 34 olduse = c->used;
35 c->used = max; 35 c->used = max;
36 36
37 { 37 {
38 register mp_digit u, *tmpa, *tmpb, *tmpc; 38 mp_digit u, *tmpa, *tmpb, *tmpc;
39 register int i; 39 int i;
40 40
41 /* alias for digit pointers */ 41 /* alias for digit pointers */
42 tmpa = a->dp; 42 tmpa = a->dp;
43 tmpb = b->dp; 43 tmpb = b->dp;
44 tmpc = c->dp; 44 tmpc = c->dp;
45 45
46 /* set carry to zero */ 46 /* set carry to zero */
47 u = 0; 47 u = 0;
48 for (i = 0; i < min; i++) { 48 for (i = 0; i < min; i++) {
49 /* T[i] = A[i] - B[i] - U */ 49 /* T[i] = A[i] - B[i] - U */
50 *tmpc = *tmpa++ - *tmpb++ - u; 50 *tmpc = (*tmpa++ - *tmpb++) - u;
51 51
52 /* U = carry bit of T[i] 52 /* U = carry bit of T[i]
53 * Note this saves performing an AND operation since 53 * Note this saves performing an AND operation since
54 * if a carry does occur it will propagate all the way to the 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 55 * MSB. As a result a single shift is enough to get the carry
56 */ 56 */
57 u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1)); 57 u = *tmpc >> ((mp_digit)((CHAR_BIT * sizeof(mp_digit)) - 1));
58 58
59 /* Clear carry from T[i] */ 59 /* Clear carry from T[i] */
60 *tmpc++ &= MP_MASK; 60 *tmpc++ &= MP_MASK;
61 } 61 }
62 62
64 for (; i < max; i++) { 64 for (; i < max; i++) {
65 /* T[i] = A[i] - U */ 65 /* T[i] = A[i] - U */
66 *tmpc = *tmpa++ - u; 66 *tmpc = *tmpa++ - u;
67 67
68 /* U = carry bit of T[i] */ 68 /* U = carry bit of T[i] */
69 u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1)); 69 u = *tmpc >> ((mp_digit)((CHAR_BIT * sizeof(mp_digit)) - 1));
70 70
71 /* Clear carry from T[i] */ 71 /* Clear carry from T[i] */
72 *tmpc++ &= MP_MASK; 72 *tmpc++ &= MP_MASK;
73 } 73 }
74 74
82 return MP_OKAY; 82 return MP_OKAY;
83 } 83 }
84 84
85 #endif 85 #endif
86 86
87 /* $Source: /cvs/libtom/libtommath/bn_s_mp_sub.c,v $ */ 87 /* $Source$ */
88 /* $Revision: 1.3 $ */ 88 /* $Revision$ */
89 /* $Date: 2006/03/31 14:18:44 $ */ 89 /* $Date$ */