Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/SharedDependencies/Sources/cryptopp/blumshub.cpp
2 views
1
// blumshub.cpp - originally written and placed in the public domain by Wei Dai
2
3
#include "pch.h"
4
#include "blumshub.h"
5
#include "integer.h"
6
7
NAMESPACE_BEGIN(CryptoPP)
8
9
PublicBlumBlumShub::PublicBlumBlumShub(const Integer &n, const Integer &seed)
10
: modn(n),
11
current(modn.Square(modn.Square(seed))),
12
maxBits(BitPrecision(n.BitCount())-1),
13
bitsLeft(maxBits)
14
{
15
}
16
17
unsigned int PublicBlumBlumShub::GenerateBit()
18
{
19
if (bitsLeft==0)
20
{
21
current = modn.Square(current);
22
bitsLeft = maxBits;
23
}
24
25
return static_cast<unsigned int>(current.GetBit(--bitsLeft));
26
}
27
28
byte PublicBlumBlumShub::GenerateByte()
29
{
30
byte b=0;
31
for (int i=0; i<8; i++)
32
b = byte((b << 1) | PublicBlumBlumShub::GenerateBit());
33
return b;
34
}
35
36
void PublicBlumBlumShub::GenerateBlock(byte *output, size_t size)
37
{
38
while (size--)
39
*output++ = PublicBlumBlumShub::GenerateByte();
40
}
41
42
void PublicBlumBlumShub::ProcessData(byte *outString, const byte *inString, size_t length)
43
{
44
while (length--)
45
*outString++ = *inString++ ^ PublicBlumBlumShub::GenerateByte();
46
}
47
48
BlumBlumShub::BlumBlumShub(const Integer &p, const Integer &q, const Integer &seed)
49
: PublicBlumBlumShub(p*q, seed),
50
p(p), q(q),
51
x0(modn.Square(seed))
52
{
53
}
54
55
void BlumBlumShub::Seek(lword index)
56
{
57
Integer i(Integer::POSITIVE, index);
58
i *= 8;
59
Integer e = a_exp_b_mod_c (2, i / maxBits + 1, (p-1)*(q-1));
60
current = modn.Exponentiate(x0, e);
61
bitsLeft = maxBits - i % maxBits;
62
}
63
64
NAMESPACE_END
65
66