Debian Codegraph @

debian/rust-vendor/blake3/README.md

raw
1 # BLAKE3
2
3 BLAKE3 is a cryptographic hash function that is:
4
5 - **Much faster** than MD5, SHA-1, SHA-2, SHA-3, and BLAKE2.
6 - **Secure**, unlike MD5 and SHA-1. And secure against length extension,
7 unlike SHA-2.
8 - **Highly parallelizable** across any number of threads and SIMD lanes,
9 because it's a Merkle tree on the inside.
10 - Capable of **verified streaming** and **incremental updates**, again
11 because it's a Merkle tree.
12 - A **PRF**, **MAC**, **KDF**, and **XOF**, as well as a regular hash.
13 - **One algorithm with no variants**, which is fast on x86-64 and also
14 on smaller architectures.
15
16 The [chart below](https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/benchmarks/bar_chart.py)
17 is an example benchmark of 16 KiB inputs on a Cascade Lake-SP 8275CL server CPU
18 from 2019. For more detailed benchmarks, see the
19 [BLAKE3 paper](https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/blake3.pdf).
20
21 <p align="center">
22 <img src="media/speed.svg" alt="performance graph">
23 </p>
24
25 BLAKE3 is based on an optimized instance of the established hash
26 function [BLAKE2](https://blake2.net) and on the [original Bao tree
27 mode](https://github.com/oconnor663/bao/blob/master/docs/spec_0.9.1.md).
28 The specifications and design rationale are available in the [BLAKE3
29 paper](https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/blake3.pdf).
30 The default output size is 256 bits. The current version of
31 [Bao](https://github.com/oconnor663/bao) implements verified streaming
32 with BLAKE3.
33
34 This repository is the official implementation of BLAKE3. It includes:
35
36 * The [`blake3`](https://crates.io/crates/blake3) Rust crate, which
37 includes optimized implementations for SSE2, SSE4.1, AVX2, AVX-512,
38 NEON, and WASM, with automatic runtime CPU feature detection on x86.
39 The `rayon` feature provides multithreading.
40
41 * The [`b3sum`](https://crates.io/crates/b3sum) Rust crate, which
42 provides a command line interface. It uses multithreading by default,
43 making it an order of magnitude faster than e.g. `sha256sum` on
44 typical desktop hardware.
45
46 * The [C implementation](c), which like the Rust implementation includes SIMD
47 optimizations (all except WASM), CPU feature detection on x86, and optional
48 multithreading. See [`c/README.md`](c/README.md).
49
50 * The [Rust reference implementation](reference_impl/reference_impl.rs),
51 which is discussed in Section 5.1 of the [BLAKE3
52 paper](https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/blake3.pdf).
53 This implementation is much smaller and simpler than the optimized
54 ones above. If you want to see how BLAKE3 works, or you're writing a
55 port that doesn't need multithreading or SIMD optimizations, start
56 here. Ports of the reference implementation to other languages are
57 hosted in separate repositories
58 ([C](https://github.com/oconnor663/blake3_reference_impl_c),
59 [Python](https://github.com/oconnor663/pure_python_blake3)).
60
61 * A [set of test
62 vectors](https://github.com/BLAKE3-team/BLAKE3/blob/master/test_vectors/test_vectors.json)
63 that covers extended outputs, all three modes, and a variety of input
64 lengths.
65
66 * [![Actions Status](https://github.com/BLAKE3-team/BLAKE3/workflows/tests/badge.svg)](https://github.com/BLAKE3-team/BLAKE3/actions)
67
68 BLAKE3 was designed by:
69
70 * [@oconnor663] (Jack O'Connor)
71 * [@sneves] (Samuel Neves)
72 * [@veorq] (Jean-Philippe Aumasson)
73 * [@zookozcash] (Zooko)
74
75 The development of BLAKE3 was sponsored by [Electric Coin Company](https://electriccoin.co).
76
77 BLAKE3 is also [specified](https://c2sp.org/BLAKE3) in the [Community
78 Cryptography Specification Project (C2SP)](https://c2sp.org).
79
80 *NOTE: BLAKE3 is not a password hashing algorithm, because it's
81 designed to be fast, whereas password hashing should not be fast. If you
82 hash passwords to store the hashes or if you derive keys from passwords,
83 we recommend [Argon2](https://github.com/P-H-C/phc-winner-argon2).*
84
85 ## Usage
86
87 ### The `b3sum` utility
88
89 The `b3sum` command line utility prints the BLAKE3 hashes of files or of
90 standard input. Prebuilt binaries are available for Linux, Windows, and
91 macOS (requiring the [unidentified developer
92 workaround](https://support.apple.com/guide/mac-help/open-a-mac-app-from-an-unidentified-developer-mh40616/mac))
93 on the [releases page](https://github.com/BLAKE3-team/BLAKE3/releases).
94 If you've [installed Rust and
95 Cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html),
96 you can also build `b3sum` yourself with:
97
98 ```bash
99 cargo install b3sum
100 ```
101
102 If `rustup` didn't configure your `PATH` for you, you might need to go
103 looking for the installed binary in e.g. `~/.cargo/bin`. You can test
104 out how fast BLAKE3 is on your machine by creating a big file and
105 hashing it, for example:
106
107 ```bash
108 # Create a 1 GB file.
109 head -c 1000000000 /dev/zero > /tmp/bigfile
110 # Hash it with SHA-256.
111 time openssl sha256 /tmp/bigfile
112 # Hash it with BLAKE3.
113 time b3sum /tmp/bigfile
114 ```
115
116 ### The `blake3` crate [![docs.rs](https://docs.rs/blake3/badge.svg)](https://docs.rs/blake3)
117
118 To use BLAKE3 from Rust code, add a dependency on the `blake3` crate to
119 your `Cargo.toml`. Here's an example of hashing some input bytes:
120
121 ```rust
122 // Hash an input all at once.
123 let hash1 = blake3::hash(b"foobarbaz");
124
125 // Hash an input incrementally.
126 let mut hasher = blake3::Hasher::new();
127 hasher.update(b"foo");
128 hasher.update(b"bar");
129 hasher.update(b"baz");
130 let hash2 = hasher.finalize();
131 assert_eq!(hash1, hash2);
132
133 // Extended output. OutputReader also implements Read and Seek.
134 let mut output = [0; 1000];
135 let mut output_reader = hasher.finalize_xof();
136 output_reader.fill(&mut output);
137 assert_eq!(hash1, output[..32]);
138
139 // Print a hash as hex.
140 println!("{}", hash1);
141 ```
142
143 Besides `hash`, BLAKE3 provides two other modes, `keyed_hash` and
144 `derive_key`. The `keyed_hash` mode takes a 256-bit key:
145
146 ```rust
147 // MAC an input all at once.
148 let example_key = [42u8; 32];
149 let mac1 = blake3::keyed_hash(&example_key, b"example input");
150
151 // MAC incrementally.
152 let mut hasher = blake3::Hasher::new_keyed(&example_key);
153 hasher.update(b"example input");
154 let mac2 = hasher.finalize();
155 assert_eq!(mac1, mac2);
156 ```
157
158 The `derive_key` mode takes a context string and some key material (not a
159 password). The context string should be hardcoded, globally unique, and
160 application-specific. A good default format for the context string is
161 `"[application] [commit timestamp] [purpose]"`:
162
163 ```rust
164 // Derive a couple of subkeys for different purposes.
165 const EMAIL_CONTEXT: &str = "BLAKE3 example 2020-01-07 17:10:44 email key";
166 const API_CONTEXT: &str = "BLAKE3 example 2020-01-07 17:11:21 API key";
167 let input_key_material = b"usually at least 32 random bytes, not a password";
168 let email_key = blake3::derive_key(EMAIL_CONTEXT, input_key_material);
169 let api_key = blake3::derive_key(API_CONTEXT, input_key_material);
170 assert_ne!(email_key, api_key);
171 ```
172
173 ### The C implementation
174
175 See [`c/README.md`](c/README.md).
176
177 ### Other implementations
178
179 There are too many implementations out there for us to keep track of,
180 but some highlights include [an optimized Go
181 implementation](https://github.com/zeebo/blake3), [Wasm bindings for
182 Node.js and browsers](https://github.com/connor4312/blake3), [binary
183 wheels for Python](https://github.com/oconnor663/blake3-py), [.NET
184 bindings](https://github.com/xoofx/Blake3.NET), and [a pure Java
185 implementation](https://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/digest/Blake3.html).
186
187 ## Contributing
188
189 Please see [CONTRIBUTING.md](CONTRIBUTING.md).
190
191 ## Licenses
192
193 This work is released into the public domain with [CC0 1.0](./LICENSE_CC0).
194 Alternatively, it is licensed under any of the following:
195
196 * [Apache 2.0](./LICENSE_A2)
197 * [Apache 2.0 with LLVM exceptions](./LICENSE_A2LLVM)
198
199
200 ## Adoption & deployment
201
202 * [Bazel](https://github.com/bazelbuild/bazel/releases/tag/6.4.0)
203 * [Cargo](https://github.com/rust-lang/cargo/pull/14137)
204 * [Ccache](https://ccache.dev/releasenotes.html#_ccache_4_0)
205 * [Chia](https://github.com/Chia-Network/chia-blockchain/blob/main/CHANGELOG.md#10beta8-aka-beta-18---2020-07-16)
206 * [Clickhouse](https://github.com/ClickHouse/ClickHouse/blob/master/rust/chcache/Cargo.toml#L7)
207 * [Farcaster](https://www.farcaster.xyz/)
208 * [IPFS](https://github.com/ipfs/go-verifcid/issues/13)
209 * [Iroh](https://www.iroh.computer/blog/blake3-hazmat-api)
210 * [LLVM](https://reviews.llvm.org/D121510)
211 * [Nix](https://github.com/NixOS/nix/pull/12379)
212 * [Nym](https://github.com/nymtech/nym/blob/59056a22c5e6b01a38da2124662bd1fa3c8abef2/common/nymsphinx/params/src/lib.rs#L5)
213 * [OpenZFS](https://github.com/openzfs/zfs/)
214 * [Redox](https://www.redox-os.org/news/pkgar-introduction/)
215 * [Solana](https://docs.rs/solana-program/1.9.5/solana_program/blake3/index.html)
216 * [Tekken 8](https://x.com/rodarmor/status/1751567502050771189)
217 * [Wasmer](https://github.com/wasmerio/wasmer/blob/4f935a8c162bf604df223003e434e4f7ca253688/lib/cache/src/hash.rs#L21)
218
219
220 ## Miscellany
221
222 - [@veorq] and [@oconnor663] did [an interview with Cryptography FM](https://cryptography.fireside.fm/3).
223 - [@oconnor663] did [an interview with Saito](https://www.youtube.com/watch?v=cJkmIt7yN_E).
224
225 [@oconnor663]: https://github.com/oconnor663
226 [@sneves]: https://github.com/sneves
227 [@veorq]: https://github.com/veorq
228 [@zookozcash]: https://github.com/zookozcash