blumshub.cpp
// blumshub.cpp - written and placed in the public domain by Wei Dai
#include "pch.h"
#include "blumshub.h"
#include "integer.h"
NAMESPACE_BEGIN(CryptoPP)
PublicBlumBlumShub::PublicBlumBlumShub(const Integer &n, const Integer &seed)
: modn(n),
current(modn.Square(modn.Square(seed))),
maxBits(BitPrecision(n.BitCount())-1),
bitsLeft(maxBits)
{
}
unsigned int PublicBlumBlumShub::GenerateBit()
{
if (bitsLeft==0)
{
current = modn.Square(current);
bitsLeft = maxBits;
}
return current.GetBit(--bitsLeft);
}
byte PublicBlumBlumShub::GenerateByte()
{
byte b=0;
for (int i=0; i<8; i++)
b = byte((b << 1) | PublicBlumBlumShub::GenerateBit());
return b;
}
void PublicBlumBlumShub::GenerateBlock(byte *output, size_t size)
{
while (size--)
*output++ = PublicBlumBlumShub::GenerateByte();
}
void PublicBlumBlumShub::ProcessData(byte *outString, const byte *inString, size_t length)
{
while (length--)
*outString++ = *inString++ ^ PublicBlumBlumShub::GenerateByte();
}
BlumBlumShub::BlumBlumShub(const Integer &p, const Integer &q, const Integer &seed)
: PublicBlumBlumShub(p*q, seed),
p(p), q(q),
x0(modn.Square(seed))
{
}
void BlumBlumShub::Seek(lword index)
{
Integer i(Integer::POSITIVE, index);
i *= 8;
Integer e = a_exp_b_mod_c (2, i / maxBits + 1, (p-1)*(q-1));
current = modn.Exponentiate(x0, e);
bitsLeft = maxBits - i % maxBits;
}
NAMESPACE_END
PublicBlumBlumShub::ProcessData
void ProcessData(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt an array of bytes.
Definition: blumshub.cpp:42
BlumBlumShub::Seek
void Seek(lword index)
Seek to an absolute position.
Definition: blumshub.cpp:55
Square
Square block cipher.
Definition: square.h:24
PublicBlumBlumShub::GenerateBit
unsigned int GenerateBit()
Generate new random bit and return it.
Definition: blumshub.cpp:17
PublicBlumBlumShub::GenerateByte
byte GenerateByte()
Generate new random byte and return it.
Definition: blumshub.cpp:28
PublicBlumBlumShub::GenerateBlock
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: blumshub.cpp:36
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:45
blumshub.h
Classes for Blum Blum Shub generator.
integer.h
Multiple precision integer with arithmetic operations.
CryptoPP
Crypto++ library namespace.
PublicBlumBlumShub
BlumBlumShub without factorization of the modulus.
Definition: blumshub.h:17
BitPrecision
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
Definition: misc.h:645
Integer::POSITIVE
the value is positive or 0
Definition: integer.h:71