Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# blumshub.cpp

    00001 // blumshub.cpp - written and placed in the public domain by Wei Dai
    00002
    00003 #include "pch.h"
    00004 #include "blumshub.h"
    00005
    00006 NAMESPACE_BEGIN(CryptoPP)
    00007
    00008 PublicBlumBlumShub::PublicBlumBlumShub(const Integer &n, const Integer &seed)
    00009         : modn(n),
    00010           maxBits(BitPrecision(n.BitCount())-1)
    00011 {
    00012         current = modn.Square(modn.Square(seed));
    00013         bitsLeft = maxBits;
    00014 }
    00015
    00016 unsigned int PublicBlumBlumShub::GenerateBit()
    00017 {
    00018         if (bitsLeft==0)
    00019         {
    00020                 current = modn.Square(current);
    00021                 bitsLeft = maxBits;
    00022         }
    00023
    00024         return current.GetBit(--bitsLeft);
    00025 }
    00026
    00027 byte PublicBlumBlumShub::GenerateByte()
    00028 {
    00029         byte b=0;
    00030         for (int i=0; i<8; i++)
    00031                 b = (b << 1) | PublicBlumBlumShub::GenerateBit();
    00032         return b;
    00033 }
    00034
    00035 BlumBlumShub::BlumBlumShub(const Integer &p, const Integer &q, const Integer &seed)
    00036         : PublicBlumBlumShub(p*q, seed),
    00037           p(p), q(q),
    00038           x0(modn.Square(seed))
    00039 {
    00040 }
    00041
    00042 void BlumBlumShub::Seek(dword index)
    00043 {
    00044         Integer e = a_exp_b_mod_c (2, ((index*8) / maxBits + 1), (p-1)*(q-1));
    00045         current = modn.Exponentiate(x0, e);
    00046         bitsLeft = maxBits - int((index*8) % maxBits);
    00047 }
    00048
    00049 NAMESPACE_END