├── aquahash.png ├── README.md ├── LICENSE └── aquahash.h /aquahash.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jandrewrogers/AquaHash/HEAD/aquahash.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AquaHash: Fast Hashing With AES Intrinsics 2 | 3 | AquaHash is a 128-bit non-cryptographic hash function that delivers state-of-the-art performance across all key sizes. The algorithm employs a novel construction based on widely available AES intrinsics. More details of its design can be [found here](http://www.jandrewrogers.com/2019/03/06/aquahash/). The source is released under Apache License v2.0. 4 | 5 | ## Performance 6 | 7 | AquaHash substantially outperforms most common hash functions at all key sizes on recent CPUs. Bulk hashing performance is an exceptional 15 bytes/cycle. Small key performance is nearly twice that of other modern small key algorithms. 8 | 9 | The performance graph below includes xxhash64 and Google's FarmHash for reference, popular algorithms optimized for small keys and large keys respectively. Intel’s Skylake microarchitecture was used to measure all algorithms. 10 | 11 | ![Small Key Performance](aquahash.png) 12 | 13 | Some reduction in relative performance is expected on older CPUs with slower AES intrinsics but should remain competitive due to the large differences in baseline performance. 14 | 15 | ## Usage 16 | 17 | The source is implemented as a single C++ header file "aquahash.h". Both incremental and non-incremental algorithm implementations are included. When use cases allow, it is preferable to use the non-incremental implementation because it will be significantly faster than incrementally constructing the hash. 18 | 19 | ### Non-Incremental Hashing 20 | 21 | ``` 22 | __m128i hash = AquaHash::Hash(uint8_t * key, size_t bytes, __m128i seed = _mm_setzero_si128()); 23 | ``` 24 | 25 | ### Incremental Hashing 26 | 27 | ``` 28 | AquaHash aqua(__m128i seed = _mm_setzero_si128()); 29 | aqua.Update(uint8_t * key, size_t bytes); 30 | ... 31 | aqua.Update(uint8_t * key, size_t bytes); 32 | __m128i hash = aqua.Finalize(); 33 | ``` 34 | 35 | The incremental object can be re-initialized at any time using: 36 | 37 | ``` 38 | aqua.Initialize(__m128i seed = _mm_setzero_si128()); 39 | ``` 40 | 41 | ### Component Algorithms 42 | 43 | AquaHash is a composite of two general purpose hashing algorithms that were designed separately, optimized for large keys and small keys respectively. These are included for reference and fully functional for all key sizes: 44 | 45 | ``` 46 | __m128i hash = AquaHash::SmallKeyAlgorithm(uint8_t * key, size_t bytes, __m128i seed = _mm_setzero_si128()); 47 | __m128i hash = AquaHash::LargeKeyAlgorithm(uint8_t * key, size_t bytes, __m128i seed = _mm_setzero_si128()); 48 | ``` 49 | 50 | ## Current Status 51 | 52 | **2019-03-06** Initial v1.0 release of the algorithm source code. Includes both incremental and non-incremental implementations, as well as reference implementations of the underlying large key and small key component algorithms. Test vectors and an implementation verification method are included. 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /aquahash.h: -------------------------------------------------------------------------------- 1 | // Copyright 2018 J. Andrew Rogers 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef AQUAHASH_H 16 | #define AQUAHASH_H 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | class AquaHash { 25 | private: 26 | // INCREMENTAL CONSTRUCTION STATE 27 | 28 | // 4 x 128-bit hashing lanes 29 | __m128i block[4]; 30 | // input block buffer 31 | __m128i input[4]; 32 | // initialization vector 33 | __m128i initialize; 34 | // cumulative input bytes 35 | size_t input_bytes; 36 | 37 | static constexpr size_t max_input = std::numeric_limits::max() - 1; 38 | // sentinel to prevent double finalization 39 | static constexpr size_t finalized = max_input + 1; 40 | 41 | public: 42 | 43 | // Reference implementation of AquaHash small key algorithm 44 | static __m128i SmallKeyAlgorithm(const uint8_t * key, const size_t bytes, __m128i initialize = _mm_setzero_si128()) { 45 | assert(bytes <= max_input); 46 | __m128i hash = initialize; 47 | 48 | // bulk hashing loop -- 128-bit block size 49 | const __m128i * ptr128 = reinterpret_cast(key); 50 | if (bytes / sizeof(hash)) { 51 | __m128i temp = _mm_set_epi64x(0xa11202c9b468bea1, 0xd75157a01452495b); 52 | for (uint32_t i = 0; i < bytes / sizeof(hash); ++i) { 53 | __m128i b = _mm_loadu_si128(ptr128++); 54 | hash = _mm_aesenc_si128(hash, b); 55 | temp = _mm_aesenc_si128(temp, b); 56 | } 57 | hash = _mm_aesenc_si128(hash, temp); 58 | } 59 | 60 | // AES sub-block processor 61 | const uint8_t * ptr8 = reinterpret_cast(ptr128); 62 | if (bytes & 8) { 63 | __m128i b = _mm_set_epi64x(*reinterpret_cast(ptr8), 64 | 0xa11202c9b468bea1); 65 | hash = _mm_xor_si128(hash, b); 66 | ptr8 += 8; 67 | } 68 | 69 | if (bytes & 4) { 70 | __m128i b = _mm_set_epi32(0xb1293b33, 0x05418592, 71 | *reinterpret_cast(ptr8), 0xd210d232); 72 | hash = _mm_xor_si128(hash, b); 73 | ptr8 += 4; 74 | } 75 | 76 | if (bytes & 2) { 77 | __m128i b = _mm_set_epi16(0xbd3d, 0xc2b7, 0xb87c, 0x4715, 78 | 0x6a6c, 0x9527, *reinterpret_cast(ptr8), 0xac2e); 79 | hash = _mm_xor_si128(hash, b); 80 | ptr8 += 2; 81 | } 82 | 83 | if (bytes & 1) { 84 | __m128i b = _mm_set_epi8(0xcc, 0x96, 0xed, 0x16, 0x74, 0xea, 0xaa, 0x03, 85 | 0x1e, 0x86, 0x3f, 0x24, 0xb2, 0xa8, *reinterpret_cast(ptr8), 0x31); 86 | hash = _mm_xor_si128(hash, b); 87 | } 88 | 89 | // this algorithm construction requires no less than three AES rounds to finalize 90 | hash = _mm_aesenc_si128(hash, _mm_set_epi64x(0x8e51ef21fabb4522, 0xe43d7a0656954b6c)); 91 | hash = _mm_aesenc_si128(hash, _mm_set_epi64x(0x56082007c71ab18f, 0x76435569a03af7fa)); 92 | return _mm_aesenc_si128(hash, _mm_set_epi64x(0xd2600de7157abc68, 0x6339e901c3031efb)); 93 | } 94 | 95 | 96 | // Reference implementation of AquaHash large key algorithm 97 | static __m128i LargeKeyAlgorithm(const uint8_t * key, const size_t bytes, __m128i initialize = _mm_setzero_si128()) { 98 | assert(bytes <= max_input); 99 | 100 | // initialize 4 x 128-bit hashing lanes, for a 512-bit block size 101 | __m128 block[4] = { _mm_xor_si128(initialize, _mm_set_epi64x(0xa11202c9b468bea1, 0xd75157a01452495b)), 102 | _mm_xor_si128(initialize, _mm_set_epi64x(0xb1293b3305418592, 0xd210d232c6429b69)), 103 | _mm_xor_si128(initialize, _mm_set_epi64x(0xbd3dc2b7b87c4715, 0x6a6c9527ac2e0e4e)), 104 | _mm_xor_si128(initialize, _mm_set_epi64x(0xcc96ed1674eaaa03, 0x1e863f24b2a8316a)) }; 105 | 106 | // bulk hashing loop -- 512-bit block size 107 | const __m128i * ptr128 = reinterpret_cast(key); 108 | for (size_t block_counter = 0; block_counter < bytes / sizeof(block); block_counter++) { 109 | block[0] = _mm_aesenc_si128(block[0], _mm_loadu_si128(ptr128++)); 110 | block[1] = _mm_aesenc_si128(block[1], _mm_loadu_si128(ptr128++)); 111 | block[2] = _mm_aesenc_si128(block[2], _mm_loadu_si128(ptr128++)); 112 | block[3] = _mm_aesenc_si128(block[3], _mm_loadu_si128(ptr128++)); 113 | } 114 | 115 | // process remaining AES blocks 116 | if (bytes & 32) { 117 | block[0] = _mm_aesenc_si128(block[0], _mm_loadu_si128(ptr128++)); 118 | block[1] = _mm_aesenc_si128(block[1], _mm_loadu_si128(ptr128++)); 119 | } 120 | 121 | if (bytes & 16) { 122 | block[2] = _mm_aesenc_si128(block[2], _mm_loadu_si128(ptr128++)); 123 | } 124 | 125 | // AES sub-block processor 126 | const uint8_t * ptr8 = reinterpret_cast(ptr128); 127 | if (bytes & 8) { 128 | __m128i b = _mm_set_epi64x(*reinterpret_cast(ptr8), 129 | 0xa11202c9b468bea1); 130 | block[3] = _mm_aesenc_si128(block[3], b); 131 | ptr8 += 8; 132 | } 133 | 134 | if (bytes & 4) { 135 | __m128i b = _mm_set_epi32(0xb1293b33, 0x05418592, 136 | *reinterpret_cast(ptr8), 0xd210d232); 137 | block[0] = _mm_aesenc_si128(block[0], b); 138 | ptr8 += 4; 139 | } 140 | 141 | if (bytes & 2) { 142 | __m128i b = _mm_set_epi16(0xbd3d, 0xc2b7, 0xb87c, 0x4715, 143 | 0x6a6c, 0x9527, *reinterpret_cast(ptr8), 0xac2e); 144 | block[1] = _mm_aesenc_si128(block[1], b); 145 | ptr8 += 2; 146 | } 147 | 148 | if (bytes & 1) { 149 | __m128i b = _mm_set_epi8(0xcc, 0x96, 0xed, 0x16, 0x74, 0xea, 0xaa, 0x03, 150 | 0x1e, 0x86, 0x3f, 0x24, 0xb2, 0xa8,*ptr8, 0x31); 151 | block[2] = _mm_aesenc_si128(block[2], b); 152 | } 153 | 154 | // indirectly mix hashing lanes 155 | const __m128i mix = _mm_xor_si128(_mm_xor_si128(block[0], block[1]), _mm_xor_si128(block[2], block[3])); 156 | block[0] = _mm_aesenc_si128(block[0], mix); 157 | block[1] = _mm_aesenc_si128(block[1], mix); 158 | block[2] = _mm_aesenc_si128(block[2], mix); 159 | block[3] = _mm_aesenc_si128(block[3], mix); 160 | 161 | // reduction from 512-bit block size to 128-bit hash 162 | __m128i hash = _mm_aesenc_si128(_mm_aesenc_si128(block[0],block[1]), _mm_aesenc_si128(block[2], block[3])); 163 | 164 | // this algorithm construction requires no less than one round to finalize 165 | return _mm_aesenc_si128(hash, _mm_set_epi64x(0x8e51ef21fabb4522, 0xe43d7a0656954b6c)); 166 | } 167 | 168 | // NON-INCREMENTAL HYBRID ALGORITHM 169 | 170 | static __m128i Hash(const uint8_t * key, const size_t bytes, __m128i initialize = _mm_setzero_si128()) { 171 | return bytes < 64 ? SmallKeyAlgorithm(key, bytes, initialize) : LargeKeyAlgorithm(key, bytes, initialize); 172 | } 173 | 174 | // INCREMENTAL HYBRID ALGORITHM 175 | 176 | // Initialize a new incremental hashing object 177 | AquaHash(const __m128i initialize = _mm_setzero_si128()) 178 | : block { _mm_xor_si128(initialize, _mm_set_epi64x(0xa11202c9b468bea1, 0xd75157a01452495b)), 179 | _mm_xor_si128(initialize, _mm_set_epi64x(0xb1293b3305418592, 0xd210d232c6429b69)), 180 | _mm_xor_si128(initialize, _mm_set_epi64x(0xbd3dc2b7b87c4715, 0x6a6c9527ac2e0e4e)), 181 | _mm_xor_si128(initialize, _mm_set_epi64x(0xcc96ed1674eaaa03, 0x1e863f24b2a8316a)) }, 182 | initialize(initialize), 183 | input_bytes(0) 184 | {} 185 | 186 | // Initialize an existing hashing object -- all previous state is destroyed 187 | void Initialize(const __m128i initialize = _mm_setzero_si128()) { 188 | this->initialize = initialize; 189 | this->input_bytes = 0; 190 | block[0] = _mm_xor_si128(initialize, _mm_set_epi64x(0xa11202c9b468bea1, 0xd75157a01452495b)); 191 | block[1] = _mm_xor_si128(initialize, _mm_set_epi64x(0xb1293b3305418592, 0xd210d232c6429b69)); 192 | block[2] = _mm_xor_si128(initialize, _mm_set_epi64x(0xbd3dc2b7b87c4715, 0x6a6c9527ac2e0e4e)); 193 | block[3] = _mm_xor_si128(initialize, _mm_set_epi64x(0xcc96ed1674eaaa03, 0x1e863f24b2a8316a)); 194 | } 195 | 196 | // Append key to existing hashing object state 197 | void Update(const uint8_t * key, size_t bytes) { 198 | assert(input_bytes != finalized); 199 | assert(bytes <= max_input && max_input - input_bytes >= bytes); 200 | 201 | if (bytes == 0) 202 | return; 203 | 204 | // input buffer may be partially filled 205 | if (input_bytes % sizeof(input)) { 206 | // pointer to first unused byte in input buffer 207 | uint8_t * ptr8 = reinterpret_cast(input) + (input_bytes % sizeof(input)); 208 | 209 | // compute initial copy size from key to input buffer 210 | size_t copy_size = sizeof(input) - (input_bytes % sizeof(input)); 211 | if (copy_size > bytes) copy_size = bytes; 212 | 213 | // append new key bytes to input buffer 214 | memcpy(ptr8, key, copy_size); 215 | input_bytes += copy_size; 216 | bytes -= copy_size; 217 | 218 | // input buffer not filled by update 219 | if (input_bytes % sizeof(input)) 220 | return; 221 | 222 | // update key pointer to first byte not in the input buffer 223 | key += copy_size; 224 | 225 | // hash input buffer 226 | block[0] = _mm_aesenc_si128(block[0], input[0]); 227 | block[1] = _mm_aesenc_si128(block[1], input[1]); 228 | block[2] = _mm_aesenc_si128(block[2], input[2]); 229 | block[3] = _mm_aesenc_si128(block[3], input[3]); 230 | } 231 | 232 | input_bytes += bytes; 233 | 234 | // input buffer is empty 235 | const __m128i * ptr128 = reinterpret_cast(key); 236 | while (bytes >= sizeof(block)) { 237 | block[0] = _mm_aesenc_si128(block[0], _mm_loadu_si128(ptr128++)); 238 | block[1] = _mm_aesenc_si128(block[1], _mm_loadu_si128(ptr128++)); 239 | block[2] = _mm_aesenc_si128(block[2], _mm_loadu_si128(ptr128++)); 240 | block[3] = _mm_aesenc_si128(block[3], _mm_loadu_si128(ptr128++)); 241 | bytes -= sizeof(block); 242 | } 243 | 244 | // load remaining bytes into input buffer 245 | if (bytes) 246 | memcpy(input, ptr128, bytes); 247 | } 248 | 249 | // Generate hash from hashing object state. After finalization, the hashing 250 | // object is in an undefined state and must be initialized before any 251 | // subsequent calls on the object. 252 | __m128i Finalize() { 253 | assert(input_bytes != finalized); 254 | if (input_bytes < sizeof(block)) { 255 | __m128i hash = SmallKeyAlgorithm(reinterpret_cast(input), input_bytes, initialize); 256 | input_bytes = finalized; 257 | return hash; 258 | } 259 | else { 260 | // process remaining AES blocks 261 | if (input_bytes & 32) { 262 | block[0] = _mm_aesenc_si128(block[0], input[0]); 263 | block[1] = _mm_aesenc_si128(block[1], input[1]); 264 | } 265 | 266 | if (input_bytes & 16) { 267 | block[2] = _mm_aesenc_si128(block[2], input[2]); 268 | } 269 | 270 | // AES sub-block processor 271 | const uint8_t * ptr8 = reinterpret_cast(&input[3]); 272 | if (input_bytes & 8) { 273 | __m128i b = _mm_set_epi64x(*reinterpret_cast(ptr8), 274 | 0xa11202c9b468bea1); 275 | block[3] = _mm_aesenc_si128(block[3], b); 276 | ptr8 += 8; 277 | } 278 | 279 | if (input_bytes & 4) { 280 | __m128i b = _mm_set_epi32(0xb1293b33, 0x05418592, 281 | *reinterpret_cast(ptr8), 0xd210d232); 282 | block[0] = _mm_aesenc_si128(block[0], b); 283 | ptr8 += 4; 284 | } 285 | 286 | if (input_bytes & 2) { 287 | __m128i b = _mm_set_epi16(0xbd3d, 0xc2b7, 0xb87c, 0x4715, 288 | 0x6a6c, 0x9527, *reinterpret_cast(ptr8), 0xac2e); 289 | block[1] = _mm_aesenc_si128(block[1], b); 290 | ptr8 += 2; 291 | } 292 | 293 | if (input_bytes & 1) { 294 | __m128i b = _mm_set_epi8(0xcc, 0x96, 0xed, 0x16, 0x74, 0xea, 0xaa, 0x03, 295 | 0x1e, 0x86, 0x3f, 0x24, 0xb2, 0xa8,*ptr8, 0x31); 296 | block[2] = _mm_aesenc_si128(block[2], b); 297 | } 298 | 299 | // indirectly mix hashing lanes 300 | const __m128i mix = _mm_xor_si128(_mm_xor_si128(block[0], block[1]), _mm_xor_si128(block[2], block[3])); 301 | block[0] = _mm_aesenc_si128(block[0], mix); 302 | block[1] = _mm_aesenc_si128(block[1], mix); 303 | block[2] = _mm_aesenc_si128(block[2], mix); 304 | block[3] = _mm_aesenc_si128(block[3], mix); 305 | 306 | // reduction from 512-bit block size to 128-bit hash 307 | __m128i hash = _mm_aesenc_si128(_mm_aesenc_si128(block[0],block[1]), _mm_aesenc_si128(block[2], block[3])); 308 | 309 | // this algorithm construction requires no less than 1 round to finalize 310 | input_bytes = finalized; 311 | return _mm_aesenc_si128(hash, _mm_set_epi64x(0x8e51ef21fabb4522, 0xe43d7a0656954b6c)); 312 | } 313 | } 314 | 315 | // Verifies the implementation matches test vectors computed several ways. 316 | // Returns zero on success or line number on test failure. 317 | static int VerifyImplementation() { 318 | // A 31-byte string is the smallest key that will exercise all hash 319 | // computation branches in the small key algorithm 320 | static constexpr char test_key_small[] = "0123456789012345678901234567890"; 321 | assert(strlen(test_key_small) == 31); 322 | 323 | // A 127-byte string is the smallest key that will exercise all hash 324 | // computation branches in the large key algorithm 325 | static constexpr char test_key_large[] = "01234567890123456789012345678901" 326 | "23456789012345678901234567890123" 327 | "45678901234567890123456789012345" 328 | "6789012345678901234567890123456"; 329 | assert(strlen(test_key_large) == 127); 330 | 331 | // TEST INITIALIZERS 332 | 333 | const __m128i initialize_0 = _mm_setzero_si128(); 334 | const __m128i initialize_1 = _mm_set1_epi64x(std::numeric_limits::max()); 335 | 336 | // TEST VECTOR HASHES 337 | 338 | // Hash(test_key_small, 31, initialize_0) 339 | const uint8_t valid_31_0[] = { 0x4E, 0xF7, 0x44, 0xCA, 0xC8, 0x10, 0xCB, 0x77, 0x90, 0xD7, 0x9E, 0xDB, 0x0E, 0x6E, 0xBE, 0x9B }; 340 | // Hash(test_key_small, 31, initialize_1) 341 | const uint8_t valid_31_1[] = { 0x30, 0xE9, 0xEF, 0xE4, 0x6B, 0x5C, 0x05, 0x2E, 0xED, 0x62, 0xE3, 0xA4, 0x90, 0x77, 0x46, 0x01 }; 342 | 343 | // Hash(test_key_large, 127, initialize_0) 344 | const uint8_t valid_127_0[] = { 0x7A, 0x39, 0xDA, 0xDC, 0x21, 0x50, 0xFB, 0xF2, 0x78, 0x92, 0xC1, 0x1C, 0x25, 0xAA, 0x03, 0x4E }; 345 | // Hash(test_key_large, 127, initialize_1) 346 | const uint8_t valid_127_1[] = { 0x0E, 0xDD, 0x5A, 0x3A, 0xB7, 0x4B, 0xFA, 0xC3, 0xFF, 0x73, 0x84, 0xA2, 0x8B, 0xB9, 0xBF, 0x13 }; 347 | 348 | // small key algorithm test with first initializer 349 | { 350 | auto hash = SmallKeyAlgorithm(reinterpret_cast(test_key_small), strlen(test_key_small), initialize_0); 351 | if (memcmp(&hash, &valid_31_0, sizeof(hash))) 352 | return __LINE__; 353 | } 354 | 355 | // small key algorithm test with second initializer 356 | { 357 | auto hash = SmallKeyAlgorithm(reinterpret_cast(test_key_small), strlen(test_key_small), initialize_1); 358 | if (memcmp(&hash, &valid_31_1, sizeof(hash))) 359 | return __LINE__; 360 | } 361 | 362 | // large key algorithm test with first initializer 363 | { 364 | auto hash = LargeKeyAlgorithm(reinterpret_cast(test_key_large), strlen(test_key_large), initialize_0); 365 | if (memcmp(&hash, &valid_127_0, sizeof(hash))) 366 | return __LINE__; 367 | } 368 | 369 | // large key algorithm test with second initializer 370 | { 371 | auto hash = LargeKeyAlgorithm(reinterpret_cast(test_key_large), strlen(test_key_large), initialize_1); 372 | if (memcmp(&hash, &valid_127_1, sizeof(hash))) 373 | return __LINE__; 374 | } 375 | 376 | // make sure hybrid algorithm matches underlying algorithm components 377 | { 378 | __m128i hash; 379 | 380 | hash = Hash(reinterpret_cast(test_key_small), strlen(test_key_small), initialize_0); 381 | if (memcmp(&hash, &valid_31_0, sizeof(hash))) 382 | return __LINE__; 383 | 384 | hash = Hash(reinterpret_cast(test_key_small), strlen(test_key_small), initialize_1); 385 | if (memcmp(&hash, &valid_31_1, sizeof(hash))) 386 | return __LINE__; 387 | 388 | hash = Hash(reinterpret_cast(test_key_large), strlen(test_key_large), initialize_0); 389 | if (memcmp(&hash, &valid_127_0, sizeof(hash))) 390 | return __LINE__; 391 | 392 | hash = Hash(reinterpret_cast(test_key_large), strlen(test_key_large), initialize_1); 393 | if (memcmp(&hash, &valid_127_1, sizeof(hash))) 394 | return __LINE__; 395 | } 396 | 397 | // verify incremental algorithm against non-incremental algorithms 398 | { 399 | // incremental on small key one-shot 400 | { 401 | AquaHash aqua(initialize_0); 402 | aqua.Update(reinterpret_cast(test_key_small), strlen(test_key_small)); 403 | auto hash = aqua.Finalize(); 404 | if (memcmp(&hash, &valid_31_0, sizeof(hash))) 405 | return __LINE__; 406 | } 407 | 408 | // incremental on large key one-shot 409 | { 410 | AquaHash aqua(initialize_0); 411 | aqua.Update(reinterpret_cast(test_key_large), strlen(test_key_large)); 412 | auto hash = aqua.Finalize(); 413 | if (memcmp(&hash, &valid_127_0, sizeof(hash))) 414 | return __LINE__; 415 | } 416 | 417 | // incremental using every possible chunk size across block boundary 418 | { 419 | for (size_t span=1; span<=strlen(test_key_large); span++) { 420 | AquaHash aqua(initialize_0); 421 | size_t div = strlen(test_key_large) / span; 422 | size_t mod = strlen(test_key_large) % span; 423 | for (size_t j=0; j(&test_key_large[j * span]), span); 425 | aqua.Update(reinterpret_cast(&test_key_large[strlen(test_key_large) - mod]), mod); 426 | auto hash = aqua.Finalize(); 427 | if (memcmp(&hash, &valid_127_0, sizeof(hash))) 428 | return __LINE__; 429 | 430 | } 431 | } 432 | } 433 | 434 | return 0; 435 | } 436 | }; 437 | 438 | 439 | #endif // #ifndef AQUAHASH_H 440 | 441 | --------------------------------------------------------------------------------