1 /* $OpenBSD: bn_asm.c,v 1.15 2017/05/02 03:59:44 deraadt Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
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
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
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.
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
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:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
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
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
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
60 # undef NDEBUG /* avoid conflicting definitions */
67 #include <openssl/opensslconf.h>
71 #if defined(BN_LLONG) || defined(BN_UMULT_HIGH)
74 bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
82 #ifndef OPENSSL_SMALL_FOOTPRINT
84 mul_add(rp[0], ap[0], w, c1);
85 mul_add(rp[1], ap[1], w, c1);
86 mul_add(rp[2], ap[2], w, c1);
87 mul_add(rp[3], ap[3], w, c1);
94 mul_add(rp[0], ap[0], w, c1);
104 bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
112 #ifndef OPENSSL_SMALL_FOOTPRINT
114 mul(rp[0], ap[0], w, c1);
115 mul(rp[1], ap[1], w, c1);
116 mul(rp[2], ap[2], w, c1);
117 mul(rp[3], ap[3], w, c1);
124 mul(rp[0], ap[0], w, c1);
133 bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
139 #ifndef OPENSSL_SMALL_FOOTPRINT
141 sqr(r[0], r[1], a[0]);
142 sqr(r[2], r[3], a[1]);
143 sqr(r[4], r[5], a[2]);
144 sqr(r[6], r[7], a[3]);
151 sqr(r[0], r[1], a[0]);
158 #else /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */
161 bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
168 return ((BN_ULONG)0);
173 #ifndef OPENSSL_SMALL_FOOTPRINT
175 mul_add(rp[0], ap[0], bl, bh, c);
176 mul_add(rp[1], ap[1], bl, bh, c);
177 mul_add(rp[2], ap[2], bl, bh, c);
178 mul_add(rp[3], ap[3], bl, bh, c);
185 mul_add(rp[0], ap[0], bl, bh, c);
194 bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w)
201 return ((BN_ULONG)0);
206 #ifndef OPENSSL_SMALL_FOOTPRINT
208 mul(rp[0], ap[0], bl, bh, carry);
209 mul(rp[1], ap[1], bl, bh, carry);
210 mul(rp[2], ap[2], bl, bh, carry);
211 mul(rp[3], ap[3], bl, bh, carry);
218 mul(rp[0], ap[0], bl, bh, carry);
227 bn_sqr_words(BN_ULONG *r, const BN_ULONG *a, int n)
233 #ifndef OPENSSL_SMALL_FOOTPRINT
235 sqr64(r[0], r[1], a[0]);
236 sqr64(r[2], r[3], a[1]);
237 sqr64(r[4], r[5], a[2]);
238 sqr64(r[6], r[7], a[3]);
245 sqr64(r[0], r[1], a[0]);
252 #endif /* !(defined(BN_LLONG) || defined(BN_UMULT_HIGH)) */
254 #if defined(BN_LLONG) && defined(BN_DIV2W)
257 bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
259 return ((BN_ULONG)(((((BN_ULLONG)h) << BN_BITS2)|l)/(BN_ULLONG)d));
264 /* Divide h,l by d and return the result. */
265 /* I need to test this some more :-( */
267 bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
269 BN_ULONG dh, dl, q,ret = 0, th, tl, t;
275 i = BN_num_bits_word(d);
276 assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i));
284 h = (h << i) | (l >> (BN_BITS2 - i));
287 dh = (d & BN_MASK2h) >> BN_BITS4;
288 dl = (d & BN_MASK2l);
290 if ((h >> BN_BITS4) == dh)
299 if ((t & BN_MASK2h) ||
302 ((l & BN_MASK2h) >> BN_BITS4))))
308 t = (tl >> BN_BITS4);
309 tl = (tl << BN_BITS4) & BN_MASK2h;
325 h = ((h << BN_BITS4) | (l >> BN_BITS4)) & BN_MASK2;
326 l = (l & BN_MASK2l) << BN_BITS4;
331 #endif /* !defined(BN_LLONG) && defined(BN_DIV2W) */
335 bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
341 return ((BN_ULONG)0);
343 #ifndef OPENSSL_SMALL_FOOTPRINT
345 ll += (BN_ULLONG)a[0] + b[0];
346 r[0] = (BN_ULONG)ll & BN_MASK2;
348 ll += (BN_ULLONG)a[1] + b[1];
349 r[1] = (BN_ULONG)ll & BN_MASK2;
351 ll += (BN_ULLONG)a[2] + b[2];
352 r[2] = (BN_ULONG)ll & BN_MASK2;
354 ll += (BN_ULLONG)a[3] + b[3];
355 r[3] = (BN_ULONG)ll & BN_MASK2;
364 ll += (BN_ULLONG)a[0] + b[0];
365 r[0] = (BN_ULONG)ll & BN_MASK2;
372 return ((BN_ULONG)ll);
374 #else /* !BN_LLONG */
376 bn_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
382 return ((BN_ULONG)0);
385 #ifndef OPENSSL_SMALL_FOOTPRINT
388 t = (t + c) & BN_MASK2;
390 l = (t + b[0]) & BN_MASK2;
394 t = (t + c) & BN_MASK2;
396 l = (t + b[1]) & BN_MASK2;
400 t = (t + c) & BN_MASK2;
402 l = (t + b[2]) & BN_MASK2;
406 t = (t + c) & BN_MASK2;
408 l = (t + b[3]) & BN_MASK2;
419 t = (t + c) & BN_MASK2;
421 l = (t + b[0]) & BN_MASK2;
429 return ((BN_ULONG)c);
431 #endif /* !BN_LLONG */
434 bn_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int n)
441 return ((BN_ULONG)0);
443 #ifndef OPENSSL_SMALL_FOOTPRINT
447 r[0] = (t1 - t2 - c) & BN_MASK2;
452 r[1] = (t1 - t2 - c) & BN_MASK2;
457 r[2] = (t1 - t2 - c) & BN_MASK2;
462 r[3] = (t1 - t2 - c) & BN_MASK2;
474 r[0] = (t1 - t2 - c) & BN_MASK2;
485 #if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT)
492 /* mul_add_c(a,b,c0,c1,c2) -- c+=a*b for three word number c=(c2,c1,c0) */
493 /* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */
494 /* sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
495 /* sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number c=(c2,c1,c0) */
499 * Keep in mind that additions to multiplication result can not
500 * overflow, because its high half cannot be all-ones.
502 #define mul_add_c(a,b,c0,c1,c2) do { \
504 BN_ULLONG t = (BN_ULLONG)(a)*(b); \
505 t += c0; /* no carry */ \
506 c0 = (BN_ULONG)Lw(t); \
507 hi = (BN_ULONG)Hw(t); \
508 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
511 #define mul_add_c2(a,b,c0,c1,c2) do { \
513 BN_ULLONG t = (BN_ULLONG)(a)*(b); \
514 BN_ULLONG tt = t+c0; /* no carry */ \
515 c0 = (BN_ULONG)Lw(tt); \
516 hi = (BN_ULONG)Hw(tt); \
517 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
518 t += c0; /* no carry */ \
519 c0 = (BN_ULONG)Lw(t); \
520 hi = (BN_ULONG)Hw(t); \
521 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
524 #define sqr_add_c(a,i,c0,c1,c2) do { \
526 BN_ULLONG t = (BN_ULLONG)a[i]*a[i]; \
527 t += c0; /* no carry */ \
528 c0 = (BN_ULONG)Lw(t); \
529 hi = (BN_ULONG)Hw(t); \
530 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
533 #define sqr_add_c2(a,i,j,c0,c1,c2) \
534 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
536 #elif defined(BN_UMULT_LOHI)
538 * Keep in mind that additions to hi can not overflow, because
539 * the high word of a multiplication result cannot be all-ones.
541 #define mul_add_c(a,b,c0,c1,c2) do { \
542 BN_ULONG ta = (a), tb = (b); \
544 BN_UMULT_LOHI(lo,hi,ta,tb); \
545 c0 += lo; hi += (c0<lo)?1:0; \
546 c1 += hi; c2 += (c1<hi)?1:0; \
549 #define mul_add_c2(a,b,c0,c1,c2) do { \
550 BN_ULONG ta = (a), tb = (b); \
551 BN_ULONG lo, hi, tt; \
552 BN_UMULT_LOHI(lo,hi,ta,tb); \
553 c0 += lo; tt = hi+((c0<lo)?1:0); \
554 c1 += tt; c2 += (c1<tt)?1:0; \
555 c0 += lo; hi += (c0<lo)?1:0; \
556 c1 += hi; c2 += (c1<hi)?1:0; \
559 #define sqr_add_c(a,i,c0,c1,c2) do { \
560 BN_ULONG ta = (a)[i]; \
562 BN_UMULT_LOHI(lo,hi,ta,ta); \
563 c0 += lo; hi += (c0<lo)?1:0; \
564 c1 += hi; c2 += (c1<hi)?1:0; \
567 #define sqr_add_c2(a,i,j,c0,c1,c2) \
568 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
570 #elif defined(BN_UMULT_HIGH)
572 * Keep in mind that additions to hi can not overflow, because
573 * the high word of a multiplication result cannot be all-ones.
575 #define mul_add_c(a,b,c0,c1,c2) do { \
576 BN_ULONG ta = (a), tb = (b); \
577 BN_ULONG lo = ta * tb; \
578 BN_ULONG hi = BN_UMULT_HIGH(ta,tb); \
579 c0 += lo; hi += (c0<lo)?1:0; \
580 c1 += hi; c2 += (c1<hi)?1:0; \
583 #define mul_add_c2(a,b,c0,c1,c2) do { \
584 BN_ULONG ta = (a), tb = (b), tt; \
585 BN_ULONG lo = ta * tb; \
586 BN_ULONG hi = BN_UMULT_HIGH(ta,tb); \
587 c0 += lo; tt = hi + ((c0<lo)?1:0); \
588 c1 += tt; c2 += (c1<tt)?1:0; \
589 c0 += lo; hi += (c0<lo)?1:0; \
590 c1 += hi; c2 += (c1<hi)?1:0; \
593 #define sqr_add_c(a,i,c0,c1,c2) do { \
594 BN_ULONG ta = (a)[i]; \
595 BN_ULONG lo = ta * ta; \
596 BN_ULONG hi = BN_UMULT_HIGH(ta,ta); \
597 c0 += lo; hi += (c0<lo)?1:0; \
598 c1 += hi; c2 += (c1<hi)?1:0; \
601 #define sqr_add_c2(a,i,j,c0,c1,c2) \
602 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
604 #else /* !BN_LLONG */
606 * Keep in mind that additions to hi can not overflow, because
607 * the high word of a multiplication result cannot be all-ones.
609 #define mul_add_c(a,b,c0,c1,c2) do { \
610 BN_ULONG lo = LBITS(a), hi = HBITS(a); \
611 BN_ULONG bl = LBITS(b), bh = HBITS(b); \
612 mul64(lo,hi,bl,bh); \
613 c0 = (c0+lo)&BN_MASK2; if (c0<lo) hi++; \
614 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
617 #define mul_add_c2(a,b,c0,c1,c2) do { \
619 BN_ULONG lo = LBITS(a), hi = HBITS(a); \
620 BN_ULONG bl = LBITS(b), bh = HBITS(b); \
621 mul64(lo,hi,bl,bh); \
623 c0 = (c0+lo)&BN_MASK2; if (c0<lo) tt++; \
624 c1 = (c1+tt)&BN_MASK2; if (c1<tt) c2++; \
625 c0 = (c0+lo)&BN_MASK2; if (c0<lo) hi++; \
626 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
629 #define sqr_add_c(a,i,c0,c1,c2) do { \
631 sqr64(lo,hi,(a)[i]); \
632 c0 = (c0+lo)&BN_MASK2; if (c0<lo) hi++; \
633 c1 = (c1+hi)&BN_MASK2; if (c1<hi) c2++; \
636 #define sqr_add_c2(a,i,j,c0,c1,c2) \
637 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
638 #endif /* !BN_LLONG */
641 bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
648 mul_add_c(a[0], b[0], c1, c2, c3);
651 mul_add_c(a[0], b[1], c2, c3, c1);
652 mul_add_c(a[1], b[0], c2, c3, c1);
655 mul_add_c(a[2], b[0], c3, c1, c2);
656 mul_add_c(a[1], b[1], c3, c1, c2);
657 mul_add_c(a[0], b[2], c3, c1, c2);
660 mul_add_c(a[0], b[3], c1, c2, c3);
661 mul_add_c(a[1], b[2], c1, c2, c3);
662 mul_add_c(a[2], b[1], c1, c2, c3);
663 mul_add_c(a[3], b[0], c1, c2, c3);
666 mul_add_c(a[4], b[0], c2, c3, c1);
667 mul_add_c(a[3], b[1], c2, c3, c1);
668 mul_add_c(a[2], b[2], c2, c3, c1);
669 mul_add_c(a[1], b[3], c2, c3, c1);
670 mul_add_c(a[0], b[4], c2, c3, c1);
673 mul_add_c(a[0], b[5], c3, c1, c2);
674 mul_add_c(a[1], b[4], c3, c1, c2);
675 mul_add_c(a[2], b[3], c3, c1, c2);
676 mul_add_c(a[3], b[2], c3, c1, c2);
677 mul_add_c(a[4], b[1], c3, c1, c2);
678 mul_add_c(a[5], b[0], c3, c1, c2);
681 mul_add_c(a[6], b[0], c1, c2, c3);
682 mul_add_c(a[5], b[1], c1, c2, c3);
683 mul_add_c(a[4], b[2], c1, c2, c3);
684 mul_add_c(a[3], b[3], c1, c2, c3);
685 mul_add_c(a[2], b[4], c1, c2, c3);
686 mul_add_c(a[1], b[5], c1, c2, c3);
687 mul_add_c(a[0], b[6], c1, c2, c3);
690 mul_add_c(a[0], b[7], c2, c3, c1);
691 mul_add_c(a[1], b[6], c2, c3, c1);
692 mul_add_c(a[2], b[5], c2, c3, c1);
693 mul_add_c(a[3], b[4], c2, c3, c1);
694 mul_add_c(a[4], b[3], c2, c3, c1);
695 mul_add_c(a[5], b[2], c2, c3, c1);
696 mul_add_c(a[6], b[1], c2, c3, c1);
697 mul_add_c(a[7], b[0], c2, c3, c1);
700 mul_add_c(a[7], b[1], c3, c1, c2);
701 mul_add_c(a[6], b[2], c3, c1, c2);
702 mul_add_c(a[5], b[3], c3, c1, c2);
703 mul_add_c(a[4], b[4], c3, c1, c2);
704 mul_add_c(a[3], b[5], c3, c1, c2);
705 mul_add_c(a[2], b[6], c3, c1, c2);
706 mul_add_c(a[1], b[7], c3, c1, c2);
709 mul_add_c(a[2], b[7], c1, c2, c3);
710 mul_add_c(a[3], b[6], c1, c2, c3);
711 mul_add_c(a[4], b[5], c1, c2, c3);
712 mul_add_c(a[5], b[4], c1, c2, c3);
713 mul_add_c(a[6], b[3], c1, c2, c3);
714 mul_add_c(a[7], b[2], c1, c2, c3);
717 mul_add_c(a[7], b[3], c2, c3, c1);
718 mul_add_c(a[6], b[4], c2, c3, c1);
719 mul_add_c(a[5], b[5], c2, c3, c1);
720 mul_add_c(a[4], b[6], c2, c3, c1);
721 mul_add_c(a[3], b[7], c2, c3, c1);
724 mul_add_c(a[4], b[7], c3, c1, c2);
725 mul_add_c(a[5], b[6], c3, c1, c2);
726 mul_add_c(a[6], b[5], c3, c1, c2);
727 mul_add_c(a[7], b[4], c3, c1, c2);
730 mul_add_c(a[7], b[5], c1, c2, c3);
731 mul_add_c(a[6], b[6], c1, c2, c3);
732 mul_add_c(a[5], b[7], c1, c2, c3);
735 mul_add_c(a[6], b[7], c2, c3, c1);
736 mul_add_c(a[7], b[6], c2, c3, c1);
739 mul_add_c(a[7], b[7], c3, c1, c2);
745 bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
752 mul_add_c(a[0], b[0], c1, c2, c3);
755 mul_add_c(a[0], b[1], c2, c3, c1);
756 mul_add_c(a[1], b[0], c2, c3, c1);
759 mul_add_c(a[2], b[0], c3, c1, c2);
760 mul_add_c(a[1], b[1], c3, c1, c2);
761 mul_add_c(a[0], b[2], c3, c1, c2);
764 mul_add_c(a[0], b[3], c1, c2, c3);
765 mul_add_c(a[1], b[2], c1, c2, c3);
766 mul_add_c(a[2], b[1], c1, c2, c3);
767 mul_add_c(a[3], b[0], c1, c2, c3);
770 mul_add_c(a[3], b[1], c2, c3, c1);
771 mul_add_c(a[2], b[2], c2, c3, c1);
772 mul_add_c(a[1], b[3], c2, c3, c1);
775 mul_add_c(a[2], b[3], c3, c1, c2);
776 mul_add_c(a[3], b[2], c3, c1, c2);
779 mul_add_c(a[3], b[3], c1, c2, c3);
785 bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
792 sqr_add_c(a, 0, c1, c2, c3);
795 sqr_add_c2(a, 1, 0, c2, c3, c1);
798 sqr_add_c(a, 1, c3, c1, c2);
799 sqr_add_c2(a, 2, 0, c3, c1, c2);
802 sqr_add_c2(a, 3, 0, c1, c2, c3);
803 sqr_add_c2(a, 2, 1, c1, c2, c3);
806 sqr_add_c(a, 2, c2, c3, c1);
807 sqr_add_c2(a, 3, 1, c2, c3, c1);
808 sqr_add_c2(a, 4, 0, c2, c3, c1);
811 sqr_add_c2(a, 5, 0, c3, c1, c2);
812 sqr_add_c2(a, 4, 1, c3, c1, c2);
813 sqr_add_c2(a, 3, 2, c3, c1, c2);
816 sqr_add_c(a, 3, c1, c2, c3);
817 sqr_add_c2(a, 4, 2, c1, c2, c3);
818 sqr_add_c2(a, 5, 1, c1, c2, c3);
819 sqr_add_c2(a, 6, 0, c1, c2, c3);
822 sqr_add_c2(a, 7, 0, c2, c3, c1);
823 sqr_add_c2(a, 6, 1, c2, c3, c1);
824 sqr_add_c2(a, 5, 2, c2, c3, c1);
825 sqr_add_c2(a, 4, 3, c2, c3, c1);
828 sqr_add_c(a, 4, c3, c1, c2);
829 sqr_add_c2(a, 5, 3, c3, c1, c2);
830 sqr_add_c2(a, 6, 2, c3, c1, c2);
831 sqr_add_c2(a, 7, 1, c3, c1, c2);
834 sqr_add_c2(a, 7, 2, c1, c2, c3);
835 sqr_add_c2(a, 6, 3, c1, c2, c3);
836 sqr_add_c2(a, 5, 4, c1, c2, c3);
839 sqr_add_c(a, 5, c2, c3, c1);
840 sqr_add_c2(a, 6, 4, c2, c3, c1);
841 sqr_add_c2(a, 7, 3, c2, c3, c1);
844 sqr_add_c2(a, 7, 4, c3, c1, c2);
845 sqr_add_c2(a, 6, 5, c3, c1, c2);
848 sqr_add_c(a, 6, c1, c2, c3);
849 sqr_add_c2(a, 7, 5, c1, c2, c3);
852 sqr_add_c2(a, 7, 6, c2, c3, c1);
855 sqr_add_c(a, 7, c3, c1, c2);
861 bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
868 sqr_add_c(a, 0, c1, c2, c3);
871 sqr_add_c2(a, 1, 0, c2, c3, c1);
874 sqr_add_c(a, 1, c3, c1, c2);
875 sqr_add_c2(a, 2, 0, c3, c1, c2);
878 sqr_add_c2(a, 3, 0, c1, c2, c3);
879 sqr_add_c2(a, 2, 1, c1, c2, c3);
882 sqr_add_c(a, 2, c2, c3, c1);
883 sqr_add_c2(a, 3, 1, c2, c3, c1);
886 sqr_add_c2(a, 3, 2, c3, c1, c2);
889 sqr_add_c(a, 3, c1, c2, c3);
894 #ifdef OPENSSL_NO_ASM
895 #ifdef OPENSSL_BN_ASM_MONT
897 * This is essentially reference implementation, which may or may not
898 * result in performance improvement. E.g. on IA-32 this routine was
899 * observed to give 40% faster rsa1024 private key operations and 10%
900 * faster rsa4096 ones, while on AMD64 it improves rsa1024 sign only
901 * by 10% and *worsens* rsa4096 sign by 15%. Once again, it's a
902 * reference implementation, one to be used as starting point for
903 * platform-specific assembler. Mentioned numbers apply to compiler
904 * generated code compiled with and without -DOPENSSL_BN_ASM_MONT and
905 * can vary not only from platform to platform, but even for compiler
906 * versions. Assembler vs. assembler improvement coefficients can
907 * [and are known to] differ and are to be documented elsewhere.
910 bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0p, int num)
912 BN_ULONG c0, c1, ml, *tp, n0;
918 #if 0 /* template for platform-specific implementation */
920 return bn_sqr_mont(rp, ap, np, n0p, num);
922 tp = reallocarray(NULL, num + 2, sizeof(BN_ULONG));
933 for (j = 0; j < num; ++j)
934 mul(tp[j], ap[j], ml, mh, c0);
936 for (j = 0; j < num; ++j)
937 mul(tp[j], ap[j], ml, c0);
944 for (i = 0; i < num; i++) {
950 for (j = 0; j < num; ++j)
951 mul_add(tp[j], ap[j], ml, mh, c0);
953 for (j = 0; j < num; ++j)
954 mul_add(tp[j], ap[j], ml, c0);
956 c1 = (tp[num] + c0) & BN_MASK2;
958 tp[num + 1] = (c1 < c0 ? 1 : 0);
961 ml = (c1 * n0) & BN_MASK2;
966 mul_add(c1, np[0], ml, mh, c0);
968 mul_add(c1, ml, np[0], c0);
970 for (j = 1; j < num; j++) {
973 mul_add(c1, np[j], ml, mh, c0);
975 mul_add(c1, ml, np[j], c0);
977 tp[j - 1] = c1 & BN_MASK2;
979 c1 = (tp[num] + c0) & BN_MASK2;
981 tp[num] = tp[num + 1] + (c1 < c0 ? 1 : 0);
984 if (tp[num] != 0 || tp[num - 1] >= np[num - 1]) {
985 c0 = bn_sub_words(rp, tp, np, num);
986 if (tp[num] != 0 || c0 == 0) {
990 memcpy(rp, tp, num * sizeof(BN_ULONG));
992 freezero(tp, (num + 2) * sizeof(BN_ULONG));
997 * Return value of 0 indicates that multiplication/convolution was not
998 * performed to signal the caller to fall down to alternative/original
1001 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num)
1004 #endif /* OPENSSL_BN_ASM_MONT */
1007 #else /* !BN_MUL_COMBA */
1009 /* hmm... is it faster just to do a multiply? */
1010 #undef bn_sqr_comba4
1012 bn_sqr_comba4(BN_ULONG *r, const BN_ULONG *a)
1015 bn_sqr_normal(r, a, 4, t);
1018 #undef bn_sqr_comba8
1020 bn_sqr_comba8(BN_ULONG *r, const BN_ULONG *a)
1023 bn_sqr_normal(r, a, 8, t);
1027 bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
1029 r[4] = bn_mul_words(&(r[0]), a, 4, b[0]);
1030 r[5] = bn_mul_add_words(&(r[1]), a, 4, b[1]);
1031 r[6] = bn_mul_add_words(&(r[2]), a, 4, b[2]);
1032 r[7] = bn_mul_add_words(&(r[3]), a, 4, b[3]);
1036 bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
1038 r[8] = bn_mul_words(&(r[0]), a, 8, b[0]);
1039 r[9] = bn_mul_add_words(&(r[1]), a, 8, b[1]);
1040 r[10] = bn_mul_add_words(&(r[2]), a, 8, b[2]);
1041 r[11] = bn_mul_add_words(&(r[3]), a, 8, b[3]);
1042 r[12] = bn_mul_add_words(&(r[4]), a, 8, b[4]);
1043 r[13] = bn_mul_add_words(&(r[5]), a, 8, b[5]);
1044 r[14] = bn_mul_add_words(&(r[6]), a, 8, b[6]);
1045 r[15] = bn_mul_add_words(&(r[7]), a, 8, b[7]);
1048 #ifdef OPENSSL_NO_ASM
1049 #ifdef OPENSSL_BN_ASM_MONT
1051 bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
1052 const BN_ULONG *np, const BN_ULONG *n0p, int num)
1054 BN_ULONG c0, c1, *tp, n0 = *n0p;
1057 tp = calloc(NULL, num + 2, sizeof(BN_ULONG));
1061 for (i = 0; i < num; i++) {
1062 c0 = bn_mul_add_words(tp, ap, num, bp[i]);
1063 c1 = (tp[num] + c0) & BN_MASK2;
1065 tp[num + 1] = (c1 < c0 ? 1 : 0);
1067 c0 = bn_mul_add_words(tp, np, num, tp[0] * n0);
1068 c1 = (tp[num] + c0) & BN_MASK2;
1070 tp[num + 1] += (c1 < c0 ? 1 : 0);
1071 for (j = 0; j <= num; j++)
1075 if (tp[num] != 0 || tp[num - 1] >= np[num - 1]) {
1076 c0 = bn_sub_words(rp, tp, np, num);
1077 if (tp[num] != 0 || c0 == 0) {
1081 memcpy(rp, tp, num * sizeof(BN_ULONG));
1083 freezero(tp, (num + 2) * sizeof(BN_ULONG));
1088 bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
1089 const BN_ULONG *np, const BN_ULONG *n0, int num)
1093 #endif /* OPENSSL_BN_ASM_MONT */
1096 #endif /* !BN_MUL_COMBA */