comparison libtommath/bn_mp_montgomery_setup.c @ 1655:f52919ffd3b1

update ltm to 1.1.0 and enable FIPS 186.4 compliant key-generation (#79) * make key-generation compliant to FIPS 186.4 * fix includes in tommath_class.h * update fuzzcorpus instead of error-out * fixup fuzzing make-targets * update Makefile.in * apply necessary patches to ltm sources * clean-up not required ltm files * update to vanilla ltm 1.1.0 this already only contains the required files * remove set/get double
author Steffen Jaeckel <s_jaeckel@gmx.de>
date Mon, 16 Sep 2019 15:50:38 +0200
parents 8bba51a55704
children 1051e4eea25a
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_MONTGOMERY_SETUP_C 2 #ifdef BN_MP_MONTGOMERY_SETUP_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.
7 * 7 *
8 * The library was designed directly after the MPI library by 8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with 9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * SPDX-License-Identifier: Unlicense
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */ 13 */
17 14
18 /* setups the montgomery reduction stuff */ 15 /* setups the montgomery reduction stuff */
19 int 16 int mp_montgomery_setup(const mp_int *n, mp_digit *rho)
20 mp_montgomery_setup (mp_int * n, mp_digit * rho)
21 { 17 {
22 mp_digit x, b; 18 mp_digit x, b;
23 19
24 /* fast inversion mod 2**k 20 /* fast inversion mod 2**k
25 * 21 *
26 * Based on the fact that 22 * Based on the fact that
27 * 23 *
28 * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n) 24 * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n)
29 * => 2*X*A - X*X*A*A = 1 25 * => 2*X*A - X*X*A*A = 1
30 * => 2*(1) - (1) = 1 26 * => 2*(1) - (1) = 1
31 */ 27 */
32 b = n->dp[0]; 28 b = n->dp[0];
33 29
34 if ((b & 1) == 0) { 30 if ((b & 1u) == 0u) {
35 return MP_VAL; 31 return MP_VAL;
36 } 32 }
37 33
38 x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */ 34 x = (((b + 2u) & 4u) << 1) + b; /* here x*a==1 mod 2**4 */
39 x *= 2 - (b * x); /* here x*a==1 mod 2**8 */ 35 x *= 2u - (b * x); /* here x*a==1 mod 2**8 */
40 #if !defined(MP_8BIT) 36 #if !defined(MP_8BIT)
41 x *= 2 - (b * x); /* here x*a==1 mod 2**16 */ 37 x *= 2u - (b * x); /* here x*a==1 mod 2**16 */
42 #endif 38 #endif
43 #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT)) 39 #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
44 x *= 2 - (b * x); /* here x*a==1 mod 2**32 */ 40 x *= 2u - (b * x); /* here x*a==1 mod 2**32 */
45 #endif 41 #endif
46 #ifdef MP_64BIT 42 #ifdef MP_64BIT
47 x *= 2 - (b * x); /* here x*a==1 mod 2**64 */ 43 x *= 2u - (b * x); /* here x*a==1 mod 2**64 */
48 #endif 44 #endif
49 45
50 /* rho = -1/m mod b */ 46 /* rho = -1/m mod b */
51 *rho = (mp_digit)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK; 47 *rho = (mp_digit)(((mp_word)1 << (mp_word)DIGIT_BIT) - x) & MP_MASK;
52 48
53 return MP_OKAY; 49 return MP_OKAY;
54 } 50 }
55 #endif 51 #endif
56 52
57 /* ref: $Format:%D$ */ 53 /* ref: HEAD -> master, tag: v1.1.0 */
58 /* git commit: $Format:%H$ */ 54 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
59 /* commit time: $Format:%ai$ */ 55 /* commit time: 2019-01-28 20:32:32 +0100 */