comparison libtommath/bn_mp_montgomery_calc_normalization.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_MONTGOMERY_CALC_NORMALIZATION_C 2 #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_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 /* 6 /*
16 * shifts with subtractions when the result is greater than b. 7 * shifts with subtractions when the result is greater than b.
17 * 8 *
18 * The method is slightly modified to shift B unconditionally upto just under 9 * The method is slightly modified to shift B unconditionally upto just under
19 * the leading bit of b. This saves alot of multiple precision shifting. 10 * the leading bit of b. This saves alot of multiple precision shifting.
20 */ 11 */
21 int mp_montgomery_calc_normalization(mp_int *a, const mp_int *b) 12 mp_err mp_montgomery_calc_normalization(mp_int *a, const mp_int *b)
22 { 13 {
23 int x, bits, res; 14 int x, bits;
15 mp_err err;
24 16
25 /* how many bits of last digit does b use */ 17 /* how many bits of last digit does b use */
26 bits = mp_count_bits(b) % DIGIT_BIT; 18 bits = mp_count_bits(b) % MP_DIGIT_BIT;
27 19
28 if (b->used > 1) { 20 if (b->used > 1) {
29 if ((res = mp_2expt(a, ((b->used - 1) * DIGIT_BIT) + bits - 1)) != MP_OKAY) { 21 if ((err = mp_2expt(a, ((b->used - 1) * MP_DIGIT_BIT) + bits - 1)) != MP_OKAY) {
30 return res; 22 return err;
31 } 23 }
32 } else { 24 } else {
33 mp_set(a, 1uL); 25 mp_set(a, 1uL);
34 bits = 1; 26 bits = 1;
35 } 27 }
36 28
37 29
38 /* now compute C = A * B mod b */ 30 /* now compute C = A * B mod b */
39 for (x = bits - 1; x < (int)DIGIT_BIT; x++) { 31 for (x = bits - 1; x < (int)MP_DIGIT_BIT; x++) {
40 if ((res = mp_mul_2(a, a)) != MP_OKAY) { 32 if ((err = mp_mul_2(a, a)) != MP_OKAY) {
41 return res; 33 return err;
42 } 34 }
43 if (mp_cmp_mag(a, b) != MP_LT) { 35 if (mp_cmp_mag(a, b) != MP_LT) {
44 if ((res = s_mp_sub(a, b, a)) != MP_OKAY) { 36 if ((err = s_mp_sub(a, b, a)) != MP_OKAY) {
45 return res; 37 return err;
46 } 38 }
47 } 39 }
48 } 40 }
49 41
50 return MP_OKAY; 42 return MP_OKAY;
51 } 43 }
52 #endif 44 #endif
53
54 /* ref: HEAD -> master, tag: v1.1.0 */
55 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
56 /* commit time: 2019-01-28 20:32:32 +0100 */