Botan  2.7.0
Crypto and TLS for C++11
mp_numth.cpp
Go to the documentation of this file.
1 /*
2 * Fused and Important MP Algorithms
3 * (C) 1999-2007 Jack Lloyd
4 * 2016 Matthias Gierlings
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #include <botan/numthry.h>
10 #include <botan/internal/mp_core.h>
11 #include <botan/internal/rounding.h>
12 #include <algorithm>
13 
14 namespace Botan {
15 
16 /*
17 * Square a BigInt
18 */
19 BigInt square(const BigInt& x)
20  {
21  BigInt z = x;
23  z.square(ws);
24  return z;
25  }
26 
27 /*
28 * Multiply-Add Operation
29 */
30 BigInt mul_add(const BigInt& a, const BigInt& b, const BigInt& c)
31  {
32  if(c.is_negative())
33  throw Invalid_Argument("mul_add: Third argument must be > 0");
34 
36  if(a.sign() != b.sign())
37  sign = BigInt::Negative;
38 
39  const size_t a_sw = a.sig_words();
40  const size_t b_sw = b.sig_words();
41  const size_t c_sw = c.sig_words();
42 
43  BigInt r(sign, std::max(a_sw + b_sw, c_sw) + 1);
44  secure_vector<word> workspace(r.size());
45 
46  bigint_mul(r.mutable_data(), r.size(),
47  a.data(), a.size(), a_sw,
48  b.data(), b.size(), b_sw,
49  workspace.data(), workspace.size());
50 
51  const size_t r_size = std::max(r.sig_words(), c_sw);
52  bigint_add2(r.mutable_data(), r_size, c.data(), c_sw);
53  return r;
54  }
55 
56 /*
57 * Subtract-Multiply Operation
58 */
59 BigInt sub_mul(const BigInt& a, const BigInt& b, const BigInt& c)
60  {
61  if(a.is_negative() || b.is_negative())
62  throw Invalid_Argument("sub_mul: First two arguments must be >= 0");
63 
64  BigInt r = a;
65  r -= b;
66  r *= c;
67  return r;
68  }
69 
70 /*
71 * Multiply-Subtract Operation
72 */
73 BigInt mul_sub(const BigInt& a, const BigInt& b, const BigInt& c)
74  {
75  if(c.is_negative() || c.is_zero())
76  throw Invalid_Argument("mul_sub: Third argument must be > 0");
77 
78  BigInt r = a;
79  r *= b;
80  r -= c;
81  return r;
82  }
83 
84 }
BigInt mul_add(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:30
bool is_negative() const
Definition: bigint.h:460
BigInt mul_sub(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:73
Sign sign() const
Definition: bigint.h:472
bool is_zero() const
Definition: bigint.h:355
word * mutable_data()
Definition: bigint.h:545
const word * data() const
Definition: bigint.h:551
BigInt sub_mul(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:59
BigInt square(const BigInt &x)
Definition: mp_numth.cpp:19
size_t size() const
Definition: bigint.h:513
Definition: alg_id.cpp:13
BigInt & square(secure_vector< word > &ws)
Definition: big_ops2.cpp:248
size_t sig_words() const
Definition: bigint.h:519
void bigint_mul(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw, word workspace[], size_t ws_size)
Definition: mp_karat.cpp:293
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:186
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88