Mercurial > dropbear
comparison libtommath/bn_s_mp_sub.c @ 1692:1051e4eea25a
Update LibTomMath to 1.2.0 (#84)
* update C files
* update other files
* update headers
* update makefiles
* remove mp_set/get_double()
* use ltm 1.2.0 API
* update ltm_desc
* use bundled tommath if system-tommath is too old
* XMALLOC etc. were changed to MP_MALLOC etc.
author | Steffen Jaeckel <s@jaeckel.eu> |
---|---|
date | Tue, 26 May 2020 17:36:47 +0200 |
parents | f52919ffd3b1 |
children |
comparison
equal
deleted
inserted
replaced
1691:2d3745d58843 | 1692:1051e4eea25a |
---|---|
1 #include "tommath_private.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 /* SPDX-License-Identifier: Unlicense */ |
5 * LibTomMath is a library that provides multiple-precision | |
6 * integer arithmetic as well as number theoretic functionality. | |
7 * | |
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. | |
11 * | |
12 * SPDX-License-Identifier: Unlicense | |
13 */ | |
14 | 5 |
15 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */ | 6 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */ |
16 int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) | 7 mp_err s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c) |
17 { | 8 { |
18 int olduse, res, min, max; | 9 int olduse, min, max; |
10 mp_err err; | |
19 | 11 |
20 /* find sizes */ | 12 /* find sizes */ |
21 min = b->used; | 13 min = b->used; |
22 max = a->used; | 14 max = a->used; |
23 | 15 |
24 /* init result */ | 16 /* init result */ |
25 if (c->alloc < max) { | 17 if (c->alloc < max) { |
26 if ((res = mp_grow(c, max)) != MP_OKAY) { | 18 if ((err = mp_grow(c, max)) != MP_OKAY) { |
27 return res; | 19 return err; |
28 } | 20 } |
29 } | 21 } |
30 olduse = c->used; | 22 olduse = c->used; |
31 c->used = max; | 23 c->used = max; |
32 | 24 |
48 /* U = carry bit of T[i] | 40 /* U = carry bit of T[i] |
49 * Note this saves performing an AND operation since | 41 * Note this saves performing an AND operation since |
50 * if a carry does occur it will propagate all the way to the | 42 * if a carry does occur it will propagate all the way to the |
51 * MSB. As a result a single shift is enough to get the carry | 43 * MSB. As a result a single shift is enough to get the carry |
52 */ | 44 */ |
53 u = *tmpc >> (((size_t)CHAR_BIT * sizeof(mp_digit)) - 1u); | 45 u = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u); |
54 | 46 |
55 /* Clear carry from T[i] */ | 47 /* Clear carry from T[i] */ |
56 *tmpc++ &= MP_MASK; | 48 *tmpc++ &= MP_MASK; |
57 } | 49 } |
58 | 50 |
60 for (; i < max; i++) { | 52 for (; i < max; i++) { |
61 /* T[i] = A[i] - U */ | 53 /* T[i] = A[i] - U */ |
62 *tmpc = *tmpa++ - u; | 54 *tmpc = *tmpa++ - u; |
63 | 55 |
64 /* U = carry bit of T[i] */ | 56 /* U = carry bit of T[i] */ |
65 u = *tmpc >> (((size_t)CHAR_BIT * sizeof(mp_digit)) - 1u); | 57 u = *tmpc >> (MP_SIZEOF_BITS(mp_digit) - 1u); |
66 | 58 |
67 /* Clear carry from T[i] */ | 59 /* Clear carry from T[i] */ |
68 *tmpc++ &= MP_MASK; | 60 *tmpc++ &= MP_MASK; |
69 } | 61 } |
70 | 62 |
71 /* clear digits above used (since we may not have grown result above) */ | 63 /* clear digits above used (since we may not have grown result above) */ |
72 for (i = c->used; i < olduse; i++) { | 64 MP_ZERO_DIGITS(tmpc, olduse - c->used); |
73 *tmpc++ = 0; | |
74 } | |
75 } | 65 } |
76 | 66 |
77 mp_clamp(c); | 67 mp_clamp(c); |
78 return MP_OKAY; | 68 return MP_OKAY; |
79 } | 69 } |
80 | 70 |
81 #endif | 71 #endif |
82 | |
83 /* ref: HEAD -> master, tag: v1.1.0 */ | |
84 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */ | |
85 /* commit time: 2019-01-28 20:32:32 +0100 */ |