Fabcoin Core  0.16.2
P2P Digital Currency
blake2-impl.h
Go to the documentation of this file.
1 /*
2  BLAKE2 reference source code package - optimized C implementations
3 
4  Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
5 
6  To the extent possible under law, the author(s) have dedicated all copyright
7  and related and neighboring rights to this software to the public domain
8  worldwide. This software is distributed without any warranty.
9 
10  You should have received a copy of the CC0 Public Domain Dedication along with
11  this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
12 */
13 #pragma once
14 #ifndef __BLAKE2_IMPL_H__
15 #define __BLAKE2_IMPL_H__
16 
17 #include <stdint.h>
18 
19 static inline uint32_t load32( const void *src )
20 {
21 #if defined(NATIVE_LITTLE_ENDIAN)
22  uint32_t w;
23  memcpy(&w, src, sizeof w);
24  return w;
25 #else
26  const uint8_t *p = ( const uint8_t * )src;
27  uint32_t w = *p++;
28  w |= ( uint32_t )( *p++ ) << 8;
29  w |= ( uint32_t )( *p++ ) << 16;
30  w |= ( uint32_t )( *p++ ) << 24;
31  return w;
32 #endif
33 }
34 
35 static inline uint64_t load64( const void *src )
36 {
37 #if defined(NATIVE_LITTLE_ENDIAN)
38  uint64_t w;
39  memcpy(&w, src, sizeof w);
40  return w;
41 #else
42  const uint8_t *p = ( const uint8_t * )src;
43  uint64_t w = *p++;
44  w |= ( uint64_t )( *p++ ) << 8;
45  w |= ( uint64_t )( *p++ ) << 16;
46  w |= ( uint64_t )( *p++ ) << 24;
47  w |= ( uint64_t )( *p++ ) << 32;
48  w |= ( uint64_t )( *p++ ) << 40;
49  w |= ( uint64_t )( *p++ ) << 48;
50  w |= ( uint64_t )( *p++ ) << 56;
51  return w;
52 #endif
53 }
54 
55 static inline void store32( void *dst, uint32_t w )
56 {
57 #if defined(NATIVE_LITTLE_ENDIAN)
58  memcpy(dst, &w, sizeof w);
59 #else
60  uint8_t *p = ( uint8_t * )dst;
61  *p++ = ( uint8_t )w; w >>= 8;
62  *p++ = ( uint8_t )w; w >>= 8;
63  *p++ = ( uint8_t )w; w >>= 8;
64  *p++ = ( uint8_t )w;
65 #endif
66 }
67 
68 static inline void store64( void *dst, uint64_t w )
69 {
70 #if defined(NATIVE_LITTLE_ENDIAN)
71  memcpy(dst, &w, sizeof w);
72 #else
73  uint8_t *p = ( uint8_t * )dst;
74  *p++ = ( uint8_t )w; w >>= 8;
75  *p++ = ( uint8_t )w; w >>= 8;
76  *p++ = ( uint8_t )w; w >>= 8;
77  *p++ = ( uint8_t )w; w >>= 8;
78  *p++ = ( uint8_t )w; w >>= 8;
79  *p++ = ( uint8_t )w; w >>= 8;
80  *p++ = ( uint8_t )w; w >>= 8;
81  *p++ = ( uint8_t )w;
82 #endif
83 }
84 
85 static inline uint64_t load48( const void *src )
86 {
87  const uint8_t *p = ( const uint8_t * )src;
88  uint64_t w = *p++;
89  w |= ( uint64_t )( *p++ ) << 8;
90  w |= ( uint64_t )( *p++ ) << 16;
91  w |= ( uint64_t )( *p++ ) << 24;
92  w |= ( uint64_t )( *p++ ) << 32;
93  w |= ( uint64_t )( *p++ ) << 40;
94  return w;
95 }
96 
97 static inline void store48( void *dst, uint64_t w )
98 {
99  uint8_t *p = ( uint8_t * )dst;
100  *p++ = ( uint8_t )w; w >>= 8;
101  *p++ = ( uint8_t )w; w >>= 8;
102  *p++ = ( uint8_t )w; w >>= 8;
103  *p++ = ( uint8_t )w; w >>= 8;
104  *p++ = ( uint8_t )w; w >>= 8;
105  *p++ = ( uint8_t )w;
106 }
107 
108 static inline uint32_t rotl32( const uint32_t w, const unsigned c )
109 {
110  return ( w << c ) | ( w >> ( 32 - c ) );
111 }
112 
113 static inline uint64_t rotl64( const uint64_t w, const unsigned c )
114 {
115  return ( w << c ) | ( w >> ( 64 - c ) );
116 }
117 
118 static inline uint32_t rotr32( const uint32_t w, const unsigned c )
119 {
120  return ( w >> c ) | ( w << ( 32 - c ) );
121 }
122 
123 static inline uint64_t rotr64( const uint64_t w, const unsigned c )
124 {
125  return ( w >> c ) | ( w << ( 64 - c ) );
126 }
127 
128 /* prevents compiler optimizing out memset() */
129 static inline void secure_zero_memory( void *v, size_t n )
130 {
131  volatile uint8_t *p = ( volatile uint8_t * )v;
132  while( n-- ) *p++ = 0;
133 }
134 
135 #endif
136 
#define c(i)
void * memcpy(void *a, const void *b, size_t c)