Crypto++ 8.7
Free C++ class library of cryptographic schemes
blumshub.cpp
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
7NAMESPACE_BEGIN(CryptoPP)
8
10 : modn(n),
11 current(modn.Square(modn.Square(seed))),
12 maxBits(BitPrecision(n.BitCount())-1),
13 bitsLeft(maxBits)
14{
15}
16
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
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
36void PublicBlumBlumShub::GenerateBlock(byte *output, size_t size)
37{
38 while (size--)
40}
41
42void PublicBlumBlumShub::ProcessData(byte *outString, const byte *inString, size_t length)
43{
44 while (length--)
45 *outString++ = *inString++ ^ PublicBlumBlumShub::GenerateByte();
46}
47
48BlumBlumShub::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
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
64NAMESPACE_END
Classes for Blum Blum Shub generator.
BlumBlumShub(const Integer &p, const Integer &q, const Integer &seed)
Construct a BlumBlumShub.
Definition: blumshub.cpp:48
void Seek(lword index)
Seek to an absolute position.
Definition: blumshub.cpp:55
Multiple precision integer with arithmetic operations.
Definition: integer.h:50
bool GetBit(size_t i) const
Provides the i-th bit of the Integer.
@ POSITIVE
the value is positive or 0
Definition: integer.h:75
const Integer & Square(const Integer &a) const
Square an element in the ring.
Definition: modarith.h:197
BlumBlumShub without factorization of the modulus.
Definition: blumshub.h:20
unsigned int GenerateBit()
Generate new random bit and return it.
Definition: blumshub.cpp:17
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: blumshub.cpp:36
byte GenerateByte()
Generate new random byte and return it.
Definition: blumshub.cpp:28
void ProcessData(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt an array of bytes.
Definition: blumshub.cpp:42
Square block cipher.
Definition: square.h:25
word64 lword
Large word type.
Definition: config_int.h:158
Multiple precision integer with arithmetic operations.
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
Definition: misc.h:842
Crypto++ library namespace.
Precompiled header file.