comparison libtommath/bn_mp_reduce_2k.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_REDUCE_2K_C 2 #ifdef BN_MP_REDUCE_2K_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 /* reduces a modulo n where n is of the form 2**p - d */ 6 /* reduces a modulo n where n is of the form 2**p - d */
19 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d) 7 mp_err mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d)
20 { 8 {
21 mp_int q; 9 mp_int q;
22 int p, res; 10 mp_err err;
11 int p;
23 12
24 if ((res = mp_init(&q)) != MP_OKAY) { 13 if ((err = mp_init(&q)) != MP_OKAY) {
25 return res; 14 return err;
26 } 15 }
27 16
28 p = mp_count_bits(n); 17 p = mp_count_bits(n);
29 top: 18 top:
30 /* q = a/2**p, a = a mod 2**p */ 19 /* q = a/2**p, a = a mod 2**p */
31 if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { 20 if ((err = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
32 goto ERR; 21 goto LBL_ERR;
33 } 22 }
34 23
35 if (d != 1) { 24 if (d != 1u) {
36 /* q = q * d */ 25 /* q = q * d */
37 if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) { 26 if ((err = mp_mul_d(&q, d, &q)) != MP_OKAY) {
38 goto ERR; 27 goto LBL_ERR;
39 } 28 }
40 } 29 }
41 30
42 /* a = a + q */ 31 /* a = a + q */
43 if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { 32 if ((err = s_mp_add(a, &q, a)) != MP_OKAY) {
44 goto ERR; 33 goto LBL_ERR;
45 } 34 }
46 35
47 if (mp_cmp_mag(a, n) != MP_LT) { 36 if (mp_cmp_mag(a, n) != MP_LT) {
48 if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { 37 if ((err = s_mp_sub(a, n, a)) != MP_OKAY) {
49 goto ERR; 38 goto LBL_ERR;
50 } 39 }
51 goto top; 40 goto top;
52 } 41 }
53 42
54 ERR: 43 LBL_ERR:
55 mp_clear(&q); 44 mp_clear(&q);
56 return res; 45 return err;
57 } 46 }
58 47
59 #endif 48 #endif
60
61 /* ref: $Format:%D$ */
62 /* git commit: $Format:%H$ */
63 /* commit time: $Format:%ai$ */