1
/*************************************************
2
* Fused and Important MP Algorithms Source File *
3
* (C) 1999-2007 The Botan Project *
4
*************************************************/
6
#include <botan/numthry.h>
7
#include <botan/mp_core.h>
8
#include <botan/util.h>
13
/*************************************************
15
*************************************************/
16
BigInt square(const BigInt& x)
18
const u32bit x_sw = x.sig_words();
20
BigInt z(BigInt::Positive, round_up(2*x_sw, 16));
21
SecureVector<word> workspace(z.size());
23
bigint_sqr(z.get_reg(), z.size(), workspace,
24
x.data(), x.size(), x_sw);
28
/*************************************************
29
* Multiply-Add Operation *
30
*************************************************/
31
BigInt mul_add(const BigInt& a, const BigInt& b, const BigInt& c)
33
if(c.is_negative() || c.is_zero())
34
throw Invalid_Argument("mul_add: Third argument must be > 0");
36
BigInt::Sign sign = BigInt::Positive;
37
if(a.sign() != b.sign())
38
sign = BigInt::Negative;
40
const u32bit a_sw = a.sig_words();
41
const u32bit b_sw = b.sig_words();
42
const u32bit c_sw = c.sig_words();
44
BigInt r(sign, std::max(a.size() + b.size(), c_sw) + 1);
45
SecureVector<word> workspace(r.size());
47
bigint_mul(r.get_reg(), r.size(), workspace,
48
a.data(), a.size(), a_sw,
49
b.data(), b.size(), b_sw);
50
const u32bit r_size = std::max(r.sig_words(), c_sw);
51
bigint_add2(r.get_reg(), r_size, c.data(), c_sw);
55
/*************************************************
56
* Subtract-Multiply Operation *
57
*************************************************/
58
BigInt sub_mul(const BigInt& a, const BigInt& b, const BigInt& c)
60
if(a.is_negative() || b.is_negative())
61
throw Invalid_Argument("sub_mul: First two arguments must be >= 0");