security/nss/lib/freebl/mpi/tests/mptest-3.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 3: Multiplication, division, and exponentiation test
     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 <time.h>
    18 #include "mpi.h"
    20 #define  SQRT 1  /* define nonzero to get square-root test  */
    21 #define  EXPT 0  /* define nonzero to get exponentiate test */
    23 int main(int argc, char *argv[])
    24 {
    25   int      ix;
    26   mp_int   a, b, c, d;
    27   mp_digit r;
    28   mp_err   res;
    30   if(argc < 3) {
    31     fprintf(stderr, "Usage: %s <a> <b>\n", argv[0]);
    32     return 1;
    33   }
    35   printf("Test 3: Multiplication and division\n\n");
    36   srand(time(NULL));
    38   mp_init(&a);
    39   mp_init(&b);
    41   mp_read_variable_radix(&a, argv[1], 10);
    42   mp_read_variable_radix(&b, argv[2], 10);
    43   printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
    44   printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
    46   mp_init(&c);
    47   printf("\nc = a * b\n");
    49   mp_mul(&a, &b, &c);
    50   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    52   printf("\nc = b * 32523\n");
    54   mp_mul_d(&b, 32523, &c);
    55   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    57   mp_init(&d);
    58   printf("\nc = a / b, d = a mod b\n");
    60   mp_div(&a, &b, &c, &d);
    61   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);  
    62   printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);  
    64   ix = rand() % 256;
    65   printf("\nc = a / %d, r = a mod %d\n", ix, ix);
    66   mp_div_d(&a, (mp_digit)ix, &c, &r);
    67   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);  
    68   printf("r = %04X\n", r);
    70 #if EXPT
    71   printf("\nc = a ** b\n");
    72   mp_expt(&a, &b, &c);
    73   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);  
    74 #endif
    76   ix = rand() % 256;
    77   printf("\nc = 2^%d\n", ix);
    78   mp_2expt(&c, ix);
    79   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    81 #if SQRT
    82   printf("\nc = sqrt(a)\n");
    83   if((res = mp_sqrt(&a, &c)) != MP_OKAY) {
    84     printf("mp_sqrt: %s\n", mp_strerror(res));
    85   } else {
    86     printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    87     mp_sqr(&c, &c);
    88     printf("c^2 = "); mp_print(&c, stdout); fputc('\n', stdout);
    89   }
    90 #endif
    92   mp_clear(&d);
    93   mp_clear(&c);
    94   mp_clear(&b);
    95   mp_clear(&a);
    97   return 0;
    98 }

mercurial