security/nss/lib/freebl/mpi/tests/mptest-4.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/mpi/tests/mptest-4.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +/*
     1.5 + * Simple test driver for MPI library
     1.6 + *
     1.7 + * Test 4: Modular arithmetic tests
     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 <string.h>
    1.16 +#include <ctype.h>
    1.17 +#include <limits.h>
    1.18 +
    1.19 +#include "mpi.h"
    1.20 +
    1.21 +int main(int argc, char *argv[])
    1.22 +{
    1.23 +  int      ix;
    1.24 +  mp_int   a, b, c, m;
    1.25 +  mp_digit r;
    1.26 +
    1.27 +  if(argc < 4) {
    1.28 +    fprintf(stderr, "Usage: %s <a> <b> <m>\n", argv[0]);
    1.29 +    return 1;
    1.30 +  }
    1.31 +
    1.32 +  printf("Test 4: Modular arithmetic\n\n");
    1.33 +
    1.34 +  mp_init(&a);
    1.35 +  mp_init(&b);
    1.36 +  mp_init(&m);
    1.37 +
    1.38 +  mp_read_radix(&a, argv[1], 10);
    1.39 +  mp_read_radix(&b, argv[2], 10);
    1.40 +  mp_read_radix(&m, argv[3], 10);
    1.41 +  printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
    1.42 +  printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
    1.43 +  printf("m = "); mp_print(&m, stdout); fputc('\n', stdout);
    1.44 +  
    1.45 +  mp_init(&c);
    1.46 +  printf("\nc = a (mod m)\n");
    1.47 +
    1.48 +  mp_mod(&a, &m, &c);
    1.49 +  printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    1.50 +
    1.51 +  printf("\nc = b (mod m)\n");
    1.52 +
    1.53 +  mp_mod(&b, &m, &c);
    1.54 +  printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    1.55 +
    1.56 +  printf("\nc = b (mod 1853)\n");
    1.57 +
    1.58 +  mp_mod_d(&b, 1853, &r);
    1.59 +  printf("c = %04X\n", r);
    1.60 +
    1.61 +  printf("\nc = (a + b) mod m\n");
    1.62 +
    1.63 +  mp_addmod(&a, &b, &m, &c);
    1.64 +  printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    1.65 +
    1.66 +  printf("\nc = (a - b) mod m\n");
    1.67 +
    1.68 +  mp_submod(&a, &b, &m, &c);
    1.69 +  printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    1.70 +
    1.71 +  printf("\nc = (a * b) mod m\n");
    1.72 +
    1.73 +  mp_mulmod(&a, &b, &m, &c);
    1.74 +  printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    1.75 +
    1.76 +  printf("\nc = (a ** b) mod m\n");
    1.77 +
    1.78 +  mp_exptmod(&a, &b, &m, &c);
    1.79 +  printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    1.80 +
    1.81 +  printf("\nIn-place modular squaring test:\n");
    1.82 +  for(ix = 0; ix < 5; ix++) {
    1.83 +    printf("a = (a * a) mod m   a = ");
    1.84 +    mp_sqrmod(&a, &m, &a);
    1.85 +    mp_print(&a, stdout);
    1.86 +    fputc('\n', stdout);
    1.87 +  }
    1.88 +  
    1.89 +
    1.90 +  mp_clear(&c);
    1.91 +  mp_clear(&m);
    1.92 +  mp_clear(&b);
    1.93 +  mp_clear(&a);
    1.94 +
    1.95 +  return 0;
    1.96 +}

mercurial