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

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:3e0251f32ea9
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/. */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <limits.h>
15
16 #include "mpi.h"
17
18 int main(int argc, char *argv[])
19 {
20 int ix;
21 mp_int a, b, c, m;
22 mp_digit r;
23
24 if(argc < 4) {
25 fprintf(stderr, "Usage: %s <a> <b> <m>\n", argv[0]);
26 return 1;
27 }
28
29 printf("Test 4: Modular arithmetic\n\n");
30
31 mp_init(&a);
32 mp_init(&b);
33 mp_init(&m);
34
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);
41
42 mp_init(&c);
43 printf("\nc = a (mod m)\n");
44
45 mp_mod(&a, &m, &c);
46 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
47
48 printf("\nc = b (mod m)\n");
49
50 mp_mod(&b, &m, &c);
51 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
52
53 printf("\nc = b (mod 1853)\n");
54
55 mp_mod_d(&b, 1853, &r);
56 printf("c = %04X\n", r);
57
58 printf("\nc = (a + b) mod m\n");
59
60 mp_addmod(&a, &b, &m, &c);
61 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
62
63 printf("\nc = (a - b) mod m\n");
64
65 mp_submod(&a, &b, &m, &c);
66 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
67
68 printf("\nc = (a * b) mod m\n");
69
70 mp_mulmod(&a, &b, &m, &c);
71 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
72
73 printf("\nc = (a ** b) mod m\n");
74
75 mp_exptmod(&a, &b, &m, &c);
76 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
77
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 }
85
86
87 mp_clear(&c);
88 mp_clear(&m);
89 mp_clear(&b);
90 mp_clear(&a);
91
92 return 0;
93 }

mercurial