hashAes1Rx4.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright (c) 2019 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #include "softAes.h"
  16. /*
  17. Calculate a 512-bit hash of 'input' using 4 lanes of AES.
  18. The input is treated as a set of round keys for the encryption
  19. of the initial state.
  20. 'inputSize' must be a multiple of 64.
  21. For a 2 MiB input, this has the same security as 32768-round
  22. AES encryption.
  23. Hashing throughput: >20 GiB/s per CPU core with hardware AES
  24. */
  25. template<bool softAes>
  26. void hashAes1Rx4(const void *input, size_t inputSize, void *hash) {
  27. const uint8_t* inptr = (uint8_t*)input;
  28. const uint8_t* inputEnd = inptr + inputSize;
  29. __m128i state0, state1, state2, state3;
  30. __m128i in0, in1, in2, in3;
  31. //intial state
  32. state0 = _mm_set_epi32(0x9d04b0ae, 0x59943385, 0x30ac8d93, 0x3fe49f5d);
  33. state1 = _mm_set_epi32(0x8a39ebf1, 0xddc10935, 0xa724ecd3, 0x7b0c6064);
  34. state2 = _mm_set_epi32(0x7ec70420, 0xdf01edda, 0x7c12ecf7, 0xfb5382e3);
  35. state3 = _mm_set_epi32(0x94a9d201, 0x5082d1c8, 0xb2e74109, 0x7728b705);
  36. //process 64 bytes at a time in 4 lanes
  37. while (inptr < inputEnd) {
  38. in0 = _mm_load_si128((__m128i*)inptr + 0);
  39. in1 = _mm_load_si128((__m128i*)inptr + 1);
  40. in2 = _mm_load_si128((__m128i*)inptr + 2);
  41. in3 = _mm_load_si128((__m128i*)inptr + 3);
  42. state0 = aesenc<softAes>(state0, in0);
  43. state1 = aesdec<softAes>(state1, in1);
  44. state2 = aesenc<softAes>(state2, in2);
  45. state3 = aesdec<softAes>(state3, in3);
  46. inptr += 64;
  47. }
  48. //two extra rounds to achieve full diffusion
  49. __m128i xkey0 = _mm_set_epi32(0x4ff637c5, 0x053bd705, 0x8231a744, 0xc3767b17);
  50. __m128i xkey1 = _mm_set_epi32(0x6594a1a6, 0xa8879d58, 0xb01da200, 0x8a8fae2e);
  51. state0 = aesenc<softAes>(state0, xkey0);
  52. state1 = aesdec<softAes>(state1, xkey0);
  53. state2 = aesenc<softAes>(state2, xkey0);
  54. state3 = aesdec<softAes>(state3, xkey0);
  55. state0 = aesenc<softAes>(state0, xkey1);
  56. state1 = aesdec<softAes>(state1, xkey1);
  57. state2 = aesenc<softAes>(state2, xkey1);
  58. state3 = aesdec<softAes>(state3, xkey1);
  59. //output hash
  60. _mm_store_si128((__m128i*)hash + 0, state0);
  61. _mm_store_si128((__m128i*)hash + 1, state1);
  62. _mm_store_si128((__m128i*)hash + 2, state2);
  63. _mm_store_si128((__m128i*)hash + 3, state3);
  64. }
  65. template void hashAes1Rx4<false>(const void *input, size_t inputSize, void *hash);
  66. template void hashAes1Rx4<true>(const void *input, size_t inputSize, void *hash);
  67. /*
  68. Fill 'buffer' with pseudorandom data based on 512-bit 'state'.
  69. The state is encrypted using a single AES round per 16 bytes of output
  70. in 4 lanes.
  71. 'outputSize' must be a multiple of 64.
  72. The modified state is written back to 'state' to allow multiple
  73. calls to this function.
  74. */
  75. template<bool softAes>
  76. void fillAes1Rx4(void *state, size_t outputSize, void *buffer) {
  77. const uint8_t* outptr = (uint8_t*)buffer;
  78. const uint8_t* outputEnd = outptr + outputSize;
  79. __m128i state0, state1, state2, state3;
  80. __m128i key0, key1, key2, key3;
  81. key0 = _mm_set_epi32(0x9274f206, 0x79498d2f, 0x7d2de6ab, 0x67a04d26);
  82. key1 = _mm_set_epi32(0xe1f7af05, 0x2a3a6f1d, 0x86658a15, 0x4f719812);
  83. key2 = _mm_set_epi32(0xd1b1f791, 0x9e2ec914, 0x14c77bce, 0xba90750e);
  84. key3 = _mm_set_epi32(0x179d0fd9, 0x6e57883c, 0xa53bbe4f, 0xaa07621f);
  85. state0 = _mm_load_si128((__m128i*)state + 0);
  86. state1 = _mm_load_si128((__m128i*)state + 1);
  87. state2 = _mm_load_si128((__m128i*)state + 2);
  88. state3 = _mm_load_si128((__m128i*)state + 3);
  89. while (outptr < outputEnd) {
  90. state0 = aesdec<softAes>(state0, key0);
  91. state1 = aesenc<softAes>(state1, key1);
  92. state2 = aesdec<softAes>(state2, key2);
  93. state3 = aesenc<softAes>(state3, key3);
  94. _mm_store_si128((__m128i*)outptr + 0, state0);
  95. _mm_store_si128((__m128i*)outptr + 1, state1);
  96. _mm_store_si128((__m128i*)outptr + 2, state2);
  97. _mm_store_si128((__m128i*)outptr + 3, state3);
  98. outptr += 64;
  99. }
  100. _mm_store_si128((__m128i*)state + 0, state0);
  101. _mm_store_si128((__m128i*)state + 1, state1);
  102. _mm_store_si128((__m128i*)state + 2, state2);
  103. _mm_store_si128((__m128i*)state + 3, state3);
  104. }
  105. template void fillAes1Rx4<true>(void *state, size_t outputSize, void *buffer);
  106. template void fillAes1Rx4<false>(void *state, size_t outputSize, void *buffer);