monotone

monotone Mtn Source Tree

Root/cryptopp/rng.cpp

1// rng.cpp - written and placed in the public domain by Wei Dai
2
3#include "pch.h"
4#include "rng.h"
5
6#include <time.h>
7#include <math.h>
8
9NAMESPACE_BEGIN(CryptoPP)
10
11// linear congruential generator
12// originally by William S. England
13
14// do not use for cryptographic purposes
15
16/*
17** Original_numbers are the original published m and q in the
18** ACM article above. John Burton has furnished numbers for
19** a reportedly better generator. The new numbers are now
20** used in this program by default.
21*/
22
23#ifndef LCRNG_ORIGINAL_NUMBERS
24const word32 LC_RNG::m=2147483647L;
25const word32 LC_RNG::q=44488L;
26
27const word16 LC_RNG::a=(unsigned int)48271L;
28const word16 LC_RNG::r=3399;
29#else
30const word32 LC_RNG::m=2147483647L;
31const word32 LC_RNG::q=127773L;
32
33const word16 LC_RNG::a=16807;
34const word16 LC_RNG::r=2836;
35#endif
36
37byte LC_RNG::GenerateByte()
38{
39word32 hi = seed/q;
40word32 lo = seed%q;
41
42long test = a*lo - r*hi;
43
44if (test > 0)
45seed = test;
46else
47seed = test+ m;
48
49return (GETBYTE(seed, 0) ^ GETBYTE(seed, 1) ^ GETBYTE(seed, 2) ^ GETBYTE(seed, 3));
50}
51
52// ********************************************************
53
54X917RNG::X917RNG(BlockTransformation *c, const byte *seed, unsigned long deterministicTimeVector)
55: cipher(c),
56 S(cipher->BlockSize()),
57 dtbuf(S),
58 randseed(seed, S),
59 randbuf(S),
60 randbuf_counter(0),
61 m_deterministicTimeVector(deterministicTimeVector)
62{
63if (m_deterministicTimeVector)
64{
65memset(dtbuf, 0, S);
66memcpy(dtbuf, (byte *)&m_deterministicTimeVector, STDMIN((int)sizeof(m_deterministicTimeVector), S));
67}
68else
69{
70time_t tstamp1 = time(0);
71xorbuf(dtbuf, (byte *)&tstamp1, STDMIN((int)sizeof(tstamp1), S));
72cipher->ProcessBlock(dtbuf);
73clock_t tstamp2 = clock();
74xorbuf(dtbuf, (byte *)&tstamp2, STDMIN((int)sizeof(tstamp2), S));
75cipher->ProcessBlock(dtbuf);
76}
77}
78
79byte X917RNG::GenerateByte()
80{
81if (randbuf_counter==0)
82{
83// calculate new enciphered timestamp
84if (m_deterministicTimeVector)
85{
86xorbuf(dtbuf, (byte *)&m_deterministicTimeVector, STDMIN((int)sizeof(m_deterministicTimeVector), S));
87while (++m_deterministicTimeVector == 0) {}// skip 0
88}
89else
90{
91clock_t tstamp = clock();
92xorbuf(dtbuf, (byte *)&tstamp, STDMIN((int)sizeof(tstamp), S));
93}
94cipher->ProcessBlock(dtbuf);
95
96// combine enciphered timestamp with seed
97xorbuf(randseed, dtbuf, S);
98
99// generate a new block of random bytes
100cipher->ProcessBlock(randseed, randbuf);
101
102// compute new seed vector
103for (int i=0; i<S; i++)
104randseed[i] = randbuf[i] ^ dtbuf[i];
105cipher->ProcessBlock(randseed);
106
107randbuf_counter=S;
108}
109return(randbuf[--randbuf_counter]);
110}
111
112MaurerRandomnessTest::MaurerRandomnessTest()
113: sum(0.0), n(0)
114{
115for (unsigned i=0; i<V; i++)
116tab[i] = 0;
117}
118
119void MaurerRandomnessTest::Put(byte inByte)
120{
121if (n >= Q)
122sum += log(double(n - tab[inByte]));
123tab[inByte] = n;
124n++;
125}
126
127void MaurerRandomnessTest::Put(const byte *inString, unsigned int length)
128{
129while (length--)
130Put(*inString++);
131}
132
133double MaurerRandomnessTest::GetTestValue() const
134{
135double fTu = (sum/(n-Q))/log(2.0);// this is the test value defined by Maurer
136
137double value = fTu * 0.1392;// arbitrarily normalize it to
138return value > 1.0 ? 1.0 : value;// a number between 0 and 1
139}
140
141NAMESPACE_END

Archive Download this file

Branches

Tags

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