Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.3 KiB

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