Back to index

lightning-sunbird  0.9+nobinonly
mptest-b.c
Go to the documentation of this file.
00001 /*
00002  * Simple test driver for MPI library
00003  *
00004  * Test GF2m: Binary Polynomial Arithmetic
00005  *
00006  * ***** BEGIN LICENSE BLOCK *****
00007  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00008  *
00009  * The contents of this file are subject to the Mozilla Public License Version
00010  * 1.1 (the "License"); you may not use this file except in compliance with
00011  * the License. You may obtain a copy of the License at
00012  * http://www.mozilla.org/MPL/
00013  *
00014  * Software distributed under the License is distributed on an "AS IS" basis,
00015  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00016  * for the specific language governing rights and limitations under the
00017  * License.
00018  *
00019  * The Original Code is the Multi-precision Binary Polynomial Arithmetic Library.
00020  *
00021  * The Initial Developer of the Original Code is
00022  * Netscape Communications Corporation.
00023  * Portions created by the Initial Developer are Copyright (C) 2001
00024  * the Initial Developer. All Rights Reserved.
00025  *
00026  * Contributor(s):
00027  *   Sheueling Chang Shantz <sheueling.chang@sun.com> and
00028  *   Douglas Stebila <douglas@stebila.ca> of Sun Laboratories.
00029  *
00030  * Alternatively, the contents of this file may be used under the terms of
00031  * either the GNU General Public License Version 2 or later (the "GPL"), or
00032  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00033  * in which case the provisions of the GPL or the LGPL are applicable instead
00034  * of those above. If you wish to allow use of your version of this file only
00035  * under the terms of either the GPL or the LGPL, and not to allow others to
00036  * use your version of this file under the terms of the MPL, indicate your
00037  * decision by deleting the provisions above and replace them with the notice
00038  * and other provisions required by the GPL or the LGPL. If you do not delete
00039  * the provisions above, a recipient may use your version of this file under
00040  * the terms of any one of the MPL, the GPL or the LGPL.
00041  *
00042  * ***** END LICENSE BLOCK ***** */
00043 
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <string.h>
00047 #include <ctype.h>
00048 #include <limits.h>
00049 
00050 #include "mp_gf2m.h"
00051 
00052 int main(int argc, char *argv[])
00053 {
00054     int      ix;
00055     mp_int   pp, a, b, x, y, order;
00056     mp_int   c, d, e;
00057     mp_digit r;
00058     mp_err   res;
00059     unsigned int p[] = {163,7,6,3,0};
00060     unsigned int ptemp[10];
00061 
00062     printf("Test b: Binary Polynomial Arithmetic\n\n");
00063 
00064     mp_init(&pp);
00065     mp_init(&a);
00066     mp_init(&b);
00067     mp_init(&x);
00068     mp_init(&y);
00069     mp_init(&order);
00070 
00071     mp_read_radix(&pp, "0800000000000000000000000000000000000000C9", 16);
00072     mp_read_radix(&a, "1", 16);
00073     mp_read_radix(&b, "020A601907B8C953CA1481EB10512F78744A3205FD", 16);
00074     mp_read_radix(&x, "03F0EBA16286A2D57EA0991168D4994637E8343E36", 16);
00075     mp_read_radix(&y, "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1", 16);
00076     mp_read_radix(&order, "040000000000000000000292FE77E70C12A4234C33", 16);
00077     printf("pp = "); mp_print(&pp, stdout); fputc('\n', stdout);
00078     printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
00079     printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
00080     printf("x = "); mp_print(&x, stdout); fputc('\n', stdout);
00081     printf("y = "); mp_print(&y, stdout); fputc('\n', stdout);
00082     printf("order = "); mp_print(&order, stdout); fputc('\n', stdout);
00083 
00084     mp_init(&c);
00085     mp_init(&d);
00086     mp_init(&e);
00087 
00088     /* Test polynomial conversion */
00089     ix = mp_bpoly2arr(&pp, ptemp, 10);
00090     if (
00091        (ix != 5) ||
00092        (ptemp[0] != p[0]) ||
00093        (ptemp[1] != p[1]) ||
00094        (ptemp[2] != p[2]) ||
00095        (ptemp[3] != p[3]) ||
00096        (ptemp[4] != p[4])
00097     ) {
00098        printf("Polynomial to array conversion not correct\n"); 
00099        return -1;
00100     }
00101 
00102     printf("Polynomial conversion test #1 successful.\n");
00103     MP_CHECKOK( mp_barr2poly(p, &c) );
00104     if (mp_cmp(&pp, &c) != 0) {
00105         printf("Array to polynomial conversion not correct\n"); 
00106         return -1;
00107     }
00108     printf("Polynomial conversion test #2 successful.\n");
00109 
00110     /* Test addition */
00111     MP_CHECKOK( mp_badd(&a, &a, &c) );
00112     if (mp_cmp_z(&c) != 0) {
00113         printf("a+a should equal zero\n"); 
00114         return -1;
00115     }
00116     printf("Addition test #1 successful.\n");
00117     MP_CHECKOK( mp_badd(&a, &b, &c) );
00118     MP_CHECKOK( mp_badd(&b, &c, &c) );
00119     if (mp_cmp(&c, &a) != 0) {
00120         printf("c = (a + b) + b should equal a\n"); 
00121         printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
00122         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00123         return -1;
00124     }
00125     printf("Addition test #2 successful.\n");
00126     
00127     /* Test multiplication */
00128     mp_set(&c, 2);
00129     MP_CHECKOK( mp_bmul(&b, &c, &c) );
00130     MP_CHECKOK( mp_badd(&b, &c, &c) );
00131     mp_set(&d, 3);
00132     MP_CHECKOK( mp_bmul(&b, &d, &d) );
00133     if (mp_cmp(&c, &d) != 0) {
00134         printf("c = (2 * b) + b should equal c = 3 * b\n"); 
00135         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00136         printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);
00137         return -1;
00138     }
00139     printf("Multiplication test #1 successful.\n");
00140 
00141     /* Test modular reduction */
00142     MP_CHECKOK( mp_bmod(&b, p, &c) );
00143     if (mp_cmp(&b, &c) != 0) {
00144         printf("c = b mod p should equal b\n"); 
00145         printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
00146         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00147         return -1;
00148     }
00149     printf("Modular reduction test #1 successful.\n");
00150     MP_CHECKOK( mp_badd(&b, &pp, &c) );
00151     MP_CHECKOK( mp_bmod(&c, p, &c) );
00152     if (mp_cmp(&b, &c) != 0) {
00153         printf("c = (b + p) mod p should equal b\n"); 
00154         printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
00155         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00156         return -1;
00157     }
00158     printf("Modular reduction test #2 successful.\n");
00159     MP_CHECKOK( mp_bmul(&b, &pp, &c) );
00160     MP_CHECKOK( mp_bmod(&c, p, &c) );
00161     if (mp_cmp_z(&c) != 0) {
00162         printf("c = (b * p) mod p should equal 0\n"); 
00163         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00164         return -1;
00165     }
00166     printf("Modular reduction test #3 successful.\n");
00167 
00168     /* Test modular multiplication */
00169     MP_CHECKOK( mp_bmulmod(&b, &pp, p, &c) );
00170     if (mp_cmp_z(&c) != 0) {
00171         printf("c = (b * p) mod p should equal 0\n"); 
00172         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00173         return -1;
00174     }
00175     printf("Modular multiplication test #1 successful.\n");
00176     mp_set(&c, 1);
00177     MP_CHECKOK( mp_badd(&pp, &c, &c) );
00178     MP_CHECKOK( mp_bmulmod(&b, &c, p, &c) );
00179     if (mp_cmp(&b, &c) != 0) {
00180         printf("c = (b * (p + 1)) mod p should equal b\n"); 
00181         printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
00182         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00183         return -1;
00184     }
00185     printf("Modular multiplication test #2 successful.\n");
00186 
00187     /* Test modular squaring */
00188     MP_CHECKOK( mp_copy(&b, &c) );
00189     MP_CHECKOK( mp_bmulmod(&b, &c, p, &c) );
00190     MP_CHECKOK( mp_bsqrmod(&b, p, &d) );
00191     if (mp_cmp(&c, &d) != 0) {
00192         printf("c = (b * b) mod p should equal d = b^2 mod p\n"); 
00193         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00194         printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);
00195         return -1;
00196     }
00197     printf("Modular squaring test #1 successful.\n");
00198     
00199     /* Test modular division */
00200     MP_CHECKOK( mp_bdivmod(&b, &x, &pp, p, &c) );
00201     MP_CHECKOK( mp_bmulmod(&c, &x, p, &c) );
00202     if (mp_cmp(&b, &c) != 0) {
00203         printf("c = (b / x) * x mod p should equal b\n"); 
00204         printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
00205         printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00206         return -1;
00207     }
00208     printf("Modular division test #1 successful.\n");
00209 
00210 CLEANUP:
00211 
00212     mp_clear(&order);
00213     mp_clear(&y);
00214     mp_clear(&x);
00215     mp_clear(&b);
00216     mp_clear(&a);
00217     mp_clear(&pp);
00218 
00219     return 0;
00220 }