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