comparison libtommath/bn_mp_lshd.c @ 1739:13d834efc376 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Thu, 15 Oct 2020 19:55:15 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1562:768ebf737aa0 1739:13d834efc376
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_LSHD_C 2 #ifdef BN_MP_LSHD_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 * The library is free for all purposes without any express
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */
17 5
18 /* shift left a certain amount of digits */ 6 /* shift left a certain amount of digits */
19 int mp_lshd (mp_int * a, int b) 7 mp_err mp_lshd(mp_int *a, int b)
20 { 8 {
21 int x, res; 9 int x;
10 mp_err err;
11 mp_digit *top, *bottom;
22 12
23 /* if its less than zero return */ 13 /* if its less than zero return */
24 if (b <= 0) { 14 if (b <= 0) {
25 return MP_OKAY; 15 return MP_OKAY;
26 } 16 }
17 /* no need to shift 0 around */
18 if (MP_IS_ZERO(a)) {
19 return MP_OKAY;
20 }
27 21
28 /* grow to fit the new digits */ 22 /* grow to fit the new digits */
29 if (a->alloc < (a->used + b)) { 23 if (a->alloc < (a->used + b)) {
30 if ((res = mp_grow (a, a->used + b)) != MP_OKAY) { 24 if ((err = mp_grow(a, a->used + b)) != MP_OKAY) {
31 return res; 25 return err;
32 } 26 }
33 } 27 }
34 28
35 { 29 /* increment the used by the shift amount then copy upwards */
36 mp_digit *top, *bottom; 30 a->used += b;
37 31
38 /* increment the used by the shift amount then copy upwards */ 32 /* top */
39 a->used += b; 33 top = a->dp + a->used - 1;
40 34
41 /* top */ 35 /* base */
42 top = a->dp + a->used - 1; 36 bottom = (a->dp + a->used - 1) - b;
43 37
44 /* base */ 38 /* much like mp_rshd this is implemented using a sliding window
45 bottom = (a->dp + a->used - 1) - b; 39 * except the window goes the otherway around. Copying from
40 * the bottom to the top. see bn_mp_rshd.c for more info.
41 */
42 for (x = a->used - 1; x >= b; x--) {
43 *top-- = *bottom--;
44 }
46 45
47 /* much like mp_rshd this is implemented using a sliding window 46 /* zero the lower digits */
48 * except the window goes the otherway around. Copying from 47 MP_ZERO_DIGITS(a->dp, b);
49 * the bottom to the top. see bn_mp_rshd.c for more info.
50 */
51 for (x = a->used - 1; x >= b; x--) {
52 *top-- = *bottom--;
53 }
54 48
55 /* zero the lower digits */ 49 return MP_OKAY;
56 top = a->dp;
57 for (x = 0; x < b; x++) {
58 *top++ = 0;
59 }
60 }
61 return MP_OKAY;
62 } 50 }
63 #endif 51 #endif
64
65 /* ref: $Format:%D$ */
66 /* git commit: $Format:%H$ */
67 /* commit time: $Format:%ai$ */