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.

99 lines
1.6 KiB

  1. // gf2_32.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #include "misc.h"
  4. #include "gf2_32.h"
  5. NAMESPACE_BEGIN(CryptoPP)
  6. GF2_32::Element GF2_32::Multiply(Element a, Element b) const
  7. {
  8. word32 table[4];
  9. table[0] = 0;
  10. table[1] = m_modulus;
  11. if (a & 0x80000000)
  12. {
  13. table[2] = m_modulus ^ (a<<1);
  14. table[3] = a<<1;
  15. }
  16. else
  17. {
  18. table[2] = a<<1;
  19. table[3] = m_modulus ^ (a<<1);
  20. }
  21. #if CRYPTOPP_FAST_ROTATE(32)
  22. b = rotrFixed(b, 30U);
  23. word32 result = table[b&2];
  24. for (int i=29; i>=0; --i)
  25. {
  26. b = rotlFixed(b, 1U);
  27. result = (result<<1) ^ table[(b&2) + (result>>31)];
  28. }
  29. return (b&1) ? result ^ a : result;
  30. #else
  31. word32 result = table[(b>>30) & 2];
  32. for (int i=29; i>=0; --i)
  33. result = (result<<1) ^ table[((b>>i)&2) + (result>>31)];
  34. return (b&1) ? result ^ a : result;
  35. #endif
  36. }
  37. GF2_32::Element GF2_32::MultiplicativeInverse(Element a) const
  38. {
  39. if (a <= 1) // 1 is a special case
  40. return a;
  41. // warning - don't try to adapt this algorithm for another situation
  42. word32 g0=m_modulus, g1=a, g2=a;
  43. word32 v0=0, v1=1, v2=1;
  44. assert(g1);
  45. while (!(g2 & 0x80000000))
  46. {
  47. g2 <<= 1;
  48. v2 <<= 1;
  49. }
  50. g2 <<= 1;
  51. v2 <<= 1;
  52. g0 ^= g2;
  53. v0 ^= v2;
  54. while (g0 != 1)
  55. {
  56. if (g1 < g0 || ((g0^g1) < g0 && (g0^g1) < g1))
  57. {
  58. assert(BitPrecision(g1) <= BitPrecision(g0));
  59. g2 = g1;
  60. v2 = v1;
  61. }
  62. else
  63. {
  64. assert(BitPrecision(g1) > BitPrecision(g0));
  65. g2 = g0; g0 = g1; g1 = g2;
  66. v2 = v0; v0 = v1; v1 = v2;
  67. }
  68. while ((g0^g2) >= g2)
  69. {
  70. assert(BitPrecision(g0) > BitPrecision(g2));
  71. g2 <<= 1;
  72. v2 <<= 1;
  73. }
  74. assert(BitPrecision(g0) == BitPrecision(g2));
  75. g0 ^= g2;
  76. v0 ^= v2;
  77. }
  78. return v0;
  79. }
  80. NAMESPACE_END