]> pd.if.org Git - zpackage/blob - tomsfastmath/src/mont/fp_montgomery_calc_normalization.c
commit files needed for zpm-fetchurl
[zpackage] / tomsfastmath / src / mont / fp_montgomery_calc_normalization.c
1 /* TomsFastMath, a fast ISO C bignum library.
2  * 
3  * This project is meant to fill in where LibTomMath
4  * falls short.  That is speed ;-)
5  *
6  * This project is public domain and free for all purposes.
7  * 
8  * Tom St Denis, tomstdenis@gmail.com
9  */
10 #include <tfm_private.h>
11
12 /* computes a = B**n mod b without division or multiplication useful for
13  * normalizing numbers in a Montgomery system.
14  */
15 void fp_montgomery_calc_normalization(fp_int *a, fp_int *b)
16 {
17   int     x, bits;
18
19   /* how many bits of last digit does b use */
20   bits = fp_count_bits (b) % DIGIT_BIT;
21   if (!bits) bits = DIGIT_BIT;
22
23   /* compute A = B^(n-1) * 2^(bits-1) */
24   if (b->used > 1) {
25      fp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1);
26   } else {
27      fp_set(a, 1);
28      bits = 1;
29   }
30
31   /* now compute C = A * B mod b */
32   for (x = bits - 1; x < (int)DIGIT_BIT; x++) {
33     fp_mul_2 (a, a);
34     if (fp_cmp_mag (a, b) != FP_LT) {
35       s_fp_sub (a, b, a);
36     }
37   }
38 }
39
40
41 /* $Source$ */
42 /* $Revision$ */
43 /* $Date$ */