Botan 2.13.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 BigInt z = x;
22 secure_vector<word> ws;
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
35 BigInt::Sign sign = BigInt::Positive;
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 }
Botan::mul_add
BigInt mul_add(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:30
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:584
Botan::Invalid_Argument
Definition: exceptn.h:136
Botan::BigInt
Definition: bigint.h:24
Botan::BigInt::Positive
Definition: bigint.h:35
Botan::mul_sub
BigInt mul_sub(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:73
Botan::BigInt::mutable_data
word * mutable_data()
Definition: bigint.h:612
Botan::BigInt::is_negative
bool is_negative() const
Definition: bigint.h:525
Botan::BigInt::size
size_t size() const
Definition: bigint.h:578
Botan::sub_mul
BigInt sub_mul(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:59
Botan::BigInt::Negative
Definition: bigint.h:35
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:65
Botan::BigInt::Sign
Sign
Definition: bigint.h:35
Botan::square
BigInt square(const BigInt &x)
Definition: mp_numth.cpp:19
Botan::BigInt::data
const word * data() const
Definition: bigint.h:618
Botan
Definition: alg_id.cpp:13
Botan::BigInt::square
BigInt & square(secure_vector< word > &ws)
Definition: big_ops2.cpp:216
Botan::bigint_mul
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:298
Botan::bigint_add2
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.h:280
Botan::BigInt::is_zero
bool is_zero() const
Definition: bigint.h:419
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:537