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
13namespace Botan {
14
15/*
16* Square a BigInt
17*/
19 {
20 const size_t x_sw = x.sig_words();
21
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*/
33BigInt 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
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*/
60BigInt 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}
size_t sig_words() const
Definition bigint.h:290
SecureVector< word > & get_reg()
Definition bigint.h:325
size_t size() const
Definition bigint.h:284
const word * data() const
Definition bigint.h:317
Sign sign() const
Definition bigint.h:257
bool is_zero() const
Definition bigint.h:176
bool is_negative() const
Definition bigint.h:245
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
T round_up(T n, T align_to)
Definition rounding.h:22
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
BigInt square(const BigInt &x)
Definition mp_numth.cpp:18
BigInt mul_add(const BigInt &a, const BigInt &b, const BigInt &c)
Definition mp_numth.cpp:33
std::invalid_argument Invalid_Argument
Definition exceptn.h:20
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition mp_asm.cpp:139
BigInt sub_mul(const BigInt &a, const BigInt &b, const BigInt &c)
Definition mp_numth.cpp:60