lookup3.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "lookup3.h"
  2. namespace sunrise::middleware::crypto::lookup3 {
  3. namespace {
  4. /** Words one main round consumes. */
  5. constexpr std::size_t kRoundWords = 3;
  6. /** Bytes in one word. */
  7. constexpr std::size_t kWordBytes = 4;
  8. /** Bits in one word. */
  9. constexpr unsigned kWordBits = 32;
  10. /** Bits in one byte. */
  11. constexpr unsigned kByteBits = 8;
  12. /** @return Value rotated left by count. */
  13. [[nodiscard]] constexpr std::uint32_t rotate(std::uint32_t value, unsigned count) noexcept {
  14. return (value << count) | (value >> (kWordBits - count));
  15. }
  16. /** Mixes three accumulators reversibly. Every rotation constant is part of the algorithm. */
  17. constexpr void mix(std::uint32_t& a, std::uint32_t& b, std::uint32_t& c) noexcept {
  18. a -= c;
  19. a ^= rotate(c, 4);
  20. c += b;
  21. b -= a;
  22. b ^= rotate(a, 6);
  23. a += c;
  24. c -= b;
  25. c ^= rotate(b, 8);
  26. b += a;
  27. a -= c;
  28. a ^= rotate(c, 16);
  29. c += b;
  30. b -= a;
  31. b ^= rotate(a, 19);
  32. a += c;
  33. c -= b;
  34. c ^= rotate(b, 4);
  35. b += a;
  36. }
  37. /** Mixes the three accumulators one way, so only c is worth reading afterwards. */
  38. constexpr void finish(std::uint32_t& a, std::uint32_t& b, std::uint32_t& c) noexcept {
  39. c ^= b;
  40. c -= rotate(b, 14);
  41. a ^= c;
  42. a -= rotate(c, 11);
  43. b ^= a;
  44. b -= rotate(a, 25);
  45. c ^= b;
  46. c -= rotate(b, 16);
  47. a ^= c;
  48. a -= rotate(c, 4);
  49. b ^= a;
  50. b -= rotate(a, 14);
  51. c ^= b;
  52. c -= rotate(b, 24);
  53. }
  54. } // namespace
  55. /** Hashes a run of 32-bit words. */
  56. std::uint32_t hash_words(std::span<const std::uint32_t> words, std::uint32_t initial) noexcept {
  57. std::uint32_t a = initial;
  58. std::uint32_t b = initial;
  59. std::uint32_t c = initial;
  60. std::size_t index = 0;
  61. // The main rounds stop with one to three words left, unless the input is empty.
  62. while (words.size() - index > kRoundWords) {
  63. a += words[index];
  64. b += words[index + 1];
  65. c += words[index + 2];
  66. mix(a, b, c);
  67. index += kRoundWords;
  68. }
  69. const std::size_t remaining = words.size() - index;
  70. if (remaining == 0) {
  71. return c;
  72. }
  73. if (remaining == kRoundWords) {
  74. c += words[index + 2];
  75. }
  76. if (remaining >= 2) {
  77. b += words[index + 1];
  78. }
  79. a += words[index];
  80. finish(a, b, c);
  81. return c;
  82. }
  83. /** Hashes a byte buffer as little-endian words. */
  84. std::uint32_t hash_bytes(std::span<const std::byte> bytes, std::uint32_t initial) noexcept {
  85. if (bytes.size() % kWordBytes != 0) {
  86. return 0;
  87. }
  88. std::uint32_t a = initial;
  89. std::uint32_t b = initial;
  90. std::uint32_t c = initial;
  91. const std::size_t count = bytes.size() / kWordBytes;
  92. // Bytes are assembled, not reinterpreted: the span has no alignment guarantee.
  93. auto word = [bytes](std::size_t position) noexcept {
  94. std::uint32_t value = 0;
  95. for (std::size_t offset = 0; offset < kWordBytes; ++offset) {
  96. value |= std::to_integer<std::uint32_t>(bytes[position * kWordBytes + offset])
  97. << (offset * kByteBits);
  98. }
  99. return value;
  100. };
  101. std::size_t index = 0;
  102. while (count - index > kRoundWords) {
  103. a += word(index);
  104. b += word(index + 1);
  105. c += word(index + 2);
  106. mix(a, b, c);
  107. index += kRoundWords;
  108. }
  109. const std::size_t remaining = count - index;
  110. if (remaining == 0) {
  111. return c;
  112. }
  113. if (remaining == kRoundWords) {
  114. c += word(index + 2);
  115. }
  116. if (remaining >= 2) {
  117. b += word(index + 1);
  118. }
  119. a += word(index);
  120. finish(a, b, c);
  121. return c;
  122. }
  123. } // namespace sunrise::middleware::crypto::lookup3