1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/freebl/mpi/tests/mptest-3.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,98 @@ 1.4 +/* 1.5 + * Simple test driver for MPI library 1.6 + * 1.7 + * Test 3: Multiplication, division, and exponentiation test 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 <time.h> 1.20 + 1.21 +#include "mpi.h" 1.22 + 1.23 +#define SQRT 1 /* define nonzero to get square-root test */ 1.24 +#define EXPT 0 /* define nonzero to get exponentiate test */ 1.25 + 1.26 +int main(int argc, char *argv[]) 1.27 +{ 1.28 + int ix; 1.29 + mp_int a, b, c, d; 1.30 + mp_digit r; 1.31 + mp_err res; 1.32 + 1.33 + if(argc < 3) { 1.34 + fprintf(stderr, "Usage: %s <a> <b>\n", argv[0]); 1.35 + return 1; 1.36 + } 1.37 + 1.38 + printf("Test 3: Multiplication and division\n\n"); 1.39 + srand(time(NULL)); 1.40 + 1.41 + mp_init(&a); 1.42 + mp_init(&b); 1.43 + 1.44 + mp_read_variable_radix(&a, argv[1], 10); 1.45 + mp_read_variable_radix(&b, argv[2], 10); 1.46 + printf("a = "); mp_print(&a, stdout); fputc('\n', stdout); 1.47 + printf("b = "); mp_print(&b, stdout); fputc('\n', stdout); 1.48 + 1.49 + mp_init(&c); 1.50 + printf("\nc = a * b\n"); 1.51 + 1.52 + mp_mul(&a, &b, &c); 1.53 + printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); 1.54 + 1.55 + printf("\nc = b * 32523\n"); 1.56 + 1.57 + mp_mul_d(&b, 32523, &c); 1.58 + printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); 1.59 + 1.60 + mp_init(&d); 1.61 + printf("\nc = a / b, d = a mod b\n"); 1.62 + 1.63 + mp_div(&a, &b, &c, &d); 1.64 + printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); 1.65 + printf("d = "); mp_print(&d, stdout); fputc('\n', stdout); 1.66 + 1.67 + ix = rand() % 256; 1.68 + printf("\nc = a / %d, r = a mod %d\n", ix, ix); 1.69 + mp_div_d(&a, (mp_digit)ix, &c, &r); 1.70 + printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); 1.71 + printf("r = %04X\n", r); 1.72 + 1.73 +#if EXPT 1.74 + printf("\nc = a ** b\n"); 1.75 + mp_expt(&a, &b, &c); 1.76 + printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); 1.77 +#endif 1.78 + 1.79 + ix = rand() % 256; 1.80 + printf("\nc = 2^%d\n", ix); 1.81 + mp_2expt(&c, ix); 1.82 + printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); 1.83 + 1.84 +#if SQRT 1.85 + printf("\nc = sqrt(a)\n"); 1.86 + if((res = mp_sqrt(&a, &c)) != MP_OKAY) { 1.87 + printf("mp_sqrt: %s\n", mp_strerror(res)); 1.88 + } else { 1.89 + printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); 1.90 + mp_sqr(&c, &c); 1.91 + printf("c^2 = "); mp_print(&c, stdout); fputc('\n', stdout); 1.92 + } 1.93 +#endif 1.94 + 1.95 + mp_clear(&d); 1.96 + mp_clear(&c); 1.97 + mp_clear(&b); 1.98 + mp_clear(&a); 1.99 + 1.100 + return 0; 1.101 +}