comparison etc/pprime.c @ 190:d8254fc979e9 libtommath-orig LTM_0.35

Initial import of libtommath 0.35
author Matt Johnston <matt@ucc.asn.au>
date Fri, 06 May 2005 08:59:30 +0000
parents 86e0b50a9b58
children
comparison
equal deleted inserted replaced
142:d29b64170cf0 190:d8254fc979e9
187 if ((res = mp_init (&c)) != MP_OKAY) { 187 if ((res = mp_init (&c)) != MP_OKAY) {
188 return res; 188 return res;
189 } 189 }
190 190
191 if ((res = mp_init (&v)) != MP_OKAY) { 191 if ((res = mp_init (&v)) != MP_OKAY) {
192 goto __C; 192 goto LBL_C;
193 } 193 }
194 194
195 /* product of first 50 primes */ 195 /* product of first 50 primes */
196 if ((res = 196 if ((res =
197 mp_read_radix (&v, 197 mp_read_radix (&v,
198 "19078266889580195013601891820992757757219839668357012055907516904309700014933909014729740190", 198 "19078266889580195013601891820992757757219839668357012055907516904309700014933909014729740190",
199 10)) != MP_OKAY) { 199 10)) != MP_OKAY) {
200 goto __V; 200 goto LBL_V;
201 } 201 }
202 202
203 if ((res = mp_init (&a)) != MP_OKAY) { 203 if ((res = mp_init (&a)) != MP_OKAY) {
204 goto __V; 204 goto LBL_V;
205 } 205 }
206 206
207 /* set the prime */ 207 /* set the prime */
208 mp_set (&a, prime_digit ()); 208 mp_set (&a, prime_digit ());
209 209
210 if ((res = mp_init (&b)) != MP_OKAY) { 210 if ((res = mp_init (&b)) != MP_OKAY) {
211 goto __A; 211 goto LBL_A;
212 } 212 }
213 213
214 if ((res = mp_init (&n)) != MP_OKAY) { 214 if ((res = mp_init (&n)) != MP_OKAY) {
215 goto __B; 215 goto LBL_B;
216 } 216 }
217 217
218 if ((res = mp_init (&x)) != MP_OKAY) { 218 if ((res = mp_init (&x)) != MP_OKAY) {
219 goto __N; 219 goto LBL_N;
220 } 220 }
221 221
222 if ((res = mp_init (&y)) != MP_OKAY) { 222 if ((res = mp_init (&y)) != MP_OKAY) {
223 goto __X; 223 goto LBL_X;
224 } 224 }
225 225
226 if ((res = mp_init (&z)) != MP_OKAY) { 226 if ((res = mp_init (&z)) != MP_OKAY) {
227 goto __Y; 227 goto LBL_Y;
228 } 228 }
229 229
230 /* now loop making the single digit */ 230 /* now loop making the single digit */
231 while (mp_count_bits (&a) < k) { 231 while (mp_count_bits (&a) < k) {
232 fprintf (stderr, "prime has %4d bits left\r", k - mp_count_bits (&a)); 232 fprintf (stderr, "prime has %4d bits left\r", k - mp_count_bits (&a));
234 top: 234 top:
235 mp_set (&b, prime_digit ()); 235 mp_set (&b, prime_digit ());
236 236
237 /* now compute z = a * b * 2 */ 237 /* now compute z = a * b * 2 */
238 if ((res = mp_mul (&a, &b, &z)) != MP_OKAY) { /* z = a * b */ 238 if ((res = mp_mul (&a, &b, &z)) != MP_OKAY) { /* z = a * b */
239 goto __Z; 239 goto LBL_Z;
240 } 240 }
241 241
242 if ((res = mp_copy (&z, &c)) != MP_OKAY) { /* c = a * b */ 242 if ((res = mp_copy (&z, &c)) != MP_OKAY) { /* c = a * b */
243 goto __Z; 243 goto LBL_Z;
244 } 244 }
245 245
246 if ((res = mp_mul_2 (&z, &z)) != MP_OKAY) { /* z = 2 * a * b */ 246 if ((res = mp_mul_2 (&z, &z)) != MP_OKAY) { /* z = 2 * a * b */
247 goto __Z; 247 goto LBL_Z;
248 } 248 }
249 249
250 /* n = z + 1 */ 250 /* n = z + 1 */
251 if ((res = mp_add_d (&z, 1, &n)) != MP_OKAY) { /* n = z + 1 */ 251 if ((res = mp_add_d (&z, 1, &n)) != MP_OKAY) { /* n = z + 1 */
252 goto __Z; 252 goto LBL_Z;
253 } 253 }
254 254
255 /* check (n, v) == 1 */ 255 /* check (n, v) == 1 */
256 if ((res = mp_gcd (&n, &v, &y)) != MP_OKAY) { /* y = (n, v) */ 256 if ((res = mp_gcd (&n, &v, &y)) != MP_OKAY) { /* y = (n, v) */
257 goto __Z; 257 goto LBL_Z;
258 } 258 }
259 259
260 if (mp_cmp_d (&y, 1) != MP_EQ) 260 if (mp_cmp_d (&y, 1) != MP_EQ)
261 goto top; 261 goto top;
262 262
264 for (ii = 0; ii < li; ii++) { 264 for (ii = 0; ii < li; ii++) {
265 mp_set (&x, bases[ii]); 265 mp_set (&x, bases[ii]);
266 266
267 /* compute x^a mod n */ 267 /* compute x^a mod n */
268 if ((res = mp_exptmod (&x, &a, &n, &y)) != MP_OKAY) { /* y = x^a mod n */ 268 if ((res = mp_exptmod (&x, &a, &n, &y)) != MP_OKAY) { /* y = x^a mod n */
269 goto __Z; 269 goto LBL_Z;
270 } 270 }
271 271
272 /* if y == 1 loop */ 272 /* if y == 1 loop */
273 if (mp_cmp_d (&y, 1) == MP_EQ) 273 if (mp_cmp_d (&y, 1) == MP_EQ)
274 continue; 274 continue;
275 275
276 /* now x^2a mod n */ 276 /* now x^2a mod n */
277 if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2a mod n */ 277 if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2a mod n */
278 goto __Z; 278 goto LBL_Z;
279 } 279 }
280 280
281 if (mp_cmp_d (&y, 1) == MP_EQ) 281 if (mp_cmp_d (&y, 1) == MP_EQ)
282 continue; 282 continue;
283 283
284 /* compute x^b mod n */ 284 /* compute x^b mod n */
285 if ((res = mp_exptmod (&x, &b, &n, &y)) != MP_OKAY) { /* y = x^b mod n */ 285 if ((res = mp_exptmod (&x, &b, &n, &y)) != MP_OKAY) { /* y = x^b mod n */
286 goto __Z; 286 goto LBL_Z;
287 } 287 }
288 288
289 /* if y == 1 loop */ 289 /* if y == 1 loop */
290 if (mp_cmp_d (&y, 1) == MP_EQ) 290 if (mp_cmp_d (&y, 1) == MP_EQ)
291 continue; 291 continue;
292 292
293 /* now x^2b mod n */ 293 /* now x^2b mod n */
294 if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2b mod n */ 294 if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2b mod n */
295 goto __Z; 295 goto LBL_Z;
296 } 296 }
297 297
298 if (mp_cmp_d (&y, 1) == MP_EQ) 298 if (mp_cmp_d (&y, 1) == MP_EQ)
299 continue; 299 continue;
300 300
301 /* compute x^c mod n == x^ab mod n */ 301 /* compute x^c mod n == x^ab mod n */
302 if ((res = mp_exptmod (&x, &c, &n, &y)) != MP_OKAY) { /* y = x^ab mod n */ 302 if ((res = mp_exptmod (&x, &c, &n, &y)) != MP_OKAY) { /* y = x^ab mod n */
303 goto __Z; 303 goto LBL_Z;
304 } 304 }
305 305
306 /* if y == 1 loop */ 306 /* if y == 1 loop */
307 if (mp_cmp_d (&y, 1) == MP_EQ) 307 if (mp_cmp_d (&y, 1) == MP_EQ)
308 continue; 308 continue;
309 309
310 /* now compute (x^c mod n)^2 */ 310 /* now compute (x^c mod n)^2 */
311 if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2ab mod n */ 311 if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2ab mod n */
312 goto __Z; 312 goto LBL_Z;
313 } 313 }
314 314
315 /* y should be 1 */ 315 /* y should be 1 */
316 if (mp_cmp_d (&y, 1) != MP_EQ) 316 if (mp_cmp_d (&y, 1) != MP_EQ)
317 continue; 317 continue;
344 mp_div (q, &b, q, NULL); 344 mp_div (q, &b, q, NULL);
345 345
346 mp_exch (&n, p); 346 mp_exch (&n, p);
347 347
348 res = MP_OKAY; 348 res = MP_OKAY;
349 __Z:mp_clear (&z); 349 LBL_Z:mp_clear (&z);
350 __Y:mp_clear (&y); 350 LBL_Y:mp_clear (&y);
351 __X:mp_clear (&x); 351 LBL_X:mp_clear (&x);
352 __N:mp_clear (&n); 352 LBL_N:mp_clear (&n);
353 __B:mp_clear (&b); 353 LBL_B:mp_clear (&b);
354 __A:mp_clear (&a); 354 LBL_A:mp_clear (&a);
355 __V:mp_clear (&v); 355 LBL_V:mp_clear (&v);
356 __C:mp_clear (&c); 356 LBL_C:mp_clear (&c);
357 return res; 357 return res;
358 } 358 }
359 359
360 360
361 int 361 int