Imported Upstream version 3.13.6
[platform/upstream/nss.git] / mozilla / security / nss / lib / freebl / mpi / tests / mptest-b.c
1 /*
2  * Simple test driver for MPI library
3  *
4  * Test GF2m: Binary Polynomial Arithmetic
5  *
6  * ***** BEGIN LICENSE BLOCK *****
7  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
8  *
9  * The contents of this file are subject to the Mozilla Public License Version
10  * 1.1 (the "License"); you may not use this file except in compliance with
11  * the License. You may obtain a copy of the License at
12  * http://www.mozilla.org/MPL/
13  *
14  * Software distributed under the License is distributed on an "AS IS" basis,
15  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16  * for the specific language governing rights and limitations under the
17  * License.
18  *
19  * The Original Code is the Multi-precision Binary Polynomial Arithmetic Library.
20  *
21  * The Initial Developer of the Original Code is
22  * Netscape Communications Corporation.
23  * Portions created by the Initial Developer are Copyright (C) 2001
24  * the Initial Developer. All Rights Reserved.
25  *
26  * Contributor(s):
27  *   Sheueling Chang Shantz <sheueling.chang@sun.com> and
28  *   Douglas Stebila <douglas@stebila.ca> of Sun Laboratories.
29  *
30  * Alternatively, the contents of this file may be used under the terms of
31  * either the GNU General Public License Version 2 or later (the "GPL"), or
32  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
33  * in which case the provisions of the GPL or the LGPL are applicable instead
34  * of those above. If you wish to allow use of your version of this file only
35  * under the terms of either the GPL or the LGPL, and not to allow others to
36  * use your version of this file under the terms of the MPL, indicate your
37  * decision by deleting the provisions above and replace them with the notice
38  * and other provisions required by the GPL or the LGPL. If you do not delete
39  * the provisions above, a recipient may use your version of this file under
40  * the terms of any one of the MPL, the GPL or the LGPL.
41  *
42  * ***** END LICENSE BLOCK ***** */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ctype.h>
48 #include <limits.h>
49
50 #include "mp_gf2m.h"
51
52 int main(int argc, char *argv[])
53 {
54     int      ix;
55     mp_int   pp, a, b, x, y, order;
56     mp_int   c, d, e;
57     mp_digit r;
58     mp_err   res;
59     unsigned int p[] = {163,7,6,3,0};
60     unsigned int ptemp[10];
61
62     printf("Test b: Binary Polynomial Arithmetic\n\n");
63
64     mp_init(&pp);
65     mp_init(&a);
66     mp_init(&b);
67     mp_init(&x);
68     mp_init(&y);
69     mp_init(&order);
70
71     mp_read_radix(&pp, "0800000000000000000000000000000000000000C9", 16);
72     mp_read_radix(&a, "1", 16);
73     mp_read_radix(&b, "020A601907B8C953CA1481EB10512F78744A3205FD", 16);
74     mp_read_radix(&x, "03F0EBA16286A2D57EA0991168D4994637E8343E36", 16);
75     mp_read_radix(&y, "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1", 16);
76     mp_read_radix(&order, "040000000000000000000292FE77E70C12A4234C33", 16);
77     printf("pp = "); mp_print(&pp, stdout); fputc('\n', stdout);
78     printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
79     printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
80     printf("x = "); mp_print(&x, stdout); fputc('\n', stdout);
81     printf("y = "); mp_print(&y, stdout); fputc('\n', stdout);
82     printf("order = "); mp_print(&order, stdout); fputc('\n', stdout);
83
84     mp_init(&c);
85     mp_init(&d);
86     mp_init(&e);
87
88     /* Test polynomial conversion */
89     ix = mp_bpoly2arr(&pp, ptemp, 10);
90     if (
91         (ix != 5) ||
92         (ptemp[0] != p[0]) ||
93         (ptemp[1] != p[1]) ||
94         (ptemp[2] != p[2]) ||
95         (ptemp[3] != p[3]) ||
96         (ptemp[4] != p[4])
97     ) {
98         printf("Polynomial to array conversion not correct\n"); 
99         return -1;
100     }
101
102     printf("Polynomial conversion test #1 successful.\n");
103     MP_CHECKOK( mp_barr2poly(p, &c) );
104     if (mp_cmp(&pp, &c) != 0) {
105         printf("Array to polynomial conversion not correct\n"); 
106         return -1;
107     }
108     printf("Polynomial conversion test #2 successful.\n");
109
110     /* Test addition */
111     MP_CHECKOK( mp_badd(&a, &a, &c) );
112     if (mp_cmp_z(&c) != 0) {
113         printf("a+a should equal zero\n"); 
114         return -1;
115     }
116     printf("Addition test #1 successful.\n");
117     MP_CHECKOK( mp_badd(&a, &b, &c) );
118     MP_CHECKOK( mp_badd(&b, &c, &c) );
119     if (mp_cmp(&c, &a) != 0) {
120         printf("c = (a + b) + b should equal a\n"); 
121         printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
122         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
123         return -1;
124     }
125     printf("Addition test #2 successful.\n");
126     
127     /* Test multiplication */
128     mp_set(&c, 2);
129     MP_CHECKOK( mp_bmul(&b, &c, &c) );
130     MP_CHECKOK( mp_badd(&b, &c, &c) );
131     mp_set(&d, 3);
132     MP_CHECKOK( mp_bmul(&b, &d, &d) );
133     if (mp_cmp(&c, &d) != 0) {
134         printf("c = (2 * b) + b should equal c = 3 * b\n"); 
135         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
136         printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);
137         return -1;
138     }
139     printf("Multiplication test #1 successful.\n");
140
141     /* Test modular reduction */
142     MP_CHECKOK( mp_bmod(&b, p, &c) );
143     if (mp_cmp(&b, &c) != 0) {
144         printf("c = b mod p should equal b\n"); 
145         printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
146         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
147         return -1;
148     }
149     printf("Modular reduction test #1 successful.\n");
150     MP_CHECKOK( mp_badd(&b, &pp, &c) );
151     MP_CHECKOK( mp_bmod(&c, p, &c) );
152     if (mp_cmp(&b, &c) != 0) {
153         printf("c = (b + p) mod p should equal b\n"); 
154         printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
155         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
156         return -1;
157     }
158     printf("Modular reduction test #2 successful.\n");
159     MP_CHECKOK( mp_bmul(&b, &pp, &c) );
160     MP_CHECKOK( mp_bmod(&c, p, &c) );
161     if (mp_cmp_z(&c) != 0) {
162         printf("c = (b * p) mod p should equal 0\n"); 
163         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
164         return -1;
165     }
166     printf("Modular reduction test #3 successful.\n");
167
168     /* Test modular multiplication */
169     MP_CHECKOK( mp_bmulmod(&b, &pp, p, &c) );
170     if (mp_cmp_z(&c) != 0) {
171         printf("c = (b * p) mod p should equal 0\n"); 
172         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
173         return -1;
174     }
175     printf("Modular multiplication test #1 successful.\n");
176     mp_set(&c, 1);
177     MP_CHECKOK( mp_badd(&pp, &c, &c) );
178     MP_CHECKOK( mp_bmulmod(&b, &c, p, &c) );
179     if (mp_cmp(&b, &c) != 0) {
180         printf("c = (b * (p + 1)) mod p should equal b\n"); 
181         printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
182         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
183         return -1;
184     }
185     printf("Modular multiplication test #2 successful.\n");
186
187     /* Test modular squaring */
188     MP_CHECKOK( mp_copy(&b, &c) );
189     MP_CHECKOK( mp_bmulmod(&b, &c, p, &c) );
190     MP_CHECKOK( mp_bsqrmod(&b, p, &d) );
191     if (mp_cmp(&c, &d) != 0) {
192         printf("c = (b * b) mod p should equal d = b^2 mod p\n"); 
193         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
194         printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);
195         return -1;
196     }
197     printf("Modular squaring test #1 successful.\n");
198     
199     /* Test modular division */
200     MP_CHECKOK( mp_bdivmod(&b, &x, &pp, p, &c) );
201     MP_CHECKOK( mp_bmulmod(&c, &x, p, &c) );
202     if (mp_cmp(&b, &c) != 0) {
203         printf("c = (b / x) * x mod p should equal b\n"); 
204         printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
205         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
206         return -1;
207     }
208     printf("Modular division test #1 successful.\n");
209
210 CLEANUP:
211
212     mp_clear(&order);
213     mp_clear(&y);
214     mp_clear(&x);
215     mp_clear(&b);
216     mp_clear(&a);
217     mp_clear(&pp);
218
219     return 0;
220 }