Botan 1.10.17

mp_numth.cpp
Go to the documentation of this file.
1 /*
2 * Fused and Important MP Algorithms
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/numthry.h>
9 #include <botan/internal/mp_core.h>
10 #include <botan/internal/rounding.h>
11 #include <algorithm>
12
13 namespace Botan {
14
15 /*
16 * Square a BigInt
17 */
18 BigInt square(const BigInt& x)
19 {
20 const size_t x_sw = x.sig_words();
21
22 BigInt z(BigInt::Positive, round_up<size_t>(2*x_sw, 16));
23 SecureVector<word> workspace(z.size());
24
25 bigint_sqr(z.get_reg(), z.size(), workspace,
26 x.data(), x.size(), x_sw);
27 return z;
28 }
29
30 /*
31 * Multiply-Add Operation
32 */
33 BigInt mul_add(const BigInt& a, const BigInt& b, const BigInt& c)
34 {
35 if(c.is_negative() || c.is_zero())
36 throw Invalid_Argument("mul_add: Third argument must be > 0");
37
38 BigInt::Sign sign = BigInt::Positive;
39 if(a.sign() != b.sign())
40 sign = BigInt::Negative;
41
42 const size_t a_sw = a.sig_words();
43 const size_t b_sw = b.sig_words();
44 const size_t c_sw = c.sig_words();
45
46 BigInt r(sign, std::max(a.size() + b.size(), c_sw) + 1);
47 SecureVector<word> workspace(r.size());
48
49 bigint_mul(r.get_reg(), r.size(), workspace,
50 a.data(), a.size(), a_sw,
51 b.data(), b.size(), b_sw);
52 const size_t r_size = std::max(r.sig_words(), c_sw);
53 bigint_add2(r.get_reg(), r_size, c.data(), c_sw);
54 return r;
55 }
56
57 /*
58 * Subtract-Multiply Operation
59 */
60 BigInt sub_mul(const BigInt& a, const BigInt& b, const BigInt& c)
61 {
62 if(a.is_negative() || b.is_negative())
63 throw Invalid_Argument("sub_mul: First two arguments must be >= 0");
64
65 BigInt r = a;
66 r -= b;
67 r *= c;
68 return r;
69 }
70
71 }
Botan::BigInt::is_negative
bool is_negative() const
Definition: bigint.h:245
Botan::BigInt
Definition: bigint.h:22
Botan::BigInt::Positive
Definition: bigint.h:33
Botan::BigInt::sign
Sign sign() const
Definition: bigint.h:257
Botan::BigInt::get_reg
SecureVector< word > & get_reg()
Definition: bigint.h:325
Botan::BigInt::is_zero
bool is_zero() const
Definition: bigint.h:176
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::BigInt::data
const word * data() const
Definition: bigint.h:317
Botan::BigInt::Negative
Definition: bigint.h:33
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:306
Botan::BigInt::Sign
Sign
Definition: bigint.h:33
Botan::BigInt::size
size_t size() const
Definition: bigint.h:284
Botan
Definition: algo_base.h:14
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290
Botan::CT::max
T max(T a, T b)
Definition: ct_utils.h:120
Botan::bigint_mul
void bigint_mul(word z[], size_t z_size, word workspace[], const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw)
Definition: mp_karat.cpp:249
Botan::sub_mul
BigInt sub_mul(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:60
Botan::bigint_add2
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:139
Botan::square
BigInt square(const BigInt &x)
Definition: mp_numth.cpp:18
Botan::SecureVector< word >
Botan::mul_add
BigInt mul_add(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:33