summaryrefslogtreecommitdiff
path: root/libtommath/bn_mp_dr_reduce.c
diff options
context:
space:
mode:
authorMatt Johnston <matt@ucc.asn.au>2020-06-26 21:07:34 +0800
committerMatt Johnston <matt@ucc.asn.au>2020-06-26 21:07:34 +0800
commit71b4ec569cb33a5dcd4abf50650ccd78e6d3b2dd (patch)
tree497adde943fd41206f8d263131b255dda1833ca1 /libtommath/bn_mp_dr_reduce.c
parent3187f27fcd533b8d16feebba9090007c82725eff (diff)
parenteaf9be05d7d9de22f2ff936882874331fc2d1a0e (diff)
downloaddropbear-71b4ec569cb33a5dcd4abf50650ccd78e6d3b2dd.tar.gz
merge coverity from maincoverity
Diffstat (limited to 'libtommath/bn_mp_dr_reduce.c')
-rw-r--r--libtommath/bn_mp_dr_reduce.c112
1 files changed, 47 insertions, 65 deletions
diff --git a/libtommath/bn_mp_dr_reduce.c b/libtommath/bn_mp_dr_reduce.c
index c85ee77..ffc33a6 100644
--- a/libtommath/bn_mp_dr_reduce.c
+++ b/libtommath/bn_mp_dr_reduce.c
@@ -1,19 +1,7 @@
-#include <tommath_private.h>
+#include "tommath_private.h"
#ifdef BN_MP_DR_REDUCE_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis
- *
- * LibTomMath is a library that provides multiple-precision
- * integer arithmetic as well as number theoretic functionality.
- *
- * The library was designed directly after the MPI library by
- * Michael Fromberger but has been written from scratch with
- * additional optimizations in place.
- *
- * The library is free for all purposes without any express
- * guarantee it works.
- *
- * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
- */
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
/* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
*
@@ -29,68 +17,62 @@
*
* Input x must be in the range 0 <= x <= (n-1)**2
*/
-int
-mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
+mp_err mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k)
{
- int err, i, m;
- mp_word r;
- mp_digit mu, *tmpx1, *tmpx2;
+ mp_err err;
+ int i, m;
+ mp_word r;
+ mp_digit mu, *tmpx1, *tmpx2;
- /* m = digits in modulus */
- m = n->used;
+ /* m = digits in modulus */
+ m = n->used;
- /* ensure that "x" has at least 2m digits */
- if (x->alloc < (m + m)) {
- if ((err = mp_grow (x, m + m)) != MP_OKAY) {
- return err;
- }
- }
+ /* ensure that "x" has at least 2m digits */
+ if (x->alloc < (m + m)) {
+ if ((err = mp_grow(x, m + m)) != MP_OKAY) {
+ return err;
+ }
+ }
-/* top of loop, this is where the code resumes if
- * another reduction pass is required.
- */
+ /* top of loop, this is where the code resumes if
+ * another reduction pass is required.
+ */
top:
- /* aliases for digits */
- /* alias for lower half of x */
- tmpx1 = x->dp;
+ /* aliases for digits */
+ /* alias for lower half of x */
+ tmpx1 = x->dp;
- /* alias for upper half of x, or x/B**m */
- tmpx2 = x->dp + m;
+ /* alias for upper half of x, or x/B**m */
+ tmpx2 = x->dp + m;
- /* set carry to zero */
- mu = 0;
+ /* set carry to zero */
+ mu = 0;
- /* compute (x mod B**m) + k * [x/B**m] inline and inplace */
- for (i = 0; i < m; i++) {
- r = (((mp_word)*tmpx2++) * (mp_word)k) + *tmpx1 + mu;
+ /* compute (x mod B**m) + k * [x/B**m] inline and inplace */
+ for (i = 0; i < m; i++) {
+ r = ((mp_word)*tmpx2++ * (mp_word)k) + *tmpx1 + mu;
*tmpx1++ = (mp_digit)(r & MP_MASK);
- mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
- }
+ mu = (mp_digit)(r >> ((mp_word)MP_DIGIT_BIT));
+ }
- /* set final carry */
- *tmpx1++ = mu;
+ /* set final carry */
+ *tmpx1++ = mu;
- /* zero words above m */
- for (i = m + 1; i < x->used; i++) {
- *tmpx1++ = 0;
- }
+ /* zero words above m */
+ MP_ZERO_DIGITS(tmpx1, (x->used - m) - 1);
- /* clamp, sub and return */
- mp_clamp (x);
+ /* clamp, sub and return */
+ mp_clamp(x);
- /* if x >= n then subtract and reduce again
- * Each successive "recursion" makes the input smaller and smaller.
- */
- if (mp_cmp_mag (x, n) != MP_LT) {
- if ((err = s_mp_sub(x, n, x)) != MP_OKAY) {
- return err;
- }
- goto top;
- }
- return MP_OKAY;
+ /* if x >= n then subtract and reduce again
+ * Each successive "recursion" makes the input smaller and smaller.
+ */
+ if (mp_cmp_mag(x, n) != MP_LT) {
+ if ((err = s_mp_sub(x, n, x)) != MP_OKAY) {
+ return err;
+ }
+ goto top;
+ }
+ return MP_OKAY;
}
#endif
-
-/* ref: $Format:%D$ */
-/* git commit: $Format:%H$ */
-/* commit time: $Format:%ai$ */