comparison libtommath/bn_mp_copy.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_MP_COPY_C 2 #ifdef BN_MP_COPY_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 /* copy, b = a */ 6 /* copy, b = a */
16 int mp_copy(const mp_int *a, mp_int *b) 7 mp_err mp_copy(const mp_int *a, mp_int *b)
17 { 8 {
18 int res, n; 9 int n;
10 mp_digit *tmpa, *tmpb;
11 mp_err err;
19 12
20 /* if dst == src do nothing */ 13 /* if dst == src do nothing */
21 if (a == b) { 14 if (a == b) {
22 return MP_OKAY; 15 return MP_OKAY;
23 } 16 }
24 17
25 /* grow dest */ 18 /* grow dest */
26 if (b->alloc < a->used) { 19 if (b->alloc < a->used) {
27 if ((res = mp_grow(b, a->used)) != MP_OKAY) { 20 if ((err = mp_grow(b, a->used)) != MP_OKAY) {
28 return res; 21 return err;
29 } 22 }
30 } 23 }
31 24
32 /* zero b and copy the parameters over */ 25 /* zero b and copy the parameters over */
33 { 26 /* pointer aliases */
34 mp_digit *tmpa, *tmpb;
35 27
36 /* pointer aliases */ 28 /* source */
29 tmpa = a->dp;
37 30
38 /* source */ 31 /* destination */
39 tmpa = a->dp; 32 tmpb = b->dp;
40 33
41 /* destination */ 34 /* copy all the digits */
42 tmpb = b->dp; 35 for (n = 0; n < a->used; n++) {
36 *tmpb++ = *tmpa++;
37 }
43 38
44 /* copy all the digits */ 39 /* clear high digits */
45 for (n = 0; n < a->used; n++) { 40 MP_ZERO_DIGITS(tmpb, b->used - n);
46 *tmpb++ = *tmpa++;
47 }
48
49 /* clear high digits */
50 for (; n < b->used; n++) {
51 *tmpb++ = 0;
52 }
53 }
54 41
55 /* copy used count and sign */ 42 /* copy used count and sign */
56 b->used = a->used; 43 b->used = a->used;
57 b->sign = a->sign; 44 b->sign = a->sign;
58 return MP_OKAY; 45 return MP_OKAY;
59 } 46 }
60 #endif 47 #endif
61
62 /* ref: HEAD -> master, tag: v1.1.0 */
63 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
64 /* commit time: 2019-01-28 20:32:32 +0100 */