Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
blake2-impl.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7/*
8 BLAKE2 reference source code package - reference C implementations
9
10 Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
11 terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
12 your option. The terms of these licenses can be found at:
13
14 - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
15 - OpenSSL license : https://www.openssl.org/source/license.html
16 - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
17
18 More information about the BLAKE2 hash function can be found at
19 https://blake2.net.
20*/
21#ifndef BLAKE2_IMPL_H
22#define BLAKE2_IMPL_H
23
24#include <stdint.h>
25#include <string.h>
26
27namespace bb::crypto {
28
29#if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
30#if defined(_MSC_VER)
31#define BLAKE2_INLINE __inline
32#elif defined(__GNUC__)
33#define BLAKE2_INLINE __inline__
34#else
35#define BLAKE2_INLINE
36#endif
37#else
38#define BLAKE2_INLINE inline
39#endif
40
41static BLAKE2_INLINE uint32_t load32(const void* src)
42{
43#if defined(NATIVE_LITTLE_ENDIAN)
44 uint32_t w;
45 memcpy(&w, src, sizeof w);
46 return w;
47#else
48 const uint8_t* p = (const uint8_t*)src;
49 return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24);
50#endif
51}
52
53static BLAKE2_INLINE uint64_t load64(const void* src)
54{
55#if defined(NATIVE_LITTLE_ENDIAN)
56 uint64_t w;
57 memcpy(&w, src, sizeof w);
58 return w;
59#else
60 const uint8_t* p = (const uint8_t*)src;
61 return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) | ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |
62 ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40) | ((uint64_t)(p[6]) << 48) | ((uint64_t)(p[7]) << 56);
63#endif
64}
65
66static BLAKE2_INLINE uint16_t load16(const void* src)
67{
68#if defined(NATIVE_LITTLE_ENDIAN)
69 uint16_t w;
70 memcpy(&w, src, sizeof w);
71 return w;
72#else
73 const uint8_t* p = (const uint8_t*)src;
74 return (uint16_t)(((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8));
75#endif
76}
77
78static BLAKE2_INLINE void store16(void* dst, uint16_t w)
79{
80#if defined(NATIVE_LITTLE_ENDIAN)
81 memcpy(dst, &w, sizeof w);
82#else
83 uint8_t* p = (uint8_t*)dst;
84 *p++ = (uint8_t)w;
85 w = (uint16_t)(w >> 8);
86 *p++ = (uint8_t)w;
87#endif
88}
89
90static BLAKE2_INLINE void store32(void* dst, uint32_t w)
91{
92#if defined(NATIVE_LITTLE_ENDIAN)
93 memcpy(dst, &w, sizeof w);
94#else
95 uint8_t* p = (uint8_t*)dst;
96 p[0] = (uint8_t)(w >> 0);
97 p[1] = (uint8_t)(w >> 8);
98 p[2] = (uint8_t)(w >> 16);
99 p[3] = (uint8_t)(w >> 24);
100#endif
101}
102
103static BLAKE2_INLINE void store64(void* dst, uint64_t w)
104{
105#if defined(NATIVE_LITTLE_ENDIAN)
106 memcpy(dst, &w, sizeof w);
107#else
108 uint8_t* p = (uint8_t*)dst;
109 p[0] = (uint8_t)(w >> 0);
110 p[1] = (uint8_t)(w >> 8);
111 p[2] = (uint8_t)(w >> 16);
112 p[3] = (uint8_t)(w >> 24);
113 p[4] = (uint8_t)(w >> 32);
114 p[5] = (uint8_t)(w >> 40);
115 p[6] = (uint8_t)(w >> 48);
116 p[7] = (uint8_t)(w >> 56);
117#endif
118}
119
120static BLAKE2_INLINE uint64_t load48(const void* src)
121{
122 const uint8_t* p = (const uint8_t*)src;
123 return ((uint64_t)(p[0]) << 0) | ((uint64_t)(p[1]) << 8) | ((uint64_t)(p[2]) << 16) | ((uint64_t)(p[3]) << 24) |
124 ((uint64_t)(p[4]) << 32) | ((uint64_t)(p[5]) << 40);
125}
126
127static BLAKE2_INLINE void store48(void* dst, uint64_t w)
128{
129 uint8_t* p = (uint8_t*)dst;
130 p[0] = (uint8_t)(w >> 0);
131 p[1] = (uint8_t)(w >> 8);
132 p[2] = (uint8_t)(w >> 16);
133 p[3] = (uint8_t)(w >> 24);
134 p[4] = (uint8_t)(w >> 32);
135 p[5] = (uint8_t)(w >> 40);
136}
137
138static BLAKE2_INLINE uint32_t rotr32(const uint32_t w, const unsigned c)
139{
140 return (w >> c) | (w << (32 - c));
141}
142
143static BLAKE2_INLINE uint64_t rotr64(const uint64_t w, const unsigned c)
144{
145 return (w >> c) | (w << (64 - c));
146}
147
148/* prevents compiler optimizing out memset() */
149static BLAKE2_INLINE void secure_zero_memory(void* v, size_t n)
150{
151 static void* (*const volatile memset_v)(void*, int, size_t) = &memset;
152 memset_v(v, 0, n);
153}
154
155} // namespace bb::crypto
156
157#endif
#define BLAKE2_INLINE