1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/freebl/mpi/utils/lap.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +/* 1.5 + * lap.c 1.6 + * 1.7 + * Find least annihilating power of a mod m 1.8 + * 1.9 + * This Source Code Form is subject to the terms of the Mozilla Public 1.10 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.11 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.12 + 1.13 +#include <stdio.h> 1.14 +#include <stdlib.h> 1.15 +#include <signal.h> 1.16 + 1.17 +#include "mpi.h" 1.18 + 1.19 +void sig_catch(int ign); 1.20 + 1.21 +int g_quit = 0; 1.22 + 1.23 +int main(int argc, char *argv[]) 1.24 +{ 1.25 + mp_int a, m, p, k; 1.26 + 1.27 + if(argc < 3) { 1.28 + fprintf(stderr, "Usage: %s <a> <m>\n", argv[0]); 1.29 + return 1; 1.30 + } 1.31 + 1.32 + mp_init(&a); 1.33 + mp_init(&m); 1.34 + mp_init(&p); 1.35 + mp_add_d(&p, 1, &p); 1.36 + 1.37 + mp_read_radix(&a, argv[1], 10); 1.38 + mp_read_radix(&m, argv[2], 10); 1.39 + 1.40 + mp_init_copy(&k, &a); 1.41 + 1.42 + signal(SIGINT, sig_catch); 1.43 +#ifndef __OS2__ 1.44 + signal(SIGHUP, sig_catch); 1.45 +#endif 1.46 + signal(SIGTERM, sig_catch); 1.47 + 1.48 + while(mp_cmp(&p, &m) < 0) { 1.49 + if(g_quit) { 1.50 + int len; 1.51 + char *buf; 1.52 + 1.53 + len = mp_radix_size(&p, 10); 1.54 + buf = malloc(len); 1.55 + mp_toradix(&p, buf, 10); 1.56 + 1.57 + fprintf(stderr, "Terminated at: %s\n", buf); 1.58 + free(buf); 1.59 + return 1; 1.60 + } 1.61 + if(mp_cmp_d(&k, 1) == 0) { 1.62 + int len; 1.63 + char *buf; 1.64 + 1.65 + len = mp_radix_size(&p, 10); 1.66 + buf = malloc(len); 1.67 + mp_toradix(&p, buf, 10); 1.68 + 1.69 + printf("%s\n", buf); 1.70 + 1.71 + free(buf); 1.72 + break; 1.73 + } 1.74 + 1.75 + mp_mulmod(&k, &a, &m, &k); 1.76 + mp_add_d(&p, 1, &p); 1.77 + } 1.78 + 1.79 + if(mp_cmp(&p, &m) >= 0) 1.80 + printf("No annihilating power.\n"); 1.81 + 1.82 + mp_clear(&p); 1.83 + mp_clear(&m); 1.84 + mp_clear(&a); 1.85 + return 0; 1.86 +} 1.87 + 1.88 +void sig_catch(int ign) 1.89 +{ 1.90 + g_quit = 1; 1.91 +}