comparison bn_s_mp_exptmod.c @ 1:22d5cf7d4b1a libtommath

Renaming branch
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:23:46 +0000
parents
children d29b64170cf0
comparison
equal deleted inserted replaced
-1:000000000000 1:22d5cf7d4b1a
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis
2 *
3 * LibTomMath is a library that provides multiple-precision
4 * integer arithmetic as well as number theoretic functionality.
5 *
6 * The library was designed directly after the MPI library by
7 * Michael Fromberger but has been written from scratch with
8 * additional optimizations in place.
9 *
10 * The library is free for all purposes without any express
11 * guarantee it works.
12 *
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org
14 */
15 #include <tommath.h>
16
17 #ifdef MP_LOW_MEM
18 #define TAB_SIZE 32
19 #else
20 #define TAB_SIZE 256
21 #endif
22
23 int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
24 {
25 mp_int M[TAB_SIZE], res, mu;
26 mp_digit buf;
27 int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
28
29 /* find window size */
30 x = mp_count_bits (X);
31 if (x <= 7) {
32 winsize = 2;
33 } else if (x <= 36) {
34 winsize = 3;
35 } else if (x <= 140) {
36 winsize = 4;
37 } else if (x <= 450) {
38 winsize = 5;
39 } else if (x <= 1303) {
40 winsize = 6;
41 } else if (x <= 3529) {
42 winsize = 7;
43 } else {
44 winsize = 8;
45 }
46
47 #ifdef MP_LOW_MEM
48 if (winsize > 5) {
49 winsize = 5;
50 }
51 #endif
52
53 /* init M array */
54 /* init first cell */
55 if ((err = mp_init(&M[1])) != MP_OKAY) {
56 return err;
57 }
58
59 /* now init the second half of the array */
60 for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
61 if ((err = mp_init(&M[x])) != MP_OKAY) {
62 for (y = 1<<(winsize-1); y < x; y++) {
63 mp_clear (&M[y]);
64 }
65 mp_clear(&M[1]);
66 return err;
67 }
68 }
69
70 /* create mu, used for Barrett reduction */
71 if ((err = mp_init (&mu)) != MP_OKAY) {
72 goto __M;
73 }
74 if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
75 goto __MU;
76 }
77
78 /* create M table
79 *
80 * The M table contains powers of the base,
81 * e.g. M[x] = G**x mod P
82 *
83 * The first half of the table is not
84 * computed though accept for M[0] and M[1]
85 */
86 if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
87 goto __MU;
88 }
89
90 /* compute the value at M[1<<(winsize-1)] by squaring
91 * M[1] (winsize-1) times
92 */
93 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
94 goto __MU;
95 }
96
97 for (x = 0; x < (winsize - 1); x++) {
98 if ((err = mp_sqr (&M[1 << (winsize - 1)],
99 &M[1 << (winsize - 1)])) != MP_OKAY) {
100 goto __MU;
101 }
102 if ((err = mp_reduce (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
103 goto __MU;
104 }
105 }
106
107 /* create upper table, that is M[x] = M[x-1] * M[1] (mod P)
108 * for x = (2**(winsize - 1) + 1) to (2**winsize - 1)
109 */
110 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
111 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
112 goto __MU;
113 }
114 if ((err = mp_reduce (&M[x], P, &mu)) != MP_OKAY) {
115 goto __MU;
116 }
117 }
118
119 /* setup result */
120 if ((err = mp_init (&res)) != MP_OKAY) {
121 goto __MU;
122 }
123 mp_set (&res, 1);
124
125 /* set initial mode and bit cnt */
126 mode = 0;
127 bitcnt = 1;
128 buf = 0;
129 digidx = X->used - 1;
130 bitcpy = 0;
131 bitbuf = 0;
132
133 for (;;) {
134 /* grab next digit as required */
135 if (--bitcnt == 0) {
136 /* if digidx == -1 we are out of digits */
137 if (digidx == -1) {
138 break;
139 }
140 /* read next digit and reset the bitcnt */
141 buf = X->dp[digidx--];
142 bitcnt = (int) DIGIT_BIT;
143 }
144
145 /* grab the next msb from the exponent */
146 y = (buf >> (mp_digit)(DIGIT_BIT - 1)) & 1;
147 buf <<= (mp_digit)1;
148
149 /* if the bit is zero and mode == 0 then we ignore it
150 * These represent the leading zero bits before the first 1 bit
151 * in the exponent. Technically this opt is not required but it
152 * does lower the # of trivial squaring/reductions used
153 */
154 if (mode == 0 && y == 0) {
155 continue;
156 }
157
158 /* if the bit is zero and mode == 1 then we square */
159 if (mode == 1 && y == 0) {
160 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
161 goto __RES;
162 }
163 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
164 goto __RES;
165 }
166 continue;
167 }
168
169 /* else we add it to the window */
170 bitbuf |= (y << (winsize - ++bitcpy));
171 mode = 2;
172
173 if (bitcpy == winsize) {
174 /* ok window is filled so square as required and multiply */
175 /* square first */
176 for (x = 0; x < winsize; x++) {
177 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
178 goto __RES;
179 }
180 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
181 goto __RES;
182 }
183 }
184
185 /* then multiply */
186 if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
187 goto __RES;
188 }
189 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
190 goto __RES;
191 }
192
193 /* empty window and reset */
194 bitcpy = 0;
195 bitbuf = 0;
196 mode = 1;
197 }
198 }
199
200 /* if bits remain then square/multiply */
201 if (mode == 2 && bitcpy > 0) {
202 /* square then multiply if the bit is set */
203 for (x = 0; x < bitcpy; x++) {
204 if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
205 goto __RES;
206 }
207 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
208 goto __RES;
209 }
210
211 bitbuf <<= 1;
212 if ((bitbuf & (1 << winsize)) != 0) {
213 /* then multiply */
214 if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
215 goto __RES;
216 }
217 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) {
218 goto __RES;
219 }
220 }
221 }
222 }
223
224 mp_exch (&res, Y);
225 err = MP_OKAY;
226 __RES:mp_clear (&res);
227 __MU:mp_clear (&mu);
228 __M:
229 mp_clear(&M[1]);
230 for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
231 mp_clear (&M[x]);
232 }
233 return err;
234 }