summaryrefslogtreecommitdiffstats
path: root/DartConfig.cmake
Commit message (Expand)AuthorAgeFilesLines
* Simplify CMake per-source license noticesBrad King2016-09-271-11/+3
* Update CDash server URLBrad King2014-06-241-2/+2
* Convert CMake to OSI-approved BSD LicenseBrad King2009-09-281-0/+11
* ENH: switch to using cdash for submissionsBill Hoffman2008-07-211-60/+6
* ENH: Switch to http submissionAndy Cedilnik2006-06-121-0/+3
* ENH: Dart changeAndy Cedilnik2005-05-271-2/+2
* COMP: Remove warning and fix the logicAndy Cedilnik2005-03-061-1/+1
* ENH: Work on xmlrpc submitAndy Cedilnik2005-03-051-5/+11
* ENH: CleanupsAndy Cedilnik2004-08-311-4/+5
* ENH: CleanupsAndy Cedilnik2004-06-091-3/+3
* ENH: Use viewcvs instead of cvswebAndy Cedilnik2004-06-091-1/+2
* ENH: Fix urlAndy Cedilnik2004-01-231-1/+1
* ENH: Add nightly reportingAndy Cedilnik2004-01-231-1/+1
* ENH: More continuous e-mail stuffAndy Cedilnik2004-01-231-6/+4
* ENH: More continuous e-mail stuffAndy Cedilnik2004-01-231-0/+8
* ENH: Improving CMake continuous dashboards. Sending continuous email for all...Brad King2004-01-231-0/+5
* move start time up one hourKen Martin2003-12-091-1/+1
* ENH: Direct link to cmake bugsAndy Cedilnik2003-07-221-2/+2
* ENH: Add proper links to bugtrackerAndy Cedilnik2003-07-021-1/+1
* ENH: Add links to bugtrackerAndy Cedilnik2003-07-021-3/+2
* More attempt to make continuous email workAndy Cedilnik2003-04-171-2/+2
* Attempt to enable sending of e-mails from continuousAndy Cedilnik2003-04-171-0/+6
* ENH: cleanupAndy Cedilnik2003-04-161-2/+1
* Add selection of drop methodAndy Cedilnik2003-01-161-4/+10
* ENH: my opinion is that it should be ADVANCED. Few people build the doc.Sebastien Barre2002-10-041-0/+1
* ENH: quick stab at a rollup buttonSebastien Barre2002-04-091-0/+2
* change EST to EDTBill Hoffman2002-04-091-1/+1
* ENH: Add PROJECT_URLSebastien Barre2002-02-141-0/+3
* ENH: Doxygen pageSebastien Barre2002-02-131-1/+2
* ENH: Changed Nightly start timeJim Miller2001-11-151-0/+4
* public no longer has anonymous ftp.Berk Geveci2001-10-241-2/+2
* Disabled doxygen and gnatsnobody2001-07-091-0/+5
* modified testingKen Martin2001-06-141-0/+13
cket'>dgp_async_socket Tcl is a high-level, general-purpose, interpreted, dynamic programming language. It was designed with the goal of being very simple but powerful.
summaryrefslogtreecommitdiffstats
path: root/libtommath/bn_mp_reduce.c
blob: a2b9bf7e8936b217712c51edcf96ed07ff520792 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <tommath_private.h>
#ifdef BN_MP_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
 */

/* reduces x mod m, assumes 0 < x < m**2, mu is
 * precomputed via mp_reduce_setup.
 * From HAC pp.604 Algorithm 14.42
 */
int mp_reduce(mp_int *x, mp_int *m, mp_int *mu)
{
   mp_int  q;
   int     res, um = m->used;

   /* q = x */
   if ((res = mp_init_copy(&q, x)) != MP_OKAY) {
      return res;
   }

   /* q1 = x / b**(k-1)  */
   mp_rshd(&q, um - 1);

   /* according to HAC this optimization is ok */
   if (((mp_digit) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) {
      if ((res = mp_mul(&q, mu, &q)) != MP_OKAY) {
         goto CLEANUP;
      }
   } else {
#ifdef BN_S_MP_MUL_HIGH_DIGS_C
      if ((res = s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) {
         goto CLEANUP;
      }
#elif defined(BN_FAST_S_MP_MUL_HIGH_DIGS_C)
      if ((res = fast_s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) {
         goto CLEANUP;
      }
#else
      {
         res = MP_VAL;
         goto CLEANUP;
      }
#endif
   }

   /* q3 = q2 / b**(k+1) */
   mp_rshd(&q, um + 1);

   /* x = x mod b**(k+1), quick (no division) */
   if ((res = mp_mod_2d(x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) {
      goto CLEANUP;
   }

   /* q = q * m mod b**(k+1), quick (no division) */
   if ((res = s_mp_mul_digs(&q, m, &q, um + 1)) != MP_OKAY) {
      goto CLEANUP;
   }

   /* x = x - q */
   if ((res = mp_sub(x, &q, x)) != MP_OKAY) {
      goto CLEANUP;
   }

   /* If x < 0, add b**(k+1) to it */
   if (mp_cmp_d(x, 0) == MP_LT) {
      mp_set(&q, 1);
      if ((res = mp_lshd(&q, um + 1)) != MP_OKAY)
         goto CLEANUP;
      if ((res = mp_add(x, &q, x)) != MP_OKAY)
         goto CLEANUP;
   }

   /* Back off if it's too big */
   while (mp_cmp(x, m) != MP_LT) {
      if ((res = s_mp_sub(x, m, x)) != MP_OKAY) {
         goto CLEANUP;
      }
   }

CLEANUP:
   mp_clear(&q);

   return res;
}
#endif

/* ref:         $Format:%D$ */
/* git commit:  $Format:%H$ */
/* commit time: $Format:%ai$ */