diff options
Diffstat (limited to 'c5/blumshub.cpp')
-rw-r--r-- | c5/blumshub.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/c5/blumshub.cpp b/c5/blumshub.cpp new file mode 100644 index 0000000..f715c84 --- /dev/null +++ b/c5/blumshub.cpp @@ -0,0 +1,49 @@ +// blumshub.cpp - written and placed in the public domain by Wei Dai + +#include "pch.h" +#include "blumshub.h" + +NAMESPACE_BEGIN(CryptoPP) + +PublicBlumBlumShub::PublicBlumBlumShub(const Integer &n, const Integer &seed) + : modn(n), + maxBits(BitPrecision(n.BitCount())-1) +{ + current = modn.Square(modn.Square(seed)); + 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 = (b << 1) | PublicBlumBlumShub::GenerateBit(); + return b; +} + +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(dword index) +{ + Integer e = a_exp_b_mod_c (2, ((index*8) / maxBits + 1), (p-1)*(q-1)); + current = modn.Exponentiate(x0, e); + bitsLeft = maxBits - int((index*8) % maxBits); +} + +NAMESPACE_END |