• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.gitignoreH A D29-Nov-197319 43

Makefile.testingH A D29-Nov-19732 KiB8362

README.mdH A D29-Nov-19739.7 KiB302238

blake3.cH A D29-Nov-197326.8 KiB612382

blake3.hH A D29-Nov-19731.9 KiB6044

blake3_avx2.cH A D29-Nov-197312.1 KiB326294

blake3_avx2_x86-64_unix.SH A D29-Nov-197364.5 KiB1,8161,809

blake3_avx2_x86-64_windows_gnu.SH A D29-Nov-197365.2 KiB1,8181,815

blake3_avx2_x86-64_windows_msvc.asmH A D29-Nov-197364.4 KiB1,8291,813

blake3_avx512.cH A D29-Nov-197346.8 KiB1,2051,046

blake3_avx512_x86-64_unix.SH A D29-Nov-197387.3 KiB2,5862,578

blake3_avx512_x86-64_windows_gnu.SH A D29-Nov-197388.9 KiB2,6162,607

blake3_avx512_x86-64_windows_msvc.asmH A D29-Nov-197389.5 KiB2,6352,626

blake3_dispatch.cH A D29-Nov-19737.4 KiB277255

blake3_impl.hH A D29-Nov-19739.9 KiB283234

blake3_neon.cH A D29-Nov-197312.6 KiB352298

blake3_portable.cH A D29-Nov-19735.8 KiB161144

blake3_sse2.cH A D29-Nov-197320.5 KiB566502

blake3_sse2_x86-64_unix.SH A D29-Nov-197367.2 KiB2,2922,284

blake3_sse2_x86-64_windows_gnu.SH A D29-Nov-197369.5 KiB2,3332,327

blake3_sse2_x86-64_windows_msvc.asmH A D29-Nov-197369.3 KiB2,3512,333

blake3_sse41.cH A D29-Nov-197320.3 KiB560497

blake3_sse41_x86-64_unix.SH A D29-Nov-197359.7 KiB2,0292,021

blake3_sse41_x86-64_windows_gnu.SH A D29-Nov-197362 KiB2,0702,064

blake3_sse41_x86-64_windows_msvc.asmH A D29-Nov-197361.7 KiB2,0902,070

example.cH A D29-Nov-1973883 3829

main.cH A D29-Nov-19733.9 KiB167141

test.pyH A D29-Nov-19733.6 KiB9870

README.md

