]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bn/bn_asm.c
c39907a7dfd369cfa8691aad28c215fc5c2c91cf
2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/crypto.h>
12 #include "internal/cryptlib.h"
15 #if defined(BN_LLONG) || defined(BN_UMULT_HIGH)
17 BN_ULONG
bn_mul_add_words(BN_ULONG
*rp
, const BN_ULONG
*ap
, int num
,
26 # ifndef OPENSSL_SMALL_FOOTPRINT
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
);
38 mul_add(rp
[0], ap
[0], w
, c1
);
47 BN_ULONG
bn_mul_words(BN_ULONG
*rp
, const BN_ULONG
*ap
, int num
, BN_ULONG w
)
55 # ifndef OPENSSL_SMALL_FOOTPRINT
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
);
67 mul(rp
[0], ap
[0], w
, c1
);
75 void bn_sqr_words(BN_ULONG
*r
, const BN_ULONG
*a
, int n
)
81 # ifndef OPENSSL_SMALL_FOOTPRINT
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]);
93 sqr(r
[0], r
[1], a
[0]);
100 #else /* !(defined(BN_LLONG) ||
101 * defined(BN_UMULT_HIGH)) */
103 BN_ULONG
bn_mul_add_words(BN_ULONG
*rp
, const BN_ULONG
*ap
, int num
,
116 # ifndef OPENSSL_SMALL_FOOTPRINT
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
);
128 mul_add(rp
[0], ap
[0], bl
, bh
, c
);
136 BN_ULONG
bn_mul_words(BN_ULONG
*rp
, const BN_ULONG
*ap
, int num
, BN_ULONG w
)
148 # ifndef OPENSSL_SMALL_FOOTPRINT
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
);
160 mul(rp
[0], ap
[0], bl
, bh
, carry
);
168 void bn_sqr_words(BN_ULONG
*r
, const BN_ULONG
*a
, int n
)
174 # ifndef OPENSSL_SMALL_FOOTPRINT
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]);
186 sqr64(r
[0], r
[1], a
[0]);
193 #endif /* !(defined(BN_LLONG) ||
194 * defined(BN_UMULT_HIGH)) */
196 #if defined(BN_LLONG) && defined(BN_DIV2W)
198 BN_ULONG
bn_div_words(BN_ULONG h
, BN_ULONG l
, BN_ULONG d
)
200 return ((BN_ULONG
)(((((BN_ULLONG
) h
) << BN_BITS2
) | l
) / (BN_ULLONG
) d
));
205 /* Divide h,l by d and return the result. */
206 /* I need to test this some more :-( */
207 BN_ULONG
bn_div_words(BN_ULONG h
, BN_ULONG l
, BN_ULONG d
)
209 BN_ULONG dh
, dl
, q
, ret
= 0, th
, tl
, t
;
215 i
= BN_num_bits_word(d
);
216 assert((i
== BN_BITS2
) || (h
<= (BN_ULONG
)1 << i
));
224 h
= (h
<< i
) | (l
>> (BN_BITS2
- i
));
227 dh
= (d
& BN_MASK2h
) >> BN_BITS4
;
228 dl
= (d
& BN_MASK2l
);
230 if ((h
>> BN_BITS4
) == dh
)
239 if ((t
& BN_MASK2h
) ||
240 ((tl
) <= ((t
<< BN_BITS4
) | ((l
& BN_MASK2h
) >> BN_BITS4
))))
246 t
= (tl
>> BN_BITS4
);
247 tl
= (tl
<< BN_BITS4
) & BN_MASK2h
;
263 h
= ((h
<< BN_BITS4
) | (l
>> BN_BITS4
)) & BN_MASK2
;
264 l
= (l
& BN_MASK2l
) << BN_BITS4
;
269 #endif /* !defined(BN_LLONG) && defined(BN_DIV2W) */
272 BN_ULONG
bn_add_words(BN_ULONG
*r
, const BN_ULONG
*a
, const BN_ULONG
*b
,
281 # ifndef OPENSSL_SMALL_FOOTPRINT
283 ll
+= (BN_ULLONG
) a
[0] + b
[0];
284 r
[0] = (BN_ULONG
)ll
& BN_MASK2
;
286 ll
+= (BN_ULLONG
) a
[1] + b
[1];
287 r
[1] = (BN_ULONG
)ll
& BN_MASK2
;
289 ll
+= (BN_ULLONG
) a
[2] + b
[2];
290 r
[2] = (BN_ULONG
)ll
& BN_MASK2
;
292 ll
+= (BN_ULLONG
) a
[3] + b
[3];
293 r
[3] = (BN_ULONG
)ll
& BN_MASK2
;
302 ll
+= (BN_ULLONG
) a
[0] + b
[0];
303 r
[0] = (BN_ULONG
)ll
& BN_MASK2
;
312 #else /* !BN_LLONG */
313 BN_ULONG
bn_add_words(BN_ULONG
*r
, const BN_ULONG
*a
, const BN_ULONG
*b
,
323 # ifndef OPENSSL_SMALL_FOOTPRINT
326 t
= (t
+ c
) & BN_MASK2
;
328 l
= (t
+ b
[0]) & BN_MASK2
;
332 t
= (t
+ c
) & BN_MASK2
;
334 l
= (t
+ b
[1]) & BN_MASK2
;
338 t
= (t
+ c
) & BN_MASK2
;
340 l
= (t
+ b
[2]) & BN_MASK2
;
344 t
= (t
+ c
) & BN_MASK2
;
346 l
= (t
+ b
[3]) & BN_MASK2
;
357 t
= (t
+ c
) & BN_MASK2
;
359 l
= (t
+ b
[0]) & BN_MASK2
;
369 #endif /* !BN_LLONG */
371 BN_ULONG
bn_sub_words(BN_ULONG
*r
, const BN_ULONG
*a
, const BN_ULONG
*b
,
381 #ifndef OPENSSL_SMALL_FOOTPRINT
384 t2
= (t1
- c
) & BN_MASK2
;
387 t1
= (t2
- t1
) & BN_MASK2
;
391 t2
= (t1
- c
) & BN_MASK2
;
394 t1
= (t2
- t1
) & BN_MASK2
;
398 t2
= (t1
- c
) & BN_MASK2
;
401 t1
= (t2
- t1
) & BN_MASK2
;
405 t2
= (t1
- c
) & BN_MASK2
;
408 t1
= (t2
- t1
) & BN_MASK2
;
419 t2
= (t1
- c
) & BN_MASK2
;
422 t1
= (t2
- t1
) & BN_MASK2
;
433 #if defined(BN_MUL_COMBA) && !defined(OPENSSL_SMALL_FOOTPRINT)
435 /* mul_add_c(a,b,c0,c1,c2) -- c+=a*b for three word number c=(c2,c1,c0) */
436 /* mul_add_c2(a,b,c0,c1,c2) -- c+=2*a*b for three word number c=(c2,c1,c0) */
437 /* sqr_add_c(a,i,c0,c1,c2) -- c+=a[i]^2 for three word number c=(c2,c1,c0) */
439 * sqr_add_c2(a,i,c0,c1,c2) -- c+=2*a[i]*a[j] for three word number
445 * Keep in mind that additions to multiplication result can not
446 * overflow, because its high half cannot be all-ones.
448 # define mul_add_c(a,b,c0,c1,c2) do { \
450 BN_ULLONG t = (BN_ULLONG)(a)*(b); \
451 t += c0; /* no carry */ \
452 c0 = (BN_ULONG)Lw(t); \
453 hi = (BN_ULONG)Hw(t); \
454 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
457 # define mul_add_c2(a,b,c0,c1,c2) do { \
459 BN_ULLONG t = (BN_ULLONG)(a)*(b); \
460 BN_ULLONG tt = t+c0; /* no carry */ \
461 c0 = (BN_ULONG)Lw(tt); \
462 hi = (BN_ULONG)Hw(tt); \
463 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
464 t += c0; /* no carry */ \
465 c0 = (BN_ULONG)Lw(t); \
466 hi = (BN_ULONG)Hw(t); \
467 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
470 # define sqr_add_c(a,i,c0,c1,c2) do { \
472 BN_ULLONG t = (BN_ULLONG)a[i]*a[i]; \
473 t += c0; /* no carry */ \
474 c0 = (BN_ULONG)Lw(t); \
475 hi = (BN_ULONG)Hw(t); \
476 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
479 # define sqr_add_c2(a,i,j,c0,c1,c2) \
480 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
482 # elif defined(BN_UMULT_LOHI)
484 * Keep in mind that additions to hi can not overflow, because
485 * the high word of a multiplication result cannot be all-ones.
487 # define mul_add_c(a,b,c0,c1,c2) do { \
488 BN_ULONG ta = (a), tb = (b); \
490 BN_UMULT_LOHI(lo,hi,ta,tb); \
491 c0 += lo; hi += (c0<lo); \
492 c1 += hi; c2 += (c1<hi); \
495 # define mul_add_c2(a,b,c0,c1,c2) do { \
496 BN_ULONG ta = (a), tb = (b); \
497 BN_ULONG lo, hi, tt; \
498 BN_UMULT_LOHI(lo,hi,ta,tb); \
499 c0 += lo; tt = hi + (c0<lo); \
500 c1 += tt; c2 += (c1<tt); \
501 c0 += lo; hi += (c0<lo); \
502 c1 += hi; c2 += (c1<hi); \
505 # define sqr_add_c(a,i,c0,c1,c2) do { \
506 BN_ULONG ta = (a)[i]; \
508 BN_UMULT_LOHI(lo,hi,ta,ta); \
509 c0 += lo; hi += (c0<lo); \
510 c1 += hi; c2 += (c1<hi); \
513 # define sqr_add_c2(a,i,j,c0,c1,c2) \
514 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
516 # elif defined(BN_UMULT_HIGH)
518 * Keep in mind that additions to hi can not overflow, because
519 * the high word of a multiplication result cannot be all-ones.
521 # define mul_add_c(a,b,c0,c1,c2) do { \
522 BN_ULONG ta = (a), tb = (b); \
523 BN_ULONG lo = ta * tb; \
524 BN_ULONG hi = BN_UMULT_HIGH(ta,tb); \
525 c0 += lo; hi += (c0<lo); \
526 c1 += hi; c2 += (c1<hi); \
529 # define mul_add_c2(a,b,c0,c1,c2) do { \
530 BN_ULONG ta = (a), tb = (b), tt; \
531 BN_ULONG lo = ta * tb; \
532 BN_ULONG hi = BN_UMULT_HIGH(ta,tb); \
533 c0 += lo; tt = hi + (c0<lo); \
534 c1 += tt; c2 += (c1<tt); \
535 c0 += lo; hi += (c0<lo); \
536 c1 += hi; c2 += (c1<hi); \
539 # define sqr_add_c(a,i,c0,c1,c2) do { \
540 BN_ULONG ta = (a)[i]; \
541 BN_ULONG lo = ta * ta; \
542 BN_ULONG hi = BN_UMULT_HIGH(ta,ta); \
543 c0 += lo; hi += (c0<lo); \
544 c1 += hi; c2 += (c1<hi); \
547 # define sqr_add_c2(a,i,j,c0,c1,c2) \
548 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
550 # else /* !BN_LLONG */
552 * Keep in mind that additions to hi can not overflow, because
553 * the high word of a multiplication result cannot be all-ones.
555 # define mul_add_c(a,b,c0,c1,c2) do { \
556 BN_ULONG lo = LBITS(a), hi = HBITS(a); \
557 BN_ULONG bl = LBITS(b), bh = HBITS(b); \
558 mul64(lo,hi,bl,bh); \
559 c0 = (c0+lo)&BN_MASK2; hi += (c0<lo); \
560 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
563 # define mul_add_c2(a,b,c0,c1,c2) do { \
565 BN_ULONG lo = LBITS(a), hi = HBITS(a); \
566 BN_ULONG bl = LBITS(b), bh = HBITS(b); \
567 mul64(lo,hi,bl,bh); \
569 c0 = (c0+lo)&BN_MASK2; tt += (c0<lo); \
570 c1 = (c1+tt)&BN_MASK2; c2 += (c1<tt); \
571 c0 = (c0+lo)&BN_MASK2; hi += (c0<lo); \
572 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
575 # define sqr_add_c(a,i,c0,c1,c2) do { \
577 sqr64(lo,hi,(a)[i]); \
578 c0 = (c0+lo)&BN_MASK2; hi += (c0<lo); \
579 c1 = (c1+hi)&BN_MASK2; c2 += (c1<hi); \
582 # define sqr_add_c2(a,i,j,c0,c1,c2) \
583 mul_add_c2((a)[i],(a)[j],c0,c1,c2)
584 # endif /* !BN_LLONG */
586 void bn_mul_comba8(BN_ULONG
*r
, BN_ULONG
*a
, BN_ULONG
*b
)
593 mul_add_c(a
[0], b
[0], c1
, c2
, c3
);
596 mul_add_c(a
[0], b
[1], c2
, c3
, c1
);
597 mul_add_c(a
[1], b
[0], c2
, c3
, c1
);
600 mul_add_c(a
[2], b
[0], c3
, c1
, c2
);
601 mul_add_c(a
[1], b
[1], c3
, c1
, c2
);
602 mul_add_c(a
[0], b
[2], c3
, c1
, c2
);
605 mul_add_c(a
[0], b
[3], c1
, c2
, c3
);
606 mul_add_c(a
[1], b
[2], c1
, c2
, c3
);
607 mul_add_c(a
[2], b
[1], c1
, c2
, c3
);
608 mul_add_c(a
[3], b
[0], c1
, c2
, c3
);
611 mul_add_c(a
[4], b
[0], c2
, c3
, c1
);
612 mul_add_c(a
[3], b
[1], c2
, c3
, c1
);
613 mul_add_c(a
[2], b
[2], c2
, c3
, c1
);
614 mul_add_c(a
[1], b
[3], c2
, c3
, c1
);
615 mul_add_c(a
[0], b
[4], c2
, c3
, c1
);
618 mul_add_c(a
[0], b
[5], c3
, c1
, c2
);
619 mul_add_c(a
[1], b
[4], c3
, c1
, c2
);
620 mul_add_c(a
[2], b
[3], c3
, c1
, c2
);
621 mul_add_c(a
[3], b
[2], c3
, c1
, c2
);
622 mul_add_c(a
[4], b
[1], c3
, c1
, c2
);
623 mul_add_c(a
[5], b
[0], c3
, c1
, c2
);
626 mul_add_c(a
[6], b
[0], c1
, c2
, c3
);
627 mul_add_c(a
[5], b
[1], c1
, c2
, c3
);
628 mul_add_c(a
[4], b
[2], c1
, c2
, c3
);
629 mul_add_c(a
[3], b
[3], c1
, c2
, c3
);
630 mul_add_c(a
[2], b
[4], c1
, c2
, c3
);
631 mul_add_c(a
[1], b
[5], c1
, c2
, c3
);
632 mul_add_c(a
[0], b
[6], c1
, c2
, c3
);
635 mul_add_c(a
[0], b
[7], c2
, c3
, c1
);
636 mul_add_c(a
[1], b
[6], c2
, c3
, c1
);
637 mul_add_c(a
[2], b
[5], c2
, c3
, c1
);
638 mul_add_c(a
[3], b
[4], c2
, c3
, c1
);
639 mul_add_c(a
[4], b
[3], c2
, c3
, c1
);
640 mul_add_c(a
[5], b
[2], c2
, c3
, c1
);
641 mul_add_c(a
[6], b
[1], c2
, c3
, c1
);
642 mul_add_c(a
[7], b
[0], c2
, c3
, c1
);
645 mul_add_c(a
[7], b
[1], c3
, c1
, c2
);
646 mul_add_c(a
[6], b
[2], c3
, c1
, c2
);
647 mul_add_c(a
[5], b
[3], c3
, c1
, c2
);
648 mul_add_c(a
[4], b
[4], c3
, c1
, c2
);
649 mul_add_c(a
[3], b
[5], c3
, c1
, c2
);
650 mul_add_c(a
[2], b
[6], c3
, c1
, c2
);
651 mul_add_c(a
[1], b
[7], c3
, c1
, c2
);
654 mul_add_c(a
[2], b
[7], c1
, c2
, c3
);
655 mul_add_c(a
[3], b
[6], c1
, c2
, c3
);
656 mul_add_c(a
[4], b
[5], c1
, c2
, c3
);
657 mul_add_c(a
[5], b
[4], c1
, c2
, c3
);
658 mul_add_c(a
[6], b
[3], c1
, c2
, c3
);
659 mul_add_c(a
[7], b
[2], c1
, c2
, c3
);
662 mul_add_c(a
[7], b
[3], c2
, c3
, c1
);
663 mul_add_c(a
[6], b
[4], c2
, c3
, c1
);
664 mul_add_c(a
[5], b
[5], c2
, c3
, c1
);
665 mul_add_c(a
[4], b
[6], c2
, c3
, c1
);
666 mul_add_c(a
[3], b
[7], c2
, c3
, c1
);
669 mul_add_c(a
[4], b
[7], c3
, c1
, c2
);
670 mul_add_c(a
[5], b
[6], c3
, c1
, c2
);
671 mul_add_c(a
[6], b
[5], c3
, c1
, c2
);
672 mul_add_c(a
[7], b
[4], c3
, c1
, c2
);
675 mul_add_c(a
[7], b
[5], c1
, c2
, c3
);
676 mul_add_c(a
[6], b
[6], c1
, c2
, c3
);
677 mul_add_c(a
[5], b
[7], c1
, c2
, c3
);
680 mul_add_c(a
[6], b
[7], c2
, c3
, c1
);
681 mul_add_c(a
[7], b
[6], c2
, c3
, c1
);
684 mul_add_c(a
[7], b
[7], c3
, c1
, c2
);
689 void bn_mul_comba4(BN_ULONG
*r
, BN_ULONG
*a
, BN_ULONG
*b
)
696 mul_add_c(a
[0], b
[0], c1
, c2
, c3
);
699 mul_add_c(a
[0], b
[1], c2
, c3
, c1
);
700 mul_add_c(a
[1], b
[0], c2
, c3
, c1
);
703 mul_add_c(a
[2], b
[0], c3
, c1
, c2
);
704 mul_add_c(a
[1], b
[1], c3
, c1
, c2
);
705 mul_add_c(a
[0], b
[2], c3
, c1
, c2
);
708 mul_add_c(a
[0], b
[3], c1
, c2
, c3
);
709 mul_add_c(a
[1], b
[2], c1
, c2
, c3
);
710 mul_add_c(a
[2], b
[1], c1
, c2
, c3
);
711 mul_add_c(a
[3], b
[0], c1
, c2
, c3
);
714 mul_add_c(a
[3], b
[1], c2
, c3
, c1
);
715 mul_add_c(a
[2], b
[2], c2
, c3
, c1
);
716 mul_add_c(a
[1], b
[3], c2
, c3
, c1
);
719 mul_add_c(a
[2], b
[3], c3
, c1
, c2
);
720 mul_add_c(a
[3], b
[2], c3
, c1
, c2
);
723 mul_add_c(a
[3], b
[3], c1
, c2
, c3
);
728 void bn_sqr_comba8(BN_ULONG
*r
, const BN_ULONG
*a
)
735 sqr_add_c(a
, 0, c1
, c2
, c3
);
738 sqr_add_c2(a
, 1, 0, c2
, c3
, c1
);
741 sqr_add_c(a
, 1, c3
, c1
, c2
);
742 sqr_add_c2(a
, 2, 0, c3
, c1
, c2
);
745 sqr_add_c2(a
, 3, 0, c1
, c2
, c3
);
746 sqr_add_c2(a
, 2, 1, c1
, c2
, c3
);
749 sqr_add_c(a
, 2, c2
, c3
, c1
);
750 sqr_add_c2(a
, 3, 1, c2
, c3
, c1
);
751 sqr_add_c2(a
, 4, 0, c2
, c3
, c1
);
754 sqr_add_c2(a
, 5, 0, c3
, c1
, c2
);
755 sqr_add_c2(a
, 4, 1, c3
, c1
, c2
);
756 sqr_add_c2(a
, 3, 2, c3
, c1
, c2
);
759 sqr_add_c(a
, 3, c1
, c2
, c3
);
760 sqr_add_c2(a
, 4, 2, c1
, c2
, c3
);
761 sqr_add_c2(a
, 5, 1, c1
, c2
, c3
);
762 sqr_add_c2(a
, 6, 0, c1
, c2
, c3
);
765 sqr_add_c2(a
, 7, 0, c2
, c3
, c1
);
766 sqr_add_c2(a
, 6, 1, c2
, c3
, c1
);
767 sqr_add_c2(a
, 5, 2, c2
, c3
, c1
);
768 sqr_add_c2(a
, 4, 3, c2
, c3
, c1
);
771 sqr_add_c(a
, 4, c3
, c1
, c2
);
772 sqr_add_c2(a
, 5, 3, c3
, c1
, c2
);
773 sqr_add_c2(a
, 6, 2, c3
, c1
, c2
);
774 sqr_add_c2(a
, 7, 1, c3
, c1
, c2
);
777 sqr_add_c2(a
, 7, 2, c1
, c2
, c3
);
778 sqr_add_c2(a
, 6, 3, c1
, c2
, c3
);
779 sqr_add_c2(a
, 5, 4, c1
, c2
, c3
);
782 sqr_add_c(a
, 5, c2
, c3
, c1
);
783 sqr_add_c2(a
, 6, 4, c2
, c3
, c1
);
784 sqr_add_c2(a
, 7, 3, c2
, c3
, c1
);
787 sqr_add_c2(a
, 7, 4, c3
, c1
, c2
);
788 sqr_add_c2(a
, 6, 5, c3
, c1
, c2
);
791 sqr_add_c(a
, 6, c1
, c2
, c3
);
792 sqr_add_c2(a
, 7, 5, c1
, c2
, c3
);
795 sqr_add_c2(a
, 7, 6, c2
, c3
, c1
);
798 sqr_add_c(a
, 7, c3
, c1
, c2
);
803 void bn_sqr_comba4(BN_ULONG
*r
, const BN_ULONG
*a
)
810 sqr_add_c(a
, 0, c1
, c2
, c3
);
813 sqr_add_c2(a
, 1, 0, c2
, c3
, c1
);
816 sqr_add_c(a
, 1, c3
, c1
, c2
);
817 sqr_add_c2(a
, 2, 0, c3
, c1
, c2
);
820 sqr_add_c2(a
, 3, 0, c1
, c2
, c3
);
821 sqr_add_c2(a
, 2, 1, c1
, c2
, c3
);
824 sqr_add_c(a
, 2, c2
, c3
, c1
);
825 sqr_add_c2(a
, 3, 1, c2
, c3
, c1
);
828 sqr_add_c2(a
, 3, 2, c3
, c1
, c2
);
831 sqr_add_c(a
, 3, c1
, c2
, c3
);
836 # ifdef OPENSSL_NO_ASM
837 # ifdef OPENSSL_BN_ASM_MONT
840 * This is essentially reference implementation, which may or may not
841 * result in performance improvement. E.g. on IA-32 this routine was
842 * observed to give 40% faster rsa1024 private key operations and 10%
843 * faster rsa4096 ones, while on AMD64 it improves rsa1024 sign only
844 * by 10% and *worsens* rsa4096 sign by 15%. Once again, it's a
845 * reference implementation, one to be used as starting point for
846 * platform-specific assembler. Mentioned numbers apply to compiler
847 * generated code compiled with and without -DOPENSSL_BN_ASM_MONT and
848 * can vary not only from platform to platform, but even for compiler
849 * versions. Assembler vs. assembler improvement coefficients can
850 * [and are known to] differ and are to be documented elsewhere.
852 int bn_mul_mont(BN_ULONG
*rp
, const BN_ULONG
*ap
, const BN_ULONG
*bp
,
853 const BN_ULONG
*np
, const BN_ULONG
*n0p
, int num
)
855 BN_ULONG c0
, c1
, ml
, *tp
, n0
;
859 volatile BN_ULONG
*vp
;
862 # if 0 /* template for platform-specific
865 return bn_sqr_mont(rp
, ap
, np
, n0p
, num
);
867 vp
= tp
= alloca((num
+ 2) * sizeof(BN_ULONG
));
876 for (j
= 0; j
< num
; ++j
)
877 mul(tp
[j
], ap
[j
], ml
, mh
, c0
);
879 for (j
= 0; j
< num
; ++j
)
880 mul(tp
[j
], ap
[j
], ml
, c0
);
887 for (i
= 0; i
< num
; i
++) {
893 for (j
= 0; j
< num
; ++j
)
894 mul_add(tp
[j
], ap
[j
], ml
, mh
, c0
);
896 for (j
= 0; j
< num
; ++j
)
897 mul_add(tp
[j
], ap
[j
], ml
, c0
);
899 c1
= (tp
[num
] + c0
) & BN_MASK2
;
901 tp
[num
+ 1] = (c1
< c0
? 1 : 0);
904 ml
= (c1
* n0
) & BN_MASK2
;
909 mul_add(c1
, np
[0], ml
, mh
, c0
);
911 mul_add(c1
, ml
, np
[0], c0
);
913 for (j
= 1; j
< num
; j
++) {
916 mul_add(c1
, np
[j
], ml
, mh
, c0
);
918 mul_add(c1
, ml
, np
[j
], c0
);
920 tp
[j
- 1] = c1
& BN_MASK2
;
922 c1
= (tp
[num
] + c0
) & BN_MASK2
;
924 tp
[num
] = tp
[num
+ 1] + (c1
< c0
? 1 : 0);
927 if (tp
[num
] != 0 || tp
[num
- 1] >= np
[num
- 1]) {
928 c0
= bn_sub_words(rp
, tp
, np
, num
);
929 if (tp
[num
] != 0 || c0
== 0) {
930 for (i
= 0; i
< num
+ 2; i
++)
935 for (i
= 0; i
< num
; i
++)
936 rp
[i
] = tp
[i
], vp
[i
] = 0;
943 * Return value of 0 indicates that multiplication/convolution was not
944 * performed to signal the caller to fall down to alternative/original
947 int bn_mul_mont(BN_ULONG
*rp
, const BN_ULONG
*ap
, const BN_ULONG
*bp
,
948 const BN_ULONG
*np
, const BN_ULONG
*n0
, int num
)
952 # endif /* OPENSSL_BN_ASM_MONT */
955 #else /* !BN_MUL_COMBA */
957 /* hmm... is it faster just to do a multiply? */
958 void bn_sqr_comba4(BN_ULONG
*r
, const BN_ULONG
*a
)
961 bn_sqr_normal(r
, a
, 4, t
);
964 void bn_sqr_comba8(BN_ULONG
*r
, const BN_ULONG
*a
)
967 bn_sqr_normal(r
, a
, 8, t
);
970 void bn_mul_comba4(BN_ULONG
*r
, BN_ULONG
*a
, BN_ULONG
*b
)
972 r
[4] = bn_mul_words(&(r
[0]), a
, 4, b
[0]);
973 r
[5] = bn_mul_add_words(&(r
[1]), a
, 4, b
[1]);
974 r
[6] = bn_mul_add_words(&(r
[2]), a
, 4, b
[2]);
975 r
[7] = bn_mul_add_words(&(r
[3]), a
, 4, b
[3]);
978 void bn_mul_comba8(BN_ULONG
*r
, BN_ULONG
*a
, BN_ULONG
*b
)
980 r
[8] = bn_mul_words(&(r
[0]), a
, 8, b
[0]);
981 r
[9] = bn_mul_add_words(&(r
[1]), a
, 8, b
[1]);
982 r
[10] = bn_mul_add_words(&(r
[2]), a
, 8, b
[2]);
983 r
[11] = bn_mul_add_words(&(r
[3]), a
, 8, b
[3]);
984 r
[12] = bn_mul_add_words(&(r
[4]), a
, 8, b
[4]);
985 r
[13] = bn_mul_add_words(&(r
[5]), a
, 8, b
[5]);
986 r
[14] = bn_mul_add_words(&(r
[6]), a
, 8, b
[6]);
987 r
[15] = bn_mul_add_words(&(r
[7]), a
, 8, b
[7]);
990 # ifdef OPENSSL_NO_ASM
991 # ifdef OPENSSL_BN_ASM_MONT
993 int bn_mul_mont(BN_ULONG
*rp
, const BN_ULONG
*ap
, const BN_ULONG
*bp
,
994 const BN_ULONG
*np
, const BN_ULONG
*n0p
, int num
)
996 BN_ULONG c0
, c1
, *tp
, n0
= *n0p
;
997 volatile BN_ULONG
*vp
;
1000 vp
= tp
= alloca((num
+ 2) * sizeof(BN_ULONG
));
1002 for (i
= 0; i
<= num
; i
++)
1005 for (i
= 0; i
< num
; i
++) {
1006 c0
= bn_mul_add_words(tp
, ap
, num
, bp
[i
]);
1007 c1
= (tp
[num
] + c0
) & BN_MASK2
;
1009 tp
[num
+ 1] = (c1
< c0
? 1 : 0);
1011 c0
= bn_mul_add_words(tp
, np
, num
, tp
[0] * n0
);
1012 c1
= (tp
[num
] + c0
) & BN_MASK2
;
1014 tp
[num
+ 1] += (c1
< c0
? 1 : 0);
1015 for (j
= 0; j
<= num
; j
++)
1019 if (tp
[num
] != 0 || tp
[num
- 1] >= np
[num
- 1]) {
1020 c0
= bn_sub_words(rp
, tp
, np
, num
);
1021 if (tp
[num
] != 0 || c0
== 0) {
1022 for (i
= 0; i
< num
+ 2; i
++)
1027 for (i
= 0; i
< num
; i
++)
1028 rp
[i
] = tp
[i
], vp
[i
] = 0;
1034 int bn_mul_mont(BN_ULONG
*rp
, const BN_ULONG
*ap
, const BN_ULONG
*bp
,
1035 const BN_ULONG
*np
, const BN_ULONG
*n0
, int num
)
1039 # endif /* OPENSSL_BN_ASM_MONT */
1042 #endif /* !BN_MUL_COMBA */