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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/mpi/utils/invmod.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,59 @@
     1.4 +/*
     1.5 + *  invmod.c
     1.6 + *
     1.7 + *  Compute modular inverses
     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 +
    1.16 +#include "mpi.h"
    1.17 +
    1.18 +int main(int argc, char *argv[])
    1.19 +{
    1.20 +  mp_int    a, m;
    1.21 +  mp_err    res;
    1.22 +  char     *buf;
    1.23 +  int       len, out = 0;
    1.24 +
    1.25 +  if(argc < 3) {
    1.26 +    fprintf(stderr, "Usage: %s <a> <m>\n", argv[0]);
    1.27 +    return 1;
    1.28 +  }
    1.29 +
    1.30 +  mp_init(&a); mp_init(&m);
    1.31 +  mp_read_radix(&a, argv[1], 10);
    1.32 +  mp_read_radix(&m, argv[2], 10);
    1.33 +
    1.34 +  if(mp_cmp(&a, &m) > 0)
    1.35 +    mp_mod(&a, &m, &a);
    1.36 +
    1.37 +  switch((res = mp_invmod(&a, &m, &a))) {
    1.38 +  case MP_OKAY:
    1.39 +    len = mp_radix_size(&a, 10);
    1.40 +    buf = malloc(len);
    1.41 +
    1.42 +    mp_toradix(&a, buf, 10);
    1.43 +    printf("%s\n", buf);
    1.44 +    free(buf);
    1.45 +    break;
    1.46 +
    1.47 +  case MP_UNDEF:
    1.48 +    printf("No inverse\n");
    1.49 +    out = 1;
    1.50 +    break;
    1.51 +
    1.52 +  default:
    1.53 +    printf("error: %s (%d)\n", mp_strerror(res), res);
    1.54 +    out = 2;
    1.55 +    break;
    1.56 +  }
    1.57 +
    1.58 +  mp_clear(&a);
    1.59 +  mp_clear(&m);
    1.60 +
    1.61 +  return out;
    1.62 +}

mercurial