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