Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 60 lines (52 sloc) 1.553 kb
e549ccfe »
2004-10-29 added libtommath-0.32
1 #include <tommath.h>
2 #ifdef BN_MP_MONTGOMERY_SETUP_C
57354e11 »
2003-02-28 added libtommath-0.12
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 *
c1da6aa2 »
2003-08-05 added libtommath-0.25
5 * LibTomMath is a library that provides multiple-precision
57354e11 »
2003-02-28 added libtommath-0.12
6 * integer arithmetic as well as number theoretic functionality.
7 *
c1da6aa2 »
2003-08-05 added libtommath-0.25
8 * The library was designed directly after the MPI library by
57354e11 »
2003-02-28 added libtommath-0.12
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 *
333aebc8 »
2007-04-18 added libtommath-0.41
15 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
57354e11 »
2003-02-28 added libtommath-0.12
16 */
17
18 /* setups the montgomery reduction stuff */
19 int
0ef44cea »
2003-05-29 added libtommath-0.18
20 mp_montgomery_setup (mp_int * n, mp_digit * rho)
57354e11 »
2003-02-28 added libtommath-0.12
21 {
fd181cc8 »
2003-05-17 added libtommath-0.17
22 mp_digit x, b;
57354e11 »
2003-02-28 added libtommath-0.12
23
0ef44cea »
2003-05-29 added libtommath-0.18
24 /* fast inversion mod 2**k
82f48582 »
2003-03-13 added libtommath-0.14
25 *
fd181cc8 »
2003-05-17 added libtommath-0.17
26 * Based on the fact that
82f48582 »
2003-03-13 added libtommath-0.14
27 *
0ef44cea »
2003-05-29 added libtommath-0.18
28 * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n)
29 * => 2*X*A - X*X*A*A = 1
30 * => 2*(1) - (1) = 1
82f48582 »
2003-03-13 added libtommath-0.14
31 */
0ef44cea »
2003-05-29 added libtommath-0.18
32 b = n->dp[0];
57354e11 »
2003-02-28 added libtommath-0.12
33
82f48582 »
2003-03-13 added libtommath-0.14
34 if ((b & 1) == 0) {
35 return MP_VAL;
57354e11 »
2003-02-28 added libtommath-0.12
36 }
37
0ef44cea »
2003-05-29 added libtommath-0.18
38 x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
39 x *= 2 - b * x; /* here x*a==1 mod 2**8 */
fd181cc8 »
2003-05-17 added libtommath-0.17
40 #if !defined(MP_8BIT)
0ef44cea »
2003-05-29 added libtommath-0.18
41 x *= 2 - b * x; /* here x*a==1 mod 2**16 */
fd181cc8 »
2003-05-17 added libtommath-0.17
42 #endif
43 #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
0ef44cea »
2003-05-29 added libtommath-0.18
44 x *= 2 - b * x; /* here x*a==1 mod 2**32 */
fd181cc8 »
2003-05-17 added libtommath-0.17
45 #endif
46 #ifdef MP_64BIT
0ef44cea »
2003-05-29 added libtommath-0.18
47 x *= 2 - b * x; /* here x*a==1 mod 2**64 */
fd181cc8 »
2003-05-17 added libtommath-0.17
48 #endif
57354e11 »
2003-02-28 added libtommath-0.12
49
0ef44cea »
2003-05-29 added libtommath-0.18
50 /* rho = -1/m mod b */
3aba4eac »
2006-12-24 added libtommath-0.40
51 *rho = (unsigned long)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;
57354e11 »
2003-02-28 added libtommath-0.12
52
82f48582 »
2003-03-13 added libtommath-0.14
53 return MP_OKAY;
57354e11 »
2003-02-28 added libtommath-0.12
54 }
e549ccfe »
2004-10-29 added libtommath-0.32
55 #endif
9294e791 »
2005-08-01 added libtommath-0.36
56
57 /* $Source$ */
58 /* $Revision$ */
59 /* $Date$ */
Something went wrong with that request. Please try again.