monotone

monotone Mtn Source Tree

Root/botan/fused.cpp

1/*************************************************
2* Fused Arithmetic Operations Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/numthry.h>
7#include <botan/mp_core.h>
8
9namespace Botan {
10
11/*************************************************
12* Fused Multiply/Add Operation *
13*************************************************/
14BigInt mul_add(const BigInt& a, const BigInt& b, const BigInt& c)
15 {
16 if(c.is_negative() || c.is_zero())
17 throw Invalid_Argument("mul_add: Third argument must be > 0");
18
19 BigInt::Sign sign = BigInt::Positive;
20 if(a.sign() != b.sign())
21 sign = BigInt::Negative;
22
23 const u32bit a_sw = a.sig_words();
24 const u32bit b_sw = b.sig_words();
25 const u32bit c_sw = c.sig_words();
26
27 BigInt r(sign, std::max(a.size() + b.size(), c_sw) + 1);
28 bigint_mul3(r.get_reg(), r.size(),
29 a.data(), a.size(), a_sw,
30 b.data(), b.size(), b_sw);
31 const u32bit r_size = std::max(r.sig_words(), c_sw);
32 bigint_add2(r.get_reg(), r_size, c.data(), c_sw);
33 return r;
34 }
35
36/*************************************************
37* Fused Subtract/Multiply Operation *
38*************************************************/
39BigInt sub_mul(const BigInt& a, const BigInt& b, const BigInt& c)
40 {
41 if(a.is_negative() || b.is_negative())
42 throw Invalid_Argument("sub_mul: First two arguments must be >= 0");
43
44 BigInt r = a;
45 r -= b;
46 r *= c;
47 return r;
48 }
49
50/*************************************************
51* Fused Multiply/Modulo Operation *
52*************************************************/
53BigInt mul_mod(const BigInt& a, const BigInt& b, const BigInt& m)
54 {
55 if(a.is_negative() || b.is_negative())
56 throw Invalid_Argument("mul_mod: First two arguments must be >= 0");
57 if(m <= 0)
58 throw Invalid_Argument("mul_mod: Modulo must be positive");
59
60 BigInt r = a;
61 r *= b;
62 r %= m;
63 return r;
64 }
65
66}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status