diff options
Diffstat (limited to 'src/lib/libcrypto/bn/bn_mul.c')
| -rw-r--r-- | src/lib/libcrypto/bn/bn_mul.c | 1306 |
1 files changed, 654 insertions, 652 deletions
diff --git a/src/lib/libcrypto/bn/bn_mul.c b/src/lib/libcrypto/bn/bn_mul.c index 12e5be80eb..4951153657 100644 --- a/src/lib/libcrypto/bn/bn_mul.c +++ b/src/lib/libcrypto/bn/bn_mul.c | |||
| @@ -5,21 +5,21 @@ | |||
| 5 | * This package is an SSL implementation written | 5 | * This package is an SSL implementation written |
| 6 | * by Eric Young (eay@cryptsoft.com). | 6 | * by Eric Young (eay@cryptsoft.com). |
| 7 | * The implementation was written so as to conform with Netscapes SSL. | 7 | * The implementation was written so as to conform with Netscapes SSL. |
| 8 | * | 8 | * |
| 9 | * This library is free for commercial and non-commercial use as long as | 9 | * This library is free for commercial and non-commercial use as long as |
| 10 | * the following conditions are aheared to. The following conditions | 10 | * the following conditions are aheared to. The following conditions |
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 | * included with this distribution is covered by the same copyright terms | 13 | * included with this distribution is covered by the same copyright terms |
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 15 | * | 15 | * |
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 | * the code are not to be removed. | 17 | * the code are not to be removed. |
| 18 | * If this package is used in a product, Eric Young should be given attribution | 18 | * If this package is used in a product, Eric Young should be given attribution |
| 19 | * as the author of the parts of the library used. | 19 | * as the author of the parts of the library used. |
| 20 | * This can be in the form of a textual message at program startup or | 20 | * This can be in the form of a textual message at program startup or |
| 21 | * in documentation (online or textual) provided with the package. | 21 | * in documentation (online or textual) provided with the package. |
| 22 | * | 22 | * |
| 23 | * Redistribution and use in source and binary forms, with or without | 23 | * Redistribution and use in source and binary forms, with or without |
| 24 | * modification, are permitted provided that the following conditions | 24 | * modification, are permitted provided that the following conditions |
| 25 | * are met: | 25 | * are met: |
| @@ -34,10 +34,10 @@ | |||
| 34 | * Eric Young (eay@cryptsoft.com)" | 34 | * Eric Young (eay@cryptsoft.com)" |
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | 35 | * The word 'cryptographic' can be left out if the rouines from the library |
| 36 | * being used are not cryptographic related :-). | 36 | * being used are not cryptographic related :-). |
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 38 | * the apps directory (application code) you must include an acknowledgement: | 38 | * the apps directory (application code) you must include an acknowledgement: |
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 40 | * | 40 | * |
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| @@ -49,7 +49,7 @@ | |||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 | * SUCH DAMAGE. | 51 | * SUCH DAMAGE. |
| 52 | * | 52 | * |
| 53 | * The licence and distribution terms for any publically available version or | 53 | * The licence and distribution terms for any publically available version or |
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
| @@ -77,10 +77,10 @@ | |||
| 77 | These functions should probably end up in bn_asm.c as soon as there are | 77 | These functions should probably end up in bn_asm.c as soon as there are |
| 78 | assembler counterparts for the systems that use assembler files. */ | 78 | assembler counterparts for the systems that use assembler files. */ |
| 79 | 79 | ||
| 80 | BN_ULONG bn_sub_part_words(BN_ULONG *r, | 80 | BN_ULONG |
| 81 | const BN_ULONG *a, const BN_ULONG *b, | 81 | bn_sub_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int cl, |
| 82 | int cl, int dl) | 82 | int dl) |
| 83 | { | 83 | { |
| 84 | BN_ULONG c, t; | 84 | BN_ULONG c, t; |
| 85 | 85 | ||
| 86 | assert(cl >= 0); | 86 | assert(cl >= 0); |
| @@ -93,121 +93,142 @@ BN_ULONG bn_sub_part_words(BN_ULONG *r, | |||
| 93 | a += cl; | 93 | a += cl; |
| 94 | b += cl; | 94 | b += cl; |
| 95 | 95 | ||
| 96 | if (dl < 0) | 96 | if (dl < 0) { |
| 97 | { | ||
| 98 | #ifdef BN_COUNT | 97 | #ifdef BN_COUNT |
| 99 | fprintf(stderr, " bn_sub_part_words %d + %d (dl < 0, c = %d)\n", cl, dl, c); | 98 | fprintf(stderr, |
| 99 | " bn_sub_part_words %d + %d (dl < 0, c = %d)\n", | ||
| 100 | cl, dl, c); | ||
| 100 | #endif | 101 | #endif |
| 101 | for (;;) | 102 | for (;;) { |
| 102 | { | ||
| 103 | t = b[0]; | 103 | t = b[0]; |
| 104 | r[0] = (0-t-c)&BN_MASK2; | 104 | r[0] = (0 - t - c) & BN_MASK2; |
| 105 | if (t != 0) c=1; | 105 | if (t != 0) |
| 106 | if (++dl >= 0) break; | 106 | c = 1; |
| 107 | if (++dl >= 0) | ||
| 108 | break; | ||
| 107 | 109 | ||
| 108 | t = b[1]; | 110 | t = b[1]; |
| 109 | r[1] = (0-t-c)&BN_MASK2; | 111 | r[1] = (0 - t - c) & BN_MASK2; |
| 110 | if (t != 0) c=1; | 112 | if (t != 0) |
| 111 | if (++dl >= 0) break; | 113 | c = 1; |
| 114 | if (++dl >= 0) | ||
| 115 | break; | ||
| 112 | 116 | ||
| 113 | t = b[2]; | 117 | t = b[2]; |
| 114 | r[2] = (0-t-c)&BN_MASK2; | 118 | r[2] = (0 - t - c) & BN_MASK2; |
| 115 | if (t != 0) c=1; | 119 | if (t != 0) |
| 116 | if (++dl >= 0) break; | 120 | c = 1; |
| 121 | if (++dl >= 0) | ||
| 122 | break; | ||
| 117 | 123 | ||
| 118 | t = b[3]; | 124 | t = b[3]; |
| 119 | r[3] = (0-t-c)&BN_MASK2; | 125 | r[3] = (0 - t - c) & BN_MASK2; |
| 120 | if (t != 0) c=1; | 126 | if (t != 0) |
| 121 | if (++dl >= 0) break; | 127 | c = 1; |
| 128 | if (++dl >= 0) | ||
| 129 | break; | ||
| 122 | 130 | ||
| 123 | b += 4; | 131 | b += 4; |
| 124 | r += 4; | 132 | r += 4; |
| 125 | } | ||
| 126 | } | 133 | } |
| 127 | else | 134 | } else { |
| 128 | { | ||
| 129 | int save_dl = dl; | 135 | int save_dl = dl; |
| 130 | #ifdef BN_COUNT | 136 | #ifdef BN_COUNT |
| 131 | fprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, c = %d)\n", cl, dl, c); | 137 | fprintf(stderr, |
| 138 | " bn_sub_part_words %d + %d (dl > 0, c = %d)\n", | ||
| 139 | cl, dl, c); | ||
| 132 | #endif | 140 | #endif |
| 133 | while(c) | 141 | while (c) { |
| 134 | { | ||
| 135 | t = a[0]; | 142 | t = a[0]; |
| 136 | r[0] = (t-c)&BN_MASK2; | 143 | r[0] = (t - c) & BN_MASK2; |
| 137 | if (t != 0) c=0; | 144 | if (t != 0) |
| 138 | if (--dl <= 0) break; | 145 | c = 0; |
| 146 | if (--dl <= 0) | ||
| 147 | break; | ||
| 139 | 148 | ||
| 140 | t = a[1]; | 149 | t = a[1]; |
| 141 | r[1] = (t-c)&BN_MASK2; | 150 | r[1] = (t - c) & BN_MASK2; |
| 142 | if (t != 0) c=0; | 151 | if (t != 0) |
| 143 | if (--dl <= 0) break; | 152 | c = 0; |
| 153 | if (--dl <= 0) | ||
| 154 | break; | ||
| 144 | 155 | ||
| 145 | t = a[2]; | 156 | t = a[2]; |
| 146 | r[2] = (t-c)&BN_MASK2; | 157 | r[2] = (t - c) & BN_MASK2; |
| 147 | if (t != 0) c=0; | 158 | if (t != 0) |
| 148 | if (--dl <= 0) break; | 159 | c = 0; |
| 160 | if (--dl <= 0) | ||
| 161 | break; | ||
| 149 | 162 | ||
| 150 | t = a[3]; | 163 | t = a[3]; |
| 151 | r[3] = (t-c)&BN_MASK2; | 164 | r[3] = (t - c) & BN_MASK2; |
| 152 | if (t != 0) c=0; | 165 | if (t != 0) |
| 153 | if (--dl <= 0) break; | 166 | c = 0; |
| 167 | if (--dl <= 0) | ||
| 168 | break; | ||
| 154 | 169 | ||
| 155 | save_dl = dl; | 170 | save_dl = dl; |
| 156 | a += 4; | 171 | a += 4; |
| 157 | r += 4; | 172 | r += 4; |
| 158 | } | 173 | } |
| 159 | if (dl > 0) | 174 | if (dl > 0) { |
| 160 | { | ||
| 161 | #ifdef BN_COUNT | 175 | #ifdef BN_COUNT |
| 162 | fprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, c == 0)\n", cl, dl); | 176 | fprintf(stderr, |
| 177 | " bn_sub_part_words %d + %d (dl > 0, c == 0)\n", | ||
| 178 | cl, dl); | ||
| 163 | #endif | 179 | #endif |
| 164 | if (save_dl > dl) | 180 | if (save_dl > dl) { |
| 165 | { | 181 | switch (save_dl - dl) { |
| 166 | switch (save_dl - dl) | ||
| 167 | { | ||
| 168 | case 1: | 182 | case 1: |
| 169 | r[1] = a[1]; | 183 | r[1] = a[1]; |
| 170 | if (--dl <= 0) break; | 184 | if (--dl <= 0) |
| 185 | break; | ||
| 171 | case 2: | 186 | case 2: |
| 172 | r[2] = a[2]; | 187 | r[2] = a[2]; |
| 173 | if (--dl <= 0) break; | 188 | if (--dl <= 0) |
| 189 | break; | ||
| 174 | case 3: | 190 | case 3: |
| 175 | r[3] = a[3]; | 191 | r[3] = a[3]; |
| 176 | if (--dl <= 0) break; | 192 | if (--dl <= 0) |
| 177 | } | 193 | break; |
| 194 | } | ||
| 178 | a += 4; | 195 | a += 4; |
| 179 | r += 4; | 196 | r += 4; |
| 180 | } | ||
| 181 | } | 197 | } |
| 182 | if (dl > 0) | 198 | } |
| 183 | { | 199 | if (dl > 0) { |
| 184 | #ifdef BN_COUNT | 200 | #ifdef BN_COUNT |
| 185 | fprintf(stderr, " bn_sub_part_words %d + %d (dl > 0, copy)\n", cl, dl); | 201 | fprintf(stderr, |
| 202 | " bn_sub_part_words %d + %d (dl > 0, copy)\n", | ||
| 203 | cl, dl); | ||
| 186 | #endif | 204 | #endif |
| 187 | for(;;) | 205 | for (;;) { |
| 188 | { | ||
| 189 | r[0] = a[0]; | 206 | r[0] = a[0]; |
| 190 | if (--dl <= 0) break; | 207 | if (--dl <= 0) |
| 208 | break; | ||
| 191 | r[1] = a[1]; | 209 | r[1] = a[1]; |
| 192 | if (--dl <= 0) break; | 210 | if (--dl <= 0) |
| 211 | break; | ||
| 193 | r[2] = a[2]; | 212 | r[2] = a[2]; |
| 194 | if (--dl <= 0) break; | 213 | if (--dl <= 0) |
| 214 | break; | ||
| 195 | r[3] = a[3]; | 215 | r[3] = a[3]; |
| 196 | if (--dl <= 0) break; | 216 | if (--dl <= 0) |
| 217 | break; | ||
| 197 | 218 | ||
| 198 | a += 4; | 219 | a += 4; |
| 199 | r += 4; | 220 | r += 4; |
| 200 | } | ||
| 201 | } | 221 | } |
| 202 | } | 222 | } |
| 203 | return c; | ||
| 204 | } | 223 | } |
| 224 | return c; | ||
| 225 | } | ||
| 205 | #endif | 226 | #endif |
| 206 | 227 | ||
| 207 | BN_ULONG bn_add_part_words(BN_ULONG *r, | 228 | BN_ULONG |
| 208 | const BN_ULONG *a, const BN_ULONG *b, | 229 | bn_add_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int cl, |
| 209 | int cl, int dl) | 230 | int dl) |
| 210 | { | 231 | { |
| 211 | BN_ULONG c, l, t; | 232 | BN_ULONG c, l, t; |
| 212 | 233 | ||
| 213 | assert(cl >= 0); | 234 | assert(cl >= 0); |
| @@ -220,160 +241,177 @@ BN_ULONG bn_add_part_words(BN_ULONG *r, | |||
| 220 | a += cl; | 241 | a += cl; |
| 221 | b += cl; | 242 | b += cl; |
| 222 | 243 | ||
| 223 | if (dl < 0) | 244 | if (dl < 0) { |
| 224 | { | ||
| 225 | int save_dl = dl; | 245 | int save_dl = dl; |
| 226 | #ifdef BN_COUNT | 246 | #ifdef BN_COUNT |
| 227 | fprintf(stderr, " bn_add_part_words %d + %d (dl < 0, c = %d)\n", cl, dl, c); | 247 | fprintf(stderr, |
| 248 | " bn_add_part_words %d + %d (dl < 0, c = %d)\n", | ||
| 249 | cl, dl, c); | ||
| 228 | #endif | 250 | #endif |
| 229 | while (c) | 251 | while (c) { |
| 230 | { | 252 | l = (c + b[0]) & BN_MASK2; |
| 231 | l=(c+b[0])&BN_MASK2; | 253 | c = (l < c); |
| 232 | c=(l < c); | 254 | r[0] = l; |
| 233 | r[0]=l; | 255 | if (++dl >= 0) |
| 234 | if (++dl >= 0) break; | 256 | break; |
| 235 | 257 | ||
| 236 | l=(c+b[1])&BN_MASK2; | 258 | l = (c + b[1]) & BN_MASK2; |
| 237 | c=(l < c); | 259 | c = (l < c); |
| 238 | r[1]=l; | 260 | r[1] = l; |
| 239 | if (++dl >= 0) break; | 261 | if (++dl >= 0) |
| 240 | 262 | break; | |
| 241 | l=(c+b[2])&BN_MASK2; | 263 | |
| 242 | c=(l < c); | 264 | l = (c + b[2]) & BN_MASK2; |
| 243 | r[2]=l; | 265 | c = (l < c); |
| 244 | if (++dl >= 0) break; | 266 | r[2] = l; |
| 245 | 267 | if (++dl >= 0) | |
| 246 | l=(c+b[3])&BN_MASK2; | 268 | break; |
| 247 | c=(l < c); | 269 | |
| 248 | r[3]=l; | 270 | l = (c + b[3]) & BN_MASK2; |
| 249 | if (++dl >= 0) break; | 271 | c = (l < c); |
| 272 | r[3] = l; | ||
| 273 | if (++dl >= 0) | ||
| 274 | break; | ||
| 250 | 275 | ||
| 251 | save_dl = dl; | 276 | save_dl = dl; |
| 252 | b+=4; | 277 | b += 4; |
| 253 | r+=4; | 278 | r += 4; |
| 254 | } | 279 | } |
| 255 | if (dl < 0) | 280 | if (dl < 0) { |
| 256 | { | ||
| 257 | #ifdef BN_COUNT | 281 | #ifdef BN_COUNT |
| 258 | fprintf(stderr, " bn_add_part_words %d + %d (dl < 0, c == 0)\n", cl, dl); | 282 | fprintf(stderr, |
| 283 | " bn_add_part_words %d + %d (dl < 0, c == 0)\n", | ||
| 284 | cl, dl); | ||
| 259 | #endif | 285 | #endif |
| 260 | if (save_dl < dl) | 286 | if (save_dl < dl) { |
| 261 | { | 287 | switch (dl - save_dl) { |
| 262 | switch (dl - save_dl) | ||
| 263 | { | ||
| 264 | case 1: | 288 | case 1: |
| 265 | r[1] = b[1]; | 289 | r[1] = b[1]; |
| 266 | if (++dl >= 0) break; | 290 | if (++dl >= 0) |
| 291 | break; | ||
| 267 | case 2: | 292 | case 2: |
| 268 | r[2] = b[2]; | 293 | r[2] = b[2]; |
| 269 | if (++dl >= 0) break; | 294 | if (++dl >= 0) |
| 295 | break; | ||
| 270 | case 3: | 296 | case 3: |
| 271 | r[3] = b[3]; | 297 | r[3] = b[3]; |
| 272 | if (++dl >= 0) break; | 298 | if (++dl >= 0) |
| 273 | } | 299 | break; |
| 300 | } | ||
| 274 | b += 4; | 301 | b += 4; |
| 275 | r += 4; | 302 | r += 4; |
| 276 | } | ||
| 277 | } | 303 | } |
| 278 | if (dl < 0) | 304 | } |
| 279 | { | 305 | if (dl < 0) { |
| 280 | #ifdef BN_COUNT | 306 | #ifdef BN_COUNT |
| 281 | fprintf(stderr, " bn_add_part_words %d + %d (dl < 0, copy)\n", cl, dl); | 307 | fprintf(stderr, |
| 308 | " bn_add_part_words %d + %d (dl < 0, copy)\n", | ||
| 309 | cl, dl); | ||
| 282 | #endif | 310 | #endif |
| 283 | for(;;) | 311 | for (;;) { |
| 284 | { | ||
| 285 | r[0] = b[0]; | 312 | r[0] = b[0]; |
| 286 | if (++dl >= 0) break; | 313 | if (++dl >= 0) |
| 314 | break; | ||
| 287 | r[1] = b[1]; | 315 | r[1] = b[1]; |
| 288 | if (++dl >= 0) break; | 316 | if (++dl >= 0) |
| 317 | break; | ||
| 289 | r[2] = b[2]; | 318 | r[2] = b[2]; |
| 290 | if (++dl >= 0) break; | 319 | if (++dl >= 0) |
| 320 | break; | ||
| 291 | r[3] = b[3]; | 321 | r[3] = b[3]; |
| 292 | if (++dl >= 0) break; | 322 | if (++dl >= 0) |
| 323 | break; | ||
| 293 | 324 | ||
| 294 | b += 4; | 325 | b += 4; |
| 295 | r += 4; | 326 | r += 4; |
| 296 | } | ||
| 297 | } | 327 | } |
| 298 | } | 328 | } |
| 299 | else | 329 | } else { |
| 300 | { | ||
| 301 | int save_dl = dl; | 330 | int save_dl = dl; |
| 302 | #ifdef BN_COUNT | 331 | #ifdef BN_COUNT |
| 303 | fprintf(stderr, " bn_add_part_words %d + %d (dl > 0)\n", cl, dl); | 332 | fprintf(stderr, |
| 333 | " bn_add_part_words %d + %d (dl > 0)\n", cl, dl); | ||
| 304 | #endif | 334 | #endif |
| 305 | while (c) | 335 | while (c) { |
| 306 | { | 336 | t = (a[0] + c) & BN_MASK2; |
| 307 | t=(a[0]+c)&BN_MASK2; | 337 | c = (t < c); |
| 308 | c=(t < c); | 338 | r[0] = t; |
| 309 | r[0]=t; | 339 | if (--dl <= 0) |
| 310 | if (--dl <= 0) break; | 340 | break; |
| 311 | 341 | ||
| 312 | t=(a[1]+c)&BN_MASK2; | 342 | t = (a[1] + c) & BN_MASK2; |
| 313 | c=(t < c); | 343 | c = (t < c); |
| 314 | r[1]=t; | 344 | r[1] = t; |
| 315 | if (--dl <= 0) break; | 345 | if (--dl <= 0) |
| 316 | 346 | break; | |
| 317 | t=(a[2]+c)&BN_MASK2; | 347 | |
| 318 | c=(t < c); | 348 | t = (a[2] + c) & BN_MASK2; |
| 319 | r[2]=t; | 349 | c = (t < c); |
| 320 | if (--dl <= 0) break; | 350 | r[2] = t; |
| 321 | 351 | if (--dl <= 0) | |
| 322 | t=(a[3]+c)&BN_MASK2; | 352 | break; |
| 323 | c=(t < c); | 353 | |
| 324 | r[3]=t; | 354 | t = (a[3] + c) & BN_MASK2; |
| 325 | if (--dl <= 0) break; | 355 | c = (t < c); |
| 356 | r[3] = t; | ||
| 357 | if (--dl <= 0) | ||
| 358 | break; | ||
| 326 | 359 | ||
| 327 | save_dl = dl; | 360 | save_dl = dl; |
| 328 | a+=4; | 361 | a += 4; |
| 329 | r+=4; | 362 | r += 4; |
| 330 | } | 363 | } |
| 331 | #ifdef BN_COUNT | 364 | #ifdef BN_COUNT |
| 332 | fprintf(stderr, " bn_add_part_words %d + %d (dl > 0, c == 0)\n", cl, dl); | 365 | fprintf(stderr, |
| 366 | " bn_add_part_words %d + %d (dl > 0, c == 0)\n", cl, dl); | ||
| 333 | #endif | 367 | #endif |
| 334 | if (dl > 0) | 368 | if (dl > 0) { |
| 335 | { | 369 | if (save_dl > dl) { |
| 336 | if (save_dl > dl) | 370 | switch (save_dl - dl) { |
| 337 | { | ||
| 338 | switch (save_dl - dl) | ||
| 339 | { | ||
| 340 | case 1: | 371 | case 1: |
| 341 | r[1] = a[1]; | 372 | r[1] = a[1]; |
| 342 | if (--dl <= 0) break; | 373 | if (--dl <= 0) |
| 374 | break; | ||
| 343 | case 2: | 375 | case 2: |
| 344 | r[2] = a[2]; | 376 | r[2] = a[2]; |
| 345 | if (--dl <= 0) break; | 377 | if (--dl <= 0) |
| 378 | break; | ||
| 346 | case 3: | 379 | case 3: |
| 347 | r[3] = a[3]; | 380 | r[3] = a[3]; |
| 348 | if (--dl <= 0) break; | 381 | if (--dl <= 0) |
| 349 | } | 382 | break; |
| 383 | } | ||
| 350 | a += 4; | 384 | a += 4; |
| 351 | r += 4; | 385 | r += 4; |
| 352 | } | ||
| 353 | } | 386 | } |
| 354 | if (dl > 0) | 387 | } |
| 355 | { | 388 | if (dl > 0) { |
| 356 | #ifdef BN_COUNT | 389 | #ifdef BN_COUNT |
| 357 | fprintf(stderr, " bn_add_part_words %d + %d (dl > 0, copy)\n", cl, dl); | 390 | fprintf(stderr, |
| 391 | " bn_add_part_words %d + %d (dl > 0, copy)\n", | ||
| 392 | cl, dl); | ||
| 358 | #endif | 393 | #endif |
| 359 | for(;;) | 394 | for (;;) { |
| 360 | { | ||
| 361 | r[0] = a[0]; | 395 | r[0] = a[0]; |
| 362 | if (--dl <= 0) break; | 396 | if (--dl <= 0) |
| 397 | break; | ||
| 363 | r[1] = a[1]; | 398 | r[1] = a[1]; |
| 364 | if (--dl <= 0) break; | 399 | if (--dl <= 0) |
| 400 | break; | ||
| 365 | r[2] = a[2]; | 401 | r[2] = a[2]; |
| 366 | if (--dl <= 0) break; | 402 | if (--dl <= 0) |
| 403 | break; | ||
| 367 | r[3] = a[3]; | 404 | r[3] = a[3]; |
| 368 | if (--dl <= 0) break; | 405 | if (--dl <= 0) |
| 406 | break; | ||
| 369 | 407 | ||
| 370 | a += 4; | 408 | a += 4; |
| 371 | r += 4; | 409 | r += 4; |
| 372 | } | ||
| 373 | } | 410 | } |
| 374 | } | 411 | } |
| 375 | return c; | ||
| 376 | } | 412 | } |
| 413 | return c; | ||
| 414 | } | ||
| 377 | 415 | ||
| 378 | #ifdef BN_RECURSION | 416 | #ifdef BN_RECURSION |
| 379 | /* Karatsuba recursive multiplication algorithm | 417 | /* Karatsuba recursive multiplication algorithm |
| @@ -390,435 +428,406 @@ BN_ULONG bn_add_part_words(BN_ULONG *r, | |||
| 390 | * a[1]*b[1] | 428 | * a[1]*b[1] |
| 391 | */ | 429 | */ |
| 392 | /* dnX may not be positive, but n2/2+dnX has to be */ | 430 | /* dnX may not be positive, but n2/2+dnX has to be */ |
| 393 | void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, | 431 | void |
| 394 | int dna, int dnb, BN_ULONG *t) | 432 | bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, int dna, |
| 395 | { | 433 | int dnb, BN_ULONG *t) |
| 396 | int n=n2/2,c1,c2; | 434 | { |
| 397 | int tna=n+dna, tnb=n+dnb; | 435 | int n = n2 / 2, c1, c2; |
| 398 | unsigned int neg,zero; | 436 | int tna = n + dna, tnb = n + dnb; |
| 399 | BN_ULONG ln,lo,*p; | 437 | unsigned int neg, zero; |
| 438 | BN_ULONG ln, lo, *p; | ||
| 400 | 439 | ||
| 401 | # ifdef BN_COUNT | 440 | # ifdef BN_COUNT |
| 402 | fprintf(stderr," bn_mul_recursive %d%+d * %d%+d\n",n2,dna,n2,dnb); | 441 | fprintf(stderr, " bn_mul_recursive %d%+d * %d%+d\n",n2,dna,n2,dnb); |
| 403 | # endif | 442 | # endif |
| 404 | # ifdef BN_MUL_COMBA | 443 | # ifdef BN_MUL_COMBA |
| 405 | # if 0 | 444 | # if 0 |
| 406 | if (n2 == 4) | 445 | if (n2 == 4) { |
| 407 | { | 446 | bn_mul_comba4(r, a, b); |
| 408 | bn_mul_comba4(r,a,b); | ||
| 409 | return; | 447 | return; |
| 410 | } | 448 | } |
| 411 | # endif | 449 | # endif |
| 412 | /* Only call bn_mul_comba 8 if n2 == 8 and the | 450 | /* Only call bn_mul_comba 8 if n2 == 8 and the |
| 413 | * two arrays are complete [steve] | 451 | * two arrays are complete [steve] |
| 414 | */ | 452 | */ |
| 415 | if (n2 == 8 && dna == 0 && dnb == 0) | 453 | if (n2 == 8 && dna == 0 && dnb == 0) { |
| 416 | { | 454 | bn_mul_comba8(r, a, b); |
| 417 | bn_mul_comba8(r,a,b); | 455 | return; |
| 418 | return; | 456 | } |
| 419 | } | ||
| 420 | # endif /* BN_MUL_COMBA */ | 457 | # endif /* BN_MUL_COMBA */ |
| 421 | /* Else do normal multiply */ | 458 | /* Else do normal multiply */ |
| 422 | if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) | 459 | if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) { |
| 423 | { | 460 | bn_mul_normal(r, a, n2 + dna, b, n2 + dnb); |
| 424 | bn_mul_normal(r,a,n2+dna,b,n2+dnb); | ||
| 425 | if ((dna + dnb) < 0) | 461 | if ((dna + dnb) < 0) |
| 426 | memset(&r[2*n2 + dna + dnb], 0, | 462 | memset(&r[2*n2 + dna + dnb], 0, |
| 427 | sizeof(BN_ULONG) * -(dna + dnb)); | 463 | sizeof(BN_ULONG) * -(dna + dnb)); |
| 428 | return; | 464 | return; |
| 429 | } | 465 | } |
| 430 | /* r=(a[0]-a[1])*(b[1]-b[0]) */ | 466 | /* r=(a[0]-a[1])*(b[1]-b[0]) */ |
| 431 | c1=bn_cmp_part_words(a,&(a[n]),tna,n-tna); | 467 | c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna); |
| 432 | c2=bn_cmp_part_words(&(b[n]),b,tnb,tnb-n); | 468 | c2 = bn_cmp_part_words(&(b[n]), b,tnb, tnb - n); |
| 433 | zero=neg=0; | 469 | zero = neg = 0; |
| 434 | switch (c1*3+c2) | 470 | switch (c1 * 3 + c2) { |
| 435 | { | ||
| 436 | case -4: | 471 | case -4: |
| 437 | bn_sub_part_words(t, &(a[n]),a, tna,tna-n); /* - */ | 472 | bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */ |
| 438 | bn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb); /* - */ | 473 | bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */ |
| 439 | break; | 474 | break; |
| 440 | case -3: | 475 | case -3: |
| 441 | zero=1; | 476 | zero = 1; |
| 442 | break; | 477 | break; |
| 443 | case -2: | 478 | case -2: |
| 444 | bn_sub_part_words(t, &(a[n]),a, tna,tna-n); /* - */ | 479 | bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */ |
| 445 | bn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n); /* + */ | 480 | bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */ |
| 446 | neg=1; | 481 | neg = 1; |
| 447 | break; | 482 | break; |
| 448 | case -1: | 483 | case -1: |
| 449 | case 0: | 484 | case 0: |
| 450 | case 1: | 485 | case 1: |
| 451 | zero=1; | 486 | zero = 1; |
| 452 | break; | 487 | break; |
| 453 | case 2: | 488 | case 2: |
| 454 | bn_sub_part_words(t, a, &(a[n]),tna,n-tna); /* + */ | 489 | bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */ |
| 455 | bn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb); /* - */ | 490 | bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */ |
| 456 | neg=1; | 491 | neg = 1; |
| 457 | break; | 492 | break; |
| 458 | case 3: | 493 | case 3: |
| 459 | zero=1; | 494 | zero = 1; |
| 460 | break; | 495 | break; |
| 461 | case 4: | 496 | case 4: |
| 462 | bn_sub_part_words(t, a, &(a[n]),tna,n-tna); | 497 | bn_sub_part_words(t, a, &(a[n]), tna, n - tna); |
| 463 | bn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n); | 498 | bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); |
| 464 | break; | 499 | break; |
| 465 | } | 500 | } |
| 466 | 501 | ||
| 467 | # ifdef BN_MUL_COMBA | 502 | # ifdef BN_MUL_COMBA |
| 468 | if (n == 4 && dna == 0 && dnb == 0) /* XXX: bn_mul_comba4 could take | 503 | if (n == 4 && dna == 0 && dnb == 0) /* XXX: bn_mul_comba4 could take |
| 469 | extra args to do this well */ | 504 | extra args to do this well */ |
| 470 | { | 505 | { |
| 471 | if (!zero) | 506 | if (!zero) |
| 472 | bn_mul_comba4(&(t[n2]),t,&(t[n])); | 507 | bn_mul_comba4(&(t[n2]), t, &(t[n])); |
| 473 | else | 508 | else |
| 474 | memset(&(t[n2]),0,8*sizeof(BN_ULONG)); | 509 | memset(&(t[n2]), 0, 8 * sizeof(BN_ULONG)); |
| 475 | 510 | ||
| 476 | bn_mul_comba4(r,a,b); | 511 | bn_mul_comba4(r, a, b); |
| 477 | bn_mul_comba4(&(r[n2]),&(a[n]),&(b[n])); | 512 | bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n])); |
| 478 | } | 513 | } else if (n == 8 && dna == 0 && dnb == 0) /* XXX: bn_mul_comba8 could |
| 479 | else if (n == 8 && dna == 0 && dnb == 0) /* XXX: bn_mul_comba8 could | ||
| 480 | take extra args to do this | 514 | take extra args to do this |
| 481 | well */ | 515 | well */ |
| 482 | { | 516 | { |
| 483 | if (!zero) | 517 | if (!zero) |
| 484 | bn_mul_comba8(&(t[n2]),t,&(t[n])); | 518 | bn_mul_comba8(&(t[n2]), t, &(t[n])); |
| 485 | else | 519 | else |
| 486 | memset(&(t[n2]),0,16*sizeof(BN_ULONG)); | 520 | memset(&(t[n2]), 0, 16 * sizeof(BN_ULONG)); |
| 487 | 521 | ||
| 488 | bn_mul_comba8(r,a,b); | 522 | bn_mul_comba8(r, a, b); |
| 489 | bn_mul_comba8(&(r[n2]),&(a[n]),&(b[n])); | 523 | bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n])); |
| 490 | } | 524 | } else |
| 491 | else | ||
| 492 | # endif /* BN_MUL_COMBA */ | 525 | # endif /* BN_MUL_COMBA */ |
| 493 | { | 526 | { |
| 494 | p= &(t[n2*2]); | 527 | p = &(t[n2 * 2]); |
| 495 | if (!zero) | 528 | if (!zero) |
| 496 | bn_mul_recursive(&(t[n2]),t,&(t[n]),n,0,0,p); | 529 | bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p); |
| 497 | else | 530 | else |
| 498 | memset(&(t[n2]),0,n2*sizeof(BN_ULONG)); | 531 | memset(&(t[n2]), 0, n2 * sizeof(BN_ULONG)); |
| 499 | bn_mul_recursive(r,a,b,n,0,0,p); | 532 | bn_mul_recursive(r, a, b, n, 0, 0, p); |
| 500 | bn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]),n,dna,dnb,p); | 533 | bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p); |
| 501 | } | 534 | } |
| 502 | 535 | ||
| 503 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign | 536 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign |
| 504 | * r[10] holds (a[0]*b[0]) | 537 | * r[10] holds (a[0]*b[0]) |
| 505 | * r[32] holds (b[1]*b[1]) | 538 | * r[32] holds (b[1]*b[1]) |
| 506 | */ | 539 | */ |
| 507 | 540 | ||
| 508 | c1=(int)(bn_add_words(t,r,&(r[n2]),n2)); | 541 | c1 = (int)(bn_add_words(t, r, &(r[n2]), n2)); |
| 509 | 542 | ||
| 510 | if (neg) /* if t[32] is negative */ | 543 | if (neg) /* if t[32] is negative */ |
| 511 | { | 544 | { |
| 512 | c1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2)); | 545 | c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2)); |
| 513 | } | 546 | } else { |
| 514 | else | ||
| 515 | { | ||
| 516 | /* Might have a carry */ | 547 | /* Might have a carry */ |
| 517 | c1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2)); | 548 | c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2)); |
| 518 | } | 549 | } |
| 519 | 550 | ||
| 520 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1]) | 551 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1]) |
| 521 | * r[10] holds (a[0]*b[0]) | 552 | * r[10] holds (a[0]*b[0]) |
| 522 | * r[32] holds (b[1]*b[1]) | 553 | * r[32] holds (b[1]*b[1]) |
| 523 | * c1 holds the carry bits | 554 | * c1 holds the carry bits |
| 524 | */ | 555 | */ |
| 525 | c1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2)); | 556 | c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2)); |
| 526 | if (c1) | 557 | if (c1) { |
| 527 | { | 558 | p = &(r[n + n2]); |
| 528 | p= &(r[n+n2]); | ||
| 529 | lo= *p; | 559 | lo= *p; |
| 530 | ln=(lo+c1)&BN_MASK2; | 560 | ln = (lo + c1) & BN_MASK2; |
| 531 | *p=ln; | 561 | *p = ln; |
| 532 | 562 | ||
| 533 | /* The overflow will stop before we over write | 563 | /* The overflow will stop before we over write |
| 534 | * words we should not overwrite */ | 564 | * words we should not overwrite */ |
| 535 | if (ln < (BN_ULONG)c1) | 565 | if (ln < (BN_ULONG)c1) { |
| 536 | { | 566 | do { |
| 537 | do { | ||
| 538 | p++; | 567 | p++; |
| 539 | lo= *p; | 568 | lo= *p; |
| 540 | ln=(lo+1)&BN_MASK2; | 569 | ln = (lo + 1) & BN_MASK2; |
| 541 | *p=ln; | 570 | *p = ln; |
| 542 | } while (ln == 0); | 571 | } while (ln == 0); |
| 543 | } | ||
| 544 | } | 572 | } |
| 545 | } | 573 | } |
| 574 | } | ||
| 546 | 575 | ||
| 547 | /* n+tn is the word length | 576 | /* n+tn is the word length |
| 548 | * t needs to be n*4 is size, as does r */ | 577 | * t needs to be n*4 is size, as does r */ |
| 549 | /* tnX may not be negative but less than n */ | 578 | /* tnX may not be negative but less than n */ |
| 550 | void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n, | 579 | void |
| 551 | int tna, int tnb, BN_ULONG *t) | 580 | bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n, int tna, |
| 552 | { | 581 | int tnb, BN_ULONG *t) |
| 553 | int i,j,n2=n*2; | 582 | { |
| 554 | int c1,c2,neg; | 583 | int i, j, n2 = n * 2; |
| 555 | BN_ULONG ln,lo,*p; | 584 | int c1, c2, neg; |
| 585 | BN_ULONG ln, lo, *p; | ||
| 556 | 586 | ||
| 557 | # ifdef BN_COUNT | 587 | # ifdef BN_COUNT |
| 558 | fprintf(stderr," bn_mul_part_recursive (%d%+d) * (%d%+d)\n", | 588 | fprintf(stderr, " bn_mul_part_recursive (%d%+d) * (%d%+d)\n", |
| 559 | n, tna, n, tnb); | 589 | n, tna, n, tnb); |
| 560 | # endif | 590 | # endif |
| 561 | if (n < 8) | 591 | if (n < 8) { |
| 562 | { | 592 | bn_mul_normal(r, a, n + tna, b, n + tnb); |
| 563 | bn_mul_normal(r,a,n+tna,b,n+tnb); | ||
| 564 | return; | 593 | return; |
| 565 | } | 594 | } |
| 566 | 595 | ||
| 567 | /* r=(a[0]-a[1])*(b[1]-b[0]) */ | 596 | /* r=(a[0]-a[1])*(b[1]-b[0]) */ |
| 568 | c1=bn_cmp_part_words(a,&(a[n]),tna,n-tna); | 597 | c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna); |
| 569 | c2=bn_cmp_part_words(&(b[n]),b,tnb,tnb-n); | 598 | c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n); |
| 570 | neg=0; | 599 | neg = 0; |
| 571 | switch (c1*3+c2) | 600 | switch (c1 * 3 + c2) { |
| 572 | { | ||
| 573 | case -4: | 601 | case -4: |
| 574 | bn_sub_part_words(t, &(a[n]),a, tna,tna-n); /* - */ | 602 | bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */ |
| 575 | bn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb); /* - */ | 603 | bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */ |
| 576 | break; | 604 | break; |
| 577 | case -3: | 605 | case -3: |
| 578 | /* break; */ | 606 | /* break; */ |
| 579 | case -2: | 607 | case -2: |
| 580 | bn_sub_part_words(t, &(a[n]),a, tna,tna-n); /* - */ | 608 | bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */ |
| 581 | bn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n); /* + */ | 609 | bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */ |
| 582 | neg=1; | 610 | neg = 1; |
| 583 | break; | 611 | break; |
| 584 | case -1: | 612 | case -1: |
| 585 | case 0: | 613 | case 0: |
| 586 | case 1: | 614 | case 1: |
| 587 | /* break; */ | 615 | /* break; */ |
| 588 | case 2: | 616 | case 2: |
| 589 | bn_sub_part_words(t, a, &(a[n]),tna,n-tna); /* + */ | 617 | bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */ |
| 590 | bn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb); /* - */ | 618 | bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */ |
| 591 | neg=1; | 619 | neg = 1; |
| 592 | break; | 620 | break; |
| 593 | case 3: | 621 | case 3: |
| 594 | /* break; */ | 622 | /* break; */ |
| 595 | case 4: | 623 | case 4: |
| 596 | bn_sub_part_words(t, a, &(a[n]),tna,n-tna); | 624 | bn_sub_part_words(t, a, &(a[n]), tna, n - tna); |
| 597 | bn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n); | 625 | bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); |
| 598 | break; | 626 | break; |
| 599 | } | 627 | } |
| 600 | /* The zero case isn't yet implemented here. The speedup | 628 | /* The zero case isn't yet implemented here. The speedup |
| 601 | would probably be negligible. */ | 629 | would probably be negligible. */ |
| 602 | # if 0 | 630 | # if 0 |
| 603 | if (n == 4) | 631 | if (n == 4) { |
| 604 | { | 632 | bn_mul_comba4(&(t[n2]), t, &(t[n])); |
| 605 | bn_mul_comba4(&(t[n2]),t,&(t[n])); | 633 | bn_mul_comba4(r, a, b); |
| 606 | bn_mul_comba4(r,a,b); | 634 | bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn); |
| 607 | bn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn); | 635 | memset(&(r[n2 + tn * 2]), 0, sizeof(BN_ULONG) * (n2 - tn * 2)); |
| 608 | memset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2)); | 636 | } else |
| 609 | } | ||
| 610 | else | ||
| 611 | # endif | 637 | # endif |
| 612 | if (n == 8) | 638 | if (n == 8) { |
| 613 | { | 639 | bn_mul_comba8(&(t[n2]), t, &(t[n])); |
| 614 | bn_mul_comba8(&(t[n2]),t,&(t[n])); | 640 | bn_mul_comba8(r, a, b); |
| 615 | bn_mul_comba8(r,a,b); | 641 | bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb); |
| 616 | bn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb); | 642 | memset(&(r[n2 + tna + tnb]), 0, |
| 617 | memset(&(r[n2+tna+tnb]),0,sizeof(BN_ULONG)*(n2-tna-tnb)); | 643 | sizeof(BN_ULONG) * (n2 - tna - tnb)); |
| 618 | } | 644 | } else { |
| 619 | else | 645 | p = &(t[n2*2]); |
| 620 | { | 646 | bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p); |
| 621 | p= &(t[n2*2]); | 647 | bn_mul_recursive(r, a, b, n, 0, 0, p); |
| 622 | bn_mul_recursive(&(t[n2]),t,&(t[n]),n,0,0,p); | 648 | i = n / 2; |
| 623 | bn_mul_recursive(r,a,b,n,0,0,p); | ||
| 624 | i=n/2; | ||
| 625 | /* If there is only a bottom half to the number, | 649 | /* If there is only a bottom half to the number, |
| 626 | * just do it */ | 650 | * just do it */ |
| 627 | if (tna > tnb) | 651 | if (tna > tnb) |
| 628 | j = tna - i; | 652 | j = tna - i; |
| 629 | else | 653 | else |
| 630 | j = tnb - i; | 654 | j = tnb - i; |
| 631 | if (j == 0) | 655 | if (j == 0) { |
| 632 | { | 656 | bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), |
| 633 | bn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]), | 657 | i, tna - i, tnb - i, p); |
| 634 | i,tna-i,tnb-i,p); | 658 | memset(&(r[n2 + i * 2]), 0, |
| 635 | memset(&(r[n2+i*2]),0,sizeof(BN_ULONG)*(n2-i*2)); | 659 | sizeof(BN_ULONG) * (n2 - i * 2)); |
| 636 | } | 660 | } |
| 637 | else if (j > 0) /* eg, n == 16, i == 8 and tn == 11 */ | 661 | else if (j > 0) /* eg, n == 16, i == 8 and tn == 11 */ |
| 638 | { | 662 | { |
| 639 | bn_mul_part_recursive(&(r[n2]),&(a[n]),&(b[n]), | 663 | bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]), |
| 640 | i,tna-i,tnb-i,p); | 664 | i, tna - i, tnb - i, p); |
| 641 | memset(&(r[n2+tna+tnb]),0, | 665 | memset(&(r[n2 + tna + tnb]), 0, |
| 642 | sizeof(BN_ULONG)*(n2-tna-tnb)); | 666 | sizeof(BN_ULONG) * (n2 - tna - tnb)); |
| 643 | } | 667 | } |
| 644 | else /* (j < 0) eg, n == 16, i == 8 and tn == 5 */ | 668 | else /* (j < 0) eg, n == 16, i == 8 and tn == 5 */ |
| 645 | { | 669 | { |
| 646 | memset(&(r[n2]),0,sizeof(BN_ULONG)*n2); | 670 | memset(&(r[n2]), 0, sizeof(BN_ULONG) * n2); |
| 647 | if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL | 671 | if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL && |
| 648 | && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) | 672 | tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) { |
| 649 | { | 673 | bn_mul_normal(&(r[n2]), &(a[n]), tna, |
| 650 | bn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb); | 674 | &(b[n]), tnb); |
| 651 | } | 675 | } else { |
| 652 | else | 676 | for (;;) { |
| 653 | { | 677 | i /= 2; |
| 654 | for (;;) | ||
| 655 | { | ||
| 656 | i/=2; | ||
| 657 | /* these simplified conditions work | 678 | /* these simplified conditions work |
| 658 | * exclusively because difference | 679 | * exclusively because difference |
| 659 | * between tna and tnb is 1 or 0 */ | 680 | * between tna and tnb is 1 or 0 */ |
| 660 | if (i < tna || i < tnb) | 681 | if (i < tna || i < tnb) { |
| 661 | { | ||
| 662 | bn_mul_part_recursive(&(r[n2]), | 682 | bn_mul_part_recursive(&(r[n2]), |
| 663 | &(a[n]),&(b[n]), | 683 | &(a[n]), &(b[n]), i, |
| 664 | i,tna-i,tnb-i,p); | 684 | tna - i, tnb - i, p); |
| 665 | break; | 685 | break; |
| 666 | } | 686 | } else if (i == tna || i == tnb) { |
| 667 | else if (i == tna || i == tnb) | ||
| 668 | { | ||
| 669 | bn_mul_recursive(&(r[n2]), | 687 | bn_mul_recursive(&(r[n2]), |
| 670 | &(a[n]),&(b[n]), | 688 | &(a[n]), &(b[n]), i, |
| 671 | i,tna-i,tnb-i,p); | 689 | tna - i, tnb - i, p); |
| 672 | break; | 690 | break; |
| 673 | } | ||
| 674 | } | 691 | } |
| 675 | } | 692 | } |
| 676 | } | 693 | } |
| 677 | } | 694 | } |
| 695 | } | ||
| 678 | 696 | ||
| 679 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign | 697 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign |
| 680 | * r[10] holds (a[0]*b[0]) | 698 | * r[10] holds (a[0]*b[0]) |
| 681 | * r[32] holds (b[1]*b[1]) | 699 | * r[32] holds (b[1]*b[1]) |
| 682 | */ | 700 | */ |
| 683 | 701 | ||
| 684 | c1=(int)(bn_add_words(t,r,&(r[n2]),n2)); | 702 | c1 = (int)(bn_add_words(t, r,&(r[n2]), n2)); |
| 685 | 703 | ||
| 686 | if (neg) /* if t[32] is negative */ | 704 | if (neg) /* if t[32] is negative */ |
| 687 | { | 705 | { |
| 688 | c1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2)); | 706 | c1 -= (int)(bn_sub_words(&(t[n2]), t,&(t[n2]), n2)); |
| 689 | } | 707 | } else { |
| 690 | else | ||
| 691 | { | ||
| 692 | /* Might have a carry */ | 708 | /* Might have a carry */ |
| 693 | c1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2)); | 709 | c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2)); |
| 694 | } | 710 | } |
| 695 | 711 | ||
| 696 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1]) | 712 | /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1]) |
| 697 | * r[10] holds (a[0]*b[0]) | 713 | * r[10] holds (a[0]*b[0]) |
| 698 | * r[32] holds (b[1]*b[1]) | 714 | * r[32] holds (b[1]*b[1]) |
| 699 | * c1 holds the carry bits | 715 | * c1 holds the carry bits |
| 700 | */ | 716 | */ |
| 701 | c1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2)); | 717 | c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2)); |
| 702 | if (c1) | 718 | if (c1) { |
| 703 | { | 719 | p = &(r[n + n2]); |
| 704 | p= &(r[n+n2]); | ||
| 705 | lo= *p; | 720 | lo= *p; |
| 706 | ln=(lo+c1)&BN_MASK2; | 721 | ln = (lo + c1)&BN_MASK2; |
| 707 | *p=ln; | 722 | *p = ln; |
| 708 | 723 | ||
| 709 | /* The overflow will stop before we over write | 724 | /* The overflow will stop before we over write |
| 710 | * words we should not overwrite */ | 725 | * words we should not overwrite */ |
| 711 | if (ln < (BN_ULONG)c1) | 726 | if (ln < (BN_ULONG)c1) { |
| 712 | { | 727 | do { |
| 713 | do { | ||
| 714 | p++; | 728 | p++; |
| 715 | lo= *p; | 729 | lo= *p; |
| 716 | ln=(lo+1)&BN_MASK2; | 730 | ln = (lo + 1) & BN_MASK2; |
| 717 | *p=ln; | 731 | *p = ln; |
| 718 | } while (ln == 0); | 732 | } while (ln == 0); |
| 719 | } | ||
| 720 | } | 733 | } |
| 721 | } | 734 | } |
| 735 | } | ||
| 722 | 736 | ||
| 723 | /* a and b must be the same size, which is n2. | 737 | /* a and b must be the same size, which is n2. |
| 724 | * r needs to be n2 words and t needs to be n2*2 | 738 | * r needs to be n2 words and t needs to be n2*2 |
| 725 | */ | 739 | */ |
| 726 | void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, | 740 | void |
| 727 | BN_ULONG *t) | 741 | bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, BN_ULONG *t) |
| 728 | { | 742 | { |
| 729 | int n=n2/2; | 743 | int n = n2 / 2; |
| 730 | 744 | ||
| 731 | # ifdef BN_COUNT | 745 | # ifdef BN_COUNT |
| 732 | fprintf(stderr," bn_mul_low_recursive %d * %d\n",n2,n2); | 746 | fprintf(stderr, " bn_mul_low_recursive %d * %d\n",n2,n2); |
| 733 | # endif | 747 | # endif |
| 734 | 748 | ||
| 735 | bn_mul_recursive(r,a,b,n,0,0,&(t[0])); | 749 | bn_mul_recursive(r, a, b, n, 0, 0, &(t[0])); |
| 736 | if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL) | 750 | if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL) { |
| 737 | { | 751 | bn_mul_low_recursive(&(t[0]), &(a[0]), &(b[n]), n, &(t[n2])); |
| 738 | bn_mul_low_recursive(&(t[0]),&(a[0]),&(b[n]),n,&(t[n2])); | 752 | bn_add_words(&(r[n]), &(r[n]), &(t[0]), n); |
| 739 | bn_add_words(&(r[n]),&(r[n]),&(t[0]),n); | 753 | bn_mul_low_recursive(&(t[0]), &(a[n]), &(b[0]), n, &(t[n2])); |
| 740 | bn_mul_low_recursive(&(t[0]),&(a[n]),&(b[0]),n,&(t[n2])); | 754 | bn_add_words(&(r[n]), &(r[n]), &(t[0]), n); |
| 741 | bn_add_words(&(r[n]),&(r[n]),&(t[0]),n); | 755 | } else { |
| 742 | } | 756 | bn_mul_low_normal(&(t[0]), &(a[0]), &(b[n]), n); |
| 743 | else | 757 | bn_mul_low_normal(&(t[n]), &(a[n]), &(b[0]), n); |
| 744 | { | 758 | bn_add_words(&(r[n]), &(r[n]), &(t[0]), n); |
| 745 | bn_mul_low_normal(&(t[0]),&(a[0]),&(b[n]),n); | 759 | bn_add_words(&(r[n]), &(r[n]), &(t[n]), n); |
| 746 | bn_mul_low_normal(&(t[n]),&(a[n]),&(b[0]),n); | ||
| 747 | bn_add_words(&(r[n]),&(r[n]),&(t[0]),n); | ||
| 748 | bn_add_words(&(r[n]),&(r[n]),&(t[n]),n); | ||
| 749 | } | ||
| 750 | } | 760 | } |
| 761 | } | ||
| 751 | 762 | ||
| 752 | /* a and b must be the same size, which is n2. | 763 | /* a and b must be the same size, which is n2. |
| 753 | * r needs to be n2 words and t needs to be n2*2 | 764 | * r needs to be n2 words and t needs to be n2*2 |
| 754 | * l is the low words of the output. | 765 | * l is the low words of the output. |
| 755 | * t needs to be n2*3 | 766 | * t needs to be n2*3 |
| 756 | */ | 767 | */ |
| 757 | void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2, | 768 | void |
| 758 | BN_ULONG *t) | 769 | bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2, |
| 759 | { | 770 | BN_ULONG *t) |
| 760 | int i,n; | 771 | { |
| 761 | int c1,c2; | 772 | int i, n; |
| 762 | int neg,oneg,zero; | 773 | int c1, c2; |
| 763 | BN_ULONG ll,lc,*lp,*mp; | 774 | int neg, oneg, zero; |
| 775 | BN_ULONG ll, lc, *lp, *mp; | ||
| 764 | 776 | ||
| 765 | # ifdef BN_COUNT | 777 | # ifdef BN_COUNT |
| 766 | fprintf(stderr," bn_mul_high %d * %d\n",n2,n2); | 778 | fprintf(stderr, " bn_mul_high %d * %d\n",n2,n2); |
| 767 | # endif | 779 | # endif |
| 768 | n=n2/2; | 780 | n = n2 / 2; |
| 769 | 781 | ||
| 770 | /* Calculate (al-ah)*(bh-bl) */ | 782 | /* Calculate (al-ah)*(bh-bl) */ |
| 771 | neg=zero=0; | 783 | neg = zero = 0; |
| 772 | c1=bn_cmp_words(&(a[0]),&(a[n]),n); | 784 | c1 = bn_cmp_words(&(a[0]), &(a[n]), n); |
| 773 | c2=bn_cmp_words(&(b[n]),&(b[0]),n); | 785 | c2 = bn_cmp_words(&(b[n]), &(b[0]), n); |
| 774 | switch (c1*3+c2) | 786 | switch (c1 * 3 + c2) { |
| 775 | { | ||
| 776 | case -4: | 787 | case -4: |
| 777 | bn_sub_words(&(r[0]),&(a[n]),&(a[0]),n); | 788 | bn_sub_words(&(r[0]), &(a[n]), &(a[0]), n); |
| 778 | bn_sub_words(&(r[n]),&(b[0]),&(b[n]),n); | 789 | bn_sub_words(&(r[n]), &(b[0]), &(b[n]), n); |
| 779 | break; | 790 | break; |
| 780 | case -3: | 791 | case -3: |
| 781 | zero=1; | 792 | zero = 1; |
| 782 | break; | 793 | break; |
| 783 | case -2: | 794 | case -2: |
| 784 | bn_sub_words(&(r[0]),&(a[n]),&(a[0]),n); | 795 | bn_sub_words(&(r[0]), &(a[n]), &(a[0]), n); |
| 785 | bn_sub_words(&(r[n]),&(b[n]),&(b[0]),n); | 796 | bn_sub_words(&(r[n]), &(b[n]), &(b[0]), n); |
| 786 | neg=1; | 797 | neg = 1; |
| 787 | break; | 798 | break; |
| 788 | case -1: | 799 | case -1: |
| 789 | case 0: | 800 | case 0: |
| 790 | case 1: | 801 | case 1: |
| 791 | zero=1; | 802 | zero = 1; |
| 792 | break; | 803 | break; |
| 793 | case 2: | 804 | case 2: |
| 794 | bn_sub_words(&(r[0]),&(a[0]),&(a[n]),n); | 805 | bn_sub_words(&(r[0]), &(a[0]), &(a[n]), n); |
| 795 | bn_sub_words(&(r[n]),&(b[0]),&(b[n]),n); | 806 | bn_sub_words(&(r[n]), &(b[0]), &(b[n]), n); |
| 796 | neg=1; | 807 | neg = 1; |
| 797 | break; | 808 | break; |
| 798 | case 3: | 809 | case 3: |
| 799 | zero=1; | 810 | zero = 1; |
| 800 | break; | 811 | break; |
| 801 | case 4: | 812 | case 4: |
| 802 | bn_sub_words(&(r[0]),&(a[0]),&(a[n]),n); | 813 | bn_sub_words(&(r[0]), &(a[0]), &(a[n]), n); |
| 803 | bn_sub_words(&(r[n]),&(b[n]),&(b[0]),n); | 814 | bn_sub_words(&(r[n]), &(b[n]), &(b[0]), n); |
| 804 | break; | 815 | break; |
| 805 | } | 816 | } |
| 806 | 817 | ||
| 807 | oneg=neg; | 818 | oneg = neg; |
| 808 | /* t[10] = (a[0]-a[1])*(b[1]-b[0]) */ | 819 | /* t[10] = (a[0]-a[1])*(b[1]-b[0]) */ |
| 809 | /* r[10] = (a[1]*b[1]) */ | 820 | /* r[10] = (a[1]*b[1]) */ |
| 810 | # ifdef BN_MUL_COMBA | 821 | # ifdef BN_MUL_COMBA |
| 811 | if (n == 8) | 822 | if (n == 8) { |
| 812 | { | 823 | bn_mul_comba8(&(t[0]), &(r[0]), &(r[n])); |
| 813 | bn_mul_comba8(&(t[0]),&(r[0]),&(r[n])); | 824 | bn_mul_comba8(r, &(a[n]), &(b[n])); |
| 814 | bn_mul_comba8(r,&(a[n]),&(b[n])); | 825 | } else |
| 815 | } | ||
| 816 | else | ||
| 817 | # endif | 826 | # endif |
| 818 | { | 827 | { |
| 819 | bn_mul_recursive(&(t[0]),&(r[0]),&(r[n]),n,0,0,&(t[n2])); | 828 | bn_mul_recursive(&(t[0]), &(r[0]), &(r[n]), n, 0, 0, &(t[n2])); |
| 820 | bn_mul_recursive(r,&(a[n]),&(b[n]),n,0,0,&(t[n2])); | 829 | bn_mul_recursive(r, &(a[n]), &(b[n]), n, 0, 0, &(t[n2])); |
| 821 | } | 830 | } |
| 822 | 831 | ||
| 823 | /* s0 == low(al*bl) | 832 | /* s0 == low(al*bl) |
| 824 | * s1 == low(ah*bh)+low((al-ah)*(bh-bl))+low(al*bl)+high(al*bl) | 833 | * s1 == low(ah*bh)+low((al-ah)*(bh-bl))+low(al*bl)+high(al*bl) |
| @@ -826,36 +835,29 @@ void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2, | |||
| 826 | * high(al*bl) == s1 - low(ah*bh+s0+(al-ah)*(bh-bl)) | 835 | * high(al*bl) == s1 - low(ah*bh+s0+(al-ah)*(bh-bl)) |
| 827 | * high(al*bl) == s1 - (r[0]+l[0]+t[0]) | 836 | * high(al*bl) == s1 - (r[0]+l[0]+t[0]) |
| 828 | */ | 837 | */ |
| 829 | if (l != NULL) | 838 | if (l != NULL) { |
| 830 | { | 839 | lp = &(t[n2 + n]); |
| 831 | lp= &(t[n2+n]); | 840 | c1 = (int)(bn_add_words(lp, &(r[0]), &(l[0]), n)); |
| 832 | c1=(int)(bn_add_words(lp,&(r[0]),&(l[0]),n)); | 841 | } else { |
| 833 | } | 842 | c1 = 0; |
| 834 | else | 843 | lp = &(r[0]); |
| 835 | { | 844 | } |
| 836 | c1=0; | ||
| 837 | lp= &(r[0]); | ||
| 838 | } | ||
| 839 | 845 | ||
| 840 | if (neg) | 846 | if (neg) |
| 841 | neg=(int)(bn_sub_words(&(t[n2]),lp,&(t[0]),n)); | 847 | neg = (int)(bn_sub_words(&(t[n2]), lp, &(t[0]), n)); |
| 842 | else | 848 | else { |
| 843 | { | 849 | bn_add_words(&(t[n2]), lp, &(t[0]), n); |
| 844 | bn_add_words(&(t[n2]),lp,&(t[0]),n); | 850 | neg = 0; |
| 845 | neg=0; | 851 | } |
| 846 | } | ||
| 847 | 852 | ||
| 848 | if (l != NULL) | 853 | if (l != NULL) { |
| 849 | { | 854 | bn_sub_words(&(t[n2 + n]), &(l[n]), &(t[n2]), n); |
| 850 | bn_sub_words(&(t[n2+n]),&(l[n]),&(t[n2]),n); | 855 | } else { |
| 851 | } | 856 | lp = &(t[n2 + n]); |
| 852 | else | 857 | mp = &(t[n2]); |
| 853 | { | 858 | for (i = 0; i < n; i++) |
| 854 | lp= &(t[n2+n]); | 859 | lp[i] = ((~mp[i]) + 1) & BN_MASK2; |
| 855 | mp= &(t[n2]); | 860 | } |
| 856 | for (i=0; i<n; i++) | ||
| 857 | lp[i]=((~mp[i])+1)&BN_MASK2; | ||
| 858 | } | ||
| 859 | 861 | ||
| 860 | /* s[0] = low(al*bl) | 862 | /* s[0] = low(al*bl) |
| 861 | * t[3] = high(al*bl) | 863 | * t[3] = high(al*bl) |
| @@ -870,297 +872,297 @@ void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2, | |||
| 870 | * R[2]=r[0]+t[3]+r[1](+-)t[1] (have carry/borrow) | 872 | * R[2]=r[0]+t[3]+r[1](+-)t[1] (have carry/borrow) |
| 871 | * R[3]=r[1]+(carry/borrow) | 873 | * R[3]=r[1]+(carry/borrow) |
| 872 | */ | 874 | */ |
| 873 | if (l != NULL) | 875 | if (l != NULL) { |
| 874 | { | 876 | lp = &(t[n2]); |
| 875 | lp= &(t[n2]); | 877 | c1 = (int)(bn_add_words(lp, &(t[n2 + n]), &(l[0]), n)); |
| 876 | c1= (int)(bn_add_words(lp,&(t[n2+n]),&(l[0]),n)); | 878 | } else { |
| 877 | } | 879 | lp = &(t[n2 + n]); |
| 878 | else | 880 | c1 = 0; |
| 879 | { | 881 | } |
| 880 | lp= &(t[n2+n]); | 882 | c1 += (int)(bn_add_words(&(t[n2]), lp, &(r[0]), n)); |
| 881 | c1=0; | ||
| 882 | } | ||
| 883 | c1+=(int)(bn_add_words(&(t[n2]),lp, &(r[0]),n)); | ||
| 884 | if (oneg) | 883 | if (oneg) |
| 885 | c1-=(int)(bn_sub_words(&(t[n2]),&(t[n2]),&(t[0]),n)); | 884 | c1 -= (int)(bn_sub_words(&(t[n2]), &(t[n2]), &(t[0]), n)); |
| 886 | else | 885 | else |
| 887 | c1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),&(t[0]),n)); | 886 | c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), &(t[0]), n)); |
| 888 | 887 | ||
| 889 | c2 =(int)(bn_add_words(&(r[0]),&(r[0]),&(t[n2+n]),n)); | 888 | c2 = (int)(bn_add_words(&(r[0]), &(r[0]), &(t[n2 + n]), n)); |
| 890 | c2+=(int)(bn_add_words(&(r[0]),&(r[0]),&(r[n]),n)); | 889 | c2 += (int)(bn_add_words(&(r[0]), &(r[0]), &(r[n]), n)); |
| 891 | if (oneg) | 890 | if (oneg) |
| 892 | c2-=(int)(bn_sub_words(&(r[0]),&(r[0]),&(t[n]),n)); | 891 | c2 -= (int)(bn_sub_words(&(r[0]), &(r[0]), &(t[n]), n)); |
| 893 | else | 892 | else |
| 894 | c2+=(int)(bn_add_words(&(r[0]),&(r[0]),&(t[n]),n)); | 893 | c2 += (int)(bn_add_words(&(r[0]), &(r[0]), &(t[n]), n)); |
| 895 | 894 | ||
| 896 | if (c1 != 0) /* Add starting at r[0], could be +ve or -ve */ | 895 | if (c1 != 0) /* Add starting at r[0], could be +ve or -ve */ |
| 897 | { | 896 | { |
| 898 | i=0; | 897 | i = 0; |
| 899 | if (c1 > 0) | 898 | if (c1 > 0) { |
| 900 | { | 899 | lc = c1; |
| 901 | lc=c1; | 900 | do { |
| 902 | do { | 901 | ll = (r[i] + lc) & BN_MASK2; |
| 903 | ll=(r[i]+lc)&BN_MASK2; | 902 | r[i++] = ll; |
| 904 | r[i++]=ll; | 903 | lc = (lc > ll); |
| 905 | lc=(lc > ll); | 904 | } while (lc); |
| 906 | } while (lc); | 905 | } else { |
| 907 | } | 906 | lc = -c1; |
| 908 | else | 907 | do { |
| 909 | { | 908 | ll = r[i]; |
| 910 | lc= -c1; | 909 | r[i++] = (ll - lc) & BN_MASK2; |
| 911 | do { | 910 | lc = (lc > ll); |
| 912 | ll=r[i]; | 911 | } while (lc); |
| 913 | r[i++]=(ll-lc)&BN_MASK2; | ||
| 914 | lc=(lc > ll); | ||
| 915 | } while (lc); | ||
| 916 | } | ||
| 917 | } | 912 | } |
| 913 | } | ||
| 918 | if (c2 != 0) /* Add starting at r[1] */ | 914 | if (c2 != 0) /* Add starting at r[1] */ |
| 919 | { | 915 | { |
| 920 | i=n; | 916 | i = n; |
| 921 | if (c2 > 0) | 917 | if (c2 > 0) { |
| 922 | { | 918 | lc = c2; |
| 923 | lc=c2; | 919 | do { |
| 924 | do { | 920 | ll = (r[i] + lc) & BN_MASK2; |
| 925 | ll=(r[i]+lc)&BN_MASK2; | 921 | r[i++] = ll; |
| 926 | r[i++]=ll; | 922 | lc = (lc > ll); |
| 927 | lc=(lc > ll); | 923 | } while (lc); |
| 928 | } while (lc); | 924 | } else { |
| 929 | } | 925 | lc = -c2; |
| 930 | else | 926 | do { |
| 931 | { | 927 | ll = r[i]; |
| 932 | lc= -c2; | 928 | r[i++] = (ll - lc) & BN_MASK2; |
| 933 | do { | 929 | lc = (lc > ll); |
| 934 | ll=r[i]; | 930 | } while (lc); |
| 935 | r[i++]=(ll-lc)&BN_MASK2; | ||
| 936 | lc=(lc > ll); | ||
| 937 | } while (lc); | ||
| 938 | } | ||
| 939 | } | 931 | } |
| 940 | } | 932 | } |
| 933 | } | ||
| 941 | #endif /* BN_RECURSION */ | 934 | #endif /* BN_RECURSION */ |
| 942 | 935 | ||
| 943 | int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) | 936 | int |
| 944 | { | 937 | BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) |
| 945 | int ret=0; | 938 | { |
| 946 | int top,al,bl; | 939 | int ret = 0; |
| 940 | int top, al, bl; | ||
| 947 | BIGNUM *rr; | 941 | BIGNUM *rr; |
| 948 | #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) | 942 | #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) |
| 949 | int i; | 943 | int i; |
| 950 | #endif | 944 | #endif |
| 951 | #ifdef BN_RECURSION | 945 | #ifdef BN_RECURSION |
| 952 | BIGNUM *t=NULL; | 946 | BIGNUM *t = NULL; |
| 953 | int j=0,k; | 947 | int j = 0, k; |
| 954 | #endif | 948 | #endif |
| 955 | 949 | ||
| 956 | #ifdef BN_COUNT | 950 | #ifdef BN_COUNT |
| 957 | fprintf(stderr,"BN_mul %d * %d\n",a->top,b->top); | 951 | fprintf(stderr, "BN_mul %d * %d\n",a->top,b->top); |
| 958 | #endif | 952 | #endif |
| 959 | 953 | ||
| 960 | bn_check_top(a); | 954 | bn_check_top(a); |
| 961 | bn_check_top(b); | 955 | bn_check_top(b); |
| 962 | bn_check_top(r); | 956 | bn_check_top(r); |
| 963 | 957 | ||
| 964 | al=a->top; | 958 | al = a->top; |
| 965 | bl=b->top; | 959 | bl = b->top; |
| 966 | 960 | ||
| 967 | if ((al == 0) || (bl == 0)) | 961 | if ((al == 0) || (bl == 0)) { |
| 968 | { | ||
| 969 | BN_zero(r); | 962 | BN_zero(r); |
| 970 | return(1); | 963 | return (1); |
| 971 | } | 964 | } |
| 972 | top=al+bl; | 965 | top = al + bl; |
| 973 | 966 | ||
| 974 | BN_CTX_start(ctx); | 967 | BN_CTX_start(ctx); |
| 975 | if ((r == a) || (r == b)) | 968 | if ((r == a) || (r == b)) { |
| 976 | { | 969 | if ((rr = BN_CTX_get(ctx)) == NULL) |
| 977 | if ((rr = BN_CTX_get(ctx)) == NULL) goto err; | 970 | goto err; |
| 978 | } | 971 | } else |
| 979 | else | ||
| 980 | rr = r; | 972 | rr = r; |
| 981 | rr->neg=a->neg^b->neg; | 973 | rr->neg = a->neg ^ b->neg; |
| 982 | 974 | ||
| 983 | #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) | 975 | #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) |
| 984 | i = al-bl; | 976 | i = al - bl; |
| 985 | #endif | 977 | #endif |
| 986 | #ifdef BN_MUL_COMBA | 978 | #ifdef BN_MUL_COMBA |
| 987 | if (i == 0) | 979 | if (i == 0) { |
| 988 | { | ||
| 989 | # if 0 | 980 | # if 0 |
| 990 | if (al == 4) | 981 | if (al == 4) { |
| 991 | { | 982 | if (bn_wexpand(rr, 8) == NULL) |
| 992 | if (bn_wexpand(rr,8) == NULL) goto err; | 983 | goto err; |
| 993 | rr->top=8; | 984 | rr->top = 8; |
| 994 | bn_mul_comba4(rr->d,a->d,b->d); | 985 | bn_mul_comba4(rr->d, a->d, b->d); |
| 995 | goto end; | 986 | goto end; |
| 996 | } | 987 | } |
| 997 | # endif | 988 | # endif |
| 998 | if (al == 8) | 989 | if (al == 8) { |
| 999 | { | 990 | if (bn_wexpand(rr, 16) == NULL) |
| 1000 | if (bn_wexpand(rr,16) == NULL) goto err; | 991 | goto err; |
| 1001 | rr->top=16; | 992 | rr->top = 16; |
| 1002 | bn_mul_comba8(rr->d,a->d,b->d); | 993 | bn_mul_comba8(rr->d, a->d, b->d); |
| 1003 | goto end; | 994 | goto end; |
| 1004 | } | ||
| 1005 | } | 995 | } |
| 996 | } | ||
| 1006 | #endif /* BN_MUL_COMBA */ | 997 | #endif /* BN_MUL_COMBA */ |
| 1007 | #ifdef BN_RECURSION | 998 | #ifdef BN_RECURSION |
| 1008 | if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) | 999 | if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) { |
| 1009 | { | 1000 | if (i >= -1 && i <= 1) { |
| 1010 | if (i >= -1 && i <= 1) | ||
| 1011 | { | ||
| 1012 | /* Find out the power of two lower or equal | 1001 | /* Find out the power of two lower or equal |
| 1013 | to the longest of the two numbers */ | 1002 | to the longest of the two numbers */ |
| 1014 | if (i >= 0) | 1003 | if (i >= 0) { |
| 1015 | { | ||
| 1016 | j = BN_num_bits_word((BN_ULONG)al); | 1004 | j = BN_num_bits_word((BN_ULONG)al); |
| 1017 | } | 1005 | } |
| 1018 | if (i == -1) | 1006 | if (i == -1) { |
| 1019 | { | ||
| 1020 | j = BN_num_bits_word((BN_ULONG)bl); | 1007 | j = BN_num_bits_word((BN_ULONG)bl); |
| 1021 | } | 1008 | } |
| 1022 | j = 1<<(j-1); | 1009 | j = 1 << (j - 1); |
| 1023 | assert(j <= al || j <= bl); | 1010 | assert(j <= al || j <= bl); |
| 1024 | k = j+j; | 1011 | k = j + j; |
| 1025 | t = BN_CTX_get(ctx); | 1012 | t = BN_CTX_get(ctx); |
| 1026 | if (t == NULL) | 1013 | if (t == NULL) |
| 1027 | goto err; | 1014 | goto err; |
| 1028 | if (al > j || bl > j) | 1015 | if (al > j || bl > j) { |
| 1029 | { | 1016 | if (bn_wexpand(t, k * 4) == NULL) |
| 1030 | if (bn_wexpand(t,k*4) == NULL) goto err; | 1017 | goto err; |
| 1031 | if (bn_wexpand(rr,k*4) == NULL) goto err; | 1018 | if (bn_wexpand(rr, k * 4) == NULL) |
| 1032 | bn_mul_part_recursive(rr->d,a->d,b->d, | 1019 | goto err; |
| 1033 | j,al-j,bl-j,t->d); | 1020 | bn_mul_part_recursive(rr->d, a->d, b->d, |
| 1034 | } | 1021 | j, al - j, bl - j, t->d); |
| 1022 | } | ||
| 1035 | else /* al <= j || bl <= j */ | 1023 | else /* al <= j || bl <= j */ |
| 1036 | { | 1024 | { |
| 1037 | if (bn_wexpand(t,k*2) == NULL) goto err; | 1025 | if (bn_wexpand(t, k * 2) == NULL) |
| 1038 | if (bn_wexpand(rr,k*2) == NULL) goto err; | 1026 | goto err; |
| 1039 | bn_mul_recursive(rr->d,a->d,b->d, | 1027 | if (bn_wexpand(rr, k * 2) == NULL) |
| 1040 | j,al-j,bl-j,t->d); | 1028 | goto err; |
| 1041 | } | 1029 | bn_mul_recursive(rr->d, a->d, b->d, |
| 1042 | rr->top=top; | 1030 | j, al - j, bl - j, t->d); |
| 1043 | goto end; | ||
| 1044 | } | 1031 | } |
| 1032 | rr->top = top; | ||
| 1033 | goto end; | ||
| 1034 | } | ||
| 1045 | #if 0 | 1035 | #if 0 |
| 1046 | if (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA)) | 1036 | if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) { |
| 1047 | { | ||
| 1048 | BIGNUM *tmp_bn = (BIGNUM *)b; | 1037 | BIGNUM *tmp_bn = (BIGNUM *)b; |
| 1049 | if (bn_wexpand(tmp_bn,al) == NULL) goto err; | 1038 | if (bn_wexpand(tmp_bn, al) == NULL) |
| 1050 | tmp_bn->d[bl]=0; | 1039 | goto err; |
| 1040 | tmp_bn->d[bl] = 0; | ||
| 1051 | bl++; | 1041 | bl++; |
| 1052 | i--; | 1042 | i--; |
| 1053 | } | 1043 | } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) { |
| 1054 | else if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA)) | ||
| 1055 | { | ||
| 1056 | BIGNUM *tmp_bn = (BIGNUM *)a; | 1044 | BIGNUM *tmp_bn = (BIGNUM *)a; |
| 1057 | if (bn_wexpand(tmp_bn,bl) == NULL) goto err; | 1045 | if (bn_wexpand(tmp_bn, bl) == NULL) |
| 1058 | tmp_bn->d[al]=0; | 1046 | goto err; |
| 1047 | tmp_bn->d[al] = 0; | ||
| 1059 | al++; | 1048 | al++; |
| 1060 | i++; | 1049 | i++; |
| 1061 | } | 1050 | } |
| 1062 | if (i == 0) | 1051 | if (i == 0) { |
| 1063 | { | ||
| 1064 | /* symmetric and > 4 */ | 1052 | /* symmetric and > 4 */ |
| 1065 | /* 16 or larger */ | 1053 | /* 16 or larger */ |
| 1066 | j=BN_num_bits_word((BN_ULONG)al); | 1054 | j = BN_num_bits_word((BN_ULONG)al); |
| 1067 | j=1<<(j-1); | 1055 | j = 1 << (j - 1); |
| 1068 | k=j+j; | 1056 | k = j + j; |
| 1069 | t = BN_CTX_get(ctx); | 1057 | t = BN_CTX_get(ctx); |
| 1070 | if (al == j) /* exact multiple */ | 1058 | if (al == j) /* exact multiple */ |
| 1071 | { | 1059 | { |
| 1072 | if (bn_wexpand(t,k*2) == NULL) goto err; | 1060 | if (bn_wexpand(t, k * 2) == NULL) |
| 1073 | if (bn_wexpand(rr,k*2) == NULL) goto err; | 1061 | goto err; |
| 1074 | bn_mul_recursive(rr->d,a->d,b->d,al,t->d); | 1062 | if (bn_wexpand(rr, k * 2) == NULL) |
| 1075 | } | 1063 | goto err; |
| 1076 | else | 1064 | bn_mul_recursive(rr->d, a->d, b->d, al, t->d); |
| 1077 | { | 1065 | } else { |
| 1078 | if (bn_wexpand(t,k*4) == NULL) goto err; | 1066 | if (bn_wexpand(t, k * 4) == NULL) |
| 1079 | if (bn_wexpand(rr,k*4) == NULL) goto err; | 1067 | goto err; |
| 1080 | bn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d); | 1068 | if (bn_wexpand(rr, k * 4) == NULL) |
| 1081 | } | 1069 | goto err; |
| 1082 | rr->top=top; | 1070 | bn_mul_part_recursive(rr->d, a->d, b->d, |
| 1083 | goto end; | 1071 | al - j, j, t->d); |
| 1084 | } | 1072 | } |
| 1085 | #endif | 1073 | rr->top = top; |
| 1074 | goto end; | ||
| 1086 | } | 1075 | } |
| 1076 | #endif | ||
| 1077 | } | ||
| 1087 | #endif /* BN_RECURSION */ | 1078 | #endif /* BN_RECURSION */ |
| 1088 | if (bn_wexpand(rr,top) == NULL) goto err; | 1079 | if (bn_wexpand(rr, top) == NULL) |
| 1089 | rr->top=top; | 1080 | goto err; |
| 1090 | bn_mul_normal(rr->d,a->d,al,b->d,bl); | 1081 | rr->top = top; |
| 1082 | bn_mul_normal(rr->d, a->d, al, b->d, bl); | ||
| 1091 | 1083 | ||
| 1092 | #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) | 1084 | #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) |
| 1093 | end: | 1085 | end: |
| 1094 | #endif | 1086 | #endif |
| 1095 | bn_correct_top(rr); | 1087 | bn_correct_top(rr); |
| 1096 | if (r != rr) BN_copy(r,rr); | 1088 | if (r != rr) |
| 1097 | ret=1; | 1089 | BN_copy(r, rr); |
| 1090 | ret = 1; | ||
| 1098 | err: | 1091 | err: |
| 1099 | bn_check_top(r); | 1092 | bn_check_top(r); |
| 1100 | BN_CTX_end(ctx); | 1093 | BN_CTX_end(ctx); |
| 1101 | return(ret); | 1094 | return (ret); |
| 1102 | } | 1095 | } |
| 1103 | 1096 | ||
| 1104 | void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb) | 1097 | void |
| 1105 | { | 1098 | bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb) |
| 1099 | { | ||
| 1106 | BN_ULONG *rr; | 1100 | BN_ULONG *rr; |
| 1107 | 1101 | ||
| 1108 | #ifdef BN_COUNT | 1102 | #ifdef BN_COUNT |
| 1109 | fprintf(stderr," bn_mul_normal %d * %d\n",na,nb); | 1103 | fprintf(stderr, " bn_mul_normal %d * %d\n", na, nb); |
| 1110 | #endif | 1104 | #endif |
| 1111 | 1105 | ||
| 1112 | if (na < nb) | 1106 | if (na < nb) { |
| 1113 | { | ||
| 1114 | int itmp; | 1107 | int itmp; |
| 1115 | BN_ULONG *ltmp; | 1108 | BN_ULONG *ltmp; |
| 1116 | 1109 | ||
| 1117 | itmp=na; na=nb; nb=itmp; | 1110 | itmp = na; |
| 1118 | ltmp=a; a=b; b=ltmp; | 1111 | na = nb; |
| 1112 | nb = itmp; | ||
| 1113 | ltmp = a; | ||
| 1114 | a = b; | ||
| 1115 | b = ltmp; | ||
| 1119 | 1116 | ||
| 1120 | } | 1117 | } |
| 1121 | rr= &(r[na]); | 1118 | rr = &(r[na]); |
| 1122 | if (nb <= 0) | 1119 | if (nb <= 0) { |
| 1123 | { | 1120 | (void)bn_mul_words(r, a, na, 0); |
| 1124 | (void)bn_mul_words(r,a,na,0); | ||
| 1125 | return; | 1121 | return; |
| 1126 | } | 1122 | } else |
| 1127 | else | 1123 | rr[0] = bn_mul_words(r, a, na, b[0]); |
| 1128 | rr[0]=bn_mul_words(r,a,na,b[0]); | 1124 | |
| 1129 | 1125 | for (;;) { | |
| 1130 | for (;;) | 1126 | if (--nb <= 0) |
| 1131 | { | 1127 | return; |
| 1132 | if (--nb <= 0) return; | 1128 | rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]); |
| 1133 | rr[1]=bn_mul_add_words(&(r[1]),a,na,b[1]); | 1129 | if (--nb <= 0) |
| 1134 | if (--nb <= 0) return; | 1130 | return; |
| 1135 | rr[2]=bn_mul_add_words(&(r[2]),a,na,b[2]); | 1131 | rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]); |
| 1136 | if (--nb <= 0) return; | 1132 | if (--nb <= 0) |
| 1137 | rr[3]=bn_mul_add_words(&(r[3]),a,na,b[3]); | 1133 | return; |
| 1138 | if (--nb <= 0) return; | 1134 | rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]); |
| 1139 | rr[4]=bn_mul_add_words(&(r[4]),a,na,b[4]); | 1135 | if (--nb <= 0) |
| 1140 | rr+=4; | 1136 | return; |
| 1141 | r+=4; | 1137 | rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]); |
| 1142 | b+=4; | 1138 | rr += 4; |
| 1143 | } | 1139 | r += 4; |
| 1140 | b += 4; | ||
| 1144 | } | 1141 | } |
| 1142 | } | ||
| 1145 | 1143 | ||
| 1146 | void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n) | 1144 | void |
| 1147 | { | 1145 | bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n) |
| 1146 | { | ||
| 1148 | #ifdef BN_COUNT | 1147 | #ifdef BN_COUNT |
| 1149 | fprintf(stderr," bn_mul_low_normal %d * %d\n",n,n); | 1148 | fprintf(stderr, " bn_mul_low_normal %d * %d\n", n, n); |
| 1150 | #endif | 1149 | #endif |
| 1151 | bn_mul_words(r,a,n,b[0]); | 1150 | bn_mul_words(r, a, n, b[0]); |
| 1152 | 1151 | ||
| 1153 | for (;;) | 1152 | for (;;) { |
| 1154 | { | 1153 | if (--n <= 0) |
| 1155 | if (--n <= 0) return; | 1154 | return; |
| 1156 | bn_mul_add_words(&(r[1]),a,n,b[1]); | 1155 | bn_mul_add_words(&(r[1]), a, n, b[1]); |
| 1157 | if (--n <= 0) return; | 1156 | if (--n <= 0) |
| 1158 | bn_mul_add_words(&(r[2]),a,n,b[2]); | 1157 | return; |
| 1159 | if (--n <= 0) return; | 1158 | bn_mul_add_words(&(r[2]), a, n, b[2]); |
| 1160 | bn_mul_add_words(&(r[3]),a,n,b[3]); | 1159 | if (--n <= 0) |
| 1161 | if (--n <= 0) return; | 1160 | return; |
| 1162 | bn_mul_add_words(&(r[4]),a,n,b[4]); | 1161 | bn_mul_add_words(&(r[3]), a, n, b[3]); |
| 1163 | r+=4; | 1162 | if (--n <= 0) |
| 1164 | b+=4; | 1163 | return; |
| 1165 | } | 1164 | bn_mul_add_words(&(r[4]), a, n, b[4]); |
| 1165 | r += 4; | ||
| 1166 | b += 4; | ||
| 1166 | } | 1167 | } |
| 1168 | } | ||
