~ubuntu-branches/debian/lenny/dropbear/lenny

« back to all changes in this revision

Viewing changes to libtommath/bn_mp_montgomery_calc_normalization.c

  • Committer: Bazaar Package Importer
  • Author(s): Gerrit Pape
  • Date: 2005-05-25 22:38:17 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050525223817-fdl653extybmz1zb
Tags: 0.45-3
* debian/dropbear.init: init script prints human readable message in case
  it's disabled (closes: #309099).
* debian/dropbear.postinst: configure: restart service through init script
  instead of start.
* debian/dropbear.prerm: set -u -> set -e.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <tommath.h>
 
2
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
1
3
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2
4
 *
3
5
 * LibTomMath is a library that provides multiple-precision
12
14
 *
13
15
 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
14
16
 */
15
 
#include <tommath.h>
16
17
 
17
 
/* calculates a = B^n mod b for Montgomery reduction
18
 
 * Where B is the base [e.g. 2^DIGIT_BIT].
19
 
 * B^n mod b is computed by first computing
20
 
 * A = B^(n-1) which doesn't require a reduction but a simple OR.
21
 
 * then C = A * B = B^n is computed by performing upto DIGIT_BIT
 
18
/*
22
19
 * shifts with subtractions when the result is greater than b.
23
20
 *
24
21
 * The method is slightly modified to shift B unconditionally upto just under
25
22
 * the leading bit of b.  This saves alot of multiple precision shifting.
26
23
 */
27
 
int
28
 
mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
 
24
int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
29
25
{
30
26
  int     x, bits, res;
31
27
 
32
28
  /* how many bits of last digit does b use */
33
29
  bits = mp_count_bits (b) % DIGIT_BIT;
34
30
 
35
 
  /* compute A = B^(n-1) * 2^(bits-1) */
36
 
  if ((res = mp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1)) != MP_OKAY) {
37
 
    return res;
 
31
 
 
32
  if (b->used > 1) {
 
33
     if ((res = mp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1)) != MP_OKAY) {
 
34
        return res;
 
35
     }
 
36
  } else {
 
37
     mp_set(a, 1);
 
38
     bits = 1;
38
39
  }
39
40
 
 
41
 
40
42
  /* now compute C = A * B mod b */
41
43
  for (x = bits - 1; x < (int)DIGIT_BIT; x++) {
42
44
    if ((res = mp_mul_2 (a, a)) != MP_OKAY) {
51
53
 
52
54
  return MP_OKAY;
53
55
}
 
56
#endif