blake2b_ref.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include "blake2b.h"
  3. #include <library/cpp/digest/argonish/internal/rotations/rotations_ref.h>
  4. namespace NArgonish {
  5. static const ui8 Sigma[12][16] = {
  6. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  7. {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
  8. {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
  9. {7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
  10. {9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
  11. {2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
  12. {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
  13. {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
  14. {6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
  15. {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
  16. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  17. {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3}};
  18. static const ui64 Iv[8] = {
  19. 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
  20. 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
  21. 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
  22. 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL};
  23. static inline void GRef(ui64 r, ui64 i, ui64& a, ui64& b, ui64& c, ui64& d, const ui64* m) {
  24. a = a + b + m[Sigma[r][2 * i + 0]];
  25. d = Rotr(d ^ a, 32);
  26. c = c + d;
  27. b = Rotr(b ^ c, 24);
  28. a = a + b + m[Sigma[r][2 * i + 1]];
  29. d = Rotr(d ^ a, 16);
  30. c = c + d;
  31. b = Rotr(b ^ c, 63);
  32. }
  33. static inline void Round(ui64 r, ui64* v, const ui64* m) {
  34. GRef(r, 0, v[0], v[4], v[8], v[12], m);
  35. GRef(r, 1, v[1], v[5], v[9], v[13], m);
  36. GRef(r, 2, v[2], v[6], v[10], v[14], m);
  37. GRef(r, 3, v[3], v[7], v[11], v[15], m);
  38. GRef(r, 4, v[0], v[5], v[10], v[15], m);
  39. GRef(r, 5, v[1], v[6], v[11], v[12], m);
  40. GRef(r, 6, v[2], v[7], v[8], v[13], m);
  41. GRef(r, 7, v[3], v[4], v[9], v[14], m);
  42. }
  43. template <>
  44. void* TBlake2B<EInstructionSet::REF>::GetIV_() const {
  45. return nullptr;
  46. }
  47. template <>
  48. void TBlake2B<EInstructionSet::REF>::InitialXor_(ui8* h, const ui8* p) {
  49. for (size_t i = 0; i < 8; ++i)
  50. ((ui64*)h)[i] = Iv[i] ^ ((ui64*)p)[i];
  51. }
  52. template <>
  53. void TBlake2B<EInstructionSet::REF>::Compress_(const ui64 block[BLAKE2B_BLOCKQWORDS]) {
  54. ui64 v[16];
  55. for (size_t i = 0; i < 8; ++i) {
  56. v[i] = State_.H[i];
  57. }
  58. v[8] = Iv[0];
  59. v[9] = Iv[1];
  60. v[10] = Iv[2];
  61. v[11] = Iv[3];
  62. v[12] = Iv[4] ^ State_.T[0];
  63. v[13] = Iv[5] ^ State_.T[1];
  64. v[14] = Iv[6] ^ State_.F[0];
  65. v[15] = Iv[7] ^ State_.F[1];
  66. for (ui64 r = 0; r < 12; ++r)
  67. Round(r, v, block);
  68. for (size_t i = 0; i < 8; ++i) {
  69. State_.H[i] = State_.H[i] ^ v[i] ^ v[i + 8];
  70. }
  71. }
  72. }