# bmw-hash
[](https://crates.io/crates/bmw-hash)
[](https://docs.rs/bmw-hash)
[](LICENSE)
[](https://github.com/bshuler/bmw-hash/actions)
Pure Rust implementation of the **Blue Midnight Wish (BMW-512)** cryptographic hash function.
BMW-512 is one of the six hash functions in the [Quark](https://en.wikipedia.org/wiki/Quark_(hash_function)) hash chain used by Divi and other cryptocurrencies. This crate fills the gap as the only Quark component without a pure Rust implementation on crates.io — the other five (BLAKE, Groestl, JH, Keccak, Skein) are all available from [RustCrypto](https://github.com/RustCrypto/hashes).
## Features
- **Pure Rust** — no C dependencies, no FFI, no unsafe code
- **`no_std` compatible** — works in embedded and WASM environments
- **RustCrypto ecosystem** — implements the [`digest::Digest`](https://docs.rs/digest/0.10) trait (v0.10)
- **Verified** — 13 test vectors validated against the [sphlib](https://github.com/aidansteele/sphlib) C reference implementation
## Usage
Add to your `Cargo.toml`:
```toml
[dependencies]
bmw-hash = "0.1"
```
### One-shot hashing
```rust
use bmw_hash::{Bmw512, Digest};
let hash = Bmw512::digest(b"Hello, World!");
println!("{:x}", hash);
```
### Incremental hashing
```rust
use bmw_hash::{Bmw512, Digest};
let mut hasher = Bmw512::new();
hasher.update(b"Hello, ");
hasher.update(b"World!");
let hash = hasher.finalize();
```
### In a Quark hash chain
```rust
use bmw_hash::{Bmw512, Digest};
use blake::Blake512; // from RustCrypto
use groestl::Groestl512;
use jh::Jh512;
use sha3::Keccak512;
use skein::Skein512;
fn quark_hash(data: &[u8]) -> Vec<u8> {
let h = Blake512::digest(data);
let h = Bmw512::digest(&h);
let h = Groestl512::digest(&h);
let h = Jh512::digest(&h);
let h = Keccak512::digest(&h);
let h = Skein512::digest(&h);
// ... (full Quark chain has 9 rounds with interleaving)
h.to_vec()
}
```
## Algorithm
BMW (Blue Midnight Wish) was a candidate in the [NIST SHA-3 competition](https://en.wikipedia.org/wiki/NIST_hash_function_competition). The 512-bit variant operates on 64-bit words with a 128-byte block size and uses the Merkle-Damgard construction with a unique double-compression finalization step.
This implementation is a direct port of the `compress_big` function from [sphlib](https://github.com/aidansteele/sphlib/blob/master/c/bmw.c) (MIT license, Thomas Pornin / Projet RNRT SAPHIR).
## Reference
- [BMW specification (NIST submission)](https://web.archive.org/web/2016*/http://people.item.ntnu.no/~d101/BlueMidnightWish/)
- [sphlib C implementation](https://github.com/aidansteele/sphlib) by Thomas Pornin
## License
[MIT](LICENSE)