Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] */ |
| 56 | |
| 57 | #include <openssl/bn.h> |
| 58 | |
| 59 | #include <assert.h> |
| 60 | |
| 61 | #include "internal.h" |
| 62 | |
| 63 | |
Brian Smith | a051bdd | 2016-02-03 21:12:37 -1000 | [diff] [blame] | 64 | /* This file has two other implementations: x86 assembly language in |
| 65 | * asm/bn-586.pl and x86_64 inline assembly in asm/x86_64-gcc.c. */ |
David Benjamin | 3e700bb | 2014-10-29 17:13:38 -0400 | [diff] [blame] | 66 | #if defined(OPENSSL_NO_ASM) || \ |
Brian Smith | a051bdd | 2016-02-03 21:12:37 -1000 | [diff] [blame] | 67 | !(defined(OPENSSL_X86) || (defined(OPENSSL_X86_64) && defined(__GNUC__))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 68 | |
David Benjamin | 81edc9b | 2015-11-18 15:57:00 -0500 | [diff] [blame] | 69 | #ifdef BN_ULLONG |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 70 | #define mul_add(r, a, w, c) \ |
| 71 | { \ |
| 72 | BN_ULLONG t; \ |
| 73 | t = (BN_ULLONG)w * (a) + (r) + (c); \ |
| 74 | (r) = Lw(t); \ |
| 75 | (c) = Hw(t); \ |
| 76 | } |
| 77 | |
| 78 | #define mul(r, a, w, c) \ |
| 79 | { \ |
| 80 | BN_ULLONG t; \ |
| 81 | t = (BN_ULLONG)w * (a) + (c); \ |
| 82 | (r) = Lw(t); \ |
| 83 | (c) = Hw(t); \ |
| 84 | } |
| 85 | |
| 86 | #define sqr(r0, r1, a) \ |
| 87 | { \ |
| 88 | BN_ULLONG t; \ |
| 89 | t = (BN_ULLONG)(a) * (a); \ |
| 90 | (r0) = Lw(t); \ |
| 91 | (r1) = Hw(t); \ |
| 92 | } |
| 93 | |
Brian Smith | a051bdd | 2016-02-03 21:12:37 -1000 | [diff] [blame] | 94 | #else |
| 95 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 96 | #define mul_add(r, a, w, c) \ |
| 97 | { \ |
| 98 | BN_ULONG high, low, ret, tmp = (a); \ |
| 99 | ret = (r); \ |
| 100 | BN_UMULT_LOHI(low, high, w, tmp); \ |
| 101 | ret += (c); \ |
| 102 | (c) = (ret < (c)) ? 1 : 0; \ |
| 103 | (c) += high; \ |
| 104 | ret += low; \ |
| 105 | (c) += (ret < low) ? 1 : 0; \ |
| 106 | (r) = ret; \ |
| 107 | } |
| 108 | |
| 109 | #define mul(r, a, w, c) \ |
| 110 | { \ |
| 111 | BN_ULONG high, low, ret, ta = (a); \ |
| 112 | BN_UMULT_LOHI(low, high, w, ta); \ |
| 113 | ret = low + (c); \ |
| 114 | (c) = high; \ |
| 115 | (c) += (ret < low) ? 1 : 0; \ |
| 116 | (r) = ret; \ |
| 117 | } |
| 118 | |
| 119 | #define sqr(r0, r1, a) \ |
| 120 | { \ |
| 121 | BN_ULONG tmp = (a); \ |
| 122 | BN_UMULT_LOHI(r0, r1, tmp, tmp); \ |
| 123 | } |
| 124 | |
David Benjamin | 81edc9b | 2015-11-18 15:57:00 -0500 | [diff] [blame] | 125 | #endif /* !BN_ULLONG */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 126 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 127 | BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, |
| 128 | BN_ULONG w) { |
| 129 | BN_ULONG c1 = 0; |
| 130 | |
| 131 | assert(num >= 0); |
| 132 | if (num <= 0) { |
| 133 | return c1; |
| 134 | } |
| 135 | |
| 136 | while (num & ~3) { |
| 137 | mul_add(rp[0], ap[0], w, c1); |
| 138 | mul_add(rp[1], ap[1], w, c1); |
| 139 | mul_add(rp[2], ap[2], w, c1); |
| 140 | mul_add(rp[3], ap[3], w, c1); |
| 141 | ap += 4; |
| 142 | rp += 4; |
| 143 | num -= 4; |
| 144 | } |
| 145 | |
| 146 | while (num) { |
| 147 | mul_add(rp[0], ap[0], w, c1); |
| 148 | ap++; |
| 149 | rp++; |
| 150 | num--; |
| 151 | } |
| 152 | |
| 153 | return c1; |
| 154 | } |
| 155 | |
| 156 | BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w) { |
| 157 | BN_ULONG c1 = 0; |
| 158 | |
| 159 | assert(num >= 0); |
| 160 | if (num <= 0) { |
| 161 | return c1; |
| 162 | } |
| 163 | |
| 164 | while (num & ~3) { |
| 165 | mul(rp[0], ap[0], w, c1); |
| 166 | mul(rp[1], ap[1], w, c1); |
| 167 | mul(rp[2], ap[2], w, c1); |
| 168 | mul(rp[3], ap[3], w, c1); |
| 169 | ap += 4; |
| 170 | rp += 4; |
| 171 | num -= 4; |
| 172 | } |
| 173 | while (num) { |
| 174 | mul(rp[0], ap[0], w, c1); |
| 175 | ap++; |
| 176 | rp++; |
| 177 | num--; |
| 178 | } |
| 179 | return c1; |
| 180 | } |
| 181 | |
| 182 | void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n) { |
| 183 | assert(n >= 0); |
| 184 | if (n <= 0) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | while (n & ~3) { |
| 189 | sqr(r[0], r[1], a[0]); |
| 190 | sqr(r[2], r[3], a[1]); |
| 191 | sqr(r[4], r[5], a[2]); |
| 192 | sqr(r[6], r[7], a[3]); |
| 193 | a += 4; |
| 194 | r += 8; |
| 195 | n -= 4; |
| 196 | } |
| 197 | while (n) { |
| 198 | sqr(r[0], r[1], a[0]); |
| 199 | a++; |
| 200 | r += 2; |
| 201 | n--; |
| 202 | } |
| 203 | } |
| 204 | |
David Benjamin | 81edc9b | 2015-11-18 15:57:00 -0500 | [diff] [blame] | 205 | #ifdef BN_ULLONG |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 206 | BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, |
| 207 | int n) { |
| 208 | BN_ULLONG ll = 0; |
| 209 | |
| 210 | assert(n >= 0); |
| 211 | if (n <= 0) { |
| 212 | return (BN_ULONG)0; |
| 213 | } |
| 214 | |
| 215 | while (n & ~3) { |
| 216 | ll += (BN_ULLONG)a[0] + b[0]; |
| 217 | r[0] = (BN_ULONG)ll & BN_MASK2; |
| 218 | ll >>= BN_BITS2; |
| 219 | ll += (BN_ULLONG)a[1] + b[1]; |
| 220 | r[1] = (BN_ULONG)ll & BN_MASK2; |
| 221 | ll >>= BN_BITS2; |
| 222 | ll += (BN_ULLONG)a[2] + b[2]; |
| 223 | r[2] = (BN_ULONG)ll & BN_MASK2; |
| 224 | ll >>= BN_BITS2; |
| 225 | ll += (BN_ULLONG)a[3] + b[3]; |
| 226 | r[3] = (BN_ULONG)ll & BN_MASK2; |
| 227 | ll >>= BN_BITS2; |
| 228 | a += 4; |
| 229 | b += 4; |
| 230 | r += 4; |
| 231 | n -= 4; |
| 232 | } |
| 233 | while (n) { |
| 234 | ll += (BN_ULLONG)a[0] + b[0]; |
| 235 | r[0] = (BN_ULONG)ll & BN_MASK2; |
| 236 | ll >>= BN_BITS2; |
| 237 | a++; |
| 238 | b++; |
| 239 | r++; |
| 240 | n--; |
| 241 | } |
| 242 | return (BN_ULONG)ll; |
| 243 | } |
| 244 | |
David Benjamin | 81edc9b | 2015-11-18 15:57:00 -0500 | [diff] [blame] | 245 | #else /* !BN_ULLONG */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 246 | |
| 247 | BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, |
| 248 | int n) { |
| 249 | BN_ULONG c, l, t; |
| 250 | |
| 251 | assert(n >= 0); |
| 252 | if (n <= 0) { |
| 253 | return (BN_ULONG)0; |
| 254 | } |
| 255 | |
| 256 | c = 0; |
| 257 | while (n & ~3) { |
| 258 | t = a[0]; |
| 259 | t = (t + c) & BN_MASK2; |
| 260 | c = (t < c); |
| 261 | l = (t + b[0]) & BN_MASK2; |
| 262 | c += (l < t); |
| 263 | r[0] = l; |
| 264 | t = a[1]; |
| 265 | t = (t + c) & BN_MASK2; |
| 266 | c = (t < c); |
| 267 | l = (t + b[1]) & BN_MASK2; |
| 268 | c += (l < t); |
| 269 | r[1] = l; |
| 270 | t = a[2]; |
| 271 | t = (t + c) & BN_MASK2; |
| 272 | c = (t < c); |
| 273 | l = (t + b[2]) & BN_MASK2; |
| 274 | c += (l < t); |
| 275 | r[2] = l; |
| 276 | t = a[3]; |
| 277 | t = (t + c) & BN_MASK2; |
| 278 | c = (t < c); |
| 279 | l = (t + b[3]) & BN_MASK2; |
| 280 | c += (l < t); |
| 281 | r[3] = l; |
| 282 | a += 4; |
| 283 | b += 4; |
| 284 | r += 4; |
| 285 | n -= 4; |
| 286 | } |
| 287 | while (n) { |
| 288 | t = a[0]; |
| 289 | t = (t + c) & BN_MASK2; |
| 290 | c = (t < c); |
| 291 | l = (t + b[0]) & BN_MASK2; |
| 292 | c += (l < t); |
| 293 | r[0] = l; |
| 294 | a++; |
| 295 | b++; |
| 296 | r++; |
| 297 | n--; |
| 298 | } |
| 299 | return (BN_ULONG)c; |
| 300 | } |
| 301 | |
David Benjamin | 81edc9b | 2015-11-18 15:57:00 -0500 | [diff] [blame] | 302 | #endif /* !BN_ULLONG */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 303 | |
| 304 | BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, |
| 305 | int n) { |
| 306 | BN_ULONG t1, t2; |
| 307 | int c = 0; |
| 308 | |
| 309 | assert(n >= 0); |
| 310 | if (n <= 0) { |
| 311 | return (BN_ULONG)0; |
| 312 | } |
| 313 | |
| 314 | while (n & ~3) { |
| 315 | t1 = a[0]; |
| 316 | t2 = b[0]; |
| 317 | r[0] = (t1 - t2 - c) & BN_MASK2; |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 318 | if (t1 != t2) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 319 | c = (t1 < t2); |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 320 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 321 | t1 = a[1]; |
| 322 | t2 = b[1]; |
| 323 | r[1] = (t1 - t2 - c) & BN_MASK2; |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 324 | if (t1 != t2) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 325 | c = (t1 < t2); |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 326 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 327 | t1 = a[2]; |
| 328 | t2 = b[2]; |
| 329 | r[2] = (t1 - t2 - c) & BN_MASK2; |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 330 | if (t1 != t2) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 331 | c = (t1 < t2); |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 332 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 333 | t1 = a[3]; |
| 334 | t2 = b[3]; |
| 335 | r[3] = (t1 - t2 - c) & BN_MASK2; |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 336 | if (t1 != t2) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 337 | c = (t1 < t2); |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 338 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 339 | a += 4; |
| 340 | b += 4; |
| 341 | r += 4; |
| 342 | n -= 4; |
| 343 | } |
| 344 | while (n) { |
| 345 | t1 = a[0]; |
| 346 | t2 = b[0]; |
| 347 | r[0] = (t1 - t2 - c) & BN_MASK2; |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 348 | if (t1 != t2) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 349 | c = (t1 < t2); |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 350 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 351 | a++; |
| 352 | b++; |
| 353 | r++; |
| 354 | n--; |
| 355 | } |
| 356 | return c; |
| 357 | } |
| 358 | |
| 359 | /* mul_add_c(a,b,c0,c1,c2) -- c+=a*b for three word number c=(c2,c1,c0) */ |
| 360 | /* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */ |
| 361 | /* sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]^2 for three word number c=(c2,c1,c0) */ |
| 362 | /* sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number c=(c2,c1,c0) */ |
| 363 | |
David Benjamin | 81edc9b | 2015-11-18 15:57:00 -0500 | [diff] [blame] | 364 | #ifdef BN_ULLONG |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 365 | |
Adam Langley | a83cc80 | 2015-01-08 11:46:35 -0800 | [diff] [blame] | 366 | /* Keep in mind that additions to multiplication result can not overflow, |
| 367 | * because its high half cannot be all-ones. */ |
| 368 | #define mul_add_c(a, b, c0, c1, c2) \ |
| 369 | do { \ |
| 370 | BN_ULONG hi; \ |
| 371 | BN_ULLONG t = (BN_ULLONG)(a) * (b); \ |
| 372 | t += c0; /* no carry */ \ |
| 373 | c0 = (BN_ULONG)Lw(t); \ |
| 374 | hi = (BN_ULONG)Hw(t); \ |
| 375 | c1 = (c1 + hi) & BN_MASK2; \ |
| 376 | if (c1 < hi) \ |
| 377 | c2++; \ |
| 378 | } while (0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 379 | |
Adam Langley | a83cc80 | 2015-01-08 11:46:35 -0800 | [diff] [blame] | 380 | #define mul_add_c2(a, b, c0, c1, c2) \ |
| 381 | do { \ |
| 382 | BN_ULONG hi; \ |
| 383 | BN_ULLONG t = (BN_ULLONG)(a) * (b); \ |
| 384 | BN_ULLONG tt = t + c0; /* no carry */ \ |
| 385 | c0 = (BN_ULONG)Lw(tt); \ |
| 386 | hi = (BN_ULONG)Hw(tt); \ |
| 387 | c1 = (c1 + hi) & BN_MASK2; \ |
| 388 | if (c1 < hi) \ |
| 389 | c2++; \ |
| 390 | t += c0; /* no carry */ \ |
| 391 | c0 = (BN_ULONG)Lw(t); \ |
| 392 | hi = (BN_ULONG)Hw(t); \ |
| 393 | c1 = (c1 + hi) & BN_MASK2; \ |
| 394 | if (c1 < hi) \ |
| 395 | c2++; \ |
| 396 | } while (0) |
| 397 | |
| 398 | #define sqr_add_c(a, i, c0, c1, c2) \ |
| 399 | do { \ |
| 400 | BN_ULONG hi; \ |
| 401 | BN_ULLONG t = (BN_ULLONG)a[i] * a[i]; \ |
| 402 | t += c0; /* no carry */ \ |
| 403 | c0 = (BN_ULONG)Lw(t); \ |
| 404 | hi = (BN_ULONG)Hw(t); \ |
| 405 | c1 = (c1 + hi) & BN_MASK2; \ |
| 406 | if (c1 < hi) \ |
| 407 | c2++; \ |
| 408 | } while (0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 409 | |
| 410 | #define sqr_add_c2(a, i, j, c0, c1, c2) mul_add_c2((a)[i], (a)[j], c0, c1, c2) |
| 411 | |
Brian Smith | a051bdd | 2016-02-03 21:12:37 -1000 | [diff] [blame] | 412 | #else |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 413 | |
Adam Langley | a83cc80 | 2015-01-08 11:46:35 -0800 | [diff] [blame] | 414 | /* Keep in mind that additions to hi can not overflow, because the high word of |
| 415 | * a multiplication result cannot be all-ones. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 416 | #define mul_add_c(a, b, c0, c1, c2) \ |
Adam Langley | a83cc80 | 2015-01-08 11:46:35 -0800 | [diff] [blame] | 417 | do { \ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 418 | BN_ULONG ta = (a), tb = (b); \ |
Adam Langley | a83cc80 | 2015-01-08 11:46:35 -0800 | [diff] [blame] | 419 | BN_ULONG lo, hi; \ |
| 420 | BN_UMULT_LOHI(lo, hi, ta, tb); \ |
| 421 | c0 += lo; \ |
| 422 | hi += (c0 < lo) ? 1 : 0; \ |
| 423 | c1 += hi; \ |
| 424 | c2 += (c1 < hi) ? 1 : 0; \ |
| 425 | } while (0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 426 | |
| 427 | #define mul_add_c2(a, b, c0, c1, c2) \ |
Adam Langley | a83cc80 | 2015-01-08 11:46:35 -0800 | [diff] [blame] | 428 | do { \ |
| 429 | BN_ULONG ta = (a), tb = (b); \ |
| 430 | BN_ULONG lo, hi, tt; \ |
| 431 | BN_UMULT_LOHI(lo, hi, ta, tb); \ |
| 432 | c0 += lo; \ |
| 433 | tt = hi + ((c0 < lo) ? 1 : 0); \ |
| 434 | c1 += tt; \ |
| 435 | c2 += (c1 < tt) ? 1 : 0; \ |
| 436 | c0 += lo; \ |
| 437 | hi += (c0 < lo) ? 1 : 0; \ |
| 438 | c1 += hi; \ |
| 439 | c2 += (c1 < hi) ? 1 : 0; \ |
| 440 | } while (0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 441 | |
| 442 | #define sqr_add_c(a, i, c0, c1, c2) \ |
Adam Langley | a83cc80 | 2015-01-08 11:46:35 -0800 | [diff] [blame] | 443 | do { \ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 444 | BN_ULONG ta = (a)[i]; \ |
Adam Langley | a83cc80 | 2015-01-08 11:46:35 -0800 | [diff] [blame] | 445 | BN_ULONG lo, hi; \ |
| 446 | BN_UMULT_LOHI(lo, hi, ta, ta); \ |
| 447 | c0 += lo; \ |
| 448 | hi += (c0 < lo) ? 1 : 0; \ |
| 449 | c1 += hi; \ |
| 450 | c2 += (c1 < hi) ? 1 : 0; \ |
| 451 | } while (0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 452 | |
| 453 | #define sqr_add_c2(a, i, j, c0, c1, c2) mul_add_c2((a)[i], (a)[j], c0, c1, c2) |
| 454 | |
David Benjamin | 81edc9b | 2015-11-18 15:57:00 -0500 | [diff] [blame] | 455 | #endif /* !BN_ULLONG */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 456 | |
| 457 | void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 458 | BN_ULONG c1, c2, c3; |
| 459 | |
| 460 | c1 = 0; |
| 461 | c2 = 0; |
| 462 | c3 = 0; |
| 463 | mul_add_c(a[0], b[0], c1, c2, c3); |
| 464 | r[0] = c1; |
| 465 | c1 = 0; |
| 466 | mul_add_c(a[0], b[1], c2, c3, c1); |
| 467 | mul_add_c(a[1], b[0], c2, c3, c1); |
| 468 | r[1] = c2; |
| 469 | c2 = 0; |
| 470 | mul_add_c(a[2], b[0], c3, c1, c2); |
| 471 | mul_add_c(a[1], b[1], c3, c1, c2); |
| 472 | mul_add_c(a[0], b[2], c3, c1, c2); |
| 473 | r[2] = c3; |
| 474 | c3 = 0; |
| 475 | mul_add_c(a[0], b[3], c1, c2, c3); |
| 476 | mul_add_c(a[1], b[2], c1, c2, c3); |
| 477 | mul_add_c(a[2], b[1], c1, c2, c3); |
| 478 | mul_add_c(a[3], b[0], c1, c2, c3); |
| 479 | r[3] = c1; |
| 480 | c1 = 0; |
| 481 | mul_add_c(a[4], b[0], c2, c3, c1); |
| 482 | mul_add_c(a[3], b[1], c2, c3, c1); |
| 483 | mul_add_c(a[2], b[2], c2, c3, c1); |
| 484 | mul_add_c(a[1], b[3], c2, c3, c1); |
| 485 | mul_add_c(a[0], b[4], c2, c3, c1); |
| 486 | r[4] = c2; |
| 487 | c2 = 0; |
| 488 | mul_add_c(a[0], b[5], c3, c1, c2); |
| 489 | mul_add_c(a[1], b[4], c3, c1, c2); |
| 490 | mul_add_c(a[2], b[3], c3, c1, c2); |
| 491 | mul_add_c(a[3], b[2], c3, c1, c2); |
| 492 | mul_add_c(a[4], b[1], c3, c1, c2); |
| 493 | mul_add_c(a[5], b[0], c3, c1, c2); |
| 494 | r[5] = c3; |
| 495 | c3 = 0; |
| 496 | mul_add_c(a[6], b[0], c1, c2, c3); |
| 497 | mul_add_c(a[5], b[1], c1, c2, c3); |
| 498 | mul_add_c(a[4], b[2], c1, c2, c3); |
| 499 | mul_add_c(a[3], b[3], c1, c2, c3); |
| 500 | mul_add_c(a[2], b[4], c1, c2, c3); |
| 501 | mul_add_c(a[1], b[5], c1, c2, c3); |
| 502 | mul_add_c(a[0], b[6], c1, c2, c3); |
| 503 | r[6] = c1; |
| 504 | c1 = 0; |
| 505 | mul_add_c(a[0], b[7], c2, c3, c1); |
| 506 | mul_add_c(a[1], b[6], c2, c3, c1); |
| 507 | mul_add_c(a[2], b[5], c2, c3, c1); |
| 508 | mul_add_c(a[3], b[4], c2, c3, c1); |
| 509 | mul_add_c(a[4], b[3], c2, c3, c1); |
| 510 | mul_add_c(a[5], b[2], c2, c3, c1); |
| 511 | mul_add_c(a[6], b[1], c2, c3, c1); |
| 512 | mul_add_c(a[7], b[0], c2, c3, c1); |
| 513 | r[7] = c2; |
| 514 | c2 = 0; |
| 515 | mul_add_c(a[7], b[1], c3, c1, c2); |
| 516 | mul_add_c(a[6], b[2], c3, c1, c2); |
| 517 | mul_add_c(a[5], b[3], c3, c1, c2); |
| 518 | mul_add_c(a[4], b[4], c3, c1, c2); |
| 519 | mul_add_c(a[3], b[5], c3, c1, c2); |
| 520 | mul_add_c(a[2], b[6], c3, c1, c2); |
| 521 | mul_add_c(a[1], b[7], c3, c1, c2); |
| 522 | r[8] = c3; |
| 523 | c3 = 0; |
| 524 | mul_add_c(a[2], b[7], c1, c2, c3); |
| 525 | mul_add_c(a[3], b[6], c1, c2, c3); |
| 526 | mul_add_c(a[4], b[5], c1, c2, c3); |
| 527 | mul_add_c(a[5], b[4], c1, c2, c3); |
| 528 | mul_add_c(a[6], b[3], c1, c2, c3); |
| 529 | mul_add_c(a[7], b[2], c1, c2, c3); |
| 530 | r[9] = c1; |
| 531 | c1 = 0; |
| 532 | mul_add_c(a[7], b[3], c2, c3, c1); |
| 533 | mul_add_c(a[6], b[4], c2, c3, c1); |
| 534 | mul_add_c(a[5], b[5], c2, c3, c1); |
| 535 | mul_add_c(a[4], b[6], c2, c3, c1); |
| 536 | mul_add_c(a[3], b[7], c2, c3, c1); |
| 537 | r[10] = c2; |
| 538 | c2 = 0; |
| 539 | mul_add_c(a[4], b[7], c3, c1, c2); |
| 540 | mul_add_c(a[5], b[6], c3, c1, c2); |
| 541 | mul_add_c(a[6], b[5], c3, c1, c2); |
| 542 | mul_add_c(a[7], b[4], c3, c1, c2); |
| 543 | r[11] = c3; |
| 544 | c3 = 0; |
| 545 | mul_add_c(a[7], b[5], c1, c2, c3); |
| 546 | mul_add_c(a[6], b[6], c1, c2, c3); |
| 547 | mul_add_c(a[5], b[7], c1, c2, c3); |
| 548 | r[12] = c1; |
| 549 | c1 = 0; |
| 550 | mul_add_c(a[6], b[7], c2, c3, c1); |
| 551 | mul_add_c(a[7], b[6], c2, c3, c1); |
| 552 | r[13] = c2; |
| 553 | c2 = 0; |
| 554 | mul_add_c(a[7], b[7], c3, c1, c2); |
| 555 | r[14] = c3; |
| 556 | r[15] = c1; |
| 557 | } |
| 558 | |
| 559 | void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 560 | BN_ULONG c1, c2, c3; |
| 561 | |
| 562 | c1 = 0; |
| 563 | c2 = 0; |
| 564 | c3 = 0; |
| 565 | mul_add_c(a[0], b[0], c1, c2, c3); |
| 566 | r[0] = c1; |
| 567 | c1 = 0; |
| 568 | mul_add_c(a[0], b[1], c2, c3, c1); |
| 569 | mul_add_c(a[1], b[0], c2, c3, c1); |
| 570 | r[1] = c2; |
| 571 | c2 = 0; |
| 572 | mul_add_c(a[2], b[0], c3, c1, c2); |
| 573 | mul_add_c(a[1], b[1], c3, c1, c2); |
| 574 | mul_add_c(a[0], b[2], c3, c1, c2); |
| 575 | r[2] = c3; |
| 576 | c3 = 0; |
| 577 | mul_add_c(a[0], b[3], c1, c2, c3); |
| 578 | mul_add_c(a[1], b[2], c1, c2, c3); |
| 579 | mul_add_c(a[2], b[1], c1, c2, c3); |
| 580 | mul_add_c(a[3], b[0], c1, c2, c3); |
| 581 | r[3] = c1; |
| 582 | c1 = 0; |
| 583 | mul_add_c(a[3], b[1], c2, c3, c1); |
| 584 | mul_add_c(a[2], b[2], c2, c3, c1); |
| 585 | mul_add_c(a[1], b[3], c2, c3, c1); |
| 586 | r[4] = c2; |
| 587 | c2 = 0; |
| 588 | mul_add_c(a[2], b[3], c3, c1, c2); |
| 589 | mul_add_c(a[3], b[2], c3, c1, c2); |
| 590 | r[5] = c3; |
| 591 | c3 = 0; |
| 592 | mul_add_c(a[3], b[3], c1, c2, c3); |
| 593 | r[6] = c1; |
| 594 | r[7] = c2; |
| 595 | } |
| 596 | |
| 597 | void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 598 | BN_ULONG c1, c2, c3; |
| 599 | |
| 600 | c1 = 0; |
| 601 | c2 = 0; |
| 602 | c3 = 0; |
| 603 | sqr_add_c(a, 0, c1, c2, c3); |
| 604 | r[0] = c1; |
| 605 | c1 = 0; |
| 606 | sqr_add_c2(a, 1, 0, c2, c3, c1); |
| 607 | r[1] = c2; |
| 608 | c2 = 0; |
| 609 | sqr_add_c(a, 1, c3, c1, c2); |
| 610 | sqr_add_c2(a, 2, 0, c3, c1, c2); |
| 611 | r[2] = c3; |
| 612 | c3 = 0; |
| 613 | sqr_add_c2(a, 3, 0, c1, c2, c3); |
| 614 | sqr_add_c2(a, 2, 1, c1, c2, c3); |
| 615 | r[3] = c1; |
| 616 | c1 = 0; |
| 617 | sqr_add_c(a, 2, c2, c3, c1); |
| 618 | sqr_add_c2(a, 3, 1, c2, c3, c1); |
| 619 | sqr_add_c2(a, 4, 0, c2, c3, c1); |
| 620 | r[4] = c2; |
| 621 | c2 = 0; |
| 622 | sqr_add_c2(a, 5, 0, c3, c1, c2); |
| 623 | sqr_add_c2(a, 4, 1, c3, c1, c2); |
| 624 | sqr_add_c2(a, 3, 2, c3, c1, c2); |
| 625 | r[5] = c3; |
| 626 | c3 = 0; |
| 627 | sqr_add_c(a, 3, c1, c2, c3); |
| 628 | sqr_add_c2(a, 4, 2, c1, c2, c3); |
| 629 | sqr_add_c2(a, 5, 1, c1, c2, c3); |
| 630 | sqr_add_c2(a, 6, 0, c1, c2, c3); |
| 631 | r[6] = c1; |
| 632 | c1 = 0; |
| 633 | sqr_add_c2(a, 7, 0, c2, c3, c1); |
| 634 | sqr_add_c2(a, 6, 1, c2, c3, c1); |
| 635 | sqr_add_c2(a, 5, 2, c2, c3, c1); |
| 636 | sqr_add_c2(a, 4, 3, c2, c3, c1); |
| 637 | r[7] = c2; |
| 638 | c2 = 0; |
| 639 | sqr_add_c(a, 4, c3, c1, c2); |
| 640 | sqr_add_c2(a, 5, 3, c3, c1, c2); |
| 641 | sqr_add_c2(a, 6, 2, c3, c1, c2); |
| 642 | sqr_add_c2(a, 7, 1, c3, c1, c2); |
| 643 | r[8] = c3; |
| 644 | c3 = 0; |
| 645 | sqr_add_c2(a, 7, 2, c1, c2, c3); |
| 646 | sqr_add_c2(a, 6, 3, c1, c2, c3); |
| 647 | sqr_add_c2(a, 5, 4, c1, c2, c3); |
| 648 | r[9] = c1; |
| 649 | c1 = 0; |
| 650 | sqr_add_c(a, 5, c2, c3, c1); |
| 651 | sqr_add_c2(a, 6, 4, c2, c3, c1); |
| 652 | sqr_add_c2(a, 7, 3, c2, c3, c1); |
| 653 | r[10] = c2; |
| 654 | c2 = 0; |
| 655 | sqr_add_c2(a, 7, 4, c3, c1, c2); |
| 656 | sqr_add_c2(a, 6, 5, c3, c1, c2); |
| 657 | r[11] = c3; |
| 658 | c3 = 0; |
| 659 | sqr_add_c(a, 6, c1, c2, c3); |
| 660 | sqr_add_c2(a, 7, 5, c1, c2, c3); |
| 661 | r[12] = c1; |
| 662 | c1 = 0; |
| 663 | sqr_add_c2(a, 7, 6, c2, c3, c1); |
| 664 | r[13] = c2; |
| 665 | c2 = 0; |
| 666 | sqr_add_c(a, 7, c3, c1, c2); |
| 667 | r[14] = c3; |
| 668 | r[15] = c1; |
| 669 | } |
| 670 | |
| 671 | void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 672 | BN_ULONG c1, c2, c3; |
| 673 | |
| 674 | c1 = 0; |
| 675 | c2 = 0; |
| 676 | c3 = 0; |
| 677 | sqr_add_c(a, 0, c1, c2, c3); |
| 678 | r[0] = c1; |
| 679 | c1 = 0; |
| 680 | sqr_add_c2(a, 1, 0, c2, c3, c1); |
| 681 | r[1] = c2; |
| 682 | c2 = 0; |
| 683 | sqr_add_c(a, 1, c3, c1, c2); |
| 684 | sqr_add_c2(a, 2, 0, c3, c1, c2); |
| 685 | r[2] = c3; |
| 686 | c3 = 0; |
| 687 | sqr_add_c2(a, 3, 0, c1, c2, c3); |
| 688 | sqr_add_c2(a, 2, 1, c1, c2, c3); |
| 689 | r[3] = c1; |
| 690 | c1 = 0; |
| 691 | sqr_add_c(a, 2, c2, c3, c1); |
| 692 | sqr_add_c2(a, 3, 1, c2, c3, c1); |
| 693 | r[4] = c2; |
| 694 | c2 = 0; |
| 695 | sqr_add_c2(a, 3, 2, c3, c1, c2); |
| 696 | r[5] = c3; |
| 697 | c3 = 0; |
| 698 | sqr_add_c(a, 3, c1, c2, c3); |
| 699 | r[6] = c1; |
| 700 | r[7] = c2; |
| 701 | } |
| 702 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 703 | #endif |