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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/mpi/tests/mptest-b.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,186 @@
     1.4 +/*
     1.5 + * Simple test driver for MPI library
     1.6 + *
     1.7 + * Test GF2m: Binary Polynomial Arithmetic
     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 "mp_gf2m.h"
    1.20 +
    1.21 +int main(int argc, char *argv[])
    1.22 +{
    1.23 +    int      ix;
    1.24 +    mp_int   pp, a, b, x, y, order;
    1.25 +    mp_int   c, d, e;
    1.26 +    mp_digit r;
    1.27 +    mp_err   res;
    1.28 +    unsigned int p[] = {163,7,6,3,0};
    1.29 +    unsigned int ptemp[10];
    1.30 +
    1.31 +    printf("Test b: Binary Polynomial Arithmetic\n\n");
    1.32 +
    1.33 +    mp_init(&pp);
    1.34 +    mp_init(&a);
    1.35 +    mp_init(&b);
    1.36 +    mp_init(&x);
    1.37 +    mp_init(&y);
    1.38 +    mp_init(&order);
    1.39 +
    1.40 +    mp_read_radix(&pp, "0800000000000000000000000000000000000000C9", 16);
    1.41 +    mp_read_radix(&a, "1", 16);
    1.42 +    mp_read_radix(&b, "020A601907B8C953CA1481EB10512F78744A3205FD", 16);
    1.43 +    mp_read_radix(&x, "03F0EBA16286A2D57EA0991168D4994637E8343E36", 16);
    1.44 +    mp_read_radix(&y, "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1", 16);
    1.45 +    mp_read_radix(&order, "040000000000000000000292FE77E70C12A4234C33", 16);
    1.46 +    printf("pp = "); mp_print(&pp, stdout); fputc('\n', stdout);
    1.47 +    printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
    1.48 +    printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
    1.49 +    printf("x = "); mp_print(&x, stdout); fputc('\n', stdout);
    1.50 +    printf("y = "); mp_print(&y, stdout); fputc('\n', stdout);
    1.51 +    printf("order = "); mp_print(&order, stdout); fputc('\n', stdout);
    1.52 +
    1.53 +    mp_init(&c);
    1.54 +    mp_init(&d);
    1.55 +    mp_init(&e);
    1.56 +
    1.57 +    /* Test polynomial conversion */
    1.58 +    ix = mp_bpoly2arr(&pp, ptemp, 10);
    1.59 +    if (
    1.60 +	(ix != 5) ||
    1.61 +	(ptemp[0] != p[0]) ||
    1.62 +	(ptemp[1] != p[1]) ||
    1.63 +	(ptemp[2] != p[2]) ||
    1.64 +	(ptemp[3] != p[3]) ||
    1.65 +	(ptemp[4] != p[4])
    1.66 +    ) {
    1.67 +	printf("Polynomial to array conversion not correct\n"); 
    1.68 +	return -1;
    1.69 +    }
    1.70 +
    1.71 +    printf("Polynomial conversion test #1 successful.\n");
    1.72 +    MP_CHECKOK( mp_barr2poly(p, &c) );
    1.73 +    if (mp_cmp(&pp, &c) != 0) {
    1.74 +        printf("Array to polynomial conversion not correct\n"); 
    1.75 +        return -1;
    1.76 +    }
    1.77 +    printf("Polynomial conversion test #2 successful.\n");
    1.78 +
    1.79 +    /* Test addition */
    1.80 +    MP_CHECKOK( mp_badd(&a, &a, &c) );
    1.81 +    if (mp_cmp_z(&c) != 0) {
    1.82 +        printf("a+a should equal zero\n"); 
    1.83 +        return -1;
    1.84 +    }
    1.85 +    printf("Addition test #1 successful.\n");
    1.86 +    MP_CHECKOK( mp_badd(&a, &b, &c) );
    1.87 +    MP_CHECKOK( mp_badd(&b, &c, &c) );
    1.88 +    if (mp_cmp(&c, &a) != 0) {
    1.89 +        printf("c = (a + b) + b should equal a\n"); 
    1.90 +        printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
    1.91 +        printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
    1.92 +        return -1;
    1.93 +    }
    1.94 +    printf("Addition test #2 successful.\n");
    1.95 +    
    1.96 +    /* Test multiplication */
    1.97 +    mp_set(&c, 2);
    1.98 +    MP_CHECKOK( mp_bmul(&b, &c, &c) );
    1.99 +    MP_CHECKOK( mp_badd(&b, &c, &c) );
   1.100 +    mp_set(&d, 3);
   1.101 +    MP_CHECKOK( mp_bmul(&b, &d, &d) );
   1.102 +    if (mp_cmp(&c, &d) != 0) {
   1.103 +        printf("c = (2 * b) + b should equal c = 3 * b\n"); 
   1.104 +        printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
   1.105 +        printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);
   1.106 +        return -1;
   1.107 +    }
   1.108 +    printf("Multiplication test #1 successful.\n");
   1.109 +
   1.110 +    /* Test modular reduction */
   1.111 +    MP_CHECKOK( mp_bmod(&b, p, &c) );
   1.112 +    if (mp_cmp(&b, &c) != 0) {
   1.113 +        printf("c = b mod p should equal b\n"); 
   1.114 +        printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
   1.115 +        printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
   1.116 +        return -1;
   1.117 +    }
   1.118 +    printf("Modular reduction test #1 successful.\n");
   1.119 +    MP_CHECKOK( mp_badd(&b, &pp, &c) );
   1.120 +    MP_CHECKOK( mp_bmod(&c, p, &c) );
   1.121 +    if (mp_cmp(&b, &c) != 0) {
   1.122 +        printf("c = (b + p) mod p should equal b\n"); 
   1.123 +        printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
   1.124 +        printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
   1.125 +        return -1;
   1.126 +    }
   1.127 +    printf("Modular reduction test #2 successful.\n");
   1.128 +    MP_CHECKOK( mp_bmul(&b, &pp, &c) );
   1.129 +    MP_CHECKOK( mp_bmod(&c, p, &c) );
   1.130 +    if (mp_cmp_z(&c) != 0) {
   1.131 +        printf("c = (b * p) mod p should equal 0\n"); 
   1.132 +        printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
   1.133 +        return -1;
   1.134 +    }
   1.135 +    printf("Modular reduction test #3 successful.\n");
   1.136 +
   1.137 +    /* Test modular multiplication */
   1.138 +    MP_CHECKOK( mp_bmulmod(&b, &pp, p, &c) );
   1.139 +    if (mp_cmp_z(&c) != 0) {
   1.140 +        printf("c = (b * p) mod p should equal 0\n"); 
   1.141 +        printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
   1.142 +        return -1;
   1.143 +    }
   1.144 +    printf("Modular multiplication test #1 successful.\n");
   1.145 +    mp_set(&c, 1);
   1.146 +    MP_CHECKOK( mp_badd(&pp, &c, &c) );
   1.147 +    MP_CHECKOK( mp_bmulmod(&b, &c, p, &c) );
   1.148 +    if (mp_cmp(&b, &c) != 0) {
   1.149 +        printf("c = (b * (p + 1)) mod p should equal b\n"); 
   1.150 +        printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
   1.151 +        printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
   1.152 +        return -1;
   1.153 +    }
   1.154 +    printf("Modular multiplication test #2 successful.\n");
   1.155 +
   1.156 +    /* Test modular squaring */
   1.157 +    MP_CHECKOK( mp_copy(&b, &c) );
   1.158 +    MP_CHECKOK( mp_bmulmod(&b, &c, p, &c) );
   1.159 +    MP_CHECKOK( mp_bsqrmod(&b, p, &d) );
   1.160 +    if (mp_cmp(&c, &d) != 0) {
   1.161 +        printf("c = (b * b) mod p should equal d = b^2 mod p\n"); 
   1.162 +        printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
   1.163 +        printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);
   1.164 +        return -1;
   1.165 +    }
   1.166 +    printf("Modular squaring test #1 successful.\n");
   1.167 +    
   1.168 +    /* Test modular division */
   1.169 +    MP_CHECKOK( mp_bdivmod(&b, &x, &pp, p, &c) );
   1.170 +    MP_CHECKOK( mp_bmulmod(&c, &x, p, &c) );
   1.171 +    if (mp_cmp(&b, &c) != 0) {
   1.172 +        printf("c = (b / x) * x mod p should equal b\n"); 
   1.173 +        printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
   1.174 +        printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
   1.175 +        return -1;
   1.176 +    }
   1.177 +    printf("Modular division test #1 successful.\n");
   1.178 +
   1.179 +CLEANUP:
   1.180 +
   1.181 +    mp_clear(&order);
   1.182 +    mp_clear(&y);
   1.183 +    mp_clear(&x);
   1.184 +    mp_clear(&b);
   1.185 +    mp_clear(&a);
   1.186 +    mp_clear(&pp);
   1.187 +
   1.188 +    return 0;
   1.189 +}

mercurial