Botan 3.14.1
Crypto and TLS for C&
Botan::word3< W > Class Template Referencefinal

#include <mp_asmi.h>

Public Member Functions

constexpr void add (W x)
constexpr W extract ()
constexpr W monty_step (W p0, W p_dash)
constexpr W monty_step_pdash1 ()
constexpr void mul (W x, W y)
constexpr void mul_x2 (W x, W y)
constexpr word3 ()

Detailed Description

template<WordType W>
class Botan::word3< W >

Helper for 3-word accumulators

A number of algorithms especially Comba multiplication and Montgomery reduction can take advantage of wide accumulators, which consume inputs via addition with outputs extracted from the low bits.

Definition at line 501 of file mp_asmi.h.

Constructor & Destructor Documentation

◆ word3()

template<WordType W>
Botan::word3< W >::word3 ( )
inlineconstexpr

Definition at line 540 of file mp_asmi.h.

540: m_w0(0), m_w1(0), m_w2(0) {}

Member Function Documentation

◆ add()

template<WordType W>
void Botan::word3< W >::add ( W x)
inlineconstexpr

◆ extract()

◆ monty_step()

template<WordType W>
W Botan::word3< W >::monty_step ( W p0,
W p_dash )
inlineconstexpr

Definition at line 673 of file mp_asmi.h.

673 {
674 W r = m_w0 * p_dash;
675 mul(r, p0);
676 m_w0 = m_w1;
677 m_w1 = m_w2;
678 m_w2 = 0;
679 return r;
680 }
constexpr void mul(W x, W y)
Definition mp_asmi.h:542

References mul().

Referenced by Botan::bigint_monty_redc_12(), Botan::bigint_monty_redc_16(), Botan::bigint_monty_redc_24(), Botan::bigint_monty_redc_32(), Botan::bigint_monty_redc_4(), Botan::bigint_monty_redc_6(), Botan::bigint_monty_redc_8(), Botan::bigint_monty_redc_generic(), and Botan::monty_redc().

◆ monty_step_pdash1()

template<WordType W>
W Botan::word3< W >::monty_step_pdash1 ( )
inlineconstexpr

Definition at line 682 of file mp_asmi.h.

682 {
683 // If p_dash == 1 then p[0] = -1 and everything simplifies
684 const W r = m_w0;
685 m_w0 += m_w1;
686 m_w1 = m_w2 + (m_w0 < m_w1);
687 m_w2 = 0;
688 return r;
689 }

Referenced by Botan::monty_redc_pdash1().

◆ mul()

template<WordType W>
void Botan::word3< W >::mul ( W x,
W y )
inlineconstexpr

Definition at line 542 of file mp_asmi.h.

542 {
543 #if defined(BOTAN_MP_USE_X86_64_ASM)
544 if constexpr(std::same_as<W, uint64_t>) {
546 W z0 = 0;
547 W z1 = 0;
548
549 asm("mulq %[y]" : "=a"(z0), "=d"(z1) : "a"(x), [y] "rm"(y) : "cc");
550
551 asm(R"(
552 addq %[z0],%[w0]
553 adcq %[z1],%[w1]
554 adcq $0,%[w2]
555 )"
556 : [w0] "=r"(m_w0), [w1] "=r"(m_w1), [w2] "=r"(m_w2)
557 : [z0] "r"(z0), [z1] "r"(z1), "0"(m_w0), "1"(m_w1), "2"(m_w2)
558 : "cc");
559 return;
560 }
561 }
562 #elif defined(BOTAN_MP_USE_AARCH64_ASM)
563 if constexpr(std::same_as<W, uint64_t>) {
565 W t0 = 0;
566 W t1 = 0;
567 asm(R"(
568 mul %[t0], %[x], %[y]
569 umulh %[t1], %[x], %[y]
570 adds %[w0], %[w0], %[t0]
571 adcs %[w1], %[w1], %[t1]
572 adc %[w2], %[w2], xzr
573 )"
574 : [w0] "+r"(m_w0), [w1] "+r"(m_w1), [w2] "+r"(m_w2), [t0] "=&r"(t0), [t1] "=&r"(t1)
575 : [x] "r"(x), [y] "r"(y)
576 : "cc");
577 return;
578 }
579 }
580 #endif
581
582 typedef typename WordInfo<W>::dword dword;
583 const auto z = dword(x) * y;
584 const auto z0 = static_cast<W>(z);
585 const auto z1 = static_cast<W>(z >> WordInfo<W>::bits);
586
587 W carry = 0;
588 m_w0 = word_add(m_w0, z0, &carry);
589 m_w1 = word_add(m_w1, z1, &carry);
590 m_w2 += carry;
591 }

