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.

t1ha.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Copyright (c) 2016-2018 Positive Technologies, https://www.ptsecurity.com,
  3. * Fast Positive Hash.
  4. *
  5. * Portions Copyright (c) 2010-2018 Leonid Yuriev <leo@yuriev.ru>,
  6. * The 1Hippeus project (t1h).
  7. *
  8. * This software is provided 'as-is', without any express or implied
  9. * warranty. In no event will the authors be held liable for any damages
  10. * arising from the use of this software.
  11. *
  12. * Permission is granted to anyone to use this software for any purpose,
  13. * including commercial applications, and to alter it and redistribute it
  14. * freely, subject to the following restrictions:
  15. *
  16. * 1. The origin of this software must not be misrepresented; you must not
  17. * claim that you wrote the original software. If you use this software
  18. * in a product, an acknowledgement in the product documentation would be
  19. * appreciated but is not required.
  20. * 2. Altered source versions must be plainly marked as such, and must not be
  21. * misrepresented as being the original software.
  22. * 3. This notice may not be removed or altered from any source distribution.
  23. */
  24. /*
  25. * t1ha = { Fast Positive Hash, aka "Позитивный Хэш" }
  26. * by [Positive Technologies](https://www.ptsecurity.ru)
  27. *
  28. * Briefly, it is a 64-bit Hash Function:
  29. * 1. Created for 64-bit little-endian platforms, in predominantly for x86_64,
  30. * but portable and without penalties it can run on any 64-bit CPU.
  31. * 2. In most cases up to 15% faster than City64, xxHash, mum-hash, metro-hash
  32. * and all others portable hash-functions (which do not use specific
  33. * hardware tricks).
  34. * 3. Not suitable for cryptography.
  35. *
  36. * The Future will Positive. Всё будет хорошо.
  37. *
  38. * ACKNOWLEDGEMENT:
  39. * The t1ha was originally developed by Leonid Yuriev (Леонид Юрьев)
  40. * for The 1Hippeus project - zerocopy messaging in the spirit of Sparta!
  41. */
  42. #pragma once
  43. #ifndef __has_attribute
  44. #define __has_attribute(x) (0)
  45. #endif
  46. #ifndef __has_include
  47. #define __has_include(x) (0)
  48. #endif
  49. #ifndef __GNUC_PREREQ
  50. #if defined(__GNUC__) && defined(__GNUC_MINOR__)
  51. #define __GNUC_PREREQ(maj, min) \
  52. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  53. #else
  54. #define __GNUC_PREREQ(maj, min) 0
  55. #endif
  56. #endif /* __GNUC_PREREQ */
  57. #ifndef __CLANG_PREREQ
  58. #ifdef __clang__
  59. #define __CLANG_PREREQ(maj, min) \
  60. ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
  61. #else
  62. #define __CLANG_PREREQ(maj, min) (0)
  63. #endif
  64. #endif /* __CLANG_PREREQ */
  65. /*****************************************************************************/
  66. #ifdef _MSC_VER
  67. /* Avoid '16' bytes padding added after data member 't1ha_context::total'
  68. * and other warnings from std-headers if warning-level > 3. */
  69. #pragma warning(push, 3)
  70. #endif
  71. #if defined(__cplusplus) && __cplusplus >= 201103L
  72. #include <climits>
  73. #include <cstddef>
  74. #include <cstdint>
  75. #else
  76. #include <limits.h>
  77. #include <stddef.h>
  78. #include <stdint.h>
  79. #endif
  80. /*****************************************************************************/
  81. #if defined(i386) || defined(__386) || defined(__i386) || defined(__i386__) || \
  82. defined(i486) || defined(__i486) || defined(__i486__) || \
  83. defined(i586) | defined(__i586) || defined(__i586__) || defined(i686) || \
  84. defined(__i686) || defined(__i686__) || defined(_M_IX86) || \
  85. defined(_X86_) || defined(__THW_INTEL__) || defined(__I86__) || \
  86. defined(__INTEL__) || defined(__x86_64) || defined(__x86_64__) || \
  87. defined(__amd64__) || defined(__amd64) || defined(_M_X64) || \
  88. defined(_M_AMD64) || defined(__IA32__) || defined(__INTEL__)
  89. #ifndef __ia32__
  90. /* LY: define neutral __ia32__ for x86 and x86-64 archs */
  91. #define __ia32__ 1
  92. #endif /* __ia32__ */
  93. #if !defined(__amd64__) && (defined(__x86_64) || defined(__x86_64__) || \
  94. defined(__amd64) || defined(_M_X64))
  95. /* LY: define trusty __amd64__ for all AMD64/x86-64 arch */
  96. #define __amd64__ 1
  97. #endif /* __amd64__ */
  98. #endif /* all x86 */
  99. #if !defined(__BYTE_ORDER__) || !defined(__ORDER_LITTLE_ENDIAN__) || \
  100. !defined(__ORDER_BIG_ENDIAN__)
  101. /* *INDENT-OFF* */
  102. /* clang-format off */
  103. #if defined(__GLIBC__) || defined(__GNU_LIBRARY__) || defined(__ANDROID__) || \
  104. defined(HAVE_ENDIAN_H) || __has_include(<endian.h>)
  105. #include <endian.h>
  106. #elif defined(__APPLE__) || defined(__MACH__) || defined(__OpenBSD__) || \
  107. defined(HAVE_MACHINE_ENDIAN_H) || __has_include(<machine/endian.h>)
  108. #include <machine/endian.h>
  109. #elif defined(HAVE_SYS_ISA_DEFS_H) || __has_include(<sys/isa_defs.h>)
  110. #include <sys/isa_defs.h>
  111. #elif (defined(HAVE_SYS_TYPES_H) && defined(HAVE_SYS_ENDIAN_H)) || \
  112. (__has_include(<sys/types.h>) && __has_include(<sys/endian.h>))
  113. #include <sys/endian.h>
  114. #include <sys/types.h>
  115. #elif defined(__bsdi__) || defined(__DragonFly__) || defined(__FreeBSD__) || \
  116. defined(__NETBSD__) || defined(__NetBSD__) || \
  117. defined(HAVE_SYS_PARAM_H) || __has_include(<sys/param.h>)
  118. #include <sys/param.h>
  119. #endif /* OS */
  120. /* *INDENT-ON* */
  121. /* clang-format on */
  122. #if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
  123. #define __ORDER_LITTLE_ENDIAN__ __LITTLE_ENDIAN
  124. #define __ORDER_BIG_ENDIAN__ __BIG_ENDIAN
  125. #define __BYTE_ORDER__ __BYTE_ORDER
  126. #elif defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
  127. #define __ORDER_LITTLE_ENDIAN__ _LITTLE_ENDIAN
  128. #define __ORDER_BIG_ENDIAN__ _BIG_ENDIAN
  129. #define __BYTE_ORDER__ _BYTE_ORDER
  130. #else
  131. #define __ORDER_LITTLE_ENDIAN__ 1234
  132. #define __ORDER_BIG_ENDIAN__ 4321
  133. #if defined(__LITTLE_ENDIAN__) || \
  134. (defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)) || \
  135. defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
  136. defined(__MIPSEL__) || defined(_MIPSEL) || defined(__MIPSEL) || \
  137. defined(_M_ARM) || defined(_M_ARM64) || defined(__e2k__) || \
  138. defined(__elbrus_4c__) || defined(__elbrus_8c__) || defined(__bfin__) || \
  139. defined(__BFIN__) || defined(__ia64__) || defined(_IA64) || \
  140. defined(__IA64__) || defined(__ia64) || defined(_M_IA64) || \
  141. defined(__itanium__) || defined(__ia32__) || defined(__CYGWIN__) || \
  142. defined(_WIN64) || defined(_WIN32) || defined(__TOS_WIN__) || \
  143. defined(__WINDOWS__)
  144. #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
  145. #elif defined(__BIG_ENDIAN__) || \
  146. (defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)) || \
  147. defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
  148. defined(__MIPSEB__) || defined(_MIPSEB) || defined(__MIPSEB) || \
  149. defined(__m68k__) || defined(M68000) || defined(__hppa__) || \
  150. defined(__hppa) || defined(__HPPA__) || defined(__sparc__) || \
  151. defined(__sparc) || defined(__370__) || defined(__THW_370__) || \
  152. defined(__s390__) || defined(__s390x__) || defined(__SYSC_ZARCH__)
  153. #define __BYTE_ORDER__ __ORDER_BIG_ENDIAN__
  154. #else
  155. #error __BYTE_ORDER__ should be defined.
  156. #endif /* Arch */
  157. #endif
  158. #endif /* __BYTE_ORDER__ || __ORDER_LITTLE_ENDIAN__ || __ORDER_BIG_ENDIAN__ */
  159. /*****************************************************************************/
  160. #ifndef __dll_export
  161. #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
  162. #if defined(__GNUC__) || __has_attribute(dllexport)
  163. #define __dll_export __attribute__((dllexport))
  164. #elif defined(_MSC_VER)
  165. #define __dll_export __declspec(dllexport)
  166. #else
  167. #define __dll_export
  168. #endif
  169. #elif defined(__GNUC__) || __has_attribute(visibility)
  170. #define __dll_export __attribute__((visibility("default")))
  171. #else
  172. #define __dll_export
  173. #endif
  174. #endif /* __dll_export */
  175. #ifndef __dll_import
  176. #if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)
  177. #if defined(__GNUC__) || __has_attribute(dllimport)
  178. #define __dll_import __attribute__((dllimport))
  179. #elif defined(_MSC_VER)
  180. #define __dll_import __declspec(dllimport)
  181. #else
  182. #define __dll_import
  183. #endif
  184. #else
  185. #define __dll_import
  186. #endif
  187. #endif /* __dll_import */
  188. #if defined(t1ha_EXPORTS)
  189. #define T1HA_API __dll_export
  190. #elif defined(t1ha_IMPORTS)
  191. #define T1HA_API __dll_import
  192. #else
  193. #define T1HA_API
  194. #endif /* T1HA_API */
  195. #if defined(_MSC_VER) && defined(__ia32__)
  196. #define T1HA_ALIGN_PREFIX __declspec(align(32)) /* required only for SIMD */
  197. #else
  198. #define T1HA_ALIGN_PREFIX
  199. #endif /* _MSC_VER */
  200. #if defined(__GNUC__) && defined(__ia32__)
  201. #define T1HA_ALIGN_SUFFIX \
  202. __attribute__((aligned(32))) /* required only for SIMD */
  203. #else
  204. #define T1HA_ALIGN_SUFFIX
  205. #endif /* GCC x86 */
  206. #ifdef __cplusplus
  207. extern "C" {
  208. #endif
  209. typedef union T1HA_ALIGN_PREFIX t1ha_state256 {
  210. uint8_t bytes[32];
  211. uint32_t u32[8];
  212. uint64_t u64[4];
  213. struct {
  214. uint64_t a, b, c, d;
  215. } n;
  216. } t1ha_state256_t T1HA_ALIGN_SUFFIX;
  217. typedef struct t1ha_context {
  218. t1ha_state256_t state;
  219. t1ha_state256_t buffer;
  220. size_t partial;
  221. uint64_t total;
  222. } t1ha_context_t;
  223. #ifdef _MSC_VER
  224. #pragma warning(pop)
  225. #endif
  226. /******************************************************************************
  227. *
  228. * t1ha2 = 64 and 128-bit, SLIGHTLY MORE ATTENTION FOR QUALITY AND STRENGTH.
  229. *
  230. * - The recommended version of "Fast Positive Hash" with good quality
  231. * for checksum, hash tables and fingerprinting.
  232. * - Portable and extremely efficiency on modern 64-bit CPUs.
  233. * Designed for 64-bit little-endian platforms,
  234. * in other cases will runs slowly.
  235. * - Great quality of hashing and still faster than other non-t1ha hashes.
  236. * Provides streaming mode and 128-bit result.
  237. *
  238. * Note: Due performance reason 64- and 128-bit results are completely
  239. * different each other, i.e. 64-bit result is NOT any part of 128-bit.
  240. */
  241. /* The at-once variant with 64-bit result */
  242. T1HA_API uint64_t t1ha2_atonce(const void *data, size_t length, uint64_t seed);
  243. /* The at-once variant with 128-bit result.
  244. * Argument `extra_result` is NOT optional and MUST be valid.
  245. * The high 64-bit part of 128-bit hash will be always unconditionally
  246. * stored to the address given by `extra_result` argument. */
  247. T1HA_API uint64_t t1ha2_atonce128(uint64_t *__restrict extra_result,
  248. const void *__restrict data, size_t length,
  249. uint64_t seed);
  250. /* The init/update/final trinity for streaming.
  251. * Return 64 or 128-bit result depentently from `extra_result` argument. */
  252. T1HA_API void t1ha2_init(t1ha_context_t *ctx, uint64_t seed_x, uint64_t seed_y);
  253. T1HA_API void t1ha2_update(t1ha_context_t *__restrict ctx,
  254. const void *__restrict data, size_t length);
  255. /* Argument `extra_result` is optional and MAY be NULL.
  256. * - If `extra_result` is NOT NULL then the 128-bit hash will be calculated,
  257. * and high 64-bit part of it will be stored to the address given
  258. * by `extra_result` argument.
  259. * - Otherwise the 64-bit hash will be calculated
  260. * and returned from function directly.
  261. *
  262. * Note: Due performance reason 64- and 128-bit results are completely
  263. * different each other, i.e. 64-bit result is NOT any part of 128-bit. */
  264. T1HA_API uint64_t t1ha2_final(t1ha_context_t *__restrict ctx,
  265. uint64_t *__restrict extra_result /* optional */);
  266. /******************************************************************************
  267. *
  268. * t1ha1 = 64-bit, BASELINE FAST PORTABLE HASH:
  269. *
  270. * - Runs faster on 64-bit platforms in other cases may runs slowly.
  271. * - Portable and stable, returns same 64-bit result
  272. * on all architectures and CPUs.
  273. * - Unfortunately it fails the "strict avalanche criteria",
  274. * see test results at https://github.com/demerphq/smhasher.
  275. *
  276. * This flaw is insignificant for the t1ha1() purposes and imperceptible
  277. * from a practical point of view.
  278. * However, nowadays this issue has resolved in the next t1ha2(),
  279. * that was initially planned to providing a bit more quality.
  280. */
  281. /* The little-endian variant. */
  282. T1HA_API uint64_t t1ha1_le(const void *data, size_t length, uint64_t seed);
  283. /* The big-endian variant. */
  284. T1HA_API uint64_t t1ha1_be(const void *data, size_t length, uint64_t seed);
  285. /* The historical nicname for generic little-endian variant. */
  286. static __inline uint64_t t1ha(const void *data, size_t length, uint64_t seed) {
  287. return t1ha1_le(data, length, seed);
  288. }
  289. /******************************************************************************
  290. *
  291. * t1ha0 = 64-bit, JUST ONLY FASTER:
  292. *
  293. * - Provides fast-as-possible hashing for current CPU, including
  294. * 32-bit systems and engaging the available hardware acceleration.
  295. * - It is a facade that selects most quick-and-dirty hash
  296. * for the current processor. For instance, on IA32 (x86) actual function
  297. * will be selected in runtime, depending on current CPU capabilities
  298. *
  299. * BE CAREFUL!!! THIS IS MEANS:
  300. *
  301. * 1. The quality of hash is a subject for tradeoffs with performance.
  302. * So, the quality and strength of t1ha0() may be lower than t1ha1(),
  303. * especially on 32-bit targets, but then much faster.
  304. * However, guaranteed that it passes all SMHasher tests.
  305. *
  306. * 2. No warranty that the hash result will be same for particular
  307. * key on another machine or another version of libt1ha.
  308. *
  309. * Briefly, such hash-results and their derivatives, should be
  310. * used only in runtime, but should not be persist or transferred
  311. * over a network.
  312. */
  313. /* The little-endian variant for 32-bit CPU. */
  314. uint64_t t1ha0_32le(const void *data, size_t length, uint64_t seed);
  315. /* The big-endian variant for 32-bit CPU. */
  316. uint64_t t1ha0_32be(const void *data, size_t length, uint64_t seed);
  317. /* Define T1HA0_AESNI_AVAILABLE to 0 for disable AES-NI support. */
  318. #ifndef T1HA0_AESNI_AVAILABLE
  319. #if (defined(__ia32__) && (!defined(_M_IX86) || _MSC_VER > 1800))
  320. #if defined(__GNUC__) && \
  321. ((defined(__clang__) && (__clang_major__ >= 4 || (__clang_major__ >= 3 && __clang_minor__ >= 8))) || \
  322. ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8) || (__GNUC__ > 4)))
  323. #define T1HA0_AESNI_AVAILABLE 1
  324. #else
  325. #define T1HA0_AESNI_AVAILABLE 0
  326. #endif
  327. #else
  328. #define T1HA0_AESNI_AVAILABLE 0
  329. #endif
  330. #endif /* T1HA0_AESNI_AVAILABLE */
  331. /* Define T1HA0_RUNTIME_SELECT to 0 for disable dispatching t1ha0 at runtime. */
  332. #ifndef T1HA0_RUNTIME_SELECT
  333. #if T1HA0_AESNI_AVAILABLE && !defined(__e2k__)
  334. #define T1HA0_RUNTIME_SELECT 1
  335. #else
  336. #define T1HA0_RUNTIME_SELECT 0
  337. #endif
  338. #endif /* T1HA0_RUNTIME_SELECT */
  339. #if T1HA0_AESNI_AVAILABLE
  340. uint64_t t1ha0_ia32aes_noavx(const void *data, size_t length, uint64_t seed);
  341. #endif /* T1HA0_AESNI_AVAILABLE */
  342. #if T1HA0_RUNTIME_SELECT
  343. #ifdef __ELF__
  344. /* ifunc/gnu_indirect_function will be used on ELF.
  345. * Please see https://en.wikipedia.org/wiki/Executable_and_Linkable_Format */
  346. T1HA_API uint64_t t1ha0(const void *data, size_t length, uint64_t seed);
  347. #else
  348. /* Otherwise function pointer will be used.
  349. * Unfortunately this may cause some overhead calling. */
  350. T1HA_API extern uint64_t (*t1ha0_funcptr)(const void *data, size_t length,
  351. uint64_t seed);
  352. static __inline uint64_t t1ha0(const void *data, size_t length, uint64_t seed) {
  353. return t1ha0_funcptr(data, length, seed);
  354. }
  355. #endif /* __ELF__ */
  356. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  357. static __inline uint64_t t1ha0(const void *data, size_t length, uint64_t seed) {
  358. #if UINTPTR_MAX > 0xffffFFFFul || ULONG_MAX > 0xffffFFFFul
  359. return t1ha1_be(data, length, seed);
  360. #else
  361. return t1ha0_32be(data, length, seed);
  362. #endif
  363. }
  364. #else
  365. static __inline uint64_t t1ha0(const void *data, size_t length, uint64_t seed) {
  366. #if UINTPTR_MAX > 0xffffFFFFul || ULONG_MAX > 0xffffFFFFul
  367. return t1ha1_le(data, length, seed);
  368. #else
  369. return t1ha0_32le(data, length, seed);
  370. #endif
  371. }
  372. #endif /* !T1HA0_RUNTIME_SELECT */
  373. #ifdef __cplusplus
  374. }
  375. #endif