
Funzione di hash crittografico veloce con modalità PRF, MAC, KDF e XOF. Supporta streaming verificato, aggiornamenti incrementali e hashing parallelizzato tramite struttura ad albero di Merkle.
# BLAKE3
BLAKE3 è una funzione hash crittografica che è:
- **Molto più veloce** di MD5, SHA-1, SHA-2, SHA-3 e BLAKE2.
- **Sicura**, a differenza di MD5 e SHA-1. E sicura contro l'estensione di lunghezza,
a differenza di SHA-2.
- **Altamente parallelizzabile** su qualsiasi numero di thread e vie SIMD,
perché è un albero di Merkle all'interno.
- **Capace di streaming verificato** e **aggiornamenti incrementali**, ancora
perché è un albero di Merkle.
- Una **PRF**, **MAC**, **KDF** e **XOF**, oltre a un hash normale.
- **Un unico algoritmo senza varianti**, che è veloce su x86-64 e anche
su architetture più piccole.
Il [grafico seguente](https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/benchmarks/bar_chart.py)
è un esempio di benchmark con input di 16 KiB su una CPU server Cascade Lake-SP 8275CL
del 2019. Per benchmark più dettagliati, consulta il
[paper su BLAKE3](https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/blake3.pdf).
<p align="center">
<img src="https://raw.githubusercontent.com/blake3-team/blake3/HEAD/media/speed.svg" alt="grafico delle prestazioni">
</p>
BLAKE3 si basa su un'istanza ottimizzata della consolidata funzione
hash [BLAKE2](https://blake2.net) e sulla [modalità ad albero originale di Bao](https://github.com/oconnor663/bao/blob/master/docs/spec_0.9.1.md).
Le specifiche e la logica di progettazione sono disponibili nel [paper su
BLAKE3](https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/blake3.pdf).
La dimensione di output predefinita è 256 bit. La versione corrente di
[Bao](https://github.com/oconnor663/bao) implementa lo streaming verificato
con BLAKE3.
Questo repository è l'implementazione ufficiale di BLAKE3. Include:
* La crate Rust [`blake3`](https://crates.io/crates/blake3), che
include implementazioni ottimizzate per SSE2, SSE4.1, AVX2, AVX-512,
NEON e WASM, con rilevamento automatico delle funzionalità CPU in fase di esecuzione su x86.
La feature `rayon` fornisce multithreading.
* La crate Rust [`b3sum`](https://crates.io/crates/b3sum), che
fornisce un'interfaccia a riga di comando. Usa il multithreading di default,
rendendolo un ordine di grandezza più veloce di ad esempio `sha256sum` su
hardware desktop tipico.
* [L'implementazione C](https://github.com/blake3-team/blake3/blob/master/c), che come l'implementazione Rust include ottimizzazioni
SIMD (tutte tranne WASM), rilevamento delle funzionalità CPU su x86 e multithreading
opzionale. Vedi [`c/README.md`](https://github.com/blake3-team/blake3/blob/master/c/README.md).
* [L'implementazione di riferimento Rust](https://github.com/blake3-team/blake3/blob/master/reference_impl/reference_impl.rs),
discussa nella Sezione 5.1 del [paper su
BLAKE3](https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/blake3.pdf).
Questa implementazione è molto più piccola e più semplice di quelle ottimizzate
sopra. Se vuoi capire come funziona BLAKE3, o stai scrivendo un
porting che non necessita di multithreading o ottimizzazioni SIMD, inizia
qui. I porting dell'implementazione di riferimento in altri linguaggi sono
ospitati in repository separati
([C](https://github.com/oconnor663/blake3_reference_impl_c),
[Python](https://github.com/oconnor663/pure_python_blake3)).
* Un [insieme di vettori di
test](https://github.com/BLAKE3-team/BLAKE3/blob/master/test_vectors/test_vectors.json)
che copre output estesi, tutte e tre le modalità e una varietà di lunghezze
di input.
* [](https://github.com/BLAKE3-team/BLAKE3/actions)
BLAKE3 è stato progettato da:
* [@oconnor663] (Jack O'Connor)
* [@sneves] (Samuel Neves)
* [@veorq] (Jean-Philippe Aumasson)
* [@zookozcash] (Zooko)
Lo sviluppo di BLAKE3 è stato sponsorizzato da [Electric Coin Company](https://electriccoin.co).
BLAKE3 è anche [specificato](https://c2sp.org/BLAKE3) nel [Community
Cryptography Specification Project (C2SP)](https://c2sp.org).
*NOTA: BLAKE3 non è un algoritmo di hashing per password, perché è
progettato per essere veloce, mentre l'hashing delle password non dovrebbe essere veloce. Se
hashate le password per memorizzare gli hash o se derivate chiavi dalle password,
raccomandiamo [Argon2](https://github.com/P-H-C/phc-winner-argon2).*
## Utilizzo
### L'utility `b3sum`
L'utility a riga di comando `b3sum` stampa gli hash BLAKE3 di file o dell'input
standard. I binari precompilati sono disponibili per Linux, Windows e
macOS (che richiede la [soluzione per sviluppatore non
identificato](https://support.apple.com/guide/mac-help/open-a-mac-app-from-an-unidentified-developer-mh40616/mac))
nella [pagina delle release](https://github.com/BLAKE3-team/BLAKE3/releases).
Se hai [installato Rust e
Cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html),
puoi anche compilare `b3sum` da te con:
```bash
cargo install b3sum
```
Se `rustup` non ha configurato il tuo `PATH` per te, potresti dover cercare
il binario installato in e.g. `~/.cargo/bin`. Puoi testare
quanto è veloce BLAKE3 sulla tua macchina creando un file grande e
calcolandone l'hash, ad esempio:
```bash
# Create a 1 GB file.
head -c 1000000000 /dev/zero > /tmp/bigfile
# Hash it with SHA-256.
time openssl sha256 /tmp/bigfile
# Hash it with BLAKE3.
time b3sum /tmp/bigfile
```
### La crate `blake3` [](https://docs.rs/blake3)
Per utilizzare BLAKE3 da codice Rust, aggiungi una dipendenza alla crate `blake3` nel
tuo `Cargo.toml`. Ecco un esempio di hashing di alcuni byte di input:
```rust
// Hash an input all at once.
let hash1 = blake3::hash(b"foobarbaz");
// Hash an input incrementally.
let mut hasher = blake3::Hasher::new();
hasher.update(b"foo");
hasher.update(b"bar");
hasher.update(b"baz");
let hash2 = hasher.finalize();
assert_eq!(hash1, hash2);
// Extended output. OutputReader also implements Read and Seek.
let mut output = [0; 1000];
let mut output_reader = hasher.finalize_xof();
output_reader.fill(&mut output);
assert_eq!(hash1, output[..32]);
// Print a hash as hex.
println!("{}", hash1);
```
Oltre a `hash`, BLAKE3 fornisce altre due modalità, `keyed_hash` e
`derive_key`. La modalità `keyed_hash` prende una chiave a 256 bit:
```rust
// MAC an input all at once.
let example_key = [42u8; 32];
let mac1 = blake3::keyed_hash(&example_key, b"example input");
// MAC incrementally.
let mut hasher = blake3::Hasher::new_keyed(&example_key);
hasher.update(b"example input");
let mac2 = hasher.finalize();
assert_eq!(mac1, mac2);
```
La modalità `derive_key` prende una stringa di contesto e del materiale chiave (non una
password). La stringa di contesto dovrebbe essere hardcodata, globalmente unica e
specifica per l'applicazione. Un buon formato predefinito per la stringa di contesto è
`"[application] [commit timestamp] [purpose]"`:
```rust
// Derive a couple of subkeys for different purposes.
const EMAIL_CONTEXT: &str = "BLAKE3 example 2020-01-07 17:10:44 email key";
const API_CONTEXT: &str = "BLAKE3 example 2020-01-07 17:11:21 API key";
let input_key_material = b"usually at least 32 random bytes, not a password";
let email_key = blake3::derive_key(EMAIL_CONTEXT, input_key_material);
let api_key = blake3::derive_key(API_CONTEXT, input_key_material);
assert_ne!(email_key, api_key);
```
### L'implementazione C
Vedi [`c/README.md`](https://github.com/blake3-team/blake3/blob/master/c/README.md).
### Altre implementazioni
Ci sono troppe implementazioni là fuori per tenerne traccia,
ma alcuni punti salienti includono [un'implementazione Go
ottimizzata](https://github.com/zeebo/blake3), [binding Wasm per
Node.js e browser](https://github.com/connor4312/blake3), [wheel
binari per Python](https://github.com/oconnor663/blake3-py), [binding
.NET](https://github.com/xoofx/Blake3.NET) e [un'implementazione Java
pura](https://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/digest/Blake3.html).
## Contribuire
Si prega di vedere [CONTRIBUTING.md](https://github.com/blake3-team/blake3/blob/master/CONTRIBUTING.md).
## Licenze
Quest'opera è rilasciata nel pubblico dominio con [CC0 1.0](https://github.com/blake3-team/blake3/blob/master/LICENSE_CC0).
In alternativa, è concessa in licenza sotto una delle seguenti:
* [Apache 2.0](https://github.com/blake3-team/blake3/blob/master/LICENSE_A2)
* [Apache 2.0 con eccezioni LLVM](https://github.com/blake3-team/blake3/blob/master/LICENSE_A2LLVM)
## Adozione e distribuzione
* [Bazel](https://github.com/bazelbuild/bazel/releases/tag/6.4.0)
* [Cargo](https://github.com/rust-lang/cargo/pull/14137)
* [Ccache](https://ccache.dev/releasenotes.html#_ccache_4_0)
* [Chia](https://github.com/Chia-Network/chia-blockchain/blob/main/CHANGELOG.md#10beta8-aka-beta-18---2020-07-16)
* [Clickhouse](https://github.com/ClickHouse/ClickHouse/blob/master/rust/chcache/Cargo.toml#L7)
* [Farcaster](https://www.farcaster.xyz/)
* [IPFS](https://github.com/ipfs/go-verifcid/issues/13)
* [Iroh](https://www.iroh.computer/blog/blake3-hazmat-api)
* [LLVM](https://reviews.llvm.org/D121510)
* [Nix](https://github.com/NixOS/nix/pull/12379)
* [Nym](https://github.com/nymtech/nym/blob/59056a22c5e6b01a38da2124662bd1fa3c8abef2/common/nymsphinx/params/src/lib.rs#L5)
* [OpenZFS](https://github.com/openzfs/zfs/)
* [Redox](https://www.redox-os.org/news/pkgar-introduction/)
* [Solana](https://docs.rs/solana-program/1.9.5/solana_program/blake3/index.html)
* [Tekken 8](https://x.com/rodarmor/status/1751567502050771189)
* [Wasmer](https://github.com/wasmerio/wasmer/blob/4f935a8c162bf604df223003e434e4f7ca253688/lib/cache/src/hash.rs#L21)
## Varie
- [@veorq] e [@oconnor663] hanno tenuto [un'intervista con Cryptography FM](https://cryptography.fireside.fm/3).
- [@oconnor663] ha tenuto [un'intervista con Saito](https://www.youtube.com/watch?v=cJkmIt7yN_E).
[@oconnor663]: https://github.com/oconnor663
[@sneves]: https://github.com/sneves
[@veorq]: https://github.com/veorq
[@zookozcash]: https://github.com/zookozcash