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

« back to all changes in this revision

Viewing changes to src/util.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
 
* Utility Functions Source File                  *
3
 
* (C) 1999-2007 The Botan Project                *
4
 
*************************************************/
5
 
 
6
 
#include <botan/util.h>
7
 
#include <botan/bit_ops.h>
8
 
#include <algorithm>
9
 
#include <cmath>
10
 
 
11
 
namespace Botan {
12
 
 
13
 
/*************************************************
14
 
* Round up n to multiple of align_to             *
15
 
*************************************************/
16
 
u32bit round_up(u32bit n, u32bit align_to)
17
 
   {
18
 
   if(n % align_to || n == 0)
19
 
      n += align_to - (n % align_to);
20
 
   return n;
21
 
   }
22
 
 
23
 
/*************************************************
24
 
* Round down n to multiple of align_to           *
25
 
*************************************************/
26
 
u32bit round_down(u32bit n, u32bit align_to)
27
 
   {
28
 
   return (n - (n % align_to));
29
 
   }
30
 
 
31
 
/*************************************************
32
 
* Return the work required for solving DL        *
33
 
*************************************************/
34
 
u32bit dl_work_factor(u32bit n_bits)
35
 
   {
36
 
   const u32bit MIN_ESTIMATE = 64;
37
 
 
38
 
   if(n_bits < 32)
39
 
      return 0;
40
 
 
41
 
   const double log_x = n_bits / 1.44;
42
 
 
43
 
   u32bit estimate = (u32bit)(2.76 * std::pow(log_x, 1.0/3.0) *
44
 
                                     std::pow(std::log(log_x), 2.0/3.0));
45
 
 
46
 
   return std::max(estimate, MIN_ESTIMATE);
47
 
   }
48
 
 
49
 
/*************************************************
50
 
* Estimate the entropy of the buffer             *
51
 
*************************************************/
52
 
u32bit entropy_estimate(const byte buffer[], u32bit length)
53
 
   {
54
 
   if(length <= 4)
55
 
      return 0;
56
 
 
57
 
   u32bit estimate = 0;
58
 
   byte last = 0, last_delta = 0, last_delta2 = 0;
59
 
 
60
 
   for(u32bit j = 0; j != length; ++j)
61
 
      {
62
 
      byte delta = last ^ buffer[j];
63
 
      last = buffer[j];
64
 
 
65
 
      byte delta2 = delta ^ last_delta;
66
 
      last_delta = delta;
67
 
 
68
 
      byte delta3 = delta2 ^ last_delta2;
69
 
      last_delta2 = delta2;
70
 
 
71
 
      byte min_delta = delta;
72
 
      if(min_delta > delta2) min_delta = delta2;
73
 
      if(min_delta > delta3) min_delta = delta3;
74
 
 
75
 
      estimate += hamming_weight(min_delta);
76
 
      }
77
 
 
78
 
   return (estimate / 2);
79
 
   }
80
 
 
81
 
}