References Botan::word_add().

Referenced by Botan::bigint_comba_mul16(), Botan::bigint_comba_mul24(), Botan::bigint_comba_mul4(), Botan::bigint_comba_mul6(), Botan::bigint_comba_mul7(), Botan::bigint_comba_mul8(), Botan::bigint_comba_mul9(), Botan::bigint_comba_sqr16(), Botan::bigint_comba_sqr24(), Botan::bigint_comba_sqr4(), Botan::bigint_comba_sqr6(), Botan::bigint_comba_sqr7(), Botan::bigint_comba_sqr8(), Botan::bigint_comba_sqr9(), Botan::bigint_monty_redc_12(), Botan::bigint_monty_redc_16(), Botan::bigint_monty_redc_24(), Botan::bigint_monty_redc_32(), Botan::bigint_monty_redc_4(), Botan::bigint_monty_redc_6(), Botan::bigint_monty_redc_8(), Botan::comba_mul(), Botan::comba_sqr(), Botan::monty_redc(), Botan::monty_redc_pdash1(), and monty_step().

◆ mul_x2()

template<WordType W>
void Botan::word3< W >::mul_x2 ( W x,
W y )
inlineconstexpr

Definition at line 593 of file mp_asmi.h.

593 {
594 #if defined(BOTAN_MP_USE_X86_64_ASM)
595 if constexpr(std::same_as<W, uint64_t>) {
597 W z0 = 0;
598 W z1 = 0;
599
600 asm("mulq %[y]" : "=a"(z0), "=d"(z1) : "a"(x), [y] "rm"(y) : "cc");
601
602 asm(R"(
603 addq %[z0],%[w0]
604 adcq %[z1],%[w1]
605 adcq $0,%[w2]
606
607 addq %[z0],%[w0]
608 adcq %[z1],%[w1]
609 adcq $0,%[w2]
610 )"
611 : [w0] "=r"(m_w0), [w1] "=r"(m_w1), [w2] "=r"(m_w2)
612 : [z0] "r"(z0), [z1] "r"(z1), "0"(m_w0), "1"(m_w1), "2"(m_w2)
613 : "cc");
614 return;
615 }
616 }
617 #elif defined(BOTAN_MP_USE_AARCH64_ASM)
618 if constexpr(std::same_as<W, uint64_t>) {
620 W t0 = 0;
621 W t1 = 0;
622 asm(R"(
623 mul %[t0], %[x], %[y]
624 umulh %[t1], %[x], %[y]
625 adds %[w0], %[w0], %[t0]
626 adcs %[w1], %[w1], %[t1]
627 adc %[w2], %[w2], xzr
628 adds %[w0], %[w0], %[t0]
629 adcs %[w1], %[w1], %[t1]
630 adc %[w2], %[w2], xzr
631 )"
632 : [w0] "+r"(m_w0), [w1] "+r"(m_w1), [w2] "+r"(m_w2), [t0] "=&r"(t0), [t1] "=&r"(t1)
633 : [x] "r"(x), [y] "r"(y)
634 : "cc");
635 return;
636 }
637 }
638 #endif
639
640 typedef typename WordInfo<W>::dword dword;
641 const auto z = dword(x) * y;
642 const auto z0 = static_cast<W>(z);
643 const auto z1 = static_cast<W>(z >> WordInfo<W>::bits);
644
645 W carry = 0;
646 m_w0 = word_add(m_w0, z0, &carry);
647 m_w1 = word_add(m_w1, z1, &carry);
648 m_w2 += carry;
649
650 carry = 0;
651 m_w0 = word_add(m_w0, z0, &carry);
652 m_w1 = word_add(m_w1, z1, &carry);
653 m_w2 += carry;
654 }

References Botan::word_add().

Referenced by Botan::bigint_comba_sqr16(), Botan::bigint_comba_sqr24(), Botan::bigint_comba_sqr4(), Botan::bigint_comba_sqr6(), Botan::bigint_comba_sqr7(), Botan::bigint_comba_sqr8(), and Botan::bigint_comba_sqr9().


The documentation for this class was generated from the following file: