Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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() || c.is_zero())
38 throw Invalid_Argument("mul_add: Third argument must be > 0");
39
40 BigInt::Sign sign = BigInt::Positive;
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 }
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:393
Botan::Invalid_Argument
Definition: exceptn.h:34
Botan::BigInt
Definition: bigint.h:23
Botan::BigInt::Positive
Definition: bigint.h:34
Botan::BigInt::mutable_data
word * mutable_data()
Definition: bigint.h:419
Botan::BigInt::is_negative
bool is_negative() const
Definition: bigint.h:348
Botan::BigInt::size
size_t size() const
Definition: bigint.h:387
Botan::BigInt::Negative
Definition: bigint.h:34
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::bigint_sqr
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:312
Botan::BigInt::Sign
Sign
Definition: bigint.h:34
Botan::BigInt::data
const word * data() const
Definition: bigint.h:425
Botan
Definition: alg_id.cpp:13
Botan::CT::max
T max(T a, T b)
Definition: ct_utils.h:173
Botan::sub_mul
BigInt sub_mul(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:57
Botan::bigint_add2
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:138
Botan::square
BigInt square(const BigInt &x)
Definition: mp_numth.cpp:19
Botan::bigint_mul
void bigint_mul(BigInt &z, const BigInt &x, const BigInt &y, word workspace[])
Definition: mp_karat.cpp:252
Botan::BigInt::is_zero
bool is_zero() const
Definition: bigint.h:250
Botan::round_up
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:22
Botan::mul_add
BigInt mul_add(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:35
Botan::mul_sub
BigInt mul_sub(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:71
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:360