/src/botan/src/lib/math/numbertheory/mp_numth.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 4.11M | { |
21 | 4.11M | BigInt z = x; |
22 | 4.11M | secure_vector<word> ws; |
23 | 4.11M | z.square(ws); |
24 | 4.11M | return z; |
25 | 4.11M | } |
26 | | |
27 | | /* |
28 | | * Multiply-Add Operation |
29 | | */ |
30 | | BigInt mul_add(const BigInt& a, const BigInt& b, const BigInt& c) |
31 | 0 | { |
32 | 0 | if(c.is_negative()) |
33 | 0 | throw Invalid_Argument("mul_add: Third argument must be > 0"); |
34 | 0 | |
35 | 0 | BigInt::Sign sign = BigInt::Positive; |
36 | 0 | if(a.sign() != b.sign()) |
37 | 0 | sign = BigInt::Negative; |
38 | 0 |
|
39 | 0 | const size_t a_sw = a.sig_words(); |
40 | 0 | const size_t b_sw = b.sig_words(); |
41 | 0 | const size_t c_sw = c.sig_words(); |
42 | 0 |
|
43 | 0 | BigInt r(sign, std::max(a_sw + b_sw, c_sw) + 1); |
44 | 0 | secure_vector<word> workspace(r.size()); |
45 | 0 |
|
46 | 0 | bigint_mul(r.mutable_data(), r.size(), |
47 | 0 | a.data(), a.size(), a_sw, |
48 | 0 | b.data(), b.size(), b_sw, |
49 | 0 | workspace.data(), workspace.size()); |
50 | 0 |
|
51 | 0 | const size_t r_size = std::max(r.sig_words(), c_sw); |
52 | 0 | bigint_add2(r.mutable_data(), r_size, c.data(), c_sw); |
53 | 0 | return r; |
54 | 0 | } |
55 | | |
56 | | /* |
57 | | * Subtract-Multiply Operation |
58 | | */ |
59 | | BigInt sub_mul(const BigInt& a, const BigInt& b, const BigInt& c) |
60 | 0 | { |
61 | 0 | if(a.is_negative() || b.is_negative()) |
62 | 0 | throw Invalid_Argument("sub_mul: First two arguments must be >= 0"); |
63 | 0 | |
64 | 0 | BigInt r = a; |
65 | 0 | r -= b; |
66 | 0 | r *= c; |
67 | 0 | return r; |
68 | 0 | } |
69 | | |
70 | | /* |
71 | | * Multiply-Subtract Operation |
72 | | */ |
73 | | BigInt mul_sub(const BigInt& a, const BigInt& b, const BigInt& c) |
74 | 0 | { |
75 | 0 | if(c.is_negative() || c.is_zero()) |
76 | 0 | throw Invalid_Argument("mul_sub: Third argument must be > 0"); |
77 | 0 | |
78 | 0 | BigInt r = a; |
79 | 0 | r *= b; |
80 | 0 | r -= c; |
81 | 0 | return r; |
82 | 0 | } |
83 | | |
84 | | } |