Botan  2.7.0
Crypto and TLS for C++11
Public Member Functions | List of all members
Botan::Modular_Reducer Class Reference

#include <reducer.h>

Public Member Functions

BigInt cube (const BigInt &x) const
 
const BigIntget_modulus () const
 
bool initialized () const
 
 Modular_Reducer ()
 
 Modular_Reducer (const BigInt &mod)
 
BigInt multiply (const BigInt &x, const BigInt &y) const
 
BigInt reduce (const BigInt &x) const
 
BigInt square (const BigInt &x) const
 

Detailed Description

Modular Reducer (using Barrett's technique)

Definition at line 18 of file reducer.h.

Constructor & Destructor Documentation

◆ Modular_Reducer() [1/2]

Botan::Modular_Reducer::Modular_Reducer ( )
inline

Definition at line 52 of file reducer.h.

52 { m_mod_words = 0; }

◆ Modular_Reducer() [2/2]

Botan::Modular_Reducer::Modular_Reducer ( const BigInt mod)
explicit

Definition at line 16 of file reducer.cpp.

References Botan::BigInt::power_of_2(), Botan::BigInt::sig_words(), and Botan::square().

17  {
18  if(mod < 0)
19  throw Invalid_Argument("Modular_Reducer: modulus must be positive");
20 
21  // Left uninitialized if mod == 0
22  m_mod_words = 0;
23 
24  if(mod > 0)
25  {
26  m_modulus = mod;
27  m_mod_words = m_modulus.sig_words();
28 
29  m_modulus_2 = Botan::square(m_modulus);
30 
31  m_mu = BigInt::power_of_2(2 * BOTAN_MP_WORD_BITS * m_mod_words) / m_modulus;
32  }
33  }
BigInt square(const BigInt &x)
Definition: mp_numth.cpp:19
static BigInt power_of_2(size_t n)
Definition: bigint.h:642
size_t sig_words() const
Definition: bigint.h:519

Member Function Documentation

◆ cube()

BigInt Botan::Modular_Reducer::cube ( const BigInt x) const
inline

Cube mod p

Parameters
xthe value to cube
Returns
(x * x * x) % p

Definition at line 47 of file reducer.h.

References Botan::square().

Referenced by Botan::EC_Group::verify_group().

48  { return multiply(x, this->square(x)); }
BigInt square(const BigInt &x) const
Definition: reducer.h:39
BigInt multiply(const BigInt &x, const BigInt &y) const
Definition: reducer.h:31

◆ get_modulus()

const BigInt& Botan::Modular_Reducer::get_modulus ( ) const
inline

Definition at line 21 of file reducer.h.

21 { return m_modulus; }

◆ initialized()

bool Botan::Modular_Reducer::initialized ( ) const
inline

Definition at line 50 of file reducer.h.

Referenced by Botan::Blinder::blind(), and Botan::Blinder::unblind().

50 { return (m_mod_words != 0); }

◆ multiply()

BigInt Botan::Modular_Reducer::multiply ( const BigInt x,
const BigInt y 
) const
inline

Multiply mod p

Parameters
xthe first operand
ythe second operand
Returns
(x * y) % p

Definition at line 31 of file reducer.h.

Referenced by Botan::Blinder::blind(), botan_mp_mod_mul(), Botan::Fixed_Window_Exponentiator::execute(), Botan::Montgomery_Params::Montgomery_Params(), Botan::ressol(), Botan::Fixed_Window_Exponentiator::set_base(), Botan::Blinder::unblind(), and Botan::EC_Group::verify_group().

32  { return reduce(x * y); }
BigInt reduce(const BigInt &x) const
Definition: reducer.cpp:38

◆ reduce()

BigInt Botan::Modular_Reducer::reduce ( const BigInt x) const

Definition at line 38 of file reducer.cpp.

References Botan::BigInt::add(), Botan::clear_mem(), Botan::BigInt::cmp(), Botan::BigInt::data(), Botan::BigInt::is_negative(), Botan::BigInt::is_nonzero(), Botan::BigInt::mask_bits(), Botan::BigInt::mul(), Botan::BigInt::Positive, Botan::BigInt::reduce_below(), Botan::BigInt::rev_sub(), Botan::BigInt::set_sign(), Botan::BigInt::sig_words(), and Botan::BigInt::size().

Referenced by Botan::DL_Group::DL_Group(), Botan::generate_dsa_primes(), Botan::Montgomery_Params::Montgomery_Params(), Botan::PointGFp_Base_Point_Precompute::mul(), and Botan::EC_Group::verify_group().

39  {
40  if(m_mod_words == 0)
41  throw Invalid_State("Modular_Reducer: Never initalized");
42 
43  const size_t x_sw = x.sig_words();
44 
45  if(x_sw >= (2*m_mod_words - 1) && x.cmp(m_modulus_2, false) >= 0)
46  {
47  // too big, fall back to normal division
48  return (x % m_modulus);
49  }
50 
51  secure_vector<word> ws;
52 
53  BigInt t1 = x;
54  t1.set_sign(BigInt::Positive);
55  t1 >>= (BOTAN_MP_WORD_BITS * (m_mod_words - 1));
56 
57  t1.mul(m_mu, ws);
58  t1 >>= (BOTAN_MP_WORD_BITS * (m_mod_words + 1));
59 
60  // TODO add masked mul to avoid computing high bits
61  t1.mul(m_modulus, ws);
62  t1.mask_bits(BOTAN_MP_WORD_BITS * (m_mod_words + 1));
63 
64  t1.rev_sub(x.data(), std::min(x_sw, m_mod_words + 1), ws);
65 
66  /*
67  * If t1 < 0 then we must add b^(k+1) where b = 2^w. To avoid a
68  * side channel perform the addition unconditionally, with ws set
69  * to either b^(k+1) or else 0.
70  */
71  const word t1_neg = t1.is_negative();
72 
73  if(ws.size() < m_mod_words + 2)
74  ws.resize(m_mod_words + 2);
75  clear_mem(ws.data(), ws.size());
76  ws[m_mod_words + 1] = t1_neg;
77 
78  t1.add(ws.data(), m_mod_words + 2, BigInt::Positive);
79 
80  t1.reduce_below(m_modulus, ws);
81 
82  if(x.is_negative() && t1.is_nonzero())
83  {
84  t1.rev_sub(m_modulus.data(), m_modulus.size(), ws);
85  }
86 
87  return t1;
88  }
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:97
const word * data() const
Definition: bigint.h:551
size_t size() const
Definition: bigint.h:513

◆ square()

BigInt Botan::Modular_Reducer::square ( const BigInt x) const
inline

Square mod p

Parameters
xthe value to square
Returns
(x * x) % p

Definition at line 39 of file reducer.h.

References Botan::square().

Referenced by Botan::Blinder::blind(), Botan::Fixed_Window_Exponentiator::execute(), Botan::Montgomery_Params::Montgomery_Params(), Botan::ressol(), and Botan::EC_Group::verify_group().

40  { return reduce(Botan::square(x)); }
BigInt square(const BigInt &x)
Definition: mp_numth.cpp:19
BigInt reduce(const BigInt &x) const
Definition: reducer.cpp:38

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