Team Fortress 2 Source Code as on 22/4/2020
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.

64 lines
1.4 KiB

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