Botan  2.4.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  const size_t x_sw = x.sig_words();
22 
23  BigInt z(BigInt::Positive, round_up(2*x_sw, 16));
24  secure_vector<word> workspace(z.size());
25 
26  bigint_sqr(z.mutable_data(), z.size(),
27  workspace.data(),
28  x.data(), x.size(), x_sw);
29  return z;
30  }
31 
32 /*
33 * Multiply-Add Operation
34 */
35 BigInt mul_add(const BigInt& a, const BigInt& b, const BigInt& c)
36  {
37  if(c.is_negative())
38  throw Invalid_Argument("mul_add: Third argument must be > 0");
39 
41  if(a.sign() != b.sign())
42  sign = BigInt::Negative;
43 
44  BigInt r(sign, std::max(a.size() + b.size(), c.sig_words()) + 1);
45  secure_vector<word> workspace(r.size());
46 
47  bigint_mul(r, a, b, workspace.data());
48 
49  const size_t r_size = std::max(r.sig_words(), c.sig_words());
50  bigint_add2(r.mutable_data(), r_size, c.data(), c.sig_words());
51  return r;
52  }
53 
54 /*
55 * Subtract-Multiply Operation
56 */
57 BigInt sub_mul(const BigInt& a, const BigInt& b, const BigInt& c)
58  {
59  if(a.is_negative() || b.is_negative())
60  throw Invalid_Argument("sub_mul: First two arguments must be >= 0");
61 
62  BigInt r = a;
63  r -= b;
64  r *= c;
65  return r;
66  }
67 
68 /*
69 * Multiply-Subtract Operation
70 */
71 BigInt mul_sub(const BigInt& a, const BigInt& b, const BigInt& c)
72  {
73  if(c.is_negative() || c.is_zero())
74  throw Invalid_Argument("mul_sub: Third argument must be > 0");
75 
76  BigInt r = a;
77  r *= b;
78  r -= c;
79  return r;
80  }
81 
82 }
BigInt mul_add(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:35
bool is_negative() const
Definition: bigint.h:353
BigInt mul_sub(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:71
Sign sign() const
Definition: bigint.h:365
bool is_zero() const
Definition: bigint.h:255
word * mutable_data()
Definition: bigint.h:424
const word * data() const
Definition: bigint.h:430
BigInt sub_mul(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:57
void bigint_sqr(word z[], size_t z_size, word workspace[], const word x[], size_t x_size, size_t x_sw)
Definition: mp_karat.cpp:321
BigInt square(const BigInt &x)
Definition: mp_numth.cpp:19
size_t size() const
Definition: bigint.h:392
Definition: alg_id.cpp:13
size_t sig_words() const
Definition: bigint.h:398
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:138
void bigint_mul(BigInt &z, const BigInt &x, const BigInt &y, word workspace[])
Definition: mp_karat.cpp:253
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:21