VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.7/crypto/bn/bn_asm.c@ 107835

Last change on this file since 107835 was 104078, checked in by vboxsync, 11 months ago

openssl-3.1.5: Applied and adjusted our OpenSSL changes to 3.1.4. bugref:10638

File size: 27.3 KB
Line 
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#include <assert.h>
11#include <openssl/crypto.h>
12#include "internal/cryptlib.h"
13#include "bn_local.h"
14
15#if defined(BN_LLONG) || defined(BN_UMULT_HIGH)
16
17BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
18 BN_ULONG w)
19{
20 BN_ULONG c1 = 0;
21
22 assert(num >= 0);
23 if (num <= 0)
24 return c1;
25
26# ifndef OPENSSL_SMALL_FOOTPRINT
27 while (num & ~3) {
28 mul_add(rp[0], ap[0], w, c1);
29 mul_add(rp[1], ap[1], w, c1);
30 mul_add(rp[2], ap[2], w, c1);
31 mul_add(rp[3], ap[3], w, c1);
32 ap += 4;
33 rp += 4;
34 num -= 4;
35 }
36# endif
37 while (num) {
38 mul_add(rp[0], ap[0], w, c1);
39 ap++;
40 rp++;
41 num--;
42 }
43
44 return c1;
45}
46
47BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
48{
49 BN_ULONG c1 = 0;
50
51 assert(num >= 0);
52 if (num <= 0)
53 return c1;
54
55# ifndef OPENSSL_SMALL_FOOTPRINT
56 while (num & ~3) {
57 mul(rp[0], ap[0], w, c1);
58 mul(rp[1], ap[1], w, c1);
59 mul(rp[2], ap[2], w, c1);
60 mul(rp[3], ap[3], w, c1);
61 ap += 4;
62 rp += 4;
63 num -= 4;
64 }
65# endif
66 while (num) {
67 mul(rp[0], ap[0], w, c1);
68 ap++;
69 rp++;
70 num--;
71 }
72 return c1;
73}
74
75void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
76{
77 assert(n >= 0);
78 if (n <= 0)
79 return;
80
81# ifndef OPENSSL_SMALL_FOOTPRINT
82 while (n & ~3) {
83 sqr(r[0], r[1], a[0]);
84 sqr(r[2], r[3], a[1]);
85 sqr(r[4], r[5], a[2]);
86 sqr(r[6], r[7], a[3]);
87 a += 4;
88 r += 8;
89 n -= 4;
90 }
91# endif
92 while (n) {
93 sqr(r[0], r[1], a[0]);
94 a++;
95 r += 2;
96 n--;
97 }
98}
99
100#else /* !(defined(BN_LLONG) ||
101 * defined(BN_UMULT_HIGH)) */
102
103BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
104 BN_ULONG w)
105{
106 BN_ULONG c = 0;
107 BN_ULONG bl, bh;
108
109 assert(num >= 0);
110 if (num <= 0)
111 return (BN_ULONG)0;
112
113 bl = LBITS(w);
114 bh = HBITS(w);
115
116# ifndef OPENSSL_SMALL_FOOTPRINT
117 while (num & ~3) {
118 mul_add(rp[0], ap[0], bl, bh, c);
119 mul_add(rp[1], ap[1], bl, bh, c);
120 mul_add(rp[2], ap[2], bl, bh, c);
121 mul_add(rp[3], ap[3], bl, bh, c);
122 ap += 4;
123 rp += 4;
124 num -= 4;
125 }
126# endif
127 while (num) {
128 mul_add(rp[0], ap[0], bl, bh, c);
129 ap++;
130 rp++;
131 num--;
132 }
133 return c;
134}
135
136BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
137{
138 BN_ULONG carry = 0;
139 BN_ULONG bl, bh;
140
141 assert(num >= 0);
142 if (num <= 0)
143 return (BN_ULONG)0;
144
145 bl = LBITS(w);
146 bh = HBITS(w);
147
148# ifndef OPENSSL_SMALL_FOOTPRINT
149 while (num & ~3) {
150 mul(rp[0], ap[0], bl, bh, carry);
151 mul(rp[1], ap[1], bl, bh, carry);
152 mul(rp[2], ap[2], bl, bh, carry);
153 mul(rp[3], ap[3], bl, bh, carry);
154 ap += 4;
155 rp += 4;
156 num -= 4;
157 }
158# endif
159 while (num) {
160 mul(rp[0], ap[0], bl, bh, carry);
161 ap++;
162 rp++;
163 num--;
164 }
165 return carry;
166}
167
168void bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
169{
170 assert(n >= 0);
171 if (n <= 0)
172 return;
173
174# ifndef OPENSSL_SMALL_FOOTPRINT
175 while (n & ~3) {
176 sqr64(r[0], r[1], a[0]);
177 sqr64(r[2], r[3], a[1]);
178 sqr64(r[4], r[5], a[2]);
179 sqr64(r[6], r[7], a[3]);
180 a += 4;
181 r += 8;
182 n -= 4;
183 }
184# endif
185 while (n) {
186 sqr64(r[0], r[1], a[0]);
187 a++;
188 r += 2;
189 n--;
190 }
191}
192
193#endif /* !(defined(BN_LLONG) ||
194 * defined(BN_UMULT_HIGH)) */
195
196#if defined(BN_LLONG) && defined(BN_DIV2W)
197
198BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
199{
200 return ((BN_ULONG)(((((BN_ULLONG) h) << BN_BITS2) | l) / (BN_ULLONG) d));
201}
202
203#else
204
205/* Divide h,l by d and return the result. */
206/* I need to test this some more :-( */
207BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
208{
209 BN_ULONG dh, dl, q, ret = 0, th, tl, t;
210 int i, count = 2;
211
212 if (d == 0)
213 return BN_MASK2;
214
215 i = BN_num_bits_word(d);
216 assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i));
217
218 i = BN_BITS2 - i;
219 if (h >= d)
220 h -= d;
221
222 if (i) {
223 d <<= i;
224 h = (h << i) | (l >> (BN_BITS2 - i));
225 l <<= i;
226 }
227 dh = (d & BN_MASK2h) >> BN_BITS4;
228 dl = (d & BN_MASK2l);
229 for (;;) {
230 if ((h >> BN_BITS4) == dh)
231 q = BN_MASK2l;
232 else
233 q = h / dh;
234
235 th = q * dh;
236 tl = dl * q;
237 for (;;) {
238 t = h - th;
239 if ((t & BN_MASK2h) ||
240 ((tl) <= ((t << BN_BITS4) | ((l & BN_MASK2h) >> BN_BITS4))))
241 break;
242 q--;
243 th -= dh;
244 tl -= dl;
245 }
246 t = (tl >> BN_BITS4);
247 tl = (tl << BN_BITS4) & BN_MASK2h;
248 th += t;
249
250 if (l < tl)
251 th++;
252 l -= tl;
253 if (h < th) {
254 h += d;
255 q--;
256 }
257 h -= th;
258
259 if (--count == 0)
260 break;
261
262 ret = q << BN_BITS4;
263 h = ((h << BN_BITS4) | (l >> BN_BITS4)) & BN_MASK2;
264 l = (l & BN_MASK2l) << BN_BITS4;
265 }
266 ret |= q;
267 return ret;
268}
269#endif /* !defined(BN_LLONG) && defined(BN_DIV2W) */
270
271#ifdef BN_LLONG
272BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
273 int n)
274{
275 BN_ULLONG ll = 0;
276
277 assert(n >= 0);
278 if (n <= 0)
279 return (BN_ULONG)0;
280
281# ifndef OPENSSL_SMALL_FOOTPRINT
282 while (n & ~3) {
283 ll += (BN_ULLONG) a[0] + b[0];
284 r[0] = (BN_ULONG)ll & BN_MASK2;
285 ll >>= BN_BITS2;
286 ll += (BN_ULLONG) a[1] + b[1];
287 r[1] = (BN_ULONG)ll & BN_MASK2;
288 ll >>= BN_BITS2;
289 ll += (BN_ULLONG) a[2] + b[2];
290 r[2] = (BN_ULONG)ll & BN_MASK2;
291 ll >>= BN_BITS2;
292 ll += (BN_ULLONG) a[3] + b[3];
293 r[3] = (BN_ULONG)ll & BN_MASK2;
294 ll >>= BN_BITS2;
295 a += 4;
296 b += 4;
297 r += 4;
298 n -= 4;
299 }
300# endif
301 while (n) {
302 ll += (BN_ULLONG) a[0] + b[0];
303 r[0] = (BN_ULONG)ll & BN_MASK2;
304 ll >>= BN_BITS2;
305 a++;
306 b++;
307 r++;
308 n--;
309 }
310 return (BN_ULONG)ll;
311}
312#else /* !BN_LLONG */
313BN_ULONG bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
314 int n)
315{
316 BN_ULONG c, l, t;
317
318 assert(n >= 0);
319 if (n <= 0)
320 return (BN_ULONG)0;
321
322 c = 0;
323# ifndef OPENSSL_SMALL_FOOTPRINT
324 while (n & ~3) {
325 t = a[0];
326 t = (t + c) & BN_MASK2;
327 c = (t < c);
328 l = (t + b[0]) & BN_MASK2;
329 c += (l < t);
330 r[0] = l;
331 t = a[1];
332 t = (t + c) & BN_MASK2;
333 c = (t < c);
334 l = (t + b[1]) & BN_MASK2;
335 c += (l < t);
336 r[1] = l;
337 t = a[2];
338 t = (t + c) & BN_MASK2;
339 c = (t < c);
340 l = (t + b[2]) & BN_MASK2;
341 c += (l < t);
342 r[2] = l;
343 t = a[3];
344 t = (t + c) & BN_MASK2;
345 c = (t < c);
346 l = (t + b[3]) & BN_MASK2;
347 c += (l < t);
348 r[3] = l;
349 a += 4;
350 b += 4;
351 r += 4;
352 n -= 4;
353 }
354# endif
355 while (n) {
356 t = a[0];
357 t = (t + c) & BN_MASK2;
358 c = (t < c);
359 l = (t + b[0]) & BN_MASK2;
360 c += (l < t);
361 r[0] = l;
362 a++;
363 b++;
364 r++;
365 n--;
366 }
367 return (BN_ULONG)c;
368}
369#endif /* !BN_LLONG */
370
371BN_ULONG bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
372 int n)
373{
374 BN_ULONG t1, t2;
375 int c = 0;
376
377 assert(n >= 0);
378 if (n <= 0)
379 return (BN_ULONG)0;
380
381#ifndef OPENSSL_SMALL_FOOTPRINT
382 while (n & ~3) {
383 t1 = a[0];
384 t2 = (t1 - c) & BN_MASK2;
385 c = (t2 > t1);
386 t1 = b[0];
387 t1 = (t2 - t1) & BN_MASK2;
388 r[0] = t1;
389 c += (t1 > t2);
390 t1 = a[1];
391 t2 = (t1 - c) & BN_MASK2;
392 c = (t2 > t1);
393 t1 = b[1];
394 t1 = (t2 - t1) & BN_MASK2;
395 r[1] = t1;
396 c += (t1 > t2);
397 t1 = a[2];
398 t2 = (t1 - c) & BN_MASK2;
399 c = (t2 > t1);
400 t1 = b[2];
401 t1 = (t2 - t1) & BN_MASK2;
402 r[2] = t1;
403 c += (t1 > t2);
404 t1 = a[3];
405 t2 = (t1 - c) & BN_MASK2;
406 c = (t2 > t1);
407 t1 = b[3];
408 t1 = (t2 - t1) & BN_MASK2;
409 r[3] = t1;
410 c += (t1 > t2);
411 a += 4;
412 b += 4;
413 r += 4;
414 n -= 4;
415 }
416#endif
417 while (n) {
418 t1 = a[0];
419 t2 = (t1 - c) & BN_MASK2;
420 c = (t2 > t1);
421 t1 = b[0];
422 t1 = (t2 - t1) & BN_MASK2;
423 r[0] = t1;
424 c += (t1 > t2);
425 a++;
426 b++;
427 r++;
428 n--;
429 }
430 return c;
431}
432
433#if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT)
434
435#ifndef ___openssl_mangling_h___ /* bird */
436# undef bn_mul_comba8
437# undef bn_mul_comba4
438# undef bn_sqr_comba8
439# undef bn_sqr_comba4
440#endif /* !___openssl_mangling_h___*/ /* bird */
441
442/* mul_add_c(a,b,c0,c1,c2) -- c+=a*b for three word number c=(c2,c1,c0) */
443/* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */
444/* sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
445/*
446 * sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number
447 * c=(c2,c1,c0)
448 */
449
450# ifdef BN_LLONG
451/*
452 * Keep in mind that additions to multiplication result can not
453 * overflow, because its high half cannot be all-ones.
454 */
455# define mul_add_c(a,b,c0,c1,c2) do { \
456 BN_ULONG hi; \
457 BN_ULLONG t = (BN_ULLONG)(a)*(b); \
458 t += c0; /* no carry */ \
459 c0 = (BN_ULONG)Lw(t); \
460 hi = (BN_ULONG)Hw(t); \
461 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
462 } while(0)
463
464# define mul_add_c2(a,b,c0,c1,c2) do { \
465 BN_ULONG hi; \
466 BN_ULLONG t = (BN_ULLONG)(a)*(b); \
467 BN_ULLONG tt = t+c0; /* no carry */ \
468 c0 = (BN_ULONG)Lw(tt); \
469 hi = (BN_ULONG)Hw(tt); \
470 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
471 t += c0; /* no carry */ \
472 c0 = (BN_ULONG)Lw(t); \
473 hi = (BN_ULONG)Hw(t); \
474 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
475 } while(0)
476
477# define sqr_add_c(a,i,c0,c1,c2) do { \
478 BN_ULONG hi; \
479 BN_ULLONG t = (BN_ULLONG)a[i]*a[i]; \
480 t += c0; /* no carry */ \
481 c0 = (BN_ULONG)Lw(t); \
482 hi = (BN_ULONG)Hw(t); \
483 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
484 } while(0)
485
486# define sqr_add_c2(a,i,j,c0,c1,c2) \
487 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
488
489# elif defined(BN_UMULT_LOHI)
490/*
491 * Keep in mind that additions to hi can not overflow, because
492 * the high word of a multiplication result cannot be all-ones.
493 */
494# define mul_add_c(a,b,c0,c1,c2) do { \
495 BN_ULONG ta = (a), tb = (b); \
496 BN_ULONG lo, hi; \
497 BN_UMULT_LOHI(lo,hi,ta,tb); \
498 c0 += lo; hi += (c0<lo); \
499 c1 += hi; c2 += (c1<hi); \
500 } while(0)
501
502# define mul_add_c2(a,b,c0,c1,c2) do { \
503 BN_ULONG ta = (a), tb = (b); \
504 BN_ULONG lo, hi, tt; \
505 BN_UMULT_LOHI(lo,hi,ta,tb); \
506 c0 += lo; tt = hi + (c0<lo); \
507 c1 += tt; c2 += (c1<tt); \
508 c0 += lo; hi += (c0<lo); \
509 c1 += hi; c2 += (c1<hi); \
510 } while(0)
511
512# define sqr_add_c(a,i,c0,c1,c2) do { \
513 BN_ULONG ta = (a)[i]; \
514 BN_ULONG lo, hi; \
515 BN_UMULT_LOHI(lo,hi,ta,ta); \
516 c0 += lo; hi += (c0<lo); \
517 c1 += hi; c2 += (c1<hi); \
518 } while(0)
519
520# define sqr_add_c2(a,i,j,c0,c1,c2) \
521 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
522
523# elif defined(BN_UMULT_HIGH)
524/*
525 * Keep in mind that additions to hi can not overflow, because
526 * the high word of a multiplication result cannot be all-ones.
527 */
528# define mul_add_c(a,b,c0,c1,c2) do { \
529 BN_ULONG ta = (a), tb = (b); \
530 BN_ULONG lo = ta * tb; \
531 BN_ULONG hi = BN_UMULT_HIGH(ta,tb); \
532 c0 += lo; hi += (c0<lo); \
533 c1 += hi; c2 += (c1<hi); \
534 } while(0)
535
536# define mul_add_c2(a,b,c0,c1,c2) do { \
537 BN_ULONG ta = (a), tb = (b), tt; \
538 BN_ULONG lo = ta * tb; \
539 BN_ULONG hi = BN_UMULT_HIGH(ta,tb); \
540 c0 += lo; tt = hi + (c0<lo); \
541 c1 += tt; c2 += (c1<tt); \
542 c0 += lo; hi += (c0<lo); \
543 c1 += hi; c2 += (c1<hi); \
544 } while(0)
545
546# define sqr_add_c(a,i,c0,c1,c2) do { \
547 BN_ULONG ta = (a)[i]; \
548 BN_ULONG lo = ta * ta; \
549 BN_ULONG hi = BN_UMULT_HIGH(ta,ta); \
550 c0 += lo; hi += (c0<lo); \
551 c1 += hi; c2 += (c1<hi); \
552 } while(0)
553
554# define sqr_add_c2(a,i,j,c0,c1,c2) \
555 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
556
557# else /* !BN_LLONG */
558/*
559 * Keep in mind that additions to hi can not overflow, because
560 * the high word of a multiplication result cannot be all-ones.
561 */
562# define mul_add_c(a,b,c0,c1,c2) do { \
563 BN_ULONG lo = LBITS(a), hi = HBITS(a); \
564 BN_ULONG bl = LBITS(b), bh = HBITS(b); \
565 mul64(lo,hi,bl,bh); \
566 c0 = (c0+lo)&BN_MASK2; hi += (c0<lo); \
567 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
568 } while(0)
569
570# define mul_add_c2(a,b,c0,c1,c2) do { \
571 BN_ULONG tt; \
572 BN_ULONG lo = LBITS(a), hi = HBITS(a); \
573 BN_ULONG bl = LBITS(b), bh = HBITS(b); \
574 mul64(lo,hi,bl,bh); \
575 tt = hi; \
576 c0 = (c0+lo)&BN_MASK2; tt += (c0<lo); \
577 c1 = (c1+tt)&BN_MASK2; c2 += (c1<tt); \
578 c0 = (c0+lo)&BN_MASK2; hi += (c0<lo); \
579 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
580 } while(0)
581
582# define sqr_add_c(a,i,c0,c1,c2) do { \
583 BN_ULONG lo, hi; \
584 sqr64(lo,hi,(a)[i]); \
585 c0 = (c0+lo)&BN_MASK2; hi += (c0<lo); \
586 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
587 } while(0)
588
589# define sqr_add_c2(a,i,j,c0,c1,c2) \
590 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
591# endif /* !BN_LLONG */
592
593void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
594{
595 BN_ULONG c1, c2, c3;
596
597 c1 = 0;
598 c2 = 0;
599 c3 = 0;
600 mul_add_c(a[0], b[0], c1, c2, c3);
601 r[0] = c1;
602 c1 = 0;
603 mul_add_c(a[0], b[1], c2, c3, c1);
604 mul_add_c(a[1], b[0], c2, c3, c1);
605 r[1] = c2;
606 c2 = 0;
607 mul_add_c(a[2], b[0], c3, c1, c2);
608 mul_add_c(a[1], b[1], c3, c1, c2);
609 mul_add_c(a[0], b[2], c3, c1, c2);
610 r[2] = c3;
611 c3 = 0;
612 mul_add_c(a[0], b[3], c1, c2, c3);
613 mul_add_c(a[1], b[2], c1, c2, c3);
614 mul_add_c(a[2], b[1], c1, c2, c3);
615 mul_add_c(a[3], b[0], c1, c2, c3);
616 r[3] = c1;
617 c1 = 0;
618 mul_add_c(a[4], b[0], c2, c3, c1);
619 mul_add_c(a[3], b[1], c2, c3, c1);
620 mul_add_c(a[2], b[2], c2, c3, c1);
621 mul_add_c(a[1], b[3], c2, c3, c1);
622 mul_add_c(a[0], b[4], c2, c3, c1);
623 r[4] = c2;
624 c2 = 0;
625 mul_add_c(a[0], b[5], c3, c1, c2);
626 mul_add_c(a[1], b[4], c3, c1, c2);
627 mul_add_c(a[2], b[3], c3, c1, c2);
628 mul_add_c(a[3], b[2], c3, c1, c2);
629 mul_add_c(a[4], b[1], c3, c1, c2);
630 mul_add_c(a[5], b[0], c3, c1, c2);
631 r[5] = c3;
632 c3 = 0;
633 mul_add_c(a[6], b[0], c1, c2, c3);
634 mul_add_c(a[5], b[1], c1, c2, c3);
635 mul_add_c(a[4], b[2], c1, c2, c3);
636 mul_add_c(a[3], b[3], c1, c2, c3);
637 mul_add_c(a[2], b[4], c1, c2, c3);
638 mul_add_c(a[1], b[5], c1, c2, c3);
639 mul_add_c(a[0], b[6], c1, c2, c3);
640 r[6] = c1;
641 c1 = 0;
642 mul_add_c(a[0], b[7], c2, c3, c1);
643 mul_add_c(a[1], b[6], c2, c3, c1);
644 mul_add_c(a[2], b[5], c2, c3, c1);
645 mul_add_c(a[3], b[4], c2, c3, c1);
646 mul_add_c(a[4], b[3], c2, c3, c1);
647 mul_add_c(a[5], b[2], c2, c3, c1);
648 mul_add_c(a[6], b[1], c2, c3, c1);
649 mul_add_c(a[7], b[0], c2, c3, c1);
650 r[7] = c2;
651 c2 = 0;
652 mul_add_c(a[7], b[1], c3, c1, c2);
653 mul_add_c(a[6], b[2], c3, c1, c2);
654 mul_add_c(a[5], b[3], c3, c1, c2);
655 mul_add_c(a[4], b[4], c3, c1, c2);
656 mul_add_c(a[3], b[5], c3, c1, c2);
657 mul_add_c(a[2], b[6], c3, c1, c2);
658 mul_add_c(a[1], b[7], c3, c1, c2);
659 r[8] = c3;
660 c3 = 0;
661 mul_add_c(a[2], b[7], c1, c2, c3);
662 mul_add_c(a[3], b[6], c1, c2, c3);
663 mul_add_c(a[4], b[5], c1, c2, c3);
664 mul_add_c(a[5], b[4], c1, c2, c3);
665 mul_add_c(a[6], b[3], c1, c2, c3);
666 mul_add_c(a[7], b[2], c1, c2, c3);
667 r[9] = c1;
668 c1 = 0;
669 mul_add_c(a[7], b[3], c2, c3, c1);
670 mul_add_c(a[6], b[4], c2, c3, c1);
671 mul_add_c(a[5], b[5], c2, c3, c1);
672 mul_add_c(a[4], b[6], c2, c3, c1);
673 mul_add_c(a[3], b[7], c2, c3, c1);
674 r[10] = c2;
675 c2 = 0;
676 mul_add_c(a[4], b[7], c3, c1, c2);
677 mul_add_c(a[5], b[6], c3, c1, c2);
678 mul_add_c(a[6], b[5], c3, c1, c2);
679 mul_add_c(a[7], b[4], c3, c1, c2);
680 r[11] = c3;
681 c3 = 0;
682 mul_add_c(a[7], b[5], c1, c2, c3);
683 mul_add_c(a[6], b[6], c1, c2, c3);
684 mul_add_c(a[5], b[7], c1, c2, c3);
685 r[12] = c1;
686 c1 = 0;
687 mul_add_c(a[6], b[7], c2, c3, c1);
688 mul_add_c(a[7], b[6], c2, c3, c1);
689 r[13] = c2;
690 c2 = 0;
691 mul_add_c(a[7], b[7], c3, c1, c2);
692 r[14] = c3;
693 r[15] = c1;
694}
695
696void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
697{
698 BN_ULONG c1, c2, c3;
699
700 c1 = 0;
701 c2 = 0;
702 c3 = 0;
703 mul_add_c(a[0], b[0], c1, c2, c3);
704 r[0] = c1;
705 c1 = 0;
706 mul_add_c(a[0], b[1], c2, c3, c1);
707 mul_add_c(a[1], b[0], c2, c3, c1);
708 r[1] = c2;
709 c2 = 0;
710 mul_add_c(a[2], b[0], c3, c1, c2);
711 mul_add_c(a[1], b[1], c3, c1, c2);
712 mul_add_c(a[0], b[2], c3, c1, c2);
713 r[2] = c3;
714 c3 = 0;
715 mul_add_c(a[0], b[3], c1, c2, c3);
716 mul_add_c(a[1], b[2], c1, c2, c3);
717 mul_add_c(a[2], b[1], c1, c2, c3);
718 mul_add_c(a[3], b[0], c1, c2, c3);
719 r[3] = c1;
720 c1 = 0;
721 mul_add_c(a[3], b[1], c2, c3, c1);
722 mul_add_c(a[2], b[2], c2, c3, c1);
723 mul_add_c(a[1], b[3], c2, c3, c1);
724 r[4] = c2;
725 c2 = 0;
726 mul_add_c(a[2], b[3], c3, c1, c2);
727 mul_add_c(a[3], b[2], c3, c1, c2);
728 r[5] = c3;
729 c3 = 0;
730 mul_add_c(a[3], b[3], c1, c2, c3);
731 r[6] = c1;
732 r[7] = c2;
733}
734
735void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
736{
737 BN_ULONG c1, c2, c3;
738
739 c1 = 0;
740 c2 = 0;
741 c3 = 0;
742 sqr_add_c(a, 0, c1, c2, c3);
743 r[0] = c1;
744 c1 = 0;
745 sqr_add_c2(a, 1, 0, c2, c3, c1);
746 r[1] = c2;
747 c2 = 0;
748 sqr_add_c(a, 1, c3, c1, c2);
749 sqr_add_c2(a, 2, 0, c3, c1, c2);
750 r[2] = c3;
751 c3 = 0;
752 sqr_add_c2(a, 3, 0, c1, c2, c3);
753 sqr_add_c2(a, 2, 1, c1, c2, c3);
754 r[3] = c1;
755 c1 = 0;
756 sqr_add_c(a, 2, c2, c3, c1);
757 sqr_add_c2(a, 3, 1, c2, c3, c1);
758 sqr_add_c2(a, 4, 0, c2, c3, c1);
759 r[4] = c2;
760 c2 = 0;
761 sqr_add_c2(a, 5, 0, c3, c1, c2);
762 sqr_add_c2(a, 4, 1, c3, c1, c2);
763 sqr_add_c2(a, 3, 2, c3, c1, c2);
764 r[5] = c3;
765 c3 = 0;
766 sqr_add_c(a, 3, c1, c2, c3);
767 sqr_add_c2(a, 4, 2, c1, c2, c3);
768 sqr_add_c2(a, 5, 1, c1, c2, c3);
769 sqr_add_c2(a, 6, 0, c1, c2, c3);
770 r[6] = c1;
771 c1 = 0;
772 sqr_add_c2(a, 7, 0, c2, c3, c1);
773 sqr_add_c2(a, 6, 1, c2, c3, c1);
774 sqr_add_c2(a, 5, 2, c2, c3, c1);
775 sqr_add_c2(a, 4, 3, c2, c3, c1);
776 r[7] = c2;
777 c2 = 0;
778 sqr_add_c(a, 4, c3, c1, c2);
779 sqr_add_c2(a, 5, 3, c3, c1, c2);
780 sqr_add_c2(a, 6, 2, c3, c1, c2);
781 sqr_add_c2(a, 7, 1, c3, c1, c2);
782 r[8] = c3;
783 c3 = 0;
784 sqr_add_c2(a, 7, 2, c1, c2, c3);
785 sqr_add_c2(a, 6, 3, c1, c2, c3);
786 sqr_add_c2(a, 5, 4, c1, c2, c3);
787 r[9] = c1;
788 c1 = 0;
789 sqr_add_c(a, 5, c2, c3, c1);
790 sqr_add_c2(a, 6, 4, c2, c3, c1);
791 sqr_add_c2(a, 7, 3, c2, c3, c1);
792 r[10] = c2;
793 c2 = 0;
794 sqr_add_c2(a, 7, 4, c3, c1, c2);
795 sqr_add_c2(a, 6, 5, c3, c1, c2);
796 r[11] = c3;
797 c3 = 0;
798 sqr_add_c(a, 6, c1, c2, c3);
799 sqr_add_c2(a, 7, 5, c1, c2, c3);
800 r[12] = c1;
801 c1 = 0;
802 sqr_add_c2(a, 7, 6, c2, c3, c1);
803 r[13] = c2;
804 c2 = 0;
805 sqr_add_c(a, 7, c3, c1, c2);
806 r[14] = c3;
807 r[15] = c1;
808}
809
810void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
811{
812 BN_ULONG c1, c2, c3;
813
814 c1 = 0;
815 c2 = 0;
816 c3 = 0;
817 sqr_add_c(a, 0, c1, c2, c3);
818 r[0] = c1;
819 c1 = 0;
820 sqr_add_c2(a, 1, 0, c2, c3, c1);
821 r[1] = c2;
822 c2 = 0;
823 sqr_add_c(a, 1, c3, c1, c2);
824 sqr_add_c2(a, 2, 0, c3, c1, c2);
825 r[2] = c3;
826 c3 = 0;
827 sqr_add_c2(a, 3, 0, c1, c2, c3);
828 sqr_add_c2(a, 2, 1, c1, c2, c3);
829 r[3] = c1;
830 c1 = 0;
831 sqr_add_c(a, 2, c2, c3, c1);
832 sqr_add_c2(a, 3, 1, c2, c3, c1);
833 r[4] = c2;
834 c2 = 0;
835 sqr_add_c2(a, 3, 2, c3, c1, c2);
836 r[5] = c3;
837 c3 = 0;
838 sqr_add_c(a, 3, c1, c2, c3);
839 r[6] = c1;
840 r[7] = c2;
841}
842
843# ifdef OPENSSL_NO_ASM
844# ifdef OPENSSL_BN_ASM_MONT
845# include <alloca.h>
846/*
847 * This is essentially reference implementation, which may or may not
848 * result in performance improvement. E.g. on IA-32 this routine was
849 * observed to give 40% faster rsa1024 private key operations and 10%
850 * faster rsa4096 ones, while on AMD64 it improves rsa1024 sign only
851 * by 10% and *worsens* rsa4096 sign by 15%. Once again, it's a
852 * reference implementation, one to be used as starting point for
853 * platform-specific assembler. Mentioned numbers apply to compiler
854 * generated code compiled with and without -DOPENSSL_BN_ASM_MONT and
855 * can vary not only from platform to platform, but even for compiler
856 * versions. Assembler vs. assembler improvement coefficients can
857 * [and are known to] differ and are to be documented elsewhere.
858 */
859int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
860 const BN_ULONG *np, const BN_ULONG *n0p, int num)
861{
862 BN_ULONG c0, c1, ml, *tp, n0;
863# ifdef mul64
864 BN_ULONG mh;
865# endif
866 volatile BN_ULONG *vp;
867 int i = 0, j;
868
869# if 0 /* template for platform-specific
870 * implementation */
871 if (ap == bp)
872 return bn_sqr_mont(rp, ap, np, n0p, num);
873# endif
874 vp = tp = alloca((num + 2) * sizeof(BN_ULONG));
875
876 n0 = *n0p;
877
878 c0 = 0;
879 ml = bp[0];
880# ifdef mul64
881 mh = HBITS(ml);
882 ml = LBITS(ml);
883 for (j = 0; j < num; ++j)
884 mul(tp[j], ap[j], ml, mh, c0);
885# else
886 for (j = 0; j < num; ++j)
887 mul(tp[j], ap[j], ml, c0);
888# endif
889
890 tp[num] = c0;
891 tp[num + 1] = 0;
892 goto enter;
893
894 for (i = 0; i < num; i++) {
895 c0 = 0;
896 ml = bp[i];
897# ifdef mul64
898 mh = HBITS(ml);
899 ml = LBITS(ml);
900 for (j = 0; j < num; ++j)
901 mul_add(tp[j], ap[j], ml, mh, c0);
902# else
903 for (j = 0; j < num; ++j)
904 mul_add(tp[j], ap[j], ml, c0);
905# endif
906 c1 = (tp[num] + c0) & BN_MASK2;
907 tp[num] = c1;
908 tp[num + 1] = (c1 < c0 ? 1 : 0);
909 enter:
910 c1 = tp[0];
911 ml = (c1 * n0) & BN_MASK2;
912 c0 = 0;
913# ifdef mul64
914 mh = HBITS(ml);
915 ml = LBITS(ml);
916 mul_add(c1, np[0], ml, mh, c0);
917# else
918 mul_add(c1, ml, np[0], c0);
919# endif
920 for (j = 1; j < num; j++) {
921 c1 = tp[j];
922# ifdef mul64
923 mul_add(c1, np[j], ml, mh, c0);
924# else
925 mul_add(c1, ml, np[j], c0);
926# endif
927 tp[j - 1] = c1 & BN_MASK2;
928 }
929 c1 = (tp[num] + c0) & BN_MASK2;
930 tp[num - 1] = c1;
931 tp[num] = tp[num + 1] + (c1 < c0 ? 1 : 0);
932 }
933
934 if (tp[num] != 0 || tp[num - 1] >= np[num - 1]) {
935 c0 = bn_sub_words(rp, tp, np, num);
936 if (tp[num] != 0 || c0 == 0) {
937 for (i = 0; i < num + 2; i++)
938 vp[i] = 0;
939 return 1;
940 }
941 }
942 for (i = 0; i < num; i++)
943 rp[i] = tp[i], vp[i] = 0;
944 vp[num] = 0;
945 vp[num + 1] = 0;
946 return 1;
947}
948# else
949/*
950 * Return value of 0 indicates that multiplication/convolution was not
951 * performed to signal the caller to fall down to alternative/original
952 * code-path.
953 */
954int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
955 const BN_ULONG *np, const BN_ULONG *n0, int num)
956{
957 return 0;
958}
959# endif /* OPENSSL_BN_ASM_MONT */
960# endif
961
962#else /* !BN_MUL_COMBA */
963
964/* hmm... is it faster just to do a multiply? */
965#ifndef ___openssl_mangling_h___ /* bird */
966# undef bn_sqr_comba4
967# undef bn_sqr_comba8
968#endif
969void bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
970{
971 BN_ULONG t[8];
972 bn_sqr_normal(r, a, 4, t);
973}
974
975void bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
976{
977 BN_ULONG t[16];
978 bn_sqr_normal(r, a, 8, t);
979}
980
981void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
982{
983 r[4] = bn_mul_words(&(r[0]), a, 4, b[0]);
984 r[5] = bn_mul_add_words(&(r[1]), a, 4, b[1]);
985 r[6] = bn_mul_add_words(&(r[2]), a, 4, b[2]);
986 r[7] = bn_mul_add_words(&(r[3]), a, 4, b[3]);
987}
988
989void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
990{
991 r[8] = bn_mul_words(&(r[0]), a, 8, b[0]);
992 r[9] = bn_mul_add_words(&(r[1]), a, 8, b[1]);
993 r[10] = bn_mul_add_words(&(r[2]), a, 8, b[2]);
994 r[11] = bn_mul_add_words(&(r[3]), a, 8, b[3]);
995 r[12] = bn_mul_add_words(&(r[4]), a, 8, b[4]);
996 r[13] = bn_mul_add_words(&(r[5]), a, 8, b[5]);
997 r[14] = bn_mul_add_words(&(r[6]), a, 8, b[6]);
998 r[15] = bn_mul_add_words(&(r[7]), a, 8, b[7]);
999}
1000
1001# ifdef OPENSSL_NO_ASM
1002# ifdef OPENSSL_BN_ASM_MONT
1003# include <alloca.h>
1004int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
1005 const BN_ULONG *np, const BN_ULONG *n0p, int num)
1006{
1007 BN_ULONG c0, c1, *tp, n0 = *n0p;
1008 volatile BN_ULONG *vp;
1009 int i = 0, j;
1010
1011 vp = tp = alloca((num + 2) * sizeof(BN_ULONG));
1012
1013 for (i = 0; i <= num; i++)
1014 tp[i] = 0;
1015
1016 for (i = 0; i < num; i++) {
1017 c0 = bn_mul_add_words(tp, ap, num, bp[i]);
1018 c1 = (tp[num] + c0) & BN_MASK2;
1019 tp[num] = c1;
1020 tp[num + 1] = (c1 < c0 ? 1 : 0);
1021
1022 c0 = bn_mul_add_words(tp, np, num, tp[0] * n0);
1023 c1 = (tp[num] + c0) & BN_MASK2;
1024 tp[num] = c1;
1025 tp[num + 1] += (c1 < c0 ? 1 : 0);
1026 for (j = 0; j <= num; j++)
1027 tp[j] = tp[j + 1];
1028 }
1029
1030 if (tp[num] != 0 || tp[num - 1] >= np[num - 1]) {
1031 c0 = bn_sub_words(rp, tp, np, num);
1032 if (tp[num] != 0 || c0 == 0) {
1033 for (i = 0; i < num + 2; i++)
1034 vp[i] = 0;
1035 return 1;
1036 }
1037 }
1038 for (i = 0; i < num; i++)
1039 rp[i] = tp[i], vp[i] = 0;
1040 vp[num] = 0;
1041 vp[num + 1] = 0;
1042 return 1;
1043}
1044# else
1045int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
1046 const BN_ULONG *np, const BN_ULONG *n0, int num)
1047{
1048 return 0;
1049}
1050# endif /* OPENSSL_BN_ASM_MONT */
1051# endif
1052
1053#endif /* !BN_MUL_COMBA */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette