Botan 3.11.0
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 454 of file mp_asmi.h.

Constructor & Destructor Documentation

◆ word3()

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

Definition at line 493 of file mp_asmi.h.

493: 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 618 of file mp_asmi.h.

618 {
619 W r = m_w0 * p_dash;
620 mul(r, p0);
621 m_w0 = m_w1;
622 m_w1 = m_w2;
623 m_w2 = 0;
624 return r;
625 }
constexpr void mul(W x, W y)
Definition mp_asmi.h:495

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 627 of file mp_asmi.h.

627 {
628 // If p_dash == 1 then p[0] = -1 and everything simplifies
629 const W r = m_w0;
630 m_w0 += m_w1;
631 m_w1 = m_w2 + (m_w0 < m_w1);
632 m_w2 = 0;
633 return r;
634 }

Referenced by Botan::monty_redc_pdash1().

◆ mul()

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

Definition at line 495 of file mp_asmi.h.

495 {
496 #if defined(BOTAN_MP_USE_X86_64_ASM)
498 W z0 = 0;
499 W z1 = 0;
500
501 asm("mulq %[y]" : "=a"(z0), "=d"(z1) : "a"(x), [y] "rm"(y) : "cc");
502
503 asm(R"(
504 addq %[z0],%[w0]
505 adcq %[z1],%[w1]
506 adcq $0,%[w2]
507 )"
508 : [w0] "=r"(m_w0), [w1] "=r"(m_w1), [w2] "=r"(m_w2)
509 : [z0] "r"(z0), [z1] "r"(z1), "0"(m_w0), "1"(m_w1), "2"(m_w2)
510 : "cc");
511 return;
512 }
513 #elif defined(BOTAN_MP_USE_AARCH64_ASM)
515 W t0 = 0;
516 W t1 = 0;
517 asm(R"(
518 mul %[t0], %[x], %[y]
519 umulh %[t1], %[x], %[y]
520 adds %[w0], %[w0], %[t0]
521 adcs %[w1], %[w1], %[t1]
522 adc %[w2], %[w2], xzr
523 )"
524 : [w0] "+r"(m_w0), [w1] "+r"(m_w1), [w2] "+r"(m_w2), [t0] "=&r"(t0), [t1] "=&r"(t1)
525 : [x] "r"(x), [y] "r"(y)
526 : "cc");
527 return;
528 }
529 #endif
530
531 typedef typename WordInfo<W>::dword dword;
532 const auto z = dword(x) * y;
533 const auto z0 = static_cast<W>(z);
534 const auto z1 = static_cast<W>(z >> WordInfo<W>::bits);
535
536 W carry = 0;
537 m_w0 = word_add(m_w0, z0, &carry);
538 m_w1 = word_add(m_w1, z1, &carry);
539 m_w2 += carry;
540 }

References Botan::carry(), and 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 542 of file mp_asmi.h.

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

References Botan::carry(), and 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: