|
1 /* |
|
2 * Simple test driver for MPI library |
|
3 * |
|
4 * Test GF2m: Binary Polynomial Arithmetic |
|
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 "mp_gf2m.h" |
|
17 |
|
18 int main(int argc, char *argv[]) |
|
19 { |
|
20 int ix; |
|
21 mp_int pp, a, b, x, y, order; |
|
22 mp_int c, d, e; |
|
23 mp_digit r; |
|
24 mp_err res; |
|
25 unsigned int p[] = {163,7,6,3,0}; |
|
26 unsigned int ptemp[10]; |
|
27 |
|
28 printf("Test b: Binary Polynomial Arithmetic\n\n"); |
|
29 |
|
30 mp_init(&pp); |
|
31 mp_init(&a); |
|
32 mp_init(&b); |
|
33 mp_init(&x); |
|
34 mp_init(&y); |
|
35 mp_init(&order); |
|
36 |
|
37 mp_read_radix(&pp, "0800000000000000000000000000000000000000C9", 16); |
|
38 mp_read_radix(&a, "1", 16); |
|
39 mp_read_radix(&b, "020A601907B8C953CA1481EB10512F78744A3205FD", 16); |
|
40 mp_read_radix(&x, "03F0EBA16286A2D57EA0991168D4994637E8343E36", 16); |
|
41 mp_read_radix(&y, "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1", 16); |
|
42 mp_read_radix(&order, "040000000000000000000292FE77E70C12A4234C33", 16); |
|
43 printf("pp = "); mp_print(&pp, stdout); fputc('\n', stdout); |
|
44 printf("a = "); mp_print(&a, stdout); fputc('\n', stdout); |
|
45 printf("b = "); mp_print(&b, stdout); fputc('\n', stdout); |
|
46 printf("x = "); mp_print(&x, stdout); fputc('\n', stdout); |
|
47 printf("y = "); mp_print(&y, stdout); fputc('\n', stdout); |
|
48 printf("order = "); mp_print(&order, stdout); fputc('\n', stdout); |
|
49 |
|
50 mp_init(&c); |
|
51 mp_init(&d); |
|
52 mp_init(&e); |
|
53 |
|
54 /* Test polynomial conversion */ |
|
55 ix = mp_bpoly2arr(&pp, ptemp, 10); |
|
56 if ( |
|
57 (ix != 5) || |
|
58 (ptemp[0] != p[0]) || |
|
59 (ptemp[1] != p[1]) || |
|
60 (ptemp[2] != p[2]) || |
|
61 (ptemp[3] != p[3]) || |
|
62 (ptemp[4] != p[4]) |
|
63 ) { |
|
64 printf("Polynomial to array conversion not correct\n"); |
|
65 return -1; |
|
66 } |
|
67 |
|
68 printf("Polynomial conversion test #1 successful.\n"); |
|
69 MP_CHECKOK( mp_barr2poly(p, &c) ); |
|
70 if (mp_cmp(&pp, &c) != 0) { |
|
71 printf("Array to polynomial conversion not correct\n"); |
|
72 return -1; |
|
73 } |
|
74 printf("Polynomial conversion test #2 successful.\n"); |
|
75 |
|
76 /* Test addition */ |
|
77 MP_CHECKOK( mp_badd(&a, &a, &c) ); |
|
78 if (mp_cmp_z(&c) != 0) { |
|
79 printf("a+a should equal zero\n"); |
|
80 return -1; |
|
81 } |
|
82 printf("Addition test #1 successful.\n"); |
|
83 MP_CHECKOK( mp_badd(&a, &b, &c) ); |
|
84 MP_CHECKOK( mp_badd(&b, &c, &c) ); |
|
85 if (mp_cmp(&c, &a) != 0) { |
|
86 printf("c = (a + b) + b should equal a\n"); |
|
87 printf("a = "); mp_print(&a, stdout); fputc('\n', stdout); |
|
88 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
89 return -1; |
|
90 } |
|
91 printf("Addition test #2 successful.\n"); |
|
92 |
|
93 /* Test multiplication */ |
|
94 mp_set(&c, 2); |
|
95 MP_CHECKOK( mp_bmul(&b, &c, &c) ); |
|
96 MP_CHECKOK( mp_badd(&b, &c, &c) ); |
|
97 mp_set(&d, 3); |
|
98 MP_CHECKOK( mp_bmul(&b, &d, &d) ); |
|
99 if (mp_cmp(&c, &d) != 0) { |
|
100 printf("c = (2 * b) + b should equal c = 3 * b\n"); |
|
101 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
102 printf("d = "); mp_print(&d, stdout); fputc('\n', stdout); |
|
103 return -1; |
|
104 } |
|
105 printf("Multiplication test #1 successful.\n"); |
|
106 |
|
107 /* Test modular reduction */ |
|
108 MP_CHECKOK( mp_bmod(&b, p, &c) ); |
|
109 if (mp_cmp(&b, &c) != 0) { |
|
110 printf("c = b mod p should equal b\n"); |
|
111 printf("b = "); mp_print(&b, stdout); fputc('\n', stdout); |
|
112 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
113 return -1; |
|
114 } |
|
115 printf("Modular reduction test #1 successful.\n"); |
|
116 MP_CHECKOK( mp_badd(&b, &pp, &c) ); |
|
117 MP_CHECKOK( mp_bmod(&c, p, &c) ); |
|
118 if (mp_cmp(&b, &c) != 0) { |
|
119 printf("c = (b + p) mod p should equal b\n"); |
|
120 printf("b = "); mp_print(&b, stdout); fputc('\n', stdout); |
|
121 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
122 return -1; |
|
123 } |
|
124 printf("Modular reduction test #2 successful.\n"); |
|
125 MP_CHECKOK( mp_bmul(&b, &pp, &c) ); |
|
126 MP_CHECKOK( mp_bmod(&c, p, &c) ); |
|
127 if (mp_cmp_z(&c) != 0) { |
|
128 printf("c = (b * p) mod p should equal 0\n"); |
|
129 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
130 return -1; |
|
131 } |
|
132 printf("Modular reduction test #3 successful.\n"); |
|
133 |
|
134 /* Test modular multiplication */ |
|
135 MP_CHECKOK( mp_bmulmod(&b, &pp, p, &c) ); |
|
136 if (mp_cmp_z(&c) != 0) { |
|
137 printf("c = (b * p) mod p should equal 0\n"); |
|
138 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
139 return -1; |
|
140 } |
|
141 printf("Modular multiplication test #1 successful.\n"); |
|
142 mp_set(&c, 1); |
|
143 MP_CHECKOK( mp_badd(&pp, &c, &c) ); |
|
144 MP_CHECKOK( mp_bmulmod(&b, &c, p, &c) ); |
|
145 if (mp_cmp(&b, &c) != 0) { |
|
146 printf("c = (b * (p + 1)) mod p should equal b\n"); |
|
147 printf("b = "); mp_print(&b, stdout); fputc('\n', stdout); |
|
148 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
149 return -1; |
|
150 } |
|
151 printf("Modular multiplication test #2 successful.\n"); |
|
152 |
|
153 /* Test modular squaring */ |
|
154 MP_CHECKOK( mp_copy(&b, &c) ); |
|
155 MP_CHECKOK( mp_bmulmod(&b, &c, p, &c) ); |
|
156 MP_CHECKOK( mp_bsqrmod(&b, p, &d) ); |
|
157 if (mp_cmp(&c, &d) != 0) { |
|
158 printf("c = (b * b) mod p should equal d = b^2 mod p\n"); |
|
159 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
160 printf("d = "); mp_print(&d, stdout); fputc('\n', stdout); |
|
161 return -1; |
|
162 } |
|
163 printf("Modular squaring test #1 successful.\n"); |
|
164 |
|
165 /* Test modular division */ |
|
166 MP_CHECKOK( mp_bdivmod(&b, &x, &pp, p, &c) ); |
|
167 MP_CHECKOK( mp_bmulmod(&c, &x, p, &c) ); |
|
168 if (mp_cmp(&b, &c) != 0) { |
|
169 printf("c = (b / x) * x mod p should equal b\n"); |
|
170 printf("b = "); mp_print(&b, stdout); fputc('\n', stdout); |
|
171 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
172 return -1; |
|
173 } |
|
174 printf("Modular division test #1 successful.\n"); |
|
175 |
|
176 CLEANUP: |
|
177 |
|
178 mp_clear(&order); |
|
179 mp_clear(&y); |
|
180 mp_clear(&x); |
|
181 mp_clear(&b); |
|
182 mp_clear(&a); |
|
183 mp_clear(&pp); |
|
184 |
|
185 return 0; |
|
186 } |