1The official C implementation of BLAKE3.
2
3# Example
4
5An example program that hashes bytes from standard input and prints the
6result:
7
8```c
9#include "blake3.h"
10#include <errno.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <unistd.h>
15
16int main() {
17  // Initialize the hasher.
18  blake3_hasher hasher;
19  blake3_hasher_init(&hasher);
20
21  // Read input bytes from stdin.
22  unsigned char buf[65536];
23  while (1) {
24    ssize_t n = read(STDIN_FILENO, buf, sizeof(buf));
25    if (n > 0) {
26      blake3_hasher_update(&hasher, buf, n);
27    } else if (n == 0) {
28      break; // end of file
29    } else {
30      fprintf(stderr, "read failed: %s\n", strerror(errno));
31      exit(1);
32    }
33  }
34
35  // Finalize the hash. BLAKE3_OUT_LEN is the default output length, 32 bytes.
36  uint8_t output[BLAKE3_OUT_LEN];
37  blake3_hasher_finalize(&hasher, output, BLAKE3_OUT_LEN);
38
39  // Print the hash as hexadecimal.
40  for (size_t i = 0; i < BLAKE3_OUT_LEN; i++) {
41    printf("%02x", output[i]);
42  }
43  printf("\n");
44  return 0;
45}
46```
47
48The code above is included in this directory as `example.c`. If you're
49on x86\_64 with a Unix-like OS, you can compile a working binary like
50this:
51
52```bash
53gcc -O3 -o example example.c blake3.c blake3_dispatch.c blake3_portable.c \
54    blake3_sse2_x86-64_unix.S blake3_sse41_x86-64_unix.S blake3_avx2_x86-64_unix.S \
55    blake3_avx512_x86-64_unix.S
56```
57
58# API
59
60## The Struct
61
62```c
63typedef struct {
64  // private fields
65} blake3_hasher;
66```
67
68An incremental BLAKE3 hashing state, which can accept any number of
69updates. This implementation doesn't allocate any heap memory, but
70`sizeof(blake3_hasher)` itself is relatively large, currently 1912 bytes
71on x86-64. This size can be reduced by restricting the maximum input
72length, as described in Section 5.4 of [the BLAKE3
73spec](https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/blake3.pdf),
74but this implementation doesn't currently support that strategy.
75
76## Common API Functions
77
78```c
79void blake3_hasher_init(
80  blake3_hasher *self);
81```
82
83Initialize a `blake3_hasher` in the default hashing mode.
84
85---
86
87```c
88void blake3_hasher_update(
89  blake3_hasher *self,
90  const void *input,
91  size_t input_len);
92```
93
94Add input to the hasher. This can be called any number of times.
95
96---
97
98```c
99void blake3_hasher_finalize(
100  const blake3_hasher *self,
101  uint8_t *out,
102  size_t out_len);
103```
104
105Finalize the hasher and return an output of any length, given in bytes.
106This doesn't modify the hasher itself, and it's possible to finalize
107again after adding more input. The constant `BLAKE3_OUT_LEN` provides
108the default output length, 32 bytes, which is recommended for most
109callers.
110
111Outputs shorter than the default length of 32 bytes (256 bits) provide
112less security. An N-bit BLAKE3 output is intended to provide N bits of
113first and second preimage resistance and N/2 bits of collision
114resistance, for any N up to 256. Longer outputs don't provide any
115additional security.
116
117Shorter BLAKE3 outputs are prefixes of longer ones. Explicitly
118requesting a short output is equivalent to truncating the default-length
119output. (Note that this is different between BLAKE2 and BLAKE3.)
120
121## Less Common API Functions
122
123```c
124void blake3_hasher_init_keyed(
125  blake3_hasher *self,
126  const uint8_t key[BLAKE3_KEY_LEN]);
127```
128
129Initialize a `blake3_hasher` in the keyed hashing mode. The key must be
130exactly 32 bytes.
131
132---
133
134```c
135void blake3_hasher_init_derive_key(
136  blake3_hasher *self,
137  const char *context);
138```
139
140Initialize a `blake3_hasher` in the key derivation mode. The context
141string is given as an initialization parameter, and afterwards input key
142material should be given with `blake3_hasher_update`. The context string
143is a null-terminated C string which should be **hardcoded, globally
144unique, and application-specific**. The context string should not
145include any dynamic input like salts, nonces, or identifiers read from a
146database at runtime. A good default format for the context string is
147`"[application] [commit timestamp] [purpose]"`, e.g., `"example.com
1482019-12-25 16:18:03 session tokens v1"`.
149
150This function is intended for application code written in C. For
151language bindings, see `blake3_hasher_init_derive_key_raw` below.
152
153---
154
155```c
156void blake3_hasher_init_derive_key_raw(
157  blake3_hasher *self,
158  const void *context,
159  size_t context_len);
160```
161
162As `blake3_hasher_init_derive_key` above, except that the context string
163is given as a pointer to an array of arbitrary bytes with a provided
164length. This is intended for writing language bindings, where C string
165conversion would add unnecessary overhead and new error cases. Unicode
166strings should be encoded as UTF-8.
167
168Application code in C should prefer `blake3_hasher_init_derive_key`,
169which takes the context as a C string. If you need to use arbitrary
170bytes as a context string in application code, consider whether you're
171violating the requirement that context strings should be hardcoded.
172
173---
174
175```c
176void blake3_hasher_finalize_seek(
177  const blake3_hasher *self,
178  uint64_t seek,
179  uint8_t *out,
180  size_t out_len);
181```
182
183The same as `blake3_hasher_finalize`, but with an additional `seek`
184parameter for the starting byte position in the output stream. To
185efficiently stream a large output without allocating memory, call this
186function in a loop, incrementing `seek` by the output length each time.
187
188# Building
189
190This implementation is just C and assembly files. It doesn't include a
191public-facing build system. (The `Makefile` in this directory is only
192for testing.) Instead, the intention is that you can include these files
193in whatever build system you're already using. This section describes
194the commands your build system should execute, or which you can execute
195by hand. Note that these steps may change in future versions.
196
197## x86
198
199Dynamic dispatch is enabled by default on x86. The implementation will
200query the CPU at runtime to detect SIMD support, and it will use the
201widest instruction set available. By default, `blake3_dispatch.c`
202expects to be linked with code for five different instruction sets:
203portable C, SSE2, SSE4.1, AVX2, and AVX-512.
204
205For each of the x86 SIMD instruction sets, four versions are available:
206three flavors of assembly (Unix, Windows MSVC, and Windows GNU) and one
207version using C intrinsics. The assembly versions are generally
208preferred. They perform better, they perform more consistently across
209different compilers, and they build more quickly. On the other hand, the
210assembly versions are x86\_64-only, and you need to select the right
211flavor for your target platform.
212
213Here's an example of building a shared library on x86\_64 Linux using
214the assembly implementations:
215
216```bash
217gcc -shared -O3 -o libblake3.so blake3.c blake3_dispatch.c blake3_portable.c \
218    blake3_sse2_x86-64_unix.S blake3_sse41_x86-64_unix.S blake3_avx2_x86-64_unix.S \
219    blake3_avx512_x86-64_unix.S
220```
221
222When building the intrinsics-based implementations, you need to build
223each implementation separately, with the corresponding instruction set
224explicitly enabled in the compiler. Here's the same shared library using
225the intrinsics-based implementations:
226
227```bash
228gcc -c -fPIC -O3 -msse2 blake3_sse2.c -o blake3_sse2.o
229gcc -c -fPIC -O3 -msse4.1 blake3_sse41.c -o blake3_sse41.o
230gcc -c -fPIC -O3 -mavx2 blake3_avx2.c -o blake3_avx2.o
231gcc -c -fPIC -O3 -mavx512f -mavx512vl blake3_avx512.c -o blake3_avx512.o
232gcc -shared -O3 -o libblake3.so blake3.c blake3_dispatch.c blake3_portable.c \
233    blake3_avx2.o blake3_avx512.o blake3_sse41.o blake3_sse2.o
234```
235
236Note above that building `blake3_avx512.c` requires both `-mavx512f` and
237`-mavx512vl` under GCC and Clang. Under MSVC, the single `/arch:AVX512`
238flag is sufficient. The MSVC equivalent of `-mavx2` is `/arch:AVX2`.
239MSVC enables SSE2 and SSE4.1 by defaut, and it doesn't have a
240corresponding flag.
241
242If you want to omit SIMD code entirely, you need to explicitly disable
243each instruction set. Here's an example of building a shared library on
244x86 with only portable code:
245
246```bash
247gcc -shared -O3 -o libblake3.so -DBLAKE3_NO_SSE2 -DBLAKE3_NO_SSE41 -DBLAKE3_NO_AVX2 \
248    -DBLAKE3_NO_AVX512 blake3.c blake3_dispatch.c blake3_portable.c
249```
250
251## ARM NEON
252
253The NEON implementation is enabled by default on AArch64, but not on
254other ARM targets, since not all of them support it. To enable it, set
255`BLAKE3_USE_NEON=1`. Here's an example of building a shared library on
256ARM Linux with NEON support:
257
258```bash
259gcc -shared -O3 -o libblake3.so -DBLAKE3_USE_NEON=1 blake3.c blake3_dispatch.c \
260    blake3_portable.c blake3_neon.c
261```
262
263To explicitiy disable using NEON instructions on AArch64, set
264`BLAKE3_USE_NEON=0`.
265
266```bash
267gcc -shared -O3 -o libblake3.so -DBLAKE3_USE_NEON=0 blake3.c blake3_dispatch.c \
268    blake3_portable.c
269```
270
271Note that on some targets (ARMv7 in particular), extra flags may be
272required to activate NEON support in the compiler. If you see an error
273like...
274
275```
276/usr/lib/gcc/armv7l-unknown-linux-gnueabihf/9.2.0/include/arm_neon.h:635:1: error: inlining failed
277in call to always_inline ‘vaddq_u32’: target specific option mismatch
278```
279
280...then you may need to add something like `-mfpu=neon-vfpv4
281-mfloat-abi=hard`.
282
283## Other Platforms
284
285The portable implementation should work on most other architectures. For
286example:
287
288```bash
289gcc -shared -O3 -o libblake3.so blake3.c blake3_dispatch.c blake3_portable.c
290```
291
292# Multithreading
293
294Unlike the Rust implementation, the C implementation doesn't currently support
295multithreading. A future version of this library could add support by taking an
296optional dependency on OpenMP or similar. Alternatively, we could expose a
297lower-level API to allow callers to implement concurrency themselves. The
298former would be more convenient and less error-prone, but the latter would give
299callers the maximum possible amount of control. The best choice here depends on
300the specific use case, so if you have a use case for multithreaded hashing in
301C, please file a GitHub issue and let us know.
302