security/nss/lib/freebl/mpi/utils/invmod.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /*
     2  *  invmod.c
     3  *
     4  *  Compute modular inverses
     5  *
     6  * This Source Code Form is subject to the terms of the Mozilla Public
     7  * License, v. 2.0. If a copy of the MPL was not distributed with this
     8  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    10 #include <stdio.h>
    11 #include <stdlib.h>
    13 #include "mpi.h"
    15 int main(int argc, char *argv[])
    16 {
    17   mp_int    a, m;
    18   mp_err    res;
    19   char     *buf;
    20   int       len, out = 0;
    22   if(argc < 3) {
    23     fprintf(stderr, "Usage: %s <a> <m>\n", argv[0]);
    24     return 1;
    25   }
    27   mp_init(&a); mp_init(&m);
    28   mp_read_radix(&a, argv[1], 10);
    29   mp_read_radix(&m, argv[2], 10);
    31   if(mp_cmp(&a, &m) > 0)
    32     mp_mod(&a, &m, &a);
    34   switch((res = mp_invmod(&a, &m, &a))) {
    35   case MP_OKAY:
    36     len = mp_radix_size(&a, 10);
    37     buf = malloc(len);
    39     mp_toradix(&a, buf, 10);
    40     printf("%s\n", buf);
    41     free(buf);
    42     break;
    44   case MP_UNDEF:
    45     printf("No inverse\n");
    46     out = 1;
    47     break;
    49   default:
    50     printf("error: %s (%d)\n", mp_strerror(res), res);
    51     out = 2;
    52     break;
    53   }
    55   mp_clear(&a);
    56   mp_clear(&m);
    58   return out;
    59 }

mercurial