Fabcoin Core  0.16.2
P2P Digital Currency
blumshub.cpp
Go to the documentation of this file.
1 // blumshub.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "blumshub.h"
5 #include "integer.h"
6 
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  {
22  bitsLeft = maxBits;
23  }
24 
25  return 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 
37 {
38  while (size--)
40 }
41 
42 void PublicBlumBlumShub::ProcessData(byte *outString, const byte *inString, size_t length)
43 {
44  while (length--)
45  *outString++ = *inString++ ^ PublicBlumBlumShub::GenerateByte();
46 }
47 
48 BlumBlumShub::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));
61  bitsLeft = maxBits - i % maxBits;
62 }
63 
Integer current
Definition: blumshub.h:35
uint8_t byte
Definition: Common.h:57
bool GetBit(size_t i) const
Provides the i-th bit of the Integer.
Definition: integer.cpp:3065
const Integer q
Definition: blumshub.h:53
BlumBlumShub(const Integer &p, const Integer &q, const Integer &seed)
Definition: blumshub.cpp:48
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
void ProcessData(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt an array of bytes.
Definition: blumshub.cpp:42
void Seek(lword index)
Seek to an absolute position.
Definition: blumshub.cpp:55
Square block cipher.
Definition: square.h:24
const Integer & Square(const Integer &a) const
Square an element in the ring.
Definition: modarith.h:177
unsigned int GenerateBit()
Generate new random bit and return it.
Definition: blumshub.cpp:17
byte GenerateByte()
Generate new random byte and return it.
Definition: blumshub.cpp:28
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: blumshub.cpp:36
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
#define b(i, j)
ModularArithmetic modn
Definition: blumshub.h:34
uint8_t const size_t const size
Definition: sha3.h:20
Classes for Blum Blum Shub generator.
uint8_t byte
Definition: Common.h:10
Multiple precision integer with arithmetic operations.
const Integer p
Definition: blumshub.h:53
#define NAMESPACE_END
Definition: config.h:201
#define e(i)
Definition: sha.cpp:733
word64 lword
Definition: config.h:245
Integer a_exp_b_mod_c(const Integer &x, const Integer &e, const Integer &m)
Definition: integer.cpp:4359
BlumBlumShub without factorization of the modulus.
Definition: blumshub.h:17
const Integer x0
Definition: blumshub.h:54
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
Definition: misc.h:654
virtual Element Exponentiate(const Element &a, const Integer &e) const
Raises a base to an exponent in the group.
Definition: algebra.cpp:316
the value is positive or 0
Definition: integer.h:69