~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/mp_numth.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*************************************************
2
 
* Fused and Important MP Algorithms Source File  *
3
 
* (C) 1999-2007 The Botan Project                *
4
 
*************************************************/
5
 
 
6
 
#include <botan/numthry.h>
7
 
#include <botan/mp_core.h>
8
 
#include <botan/util.h>
9
 
#include <algorithm>
10
 
 
11
 
namespace Botan {
12
 
 
13
 
/*************************************************
14
 
* Square a BigInt                                *
15
 
*************************************************/
16
 
BigInt square(const BigInt& x)
17
 
   {
18
 
   const u32bit x_sw = x.sig_words();
19
 
 
20
 
   BigInt z(BigInt::Positive, round_up(2*x_sw, 16));
21
 
   SecureVector<word> workspace(z.size());
22
 
 
23
 
   bigint_sqr(z.get_reg(), z.size(), workspace,
24
 
              x.data(), x.size(), x_sw);
25
 
   return z;
26
 
   }
27
 
 
28
 
/*************************************************
29
 
* Multiply-Add Operation                         *
30
 
*************************************************/
31
 
BigInt mul_add(const BigInt& a, const BigInt& b, const BigInt& c)
32
 
   {
33
 
   if(c.is_negative() || c.is_zero())
34
 
      throw Invalid_Argument("mul_add: Third argument must be > 0");
35
 
 
36
 
   BigInt::Sign sign = BigInt::Positive;
37
 
   if(a.sign() != b.sign())
38
 
      sign = BigInt::Negative;
39
 
 
40
 
   const u32bit a_sw = a.sig_words();
41
 
   const u32bit b_sw = b.sig_words();
42
 
   const u32bit c_sw = c.sig_words();
43
 
 
44
 
   BigInt r(sign, std::max(a.size() + b.size(), c_sw) + 1);
45
 
   SecureVector<word> workspace(r.size());
46
 
 
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);
52
 
   return r;
53
 
   }
54
 
 
55
 
/*************************************************
56
 
* Subtract-Multiply Operation                    *
57
 
*************************************************/
58
 
BigInt sub_mul(const BigInt& a, const BigInt& b, const BigInt& c)
59
 
   {
60
 
   if(a.is_negative() || b.is_negative())
61
 
      throw Invalid_Argument("sub_mul: First two arguments must be >= 0");
62
 
 
63
 
   BigInt r = a;
64
 
   r -= b;
65
 
   r *= c;
66
 
   return r;
67
 
   }
68
 
 
69
 
}