File Coverage

prvhash_core.h
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition n/a
subroutine n/a
pod n/a
total 13 13 100.0


line stmt bran cond sub pod time code
1             /**
2             * prvhash_core.h version 4.3.4
3             *
4             * The inclusion file for the "prvhash_core*" PRVHASH core functions for
5             * various state variable sizes. Also includes several auxiliary functions and
6             * macros for endianness-correction.
7             *
8             * Description is available at https://github.com/avaneev/prvhash
9             * E-mail: aleksey.vaneev@gmail.com or info@voxengo.com
10             *
11             * License
12             *
13             * Copyright (c) 2020-2025 Aleksey Vaneev
14             *
15             * Permission is hereby granted, free of charge, to any person obtaining a
16             * copy of this software and associated documentation files (the "Software"),
17             * to deal in the Software without restriction, including without limitation
18             * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19             * and/or sell copies of the Software, and to permit persons to whom the
20             * Software is furnished to do so, subject to the following conditions:
21             *
22             * The above copyright notice and this permission notice shall be included in
23             * all copies or substantial portions of the Software.
24             *
25             * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26             * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27             * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28             * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29             * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30             * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31             * DEALINGS IN THE SOFTWARE.
32             */
33            
34             #ifndef PRVHASH_CORE_INCLUDED
35             #define PRVHASH_CORE_INCLUDED
36            
37             #include
38             #include
39            
40             /**
41             * @def PRVHASH_INIT_COUNT
42             * @brief Common number of PRVHASH initialization rounds.
43             */
44            
45             #define PRVHASH_INIT_COUNT 5
46            
47             /**
48             * @def PRVHASH_GCC_BUILTINS
49             * @brief Macro that denotes availability of GCC-style built-in functions.
50             */
51            
52             #if defined( __GNUC__ ) || defined( __clang__ ) || \
53             defined( __IBMC__ ) || defined( __IBMCPP__ ) || defined( __COMPCERT__ )
54            
55             #define PRVHASH_GCC_BUILTINS
56            
57             #endif // GCC built-ins check
58            
59             /**
60             * @def PRVHASH_INLINE
61             * @brief Macro to force code inlining.
62             */
63            
64             #if defined( PRVHASH_GCC_BUILTINS )
65            
66             #define PRVHASH_INLINE inline __attribute__((always_inline))
67            
68             #elif defined( _MSC_VER )
69            
70             #define PRVHASH_INLINE inline __forceinline
71            
72             #else // defined( _MSC_VER )
73            
74             #define PRVHASH_INLINE inline
75            
76             #endif // defined( _MSC_VER )
77            
78             /**
79             * This function runs a single PRVHASH random number generation round. This
80             * function can be used both as a hash generator and as a general-purpose
81             * random number generator. In either case, it is advisable to initially run
82             * this function 5 times (independent of state variable's size), before using
83             * its random output, to neutralize any possible oddities of state variables'
84             * initial values (including zero values). Note that after such
85             * initialization, any further "strange" or zero values in the hashword array
86             * do not have any influence over the quality of the output (since they get
87             * mixed with the Seed that already became uniformly-random).
88             *
89             * To generate hashes, the "Seed" and "lcg" variables should be simultaneously
90             * XORed with the same entropy input, prior to calling this function.
91             * Additionally, the "Seed" can be XORed with a good-quality uniformly-random
92             * entropy (including output of another PRVHASH system): this is called
93             * "daisy-chaining", it does not interfere with hashing.
94             *
95             * @param[in,out] Seed0 The current "Seed" value. Can be initialized to any
96             * value.
97             * @param[in,out] lcg0 The current "lcg" value. Can be initialized to any
98             * value.
99             * @param[in,out] Hash0 Current hash word in a hash word array.
100             * @return Current random value.
101             */
102            
103             static PRVHASH_INLINE uint64_t prvhash_core64( uint64_t* const Seed0,
104             uint64_t* const lcg0, uint64_t* const Hash0 )
105             {
106 257           uint64_t Seed = *Seed0; uint64_t lcg = *lcg0; uint64_t Hash = *Hash0;
107            
108 257           Seed *= lcg * 2 + 1;
109 257           const uint64_t rs = Seed >> 32 | Seed << 32;
110 257           Hash += rs + 0xAAAAAAAAAAAAAAAA;
111 257           lcg += Seed + 0x5555555555555555;
112 257           Seed ^= Hash;
113 257           const uint64_t out = lcg ^ rs;
114            
115 257           *Seed0 = Seed; *lcg0 = lcg; *Hash0 = Hash;
116            
117 257           return( out );
118             }
119            
120             static PRVHASH_INLINE uint32_t prvhash_core32( uint32_t* const Seed0,
121             uint32_t* const lcg0, uint32_t* const Hash0 )
122             {
123             uint32_t Seed = *Seed0; uint32_t lcg = *lcg0; uint32_t Hash = *Hash0;
124            
125             Seed *= lcg * 2 + 1;
126             const uint32_t rs = Seed >> 16 | Seed << 16;
127             Hash += rs + 0xAAAAAAAA;
128             lcg += Seed + 0x55555555;
129             Seed ^= Hash;
130             const uint32_t out = lcg ^ rs;
131            
132             *Seed0 = Seed; *lcg0 = lcg; *Hash0 = Hash;
133            
134             return( out );
135             }
136            
137             static PRVHASH_INLINE uint16_t prvhash_core16( uint16_t* const Seed0,
138             uint16_t* const lcg0, uint16_t* const Hash0 )
139             {
140             uint16_t Seed = *Seed0; uint16_t lcg = *lcg0; uint16_t Hash = *Hash0;
141            
142             Seed *= (uint16_t) ( lcg * 2 + 1 );
143             const uint16_t rs = (uint16_t) ( Seed >> 8 | Seed << 8 );
144             Hash += (uint16_t) ( rs + 0xAAAA );
145             lcg += (uint16_t) ( Seed + 0x5555 );
146             Seed ^= Hash;
147             const uint16_t out = (uint16_t) ( lcg ^ rs );
148            
149             *Seed0 = Seed; *lcg0 = lcg; *Hash0 = Hash;
150            
151             return( out );
152             }
153            
154             static PRVHASH_INLINE uint8_t prvhash_core8( uint8_t* const Seed0,
155             uint8_t* const lcg0, uint8_t* const Hash0 )
156             {
157             uint8_t Seed = *Seed0; uint8_t lcg = *lcg0; uint8_t Hash = *Hash0;
158            
159             Seed *= (uint8_t) ( lcg * 2 + 1 );
160             const uint8_t rs = (uint8_t) ( Seed >> 4 | Seed << 4 );
161             Hash += (uint8_t) ( rs + 0xAA );
162             lcg += (uint8_t) ( Seed + 0x55 );
163             Seed ^= Hash;
164             const uint8_t out = (uint8_t) ( lcg ^ rs );
165            
166             *Seed0 = Seed; *lcg0 = lcg; *Hash0 = Hash;
167            
168             return( out );
169             }
170            
171             static PRVHASH_INLINE uint8_t prvhash_core4( uint8_t* const Seed0,
172             uint8_t* const lcg0, uint8_t* const Hash0 )
173             {
174             uint8_t Seed = *Seed0; uint8_t lcg = *lcg0; uint8_t Hash = *Hash0;
175            
176             Seed *= (uint8_t) ( lcg * 2 + 1 );
177             Seed &= 15;
178             const uint8_t rs = (uint8_t) (( Seed >> 2 | Seed << 2 ) & 15 );
179             Hash += (uint8_t) ( rs + 0xA );
180             Hash &= 15;
181             lcg += (uint8_t) ( Seed + 0x5 );
182             lcg &= 15;
183             Seed ^= Hash;
184             const uint8_t out = (uint8_t) ( lcg ^ rs );
185            
186             *Seed0 = Seed; *lcg0 = lcg; *Hash0 = Hash;
187            
188             return( out );
189             }
190            
191             static PRVHASH_INLINE uint8_t prvhash_core2( uint8_t* const Seed0,
192             uint8_t* const lcg0, uint8_t* const Hash0 )
193             {
194             uint8_t Seed = *Seed0; uint8_t lcg = *lcg0; uint8_t Hash = *Hash0;
195            
196             Seed *= (uint8_t) ( lcg * 2 + 1 );
197             Seed &= 3;
198             const uint8_t rs = (uint8_t) (( Seed >> 1 | Seed << 1 ) & 3 );
199             Hash += (uint8_t) ( rs + 0x2 );
200             Hash &= 3;
201             lcg += (uint8_t) ( Seed + 0x1 );
202             lcg &= 3;
203             Seed ^= Hash;
204             const uint8_t out = (uint8_t) ( lcg ^ rs );
205            
206             *Seed0 = Seed; *lcg0 = lcg; *Hash0 = Hash;
207            
208             return( out );
209             }
210            
211             #if defined( __SIZEOF_INT128__ )
212            
213             static PRVHASH_INLINE __uint128_t prvhash_core128( __uint128_t* const Seed0,
214             __uint128_t* const lcg0, __uint128_t* const Hash0 )
215             {
216             __uint128_t Seed = *Seed0; __uint128_t lcg = *lcg0;
217             __uint128_t Hash = *Hash0;
218            
219             Seed *= lcg * 2 + 1;
220             const __uint128_t rs = Seed >> 64 | Seed << 64;
221             Hash += rs +
222             ( 0xAAAAAAAAAAAAAAAA | (__uint128_t) 0xAAAAAAAAAAAAAAAA << 64 );
223            
224             lcg += Seed +
225             ( 0x5555555555555555 | (__uint128_t) 0x5555555555555555 << 64 );
226            
227             Seed ^= Hash;
228             const __uint128_t out = lcg ^ rs;
229            
230             *Seed0 = Seed; *lcg0 = lcg; *Hash0 = Hash;
231            
232             return( out );
233             }
234            
235             #endif // defined( __SIZEOF_INT128__ )
236            
237             /**
238             * @def PRVHASH_LITTLE_ENDIAN
239             * @brief Endianness definition macro, can be used as a logical constant.
240             */
241            
242             #if defined( __LITTLE_ENDIAN__ ) || defined( __LITTLE_ENDIAN ) || \
243             defined( _LITTLE_ENDIAN ) || defined( _WIN32 ) || defined( i386 ) || \
244             defined( __i386 ) || defined( __i386__ ) || defined( _M_IX86 ) || \
245             defined( _M_X64 ) || defined( _M_AMD64 ) || defined( __x86_64__ ) || \
246             defined( __amd64 ) || defined( __amd64__ ) || \
247             ( defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
248            
249             #define PRVHASH_LITTLE_ENDIAN 1
250            
251             #elif defined( __BIG_ENDIAN__ ) || defined( __BIG_ENDIAN ) || \
252             defined( _BIG_ENDIAN ) || defined( __SYSC_ZARCH__ ) || \
253             defined( __zarch__ ) || defined( __s390x__ ) || defined( __sparc ) || \
254             defined( __sparc__ ) || \
255             ( defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
256            
257             #define PRVHASH_LITTLE_ENDIAN 0
258            
259             #else // defined( __BIG_ENDIAN__ )
260            
261             #warning PRVHASH: cannot determine endianness, assuming little-endian.
262            
263             #define PRVHASH_LITTLE_ENDIAN 1
264            
265             #endif // defined( __BIG_ENDIAN__ )
266            
267             /**
268             * @def PRVHASH_EC32( x )
269             * @brief Endianness-correction macro, for 32-bit variables.
270             * @param x Value to correct.
271             */
272            
273             /**
274             * @def PRVHASH_EC64( x )
275             * @brief Endianness-correction macro, for 64-bit variables.
276             * @param x Value to correct.
277             */
278            
279             #if PRVHASH_LITTLE_ENDIAN
280            
281             #define PRVHASH_EC32( v ) ( v )
282             #define PRVHASH_EC64( v ) ( v )
283            
284             #else // PRVHASH_LITTLE_ENDIAN
285            
286             #if defined( PRVHASH_GCC_BUILTINS )
287            
288             #define PRVHASH_EC32( v ) __builtin_bswap32( v )
289             #define PRVHASH_EC64( v ) __builtin_bswap64( v )
290            
291             #elif defined( _MSC_VER )
292            
293             #include
294            
295             #define PRVHASH_EC32( v ) _byteswap_ulong( v )
296             #define PRVHASH_EC64( v ) _byteswap_uint64( v )
297            
298             #else // defined( _MSC_VER )
299            
300             #define PRVHASH_EC32( v ) ( \
301             ( v & 0xFF000000 ) >> 24 | \
302             ( v & 0x00FF0000 ) >> 8 | \
303             ( v & 0x0000FF00 ) << 8 | \
304             ( v & 0x000000FF ) << 24 )
305            
306             #define PRVHASH_EC64( v ) ( \
307             ( v & 0xFF00000000000000 ) >> 56 | \
308             ( v & 0x00FF000000000000 ) >> 40 | \
309             ( v & 0x0000FF0000000000 ) >> 24 | \
310             ( v & 0x000000FF00000000 ) >> 8 | \
311             ( v & 0x00000000FF000000 ) << 8 | \
312             ( v & 0x0000000000FF0000 ) << 24 | \
313             ( v & 0x000000000000FF00 ) << 40 | \
314             ( v & 0x00000000000000FF ) << 56 )
315            
316             #endif // defined( _MSC_VER )
317            
318             #endif // PRVHASH_LITTLE_ENDIAN
319            
320             /**
321             * An auxiliary function that returns an unsigned 32-bit value created out of
322             * individual bytes in a buffer. This function is used to convert endianness
323             * of supplied 32-bit unsigned values, and to avoid unaligned memory accesses.
324             *
325             * @param p 4-byte buffer. Alignment is unimportant.
326             */
327            
328             static PRVHASH_INLINE uint32_t prvhash_lu32ec( const uint8_t* const p )
329             {
330             uint32_t v;
331 8           memcpy( &v, p, 4 );
332            
333 8           return( PRVHASH_EC32( v ));
334             }
335            
336             /**
337             * An auxiliary function that returns an unsigned 64-bit value created out of
338             * individual bytes in a buffer. This function is used to convert endianness
339             * of supplied 64-bit unsigned values, and to avoid unaligned memory accesses.
340             *
341             * @param p 8-byte buffer. Alignment is unimportant.
342             */
343            
344             static PRVHASH_INLINE uint64_t prvhash_lu64ec( const uint8_t* const p )
345             {
346             uint64_t v;
347 13           memcpy( &v, p, 8 );
348            
349 13           return( PRVHASH_EC64( v ));
350             }
351            
352             #endif // PRVHASH_CORE_INCLUDED