1
/*************************************************
2
* Utility Functions Source File *
3
* (C) 1999-2007 The Botan Project *
4
*************************************************/
6
#include <botan/util.h>
7
#include <botan/bit_ops.h>
13
/*************************************************
14
* Round up n to multiple of align_to *
15
*************************************************/
16
u32bit round_up(u32bit n, u32bit align_to)
18
if(n % align_to || n == 0)
19
n += align_to - (n % align_to);
23
/*************************************************
24
* Round down n to multiple of align_to *
25
*************************************************/
26
u32bit round_down(u32bit n, u32bit align_to)
28
return (n - (n % align_to));
31
/*************************************************
32
* Return the work required for solving DL *
33
*************************************************/
34
u32bit dl_work_factor(u32bit n_bits)
36
const u32bit MIN_ESTIMATE = 64;
41
const double log_x = n_bits / 1.44;
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));
46
return std::max(estimate, MIN_ESTIMATE);
49
/*************************************************
50
* Estimate the entropy of the buffer *
51
*************************************************/
52
u32bit entropy_estimate(const byte buffer[], u32bit length)
58
byte last = 0, last_delta = 0, last_delta2 = 0;
60
for(u32bit j = 0; j != length; ++j)
62
byte delta = last ^ buffer[j];
65
byte delta2 = delta ^ last_delta;
68
byte delta3 = delta2 ^ last_delta2;
71
byte min_delta = delta;
72
if(min_delta > delta2) min_delta = delta2;
73
if(min_delta > delta3) min_delta = delta3;
75
estimate += hamming_weight(min_delta);
78
return (estimate / 2);