security/nss/lib/freebl/mpi/tests/mptest-4.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  * Simple test driver for MPI library
     3  *
     4  * Test 4: Modular arithmetic tests
     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>
    12 #include <string.h>
    13 #include <ctype.h>
    14 #include <limits.h>
    16 #include "mpi.h"
    18 int main(int argc, char *argv[])
    19 {
    20   int      ix;
    21   mp_int   a, b, c, m;
    22   mp_digit r;
    24   if(argc < 4) {
    25     fprintf(stderr, "Usage: %s <a> <b> <m>\n", argv[0]);
    26     return 1;
    27   }
    29   printf("Test 4: Modular arithmetic\n\n");
    31   mp_init(&a);
    32   mp_init(&b);
    33   mp_init(&m);
    35   mp_read_radix(&a, argv[1], 10);
    36   mp_read_radix(&b, argv[2], 10);
    37   mp_read_radix(&m, argv[3], 10);
    38   printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
    39   printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
    40   printf("m = "); mp_print(&m, stdout); fputc('\n', stdout);
    42   mp_init(&c);
    43   printf("\nc = a (mod m)\n");
    45   mp_mod(&a, &m, &c);
    46   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    48   printf("\nc = b (mod m)\n");
    50   mp_mod(&b, &m, &c);
    51   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    53   printf("\nc = b (mod 1853)\n");
    55   mp_mod_d(&b, 1853, &r);
    56   printf("c = %04X\n", r);
    58   printf("\nc = (a + b) mod m\n");
    60   mp_addmod(&a, &b, &m, &c);
    61   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    63   printf("\nc = (a - b) mod m\n");
    65   mp_submod(&a, &b, &m, &c);
    66   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    68   printf("\nc = (a * b) mod m\n");
    70   mp_mulmod(&a, &b, &m, &c);
    71   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    73   printf("\nc = (a ** b) mod m\n");
    75   mp_exptmod(&a, &b, &m, &c);
    76   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    78   printf("\nIn-place modular squaring test:\n");
    79   for(ix = 0; ix < 5; ix++) {
    80     printf("a = (a * a) mod m   a = ");
    81     mp_sqrmod(&a, &m, &a);
    82     mp_print(&a, stdout);
    83     fputc('\n', stdout);
    84   }
    87   mp_clear(&c);
    88   mp_clear(&m);
    89   mp_clear(&b);
    90   mp_clear(&a);
    92   return 0;
    93 }

mercurial