Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "mp_gf2m.h" |
michael@0 | 6 | #include "mp_gf2m-priv.h" |
michael@0 | 7 | #include "mplogic.h" |
michael@0 | 8 | #include "mpi-priv.h" |
michael@0 | 9 | |
michael@0 | 10 | const mp_digit mp_gf2m_sqr_tb[16] = |
michael@0 | 11 | { |
michael@0 | 12 | 0, 1, 4, 5, 16, 17, 20, 21, |
michael@0 | 13 | 64, 65, 68, 69, 80, 81, 84, 85 |
michael@0 | 14 | }; |
michael@0 | 15 | |
michael@0 | 16 | /* Multiply two binary polynomials mp_digits a, b. |
michael@0 | 17 | * Result is a polynomial with degree < 2 * MP_DIGIT_BITS - 1. |
michael@0 | 18 | * Output in two mp_digits rh, rl. |
michael@0 | 19 | */ |
michael@0 | 20 | #if MP_DIGIT_BITS == 32 |
michael@0 | 21 | void |
michael@0 | 22 | s_bmul_1x1(mp_digit *rh, mp_digit *rl, const mp_digit a, const mp_digit b) |
michael@0 | 23 | { |
michael@0 | 24 | register mp_digit h, l, s; |
michael@0 | 25 | mp_digit tab[8], top2b = a >> 30; |
michael@0 | 26 | register mp_digit a1, a2, a4; |
michael@0 | 27 | |
michael@0 | 28 | a1 = a & (0x3FFFFFFF); a2 = a1 << 1; a4 = a2 << 1; |
michael@0 | 29 | |
michael@0 | 30 | tab[0] = 0; tab[1] = a1; tab[2] = a2; tab[3] = a1^a2; |
michael@0 | 31 | tab[4] = a4; tab[5] = a1^a4; tab[6] = a2^a4; tab[7] = a1^a2^a4; |
michael@0 | 32 | |
michael@0 | 33 | s = tab[b & 0x7]; l = s; |
michael@0 | 34 | s = tab[b >> 3 & 0x7]; l ^= s << 3; h = s >> 29; |
michael@0 | 35 | s = tab[b >> 6 & 0x7]; l ^= s << 6; h ^= s >> 26; |
michael@0 | 36 | s = tab[b >> 9 & 0x7]; l ^= s << 9; h ^= s >> 23; |
michael@0 | 37 | s = tab[b >> 12 & 0x7]; l ^= s << 12; h ^= s >> 20; |
michael@0 | 38 | s = tab[b >> 15 & 0x7]; l ^= s << 15; h ^= s >> 17; |
michael@0 | 39 | s = tab[b >> 18 & 0x7]; l ^= s << 18; h ^= s >> 14; |
michael@0 | 40 | s = tab[b >> 21 & 0x7]; l ^= s << 21; h ^= s >> 11; |
michael@0 | 41 | s = tab[b >> 24 & 0x7]; l ^= s << 24; h ^= s >> 8; |
michael@0 | 42 | s = tab[b >> 27 & 0x7]; l ^= s << 27; h ^= s >> 5; |
michael@0 | 43 | s = tab[b >> 30 ]; l ^= s << 30; h ^= s >> 2; |
michael@0 | 44 | |
michael@0 | 45 | /* compensate for the top two bits of a */ |
michael@0 | 46 | |
michael@0 | 47 | if (top2b & 01) { l ^= b << 30; h ^= b >> 2; } |
michael@0 | 48 | if (top2b & 02) { l ^= b << 31; h ^= b >> 1; } |
michael@0 | 49 | |
michael@0 | 50 | *rh = h; *rl = l; |
michael@0 | 51 | } |
michael@0 | 52 | #else |
michael@0 | 53 | void |
michael@0 | 54 | s_bmul_1x1(mp_digit *rh, mp_digit *rl, const mp_digit a, const mp_digit b) |
michael@0 | 55 | { |
michael@0 | 56 | register mp_digit h, l, s; |
michael@0 | 57 | mp_digit tab[16], top3b = a >> 61; |
michael@0 | 58 | register mp_digit a1, a2, a4, a8; |
michael@0 | 59 | |
michael@0 | 60 | a1 = a & (0x1FFFFFFFFFFFFFFFULL); a2 = a1 << 1; |
michael@0 | 61 | a4 = a2 << 1; a8 = a4 << 1; |
michael@0 | 62 | tab[ 0] = 0; tab[ 1] = a1; tab[ 2] = a2; tab[ 3] = a1^a2; |
michael@0 | 63 | tab[ 4] = a4; tab[ 5] = a1^a4; tab[ 6] = a2^a4; tab[ 7] = a1^a2^a4; |
michael@0 | 64 | tab[ 8] = a8; tab[ 9] = a1^a8; tab[10] = a2^a8; tab[11] = a1^a2^a8; |
michael@0 | 65 | tab[12] = a4^a8; tab[13] = a1^a4^a8; tab[14] = a2^a4^a8; tab[15] = a1^a2^a4^a8; |
michael@0 | 66 | |
michael@0 | 67 | s = tab[b & 0xF]; l = s; |
michael@0 | 68 | s = tab[b >> 4 & 0xF]; l ^= s << 4; h = s >> 60; |
michael@0 | 69 | s = tab[b >> 8 & 0xF]; l ^= s << 8; h ^= s >> 56; |
michael@0 | 70 | s = tab[b >> 12 & 0xF]; l ^= s << 12; h ^= s >> 52; |
michael@0 | 71 | s = tab[b >> 16 & 0xF]; l ^= s << 16; h ^= s >> 48; |
michael@0 | 72 | s = tab[b >> 20 & 0xF]; l ^= s << 20; h ^= s >> 44; |
michael@0 | 73 | s = tab[b >> 24 & 0xF]; l ^= s << 24; h ^= s >> 40; |
michael@0 | 74 | s = tab[b >> 28 & 0xF]; l ^= s << 28; h ^= s >> 36; |
michael@0 | 75 | s = tab[b >> 32 & 0xF]; l ^= s << 32; h ^= s >> 32; |
michael@0 | 76 | s = tab[b >> 36 & 0xF]; l ^= s << 36; h ^= s >> 28; |
michael@0 | 77 | s = tab[b >> 40 & 0xF]; l ^= s << 40; h ^= s >> 24; |
michael@0 | 78 | s = tab[b >> 44 & 0xF]; l ^= s << 44; h ^= s >> 20; |
michael@0 | 79 | s = tab[b >> 48 & 0xF]; l ^= s << 48; h ^= s >> 16; |
michael@0 | 80 | s = tab[b >> 52 & 0xF]; l ^= s << 52; h ^= s >> 12; |
michael@0 | 81 | s = tab[b >> 56 & 0xF]; l ^= s << 56; h ^= s >> 8; |
michael@0 | 82 | s = tab[b >> 60 ]; l ^= s << 60; h ^= s >> 4; |
michael@0 | 83 | |
michael@0 | 84 | /* compensate for the top three bits of a */ |
michael@0 | 85 | |
michael@0 | 86 | if (top3b & 01) { l ^= b << 61; h ^= b >> 3; } |
michael@0 | 87 | if (top3b & 02) { l ^= b << 62; h ^= b >> 2; } |
michael@0 | 88 | if (top3b & 04) { l ^= b << 63; h ^= b >> 1; } |
michael@0 | 89 | |
michael@0 | 90 | *rh = h; *rl = l; |
michael@0 | 91 | } |
michael@0 | 92 | #endif |
michael@0 | 93 | |
michael@0 | 94 | /* Compute xor-multiply of two binary polynomials (a1, a0) x (b1, b0) |
michael@0 | 95 | * result is a binary polynomial in 4 mp_digits r[4]. |
michael@0 | 96 | * The caller MUST ensure that r has the right amount of space allocated. |
michael@0 | 97 | */ |
michael@0 | 98 | void |
michael@0 | 99 | s_bmul_2x2(mp_digit *r, const mp_digit a1, const mp_digit a0, const mp_digit b1, |
michael@0 | 100 | const mp_digit b0) |
michael@0 | 101 | { |
michael@0 | 102 | mp_digit m1, m0; |
michael@0 | 103 | /* r[3] = h1, r[2] = h0; r[1] = l1; r[0] = l0 */ |
michael@0 | 104 | s_bmul_1x1(r+3, r+2, a1, b1); |
michael@0 | 105 | s_bmul_1x1(r+1, r, a0, b0); |
michael@0 | 106 | s_bmul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1); |
michael@0 | 107 | /* Correction on m1 ^= l1 ^ h1; m0 ^= l0 ^ h0; */ |
michael@0 | 108 | r[2] ^= m1 ^ r[1] ^ r[3]; /* h0 ^= m1 ^ l1 ^ h1; */ |
michael@0 | 109 | r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0; /* l1 ^= l0 ^ h0 ^ m0; */ |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | /* Compute xor-multiply of two binary polynomials (a2, a1, a0) x (b2, b1, b0) |
michael@0 | 113 | * result is a binary polynomial in 6 mp_digits r[6]. |
michael@0 | 114 | * The caller MUST ensure that r has the right amount of space allocated. |
michael@0 | 115 | */ |
michael@0 | 116 | void |
michael@0 | 117 | s_bmul_3x3(mp_digit *r, const mp_digit a2, const mp_digit a1, const mp_digit a0, |
michael@0 | 118 | const mp_digit b2, const mp_digit b1, const mp_digit b0) |
michael@0 | 119 | { |
michael@0 | 120 | mp_digit zm[4]; |
michael@0 | 121 | |
michael@0 | 122 | s_bmul_1x1(r+5, r+4, a2, b2); /* fill top 2 words */ |
michael@0 | 123 | s_bmul_2x2(zm, a1, a2^a0, b1, b2^b0); /* fill middle 4 words */ |
michael@0 | 124 | s_bmul_2x2(r, a1, a0, b1, b0); /* fill bottom 4 words */ |
michael@0 | 125 | |
michael@0 | 126 | zm[3] ^= r[3]; |
michael@0 | 127 | zm[2] ^= r[2]; |
michael@0 | 128 | zm[1] ^= r[1] ^ r[5]; |
michael@0 | 129 | zm[0] ^= r[0] ^ r[4]; |
michael@0 | 130 | |
michael@0 | 131 | r[5] ^= zm[3]; |
michael@0 | 132 | r[4] ^= zm[2]; |
michael@0 | 133 | r[3] ^= zm[1]; |
michael@0 | 134 | r[2] ^= zm[0]; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | /* Compute xor-multiply of two binary polynomials (a3, a2, a1, a0) x (b3, b2, b1, b0) |
michael@0 | 138 | * result is a binary polynomial in 8 mp_digits r[8]. |
michael@0 | 139 | * The caller MUST ensure that r has the right amount of space allocated. |
michael@0 | 140 | */ |
michael@0 | 141 | void s_bmul_4x4(mp_digit *r, const mp_digit a3, const mp_digit a2, const mp_digit a1, |
michael@0 | 142 | const mp_digit a0, const mp_digit b3, const mp_digit b2, const mp_digit b1, |
michael@0 | 143 | const mp_digit b0) |
michael@0 | 144 | { |
michael@0 | 145 | mp_digit zm[4]; |
michael@0 | 146 | |
michael@0 | 147 | s_bmul_2x2(r+4, a3, a2, b3, b2); /* fill top 4 words */ |
michael@0 | 148 | s_bmul_2x2(zm, a3^a1, a2^a0, b3^b1, b2^b0); /* fill middle 4 words */ |
michael@0 | 149 | s_bmul_2x2(r, a1, a0, b1, b0); /* fill bottom 4 words */ |
michael@0 | 150 | |
michael@0 | 151 | zm[3] ^= r[3] ^ r[7]; |
michael@0 | 152 | zm[2] ^= r[2] ^ r[6]; |
michael@0 | 153 | zm[1] ^= r[1] ^ r[5]; |
michael@0 | 154 | zm[0] ^= r[0] ^ r[4]; |
michael@0 | 155 | |
michael@0 | 156 | r[5] ^= zm[3]; |
michael@0 | 157 | r[4] ^= zm[2]; |
michael@0 | 158 | r[3] ^= zm[1]; |
michael@0 | 159 | r[2] ^= zm[0]; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | /* Compute addition of two binary polynomials a and b, |
michael@0 | 163 | * store result in c; c could be a or b, a and b could be equal; |
michael@0 | 164 | * c is the bitwise XOR of a and b. |
michael@0 | 165 | */ |
michael@0 | 166 | mp_err |
michael@0 | 167 | mp_badd(const mp_int *a, const mp_int *b, mp_int *c) |
michael@0 | 168 | { |
michael@0 | 169 | mp_digit *pa, *pb, *pc; |
michael@0 | 170 | mp_size ix; |
michael@0 | 171 | mp_size used_pa, used_pb; |
michael@0 | 172 | mp_err res = MP_OKAY; |
michael@0 | 173 | |
michael@0 | 174 | /* Add all digits up to the precision of b. If b had more |
michael@0 | 175 | * precision than a initially, swap a, b first |
michael@0 | 176 | */ |
michael@0 | 177 | if (MP_USED(a) >= MP_USED(b)) { |
michael@0 | 178 | pa = MP_DIGITS(a); |
michael@0 | 179 | pb = MP_DIGITS(b); |
michael@0 | 180 | used_pa = MP_USED(a); |
michael@0 | 181 | used_pb = MP_USED(b); |
michael@0 | 182 | } else { |
michael@0 | 183 | pa = MP_DIGITS(b); |
michael@0 | 184 | pb = MP_DIGITS(a); |
michael@0 | 185 | used_pa = MP_USED(b); |
michael@0 | 186 | used_pb = MP_USED(a); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | /* Make sure c has enough precision for the output value */ |
michael@0 | 190 | MP_CHECKOK( s_mp_pad(c, used_pa) ); |
michael@0 | 191 | |
michael@0 | 192 | /* Do word-by-word xor */ |
michael@0 | 193 | pc = MP_DIGITS(c); |
michael@0 | 194 | for (ix = 0; ix < used_pb; ix++) { |
michael@0 | 195 | (*pc++) = (*pa++) ^ (*pb++); |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | /* Finish the rest of digits until we're actually done */ |
michael@0 | 199 | for (; ix < used_pa; ++ix) { |
michael@0 | 200 | *pc++ = *pa++; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | MP_USED(c) = used_pa; |
michael@0 | 204 | MP_SIGN(c) = ZPOS; |
michael@0 | 205 | s_mp_clamp(c); |
michael@0 | 206 | |
michael@0 | 207 | CLEANUP: |
michael@0 | 208 | return res; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | #define s_mp_div2(a) MP_CHECKOK( mpl_rsh((a), (a), 1) ); |
michael@0 | 212 | |
michael@0 | 213 | /* Compute binary polynomial multiply d = a * b */ |
michael@0 | 214 | static void |
michael@0 | 215 | s_bmul_d(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *d) |
michael@0 | 216 | { |
michael@0 | 217 | mp_digit a_i, a0b0, a1b1, carry = 0; |
michael@0 | 218 | while (a_len--) { |
michael@0 | 219 | a_i = *a++; |
michael@0 | 220 | s_bmul_1x1(&a1b1, &a0b0, a_i, b); |
michael@0 | 221 | *d++ = a0b0 ^ carry; |
michael@0 | 222 | carry = a1b1; |
michael@0 | 223 | } |
michael@0 | 224 | *d = carry; |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | /* Compute binary polynomial xor multiply accumulate d ^= a * b */ |
michael@0 | 228 | static void |
michael@0 | 229 | s_bmul_d_add(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *d) |
michael@0 | 230 | { |
michael@0 | 231 | mp_digit a_i, a0b0, a1b1, carry = 0; |
michael@0 | 232 | while (a_len--) { |
michael@0 | 233 | a_i = *a++; |
michael@0 | 234 | s_bmul_1x1(&a1b1, &a0b0, a_i, b); |
michael@0 | 235 | *d++ ^= a0b0 ^ carry; |
michael@0 | 236 | carry = a1b1; |
michael@0 | 237 | } |
michael@0 | 238 | *d ^= carry; |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | /* Compute binary polynomial xor multiply c = a * b. |
michael@0 | 242 | * All parameters may be identical. |
michael@0 | 243 | */ |
michael@0 | 244 | mp_err |
michael@0 | 245 | mp_bmul(const mp_int *a, const mp_int *b, mp_int *c) |
michael@0 | 246 | { |
michael@0 | 247 | mp_digit *pb, b_i; |
michael@0 | 248 | mp_int tmp; |
michael@0 | 249 | mp_size ib, a_used, b_used; |
michael@0 | 250 | mp_err res = MP_OKAY; |
michael@0 | 251 | |
michael@0 | 252 | MP_DIGITS(&tmp) = 0; |
michael@0 | 253 | |
michael@0 | 254 | ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG); |
michael@0 | 255 | |
michael@0 | 256 | if (a == c) { |
michael@0 | 257 | MP_CHECKOK( mp_init_copy(&tmp, a) ); |
michael@0 | 258 | if (a == b) |
michael@0 | 259 | b = &tmp; |
michael@0 | 260 | a = &tmp; |
michael@0 | 261 | } else if (b == c) { |
michael@0 | 262 | MP_CHECKOK( mp_init_copy(&tmp, b) ); |
michael@0 | 263 | b = &tmp; |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | if (MP_USED(a) < MP_USED(b)) { |
michael@0 | 267 | const mp_int *xch = b; /* switch a and b if b longer */ |
michael@0 | 268 | b = a; |
michael@0 | 269 | a = xch; |
michael@0 | 270 | } |
michael@0 | 271 | |
michael@0 | 272 | MP_USED(c) = 1; MP_DIGIT(c, 0) = 0; |
michael@0 | 273 | MP_CHECKOK( s_mp_pad(c, USED(a) + USED(b)) ); |
michael@0 | 274 | |
michael@0 | 275 | pb = MP_DIGITS(b); |
michael@0 | 276 | s_bmul_d(MP_DIGITS(a), MP_USED(a), *pb++, MP_DIGITS(c)); |
michael@0 | 277 | |
michael@0 | 278 | /* Outer loop: Digits of b */ |
michael@0 | 279 | a_used = MP_USED(a); |
michael@0 | 280 | b_used = MP_USED(b); |
michael@0 | 281 | MP_USED(c) = a_used + b_used; |
michael@0 | 282 | for (ib = 1; ib < b_used; ib++) { |
michael@0 | 283 | b_i = *pb++; |
michael@0 | 284 | |
michael@0 | 285 | /* Inner product: Digits of a */ |
michael@0 | 286 | if (b_i) |
michael@0 | 287 | s_bmul_d_add(MP_DIGITS(a), a_used, b_i, MP_DIGITS(c) + ib); |
michael@0 | 288 | else |
michael@0 | 289 | MP_DIGIT(c, ib + a_used) = b_i; |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | s_mp_clamp(c); |
michael@0 | 293 | |
michael@0 | 294 | SIGN(c) = ZPOS; |
michael@0 | 295 | |
michael@0 | 296 | CLEANUP: |
michael@0 | 297 | mp_clear(&tmp); |
michael@0 | 298 | return res; |
michael@0 | 299 | } |
michael@0 | 300 | |
michael@0 | 301 | |
michael@0 | 302 | /* Compute modular reduction of a and store result in r. |
michael@0 | 303 | * r could be a. |
michael@0 | 304 | * For modular arithmetic, the irreducible polynomial f(t) is represented |
michael@0 | 305 | * as an array of int[], where f(t) is of the form: |
michael@0 | 306 | * f(t) = t^p[0] + t^p[1] + ... + t^p[k] |
michael@0 | 307 | * where m = p[0] > p[1] > ... > p[k] = 0. |
michael@0 | 308 | */ |
michael@0 | 309 | mp_err |
michael@0 | 310 | mp_bmod(const mp_int *a, const unsigned int p[], mp_int *r) |
michael@0 | 311 | { |
michael@0 | 312 | int j, k; |
michael@0 | 313 | int n, dN, d0, d1; |
michael@0 | 314 | mp_digit zz, *z, tmp; |
michael@0 | 315 | mp_size used; |
michael@0 | 316 | mp_err res = MP_OKAY; |
michael@0 | 317 | |
michael@0 | 318 | /* The algorithm does the reduction in place in r, |
michael@0 | 319 | * if a != r, copy a into r first so reduction can be done in r |
michael@0 | 320 | */ |
michael@0 | 321 | if (a != r) { |
michael@0 | 322 | MP_CHECKOK( mp_copy(a, r) ); |
michael@0 | 323 | } |
michael@0 | 324 | z = MP_DIGITS(r); |
michael@0 | 325 | |
michael@0 | 326 | /* start reduction */ |
michael@0 | 327 | /*dN = p[0] / MP_DIGIT_BITS; */ |
michael@0 | 328 | dN = p[0] >> MP_DIGIT_BITS_LOG_2; |
michael@0 | 329 | used = MP_USED(r); |
michael@0 | 330 | |
michael@0 | 331 | for (j = used - 1; j > dN;) { |
michael@0 | 332 | |
michael@0 | 333 | zz = z[j]; |
michael@0 | 334 | if (zz == 0) { |
michael@0 | 335 | j--; continue; |
michael@0 | 336 | } |
michael@0 | 337 | z[j] = 0; |
michael@0 | 338 | |
michael@0 | 339 | for (k = 1; p[k] > 0; k++) { |
michael@0 | 340 | /* reducing component t^p[k] */ |
michael@0 | 341 | n = p[0] - p[k]; |
michael@0 | 342 | /*d0 = n % MP_DIGIT_BITS; */ |
michael@0 | 343 | d0 = n & MP_DIGIT_BITS_MASK; |
michael@0 | 344 | d1 = MP_DIGIT_BITS - d0; |
michael@0 | 345 | /*n /= MP_DIGIT_BITS; */ |
michael@0 | 346 | n >>= MP_DIGIT_BITS_LOG_2; |
michael@0 | 347 | z[j-n] ^= (zz>>d0); |
michael@0 | 348 | if (d0) |
michael@0 | 349 | z[j-n-1] ^= (zz<<d1); |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | /* reducing component t^0 */ |
michael@0 | 353 | n = dN; |
michael@0 | 354 | /*d0 = p[0] % MP_DIGIT_BITS;*/ |
michael@0 | 355 | d0 = p[0] & MP_DIGIT_BITS_MASK; |
michael@0 | 356 | d1 = MP_DIGIT_BITS - d0; |
michael@0 | 357 | z[j-n] ^= (zz >> d0); |
michael@0 | 358 | if (d0) |
michael@0 | 359 | z[j-n-1] ^= (zz << d1); |
michael@0 | 360 | |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | /* final round of reduction */ |
michael@0 | 364 | while (j == dN) { |
michael@0 | 365 | |
michael@0 | 366 | /* d0 = p[0] % MP_DIGIT_BITS; */ |
michael@0 | 367 | d0 = p[0] & MP_DIGIT_BITS_MASK; |
michael@0 | 368 | zz = z[dN] >> d0; |
michael@0 | 369 | if (zz == 0) break; |
michael@0 | 370 | d1 = MP_DIGIT_BITS - d0; |
michael@0 | 371 | |
michael@0 | 372 | /* clear up the top d1 bits */ |
michael@0 | 373 | if (d0) { |
michael@0 | 374 | z[dN] = (z[dN] << d1) >> d1; |
michael@0 | 375 | } else { |
michael@0 | 376 | z[dN] = 0; |
michael@0 | 377 | } |
michael@0 | 378 | *z ^= zz; /* reduction t^0 component */ |
michael@0 | 379 | |
michael@0 | 380 | for (k = 1; p[k] > 0; k++) { |
michael@0 | 381 | /* reducing component t^p[k]*/ |
michael@0 | 382 | /* n = p[k] / MP_DIGIT_BITS; */ |
michael@0 | 383 | n = p[k] >> MP_DIGIT_BITS_LOG_2; |
michael@0 | 384 | /* d0 = p[k] % MP_DIGIT_BITS; */ |
michael@0 | 385 | d0 = p[k] & MP_DIGIT_BITS_MASK; |
michael@0 | 386 | d1 = MP_DIGIT_BITS - d0; |
michael@0 | 387 | z[n] ^= (zz << d0); |
michael@0 | 388 | tmp = zz >> d1; |
michael@0 | 389 | if (d0 && tmp) |
michael@0 | 390 | z[n+1] ^= tmp; |
michael@0 | 391 | } |
michael@0 | 392 | } |
michael@0 | 393 | |
michael@0 | 394 | s_mp_clamp(r); |
michael@0 | 395 | CLEANUP: |
michael@0 | 396 | return res; |
michael@0 | 397 | } |
michael@0 | 398 | |
michael@0 | 399 | /* Compute the product of two polynomials a and b, reduce modulo p, |
michael@0 | 400 | * Store the result in r. r could be a or b; a could be b. |
michael@0 | 401 | */ |
michael@0 | 402 | mp_err |
michael@0 | 403 | mp_bmulmod(const mp_int *a, const mp_int *b, const unsigned int p[], mp_int *r) |
michael@0 | 404 | { |
michael@0 | 405 | mp_err res; |
michael@0 | 406 | |
michael@0 | 407 | if (a == b) return mp_bsqrmod(a, p, r); |
michael@0 | 408 | if ((res = mp_bmul(a, b, r) ) != MP_OKAY) |
michael@0 | 409 | return res; |
michael@0 | 410 | return mp_bmod(r, p, r); |
michael@0 | 411 | } |
michael@0 | 412 | |
michael@0 | 413 | /* Compute binary polynomial squaring c = a*a mod p . |
michael@0 | 414 | * Parameter r and a can be identical. |
michael@0 | 415 | */ |
michael@0 | 416 | |
michael@0 | 417 | mp_err |
michael@0 | 418 | mp_bsqrmod(const mp_int *a, const unsigned int p[], mp_int *r) |
michael@0 | 419 | { |
michael@0 | 420 | mp_digit *pa, *pr, a_i; |
michael@0 | 421 | mp_int tmp; |
michael@0 | 422 | mp_size ia, a_used; |
michael@0 | 423 | mp_err res; |
michael@0 | 424 | |
michael@0 | 425 | ARGCHK(a != NULL && r != NULL, MP_BADARG); |
michael@0 | 426 | MP_DIGITS(&tmp) = 0; |
michael@0 | 427 | |
michael@0 | 428 | if (a == r) { |
michael@0 | 429 | MP_CHECKOK( mp_init_copy(&tmp, a) ); |
michael@0 | 430 | a = &tmp; |
michael@0 | 431 | } |
michael@0 | 432 | |
michael@0 | 433 | MP_USED(r) = 1; MP_DIGIT(r, 0) = 0; |
michael@0 | 434 | MP_CHECKOK( s_mp_pad(r, 2*USED(a)) ); |
michael@0 | 435 | |
michael@0 | 436 | pa = MP_DIGITS(a); |
michael@0 | 437 | pr = MP_DIGITS(r); |
michael@0 | 438 | a_used = MP_USED(a); |
michael@0 | 439 | MP_USED(r) = 2 * a_used; |
michael@0 | 440 | |
michael@0 | 441 | for (ia = 0; ia < a_used; ia++) { |
michael@0 | 442 | a_i = *pa++; |
michael@0 | 443 | *pr++ = gf2m_SQR0(a_i); |
michael@0 | 444 | *pr++ = gf2m_SQR1(a_i); |
michael@0 | 445 | } |
michael@0 | 446 | |
michael@0 | 447 | MP_CHECKOK( mp_bmod(r, p, r) ); |
michael@0 | 448 | s_mp_clamp(r); |
michael@0 | 449 | SIGN(r) = ZPOS; |
michael@0 | 450 | |
michael@0 | 451 | CLEANUP: |
michael@0 | 452 | mp_clear(&tmp); |
michael@0 | 453 | return res; |
michael@0 | 454 | } |
michael@0 | 455 | |
michael@0 | 456 | /* Compute binary polynomial y/x mod p, y divided by x, reduce modulo p. |
michael@0 | 457 | * Store the result in r. r could be x or y, and x could equal y. |
michael@0 | 458 | * Uses algorithm Modular_Division_GF(2^m) from |
michael@0 | 459 | * Chang-Shantz, S. "From Euclid's GCD to Montgomery Multiplication to |
michael@0 | 460 | * the Great Divide". |
michael@0 | 461 | */ |
michael@0 | 462 | int |
michael@0 | 463 | mp_bdivmod(const mp_int *y, const mp_int *x, const mp_int *pp, |
michael@0 | 464 | const unsigned int p[], mp_int *r) |
michael@0 | 465 | { |
michael@0 | 466 | mp_int aa, bb, uu; |
michael@0 | 467 | mp_int *a, *b, *u, *v; |
michael@0 | 468 | mp_err res = MP_OKAY; |
michael@0 | 469 | |
michael@0 | 470 | MP_DIGITS(&aa) = 0; |
michael@0 | 471 | MP_DIGITS(&bb) = 0; |
michael@0 | 472 | MP_DIGITS(&uu) = 0; |
michael@0 | 473 | |
michael@0 | 474 | MP_CHECKOK( mp_init_copy(&aa, x) ); |
michael@0 | 475 | MP_CHECKOK( mp_init_copy(&uu, y) ); |
michael@0 | 476 | MP_CHECKOK( mp_init_copy(&bb, pp) ); |
michael@0 | 477 | MP_CHECKOK( s_mp_pad(r, USED(pp)) ); |
michael@0 | 478 | MP_USED(r) = 1; MP_DIGIT(r, 0) = 0; |
michael@0 | 479 | |
michael@0 | 480 | a = &aa; b= &bb; u=&uu; v=r; |
michael@0 | 481 | /* reduce x and y mod p */ |
michael@0 | 482 | MP_CHECKOK( mp_bmod(a, p, a) ); |
michael@0 | 483 | MP_CHECKOK( mp_bmod(u, p, u) ); |
michael@0 | 484 | |
michael@0 | 485 | while (!mp_isodd(a)) { |
michael@0 | 486 | s_mp_div2(a); |
michael@0 | 487 | if (mp_isodd(u)) { |
michael@0 | 488 | MP_CHECKOK( mp_badd(u, pp, u) ); |
michael@0 | 489 | } |
michael@0 | 490 | s_mp_div2(u); |
michael@0 | 491 | } |
michael@0 | 492 | |
michael@0 | 493 | do { |
michael@0 | 494 | if (mp_cmp_mag(b, a) > 0) { |
michael@0 | 495 | MP_CHECKOK( mp_badd(b, a, b) ); |
michael@0 | 496 | MP_CHECKOK( mp_badd(v, u, v) ); |
michael@0 | 497 | do { |
michael@0 | 498 | s_mp_div2(b); |
michael@0 | 499 | if (mp_isodd(v)) { |
michael@0 | 500 | MP_CHECKOK( mp_badd(v, pp, v) ); |
michael@0 | 501 | } |
michael@0 | 502 | s_mp_div2(v); |
michael@0 | 503 | } while (!mp_isodd(b)); |
michael@0 | 504 | } |
michael@0 | 505 | else if ((MP_DIGIT(a,0) == 1) && (MP_USED(a) == 1)) |
michael@0 | 506 | break; |
michael@0 | 507 | else { |
michael@0 | 508 | MP_CHECKOK( mp_badd(a, b, a) ); |
michael@0 | 509 | MP_CHECKOK( mp_badd(u, v, u) ); |
michael@0 | 510 | do { |
michael@0 | 511 | s_mp_div2(a); |
michael@0 | 512 | if (mp_isodd(u)) { |
michael@0 | 513 | MP_CHECKOK( mp_badd(u, pp, u) ); |
michael@0 | 514 | } |
michael@0 | 515 | s_mp_div2(u); |
michael@0 | 516 | } while (!mp_isodd(a)); |
michael@0 | 517 | } |
michael@0 | 518 | } while (1); |
michael@0 | 519 | |
michael@0 | 520 | MP_CHECKOK( mp_copy(u, r) ); |
michael@0 | 521 | |
michael@0 | 522 | CLEANUP: |
michael@0 | 523 | mp_clear(&aa); |
michael@0 | 524 | mp_clear(&bb); |
michael@0 | 525 | mp_clear(&uu); |
michael@0 | 526 | return res; |
michael@0 | 527 | |
michael@0 | 528 | } |
michael@0 | 529 | |
michael@0 | 530 | /* Convert the bit-string representation of a polynomial a into an array |
michael@0 | 531 | * of integers corresponding to the bits with non-zero coefficient. |
michael@0 | 532 | * Up to max elements of the array will be filled. Return value is total |
michael@0 | 533 | * number of coefficients that would be extracted if array was large enough. |
michael@0 | 534 | */ |
michael@0 | 535 | int |
michael@0 | 536 | mp_bpoly2arr(const mp_int *a, unsigned int p[], int max) |
michael@0 | 537 | { |
michael@0 | 538 | int i, j, k; |
michael@0 | 539 | mp_digit top_bit, mask; |
michael@0 | 540 | |
michael@0 | 541 | top_bit = 1; |
michael@0 | 542 | top_bit <<= MP_DIGIT_BIT - 1; |
michael@0 | 543 | |
michael@0 | 544 | for (k = 0; k < max; k++) p[k] = 0; |
michael@0 | 545 | k = 0; |
michael@0 | 546 | |
michael@0 | 547 | for (i = MP_USED(a) - 1; i >= 0; i--) { |
michael@0 | 548 | mask = top_bit; |
michael@0 | 549 | for (j = MP_DIGIT_BIT - 1; j >= 0; j--) { |
michael@0 | 550 | if (MP_DIGITS(a)[i] & mask) { |
michael@0 | 551 | if (k < max) p[k] = MP_DIGIT_BIT * i + j; |
michael@0 | 552 | k++; |
michael@0 | 553 | } |
michael@0 | 554 | mask >>= 1; |
michael@0 | 555 | } |
michael@0 | 556 | } |
michael@0 | 557 | |
michael@0 | 558 | return k; |
michael@0 | 559 | } |
michael@0 | 560 | |
michael@0 | 561 | /* Convert the coefficient array representation of a polynomial to a |
michael@0 | 562 | * bit-string. The array must be terminated by 0. |
michael@0 | 563 | */ |
michael@0 | 564 | mp_err |
michael@0 | 565 | mp_barr2poly(const unsigned int p[], mp_int *a) |
michael@0 | 566 | { |
michael@0 | 567 | |
michael@0 | 568 | mp_err res = MP_OKAY; |
michael@0 | 569 | int i; |
michael@0 | 570 | |
michael@0 | 571 | mp_zero(a); |
michael@0 | 572 | for (i = 0; p[i] > 0; i++) { |
michael@0 | 573 | MP_CHECKOK( mpl_set_bit(a, p[i], 1) ); |
michael@0 | 574 | } |
michael@0 | 575 | MP_CHECKOK( mpl_set_bit(a, 0, 1) ); |
michael@0 | 576 | |
michael@0 | 577 | CLEANUP: |
michael@0 | 578 | return res; |
michael@0 | 579 | } |