merkleforge-hash 0.2.1

Pluggable hash function adapters for MerkleForge (SHA-256, Keccak-256, BLAKE3)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
<!-- # merkle-hash

> Pluggable cryptographic hash adapters for `MerkleForge` — SHA-256, Keccak-256, and BLAKE3.

[![Crates.io](https://img.shields.io/crates/v/merkle-hash.svg)](https://crates.io/crates/merkle-hash)
[![docs.rs](https://docs.rs/merkle-hash/badge.svg)](https://docs.rs/merkle-hash)
[![License: MIT OR Apache-2.0](https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-blue.svg)](#license)

---

`merkle-hash` provides the three hash function adapters used by `MerkleForge`. Each one implements the [`HashFunction`](https://docs.rs/merkle-core/latest/merkle_core/traits/trait.HashFunction.html) trait from `merkle-core`, so any tree variant in `merkle-variants` can be driven by any adapter — swapping algorithms is a one-type-parameter change with zero runtime overhead.

---

## Contents

- [Installation](#installation)
- [Quick start](#quick-start)
- [Adapters](#adapters)
  - [Sha256](#sha256)
  - [Keccak256](#keccak256)
  - [Blake3](#blake3)
- [Choosing an adapter](#choosing-an-adapter)
- [Benchmark comparison](#benchmark-comparison)
- [Domain separation](#domain-separation)
- [Implementing a custom `HashFunction`](#implementing-a-custom-hashfunction)
- [Safety](#safety)
- [License](#license)

---

## Installation

```toml
[dependencies]
merkle-hash = "0.1"
merkle-core = "0.1"
```

All three adapters are compiled by default. There are no feature flags, the upstream crates (`sha2`, `tiny-keccak`, `blake3`) are small and compile quickly.

---

## Quick start

```rust
use merkle_hash::{Sha256, Keccak256, Blake3, HashFunction};

// Hash a leaf pre-image
let sha_digest   = Sha256::hash(b"alice:100");
let keccak_digest = Keccak256::hash(b"alice:100");
let blake_digest  = Blake3::hash(b"alice:100");

// All three produce 32-byte digests
assert_eq!(sha_digest.len(),    32);
assert_eq!(keccak_digest.len(), 32);
assert_eq!(blake_digest.len(),  32);

// The digests are different — three distinct algorithms
assert_ne!(sha_digest, keccak_digest);
assert_ne!(sha_digest, blake_digest);

// Hash two child nodes together to form a parent
let parent = Sha256::hash_nodes(&sha_digest, &sha_digest);
assert_eq!(parent.len(), 32);
```

Swapping the hash function that drives a tree:

```rust
// Before — SHA-256
let mut tree = BinaryMerkleTree::<Sha256>::new();

// After — BLAKE3, same API, zero other changes
let mut tree = BinaryMerkleTree::<Blake3>::new();
```

---

## Adapters

### `Sha256`

```rust
use merkle_hash::{Sha256, HashFunction};

let digest = Sha256::hash(b"transaction data");
// digest: [u8; 32]

let parent = Sha256::hash_nodes(&digest, &digest);
// parent: [u8; 32]

println!("{}", Sha256::algorithm_name()); // "SHA-256"
println!("{}", Sha256::digest_size());    // 32
```

**Upstream crate:** [`sha2`](https://crates.io/crates/sha2)

**Hardware acceleration:** The `sha2` crate detects Intel SHA Extensions and AVX2 at compile time and uses them automatically. On supported x86-64 CPUs this yields roughly a 50% throughput improvement over the software path (Drake, 2019).

**Domain separation:**
- Leaf hash: `SHA-256(0x00 || data)`
- Internal node hash: `SHA-256(0x01 || left || right)`

**`empty()` sentinel:** Pre-computed as `SHA-256(0x00)` — avoids a runtime hash call every time the tree needs an empty-slot placeholder.

```
empty = 6e340b9cffb37a989ca544e6bb780a2c78901d3fb3378768501a30617afa01d
```

---

### `Keccak256`

```rust
use merkle_hash::{Keccak256, HashFunction};

let digest = Keccak256::hash(b"transaction data");
// digest: [u8; 32]  — identical to web3.utils.keccak256("transaction data")

println!("{}", Keccak256::algorithm_name()); // "Keccak-256"
```

**Upstream crate:** [`tiny-keccak`](https://crates.io/crates/tiny-keccak) with the `keccak` feature

> **Important:** Keccak-256 is **not** the same as NIST SHA-3. They use different padding. If you need to produce digests that match Ethereum tooling (`web3.utils.keccak256`, Solidity's `keccak256()`, `ethers.utils.keccak256`), use `Keccak256`. Using any SHA-3 crate will produce different output.

**When to use:** Any tree that must produce state roots verifiable by Ethereum tooling — most importantly the `MerklePatriciaTrie` variant.

**Domain separation:**
- Leaf hash: `Keccak-256(0x00 || data)`
- Internal node hash: `Keccak-256(0x01 || left || right)`

**Known vector:**

```
Keccak-256("") = c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470
```

---

### `Blake3`

```rust
use merkle_hash::{Blake3, HashFunction};

let digest = Blake3::hash(b"transaction data");
// digest: [u8; 32]

println!("{}", Blake3::algorithm_name()); // "BLAKE3"
```

**Upstream crate:** [`blake3`](https://crates.io/crates/blake3)

**Domain separation:** BLAKE3 has native support for context strings via `blake3::derive_key`. This is cryptographically cleaner and more efficient than prepending a prefix byte — no extra memory allocation, no length extension risk.

```rust
// Internally, Blake3 uses:
const LEAF_CONTEXT: &str = "MerkleForge 2026 leaf v1";
const NODE_CONTEXT: &str = "MerkleForge 2024 internal-node v1";

// Leaf:  blake3::derive_key("MerkleForge 2026 leaf v1",          data)
// Node:  blake3::derive_key("MerkleForge 2026 internal-node v1", left || right)
```

Because the context strings are distinct and fixed, leaf and internal-node digests are guaranteed to never collide regardless of input.

**When to use:** Throughput-sensitive workloads where Ethereum compatibility is not required — background indexers, proof batch generation, high-frequency state updates.

---

## Choosing an adapter

| | `Sha256` | `Keccak256` | `Blake3` |
|---|---|---|---|
| **Algorithm** | SHA-256 | Keccak-256 | BLAKE3 |
| **Digest size** | 32 bytes | 32 bytes | 32 bytes |
| **Ethereum compatible** | ✗ | ✅ Required for MPT | ✗ |
| **Hardware acceleration** | ✅ SHA/AVX2 extensions | ✗ software only | ✅ SIMD, multi-core |
| **Software throughput** | Moderate | Moderate | Fastest |
| **Domain separation method** | `0x00`/`0x01` prefix bytes | `0x00`/`0x01` prefix bytes | `derive_key` context strings |
| **Best for** | Production x86-64, Bitcoin-style SPV | Any Ethereum-compatible tree | Maximum throughput, non-Ethereum |

**Decision guide:**

- Building a `MerklePatriciaTrie` whose roots need to match Ethereum → **`Keccak256`**, no choice.
- Running on a modern server with SHA Extensions or on ARM with SHA instructions → **`Sha256`** will match or beat BLAKE3 on hardware paths.
- Building a binary or sparse tree where raw throughput matters and Ethereum compatibility is not required → **`Blake3`**.
- Not sure? Start with **`Sha256`** — it's the most battle-tested, has the widest hardware support, and will be easy to reason about in security reviews.

---

## Benchmark comparison

> **Note:** The numbers below are from published literature (AITCS, 2024; Drake, 2019). The `merkle-bench` crate will produce project-specific numbers on standardised hardware in Phase 5 of the implementation roadmap. Those results will replace this table.

### Single-block throughput (software path, ~64 bytes)

| Algorithm | Approx. latency | Notes |
|-----------|----------------|-------|
| BLAKE3 | ~100–120 ns | Fastest on software path across all input sizes |
| SHA-256 (software) | ~250–300 ns | 50% slower than BLAKE3 without hardware extensions |
| SHA-256 (hardware) | ~120–150 ns | SHA Extensions close the gap significantly |
| Keccak-256 | ~300–400 ns | No hardware acceleration; consistently slowest |

### Sustained throughput (large buffers, MB/s)

| Algorithm | Typical throughput | Scales across cores? |
|-----------|-------------------|----------------------|
| BLAKE3 | 1–4 GB/s | ✅ Internal tree parallelism |
| SHA-256 (hardware) | 500 MB/s–1 GB/s | ✗ Single-core |
| SHA-256 (software) | 200–400 MB/s | ✗ Single-core |
| Keccak-256 | 150–300 MB/s | ✗ Single-core |

These gaps directly determine tree construction speed. A tree with 1,000,000 leaves requires roughly 2,000,000 hash calls (one per leaf plus one per internal node). At 64 bytes per input:

| Algorithm | Estimated construction time (1M leaves) |
|-----------|----------------------------------------|
| BLAKE3 | ~200–400 ms |
| SHA-256 (hardware) | ~300–600 ms |
| SHA-256 (software) | ~500 ms–1 s |
| Keccak-256 | ~600 ms–1.2 s |

The `merkle-bench` Criterion suite (`cargo bench --bench hash_throughput`) measures your specific hardware so you can make an informed decision rather than relying on generalised figures.

---

## Domain separation

All three adapters enforce domain separation between leaf hashes and internal-node hashes. This prevents a class of second-preimage attacks where an attacker constructs a proof by substituting an internal node for a leaf.

**The attack without domain separation:**

```
Suppose H(A || B) == H(leaf_data).
An attacker could present [A, B] as a "leaf" and fool a verifier
that checks only the final root, not whether the proof path is valid.
```

**How each adapter prevents it:**

| Adapter | Leaf | Internal node |
|---------|------|---------------|
| `Sha256` | `SHA-256(0x00 \|d\| data)` | `SHA-256(0x01 \|\| left \|\| right)` |
| `Keccak256` | `Keccak-256(0x00 \|\| data)` | `Keccak-256(0x01 \|\| left \|\| right)` |
| `Blake3` | `derive_key("MerkleForge 2026 leaf v1", data)` | `derive_key("MerkleForge 2026 internal-node v1", left \|\| right)` |
d
The `0x00`/`0x01` byte prefix used by `Sha256` and `Keccak256` follows RFC 6962 (Certificate Transparency). BLAKE3's `derive_key` mode is equivalent but uses full context strings instead of a single byte, which is both more readable and more collision-resistant at the domain boundary.

If you implement a custom `HashFunction`, you **must** apply the same separation. The `ProofVerifier` in `merkle-core` assumes it.

---

## Implementing a custom `HashFunction`

If none of the three adapters fit your use case — say, you need BLAKE2b for a specific protocol, or a truncated digest for a constrained environment — implement the trait directly in your own crate.

### Minimal implementation

```rust
use merkle_core::traits::HashFunction;

pub struct MyHash;

impl HashFunction for MyHash {
    type Digest = [u8; 32];

    fn hash(data: &[u8]) -> [u8; 32] {
        // REQUIRED: leaf domain separation
        // Prefix 0x00 before hashing so leaf digests can never
        // collide with internal-node digests.
        todo!("your_hash_crate::hash([0x00, data].concat())")
    }

    fn hash_nodes(left: &[u8; 32], right: &[u8; 32]) -> [u8; 32] {
        // REQUIRED: internal-node domain separation
        // Prefix 0x01 so this can never equal a leaf hash.
        todo!("your_hash_crate::hash([0x01, left, right].concat())")
    }

    fn algorithm_name() -> &'static str { "MyHash-256" }
    fn digest_size() -> usize { 32 }
}
```

### Full example — BLAKE2b-256

```rust
use merkle_core::traits::HashFunction;
use blake2::{Blake2b, Digest};
use blake2::digest::consts::U32;

pub struct Blake2b256;

impl HashFunction for Blake2b256 {
    type Digest = [u8; 32];

    fn hash(data: &[u8]) -> [u8; 32] {
        let mut h = Blake2b::<U32>::new();
        h.update([0x00]); // leaf prefix
        h.update(data);
        h.finalize().into()
    }

    fn hash_nodes(left: &[u8; 32], right: &[u8; 32]) -> [u8; 32] {
        let mut h = Blake2b::<U32>::new();
        h.update([0x01]); // internal-node prefix
        h.update(left);
        h.update(right);
        h.finalize().into()
    }

    fn algorithm_name() -> &'static str { "BLAKE2b-256" }
    fn digest_size() -> usize { 32 }
}
```

Once implemented, `Blake2b256` drops straight into any `merkle-variants` tree:

```rust
let mut tree = BinaryMerkleTree::<Blake2b256>::new();
tree.insert(b"leaf data")?;
```

### Checklist before shipping a custom adapter

- [ ] `hash` applies a distinct domain prefix or context to leaf inputs
- [ ] `hash_nodes` applies a different domain prefix or context to node inputs
- [ ] `hash_nodes` is **non-commutative** — `H(A, B) ≠ H(B, A)` for most inputs
- [ ] `Digest` implements `AsRef<[u8]> + Clone + Debug + PartialEq + Eq + Send + Sync + 'static`
- [ ] The implementation is deterministic — same input always produces the same output
- [ ] `digest_size()` returns the correct byte length of `Digest`
- [ ] If you override `empty()`, it equals `hash(&[])` or an equally valid sentinel

### Using a non-32-byte digest

The `Digest` associated type is not constrained to `[u8; 32]`. You can use any fixed-size array, or a custom newtype, as long as it satisfies the bounds:

```rust
pub struct Truncated128;

impl HashFunction for Truncated128 {
    type Digest = [u8; 16];   // 128-bit output

    fn hash(data: &[u8]) -> [u8; 16] {
        let full = Sha256::hash(data); // compute full SHA-256
        full[..16].try_into().unwrap() // truncate to 128 bits
    }

    fn hash_nodes(left: &[u8; 16], right: &[u8; 16]) -> [u8; 16] {
        let mut buf = [0u8; 33]; // 0x01 + 16 + 16
        buf[0] = 0x01;
        buf[1..17].copy_from_slice(left);
        buf[17..33].copy_from_slice(right);
        let full = Sha256::hash(&buf);
        full[..16].try_into().unwrap()
    }

    fn algorithm_name() -> &'static str { "SHA-256/128" }
    fn digest_size() -> usize { 16 }
}
```

> **Warning:** Truncating a digest reduces collision resistance. A 128-bit digest has a birthday-bound collision probability of ~2⁻⁶⁴. Use a full-width digest for any production security context.

---

## Safety

`#[forbid(unsafe_code)]` is set at the crate root. `merkle-hash` contains no unsafe blocks. All three upstream crates (`sha2`, `tiny-keccak`, `blake3`) are widely audited and used in production blockchain infrastructure.

---

## License

Licensed under either of [MIT](../LICENSE-MIT) or [Apache-2.0](../LICENSE-APACHE) at your option. -->


# merkleforge-hash

> Pluggable cryptographic hash adapters for `MerkleForge` — SHA-256, Keccak-256, and BLAKE3.

[![Crates.io](https://img.shields.io/crates/v/merkleforge-hash.svg)](https://crates.io/crates/merkleforge-hash)
[![docs.rs](https://docs.rs/merkleforge-hash/badge.svg)](https://docs.rs/merkleforge-hash)
[![License: MIT OR Apache-2.0](https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-blue.svg)](#license)

---

> ⚠️ **Research Software — Not Production Ready**
>
> MerkleForge is a final-year academic research project. This crate has not
> been independently security-audited. Use at your own risk.

---

`merkleforge-hash` provides the three hash function adapters used by
`MerkleForge`. Each one implements the
[`HashFunction`](https://docs.rs/merkle-core/latest/merkle_core/traits/hash_function/trait.HashFunction.html)
trait from `merkle-core`, so any tree variant in `merkle-variants` can
be driven by any adapter — swapping algorithms is a one-type-parameter change
with zero runtime overhead.

---

## Contents

- [Installation]#installation
- [Quick start]#quick-start
- [Adapters]#adapters
  - [Sha256]#sha256
  - [Keccak256]#keccak256
  - [Blake3]#blake3
- [Choosing an adapter]#choosing-an-adapter
- [Benchmark comparison]#benchmark-comparison
- [Domain separation]#domain-separation
- [Implementing a custom `HashFunction`]#implementing-a-custom-hashfunction
- [Safety]#safety
- [License]#license

---

## Installation

```toml
[dependencies]
merkleforge-hash = "0.1.1"
merkle-core = "0.1.1"
```

All three adapters are compiled by default. There are no feature flags — the
upstream crates (`sha2`, `tiny-keccak`, `blake3`) are small and compile
quickly.

---

## Quick start

```rust
use merkleforge_hash::{Sha256, Keccak256, Blake3, HashFunction};

// Hash a leaf pre-image
let sha_digest    = Sha256::hash(b"alice:100");
let keccak_digest = Keccak256::hash(b"alice:100");
let blake_digest  = Blake3::hash(b"alice:100");

// All three produce 32-byte digests
assert_eq!(sha_digest.len(),    32);
assert_eq!(keccak_digest.len(), 32);
assert_eq!(blake_digest.len(),  32);

// The digests are different — three distinct algorithms
assert_ne!(sha_digest, keccak_digest);
assert_ne!(sha_digest, blake_digest);

// Hash two child nodes together to form a parent
let parent = Sha256::hash_nodes(&sha_digest, &sha_digest);
assert_eq!(parent.len(), 32);
```

Swapping the hash function that drives a tree:

```rust
// Before — SHA-256
let mut tree = BinaryMerkleTree::<Sha256>::new();

// After — BLAKE3, same API, zero other changes
let mut tree = BinaryMerkleTree::<Blake3>::new();
```

---

## Adapters

### `Sha256`

```rust
use merkleforge_hash::{Sha256, HashFunction};

let digest = Sha256::hash(b"transaction data");
// digest: [u8; 32]

let parent = Sha256::hash_nodes(&digest, &digest);
// parent: [u8; 32]

println!("{}", Sha256::algorithm_name()); // "SHA-256"
println!("{}", Sha256::digest_size());    // 32
```

**Upstream crate:** [`sha2`](https://crates.io/crates/sha2)

**Hardware acceleration:** The `sha2` crate detects Intel SHA Extensions and
AVX2 at compile time and uses them automatically. On supported x86-64 CPUs
this yields roughly a 50% throughput improvement over the software path
(Drake, 2019).

**Domain separation:**
- Leaf hash: `SHA-256(0x00 || data)`
- Internal node hash: `SHA-256(0x01 || left || right)`

**`empty()` sentinel:** Pre-computed as `SHA-256(0x00)` — avoids a runtime
hash call every time the tree needs an empty-slot placeholder.

```
empty = 6e340b9cffb37a989ca544e6bb780a2c78901d3fb3378768501a30617afa01d
```

---

### `Keccak256`

```rust
use merkleforge_hash::{Keccak256, HashFunction};

let digest = Keccak256::hash(b"transaction data");
// digest: [u8; 32]  — identical to web3.utils.keccak256("transaction data")

println!("{}", Keccak256::algorithm_name()); // "Keccak-256"
```

**Upstream crate:** [`tiny-keccak`](https://crates.io/crates/tiny-keccak)
with the `keccak` feature.

> **Important:** Keccak-256 is **not** the same as NIST SHA-3. They use
> different padding. If you need to produce digests that match Ethereum
> tooling (`web3.utils.keccak256`, Solidity's `keccak256()`,
> `ethers.utils.keccak256`), use `Keccak256`. Using any SHA-3 crate will
> produce different output.

**When to use:** Any tree that must produce state roots verifiable by
Ethereum tooling — most importantly the `MerklePatriciaTrie` variant.

**Domain separation:**
- Leaf hash: `Keccak-256(0x00 || data)`
- Internal node hash: `Keccak-256(0x01 || left || right)`

**Known vector:**

```
Keccak-256("") = c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470
```

---

### `Blake3`

```rust
use merkleforge_hash::{Blake3, HashFunction};

let digest = Blake3::hash(b"transaction data");
// digest: [u8; 32]

println!("{}", Blake3::algorithm_name()); // "BLAKE3"
```

**Upstream crate:** [`blake3`](https://crates.io/crates/blake3)

**Domain separation:** BLAKE3 has native support for context strings via
`blake3::derive_key`. This is cryptographically cleaner and more efficient
than prepending a prefix byte — no extra memory allocation, no length
extension risk.

```rust
// Internally, Blake3 uses:
const LEAF_CONTEXT: &str = "MerkleForge 2026 leaf v1";
const NODE_CONTEXT: &str = "MerkleForge 2026 internal-node v1";

// Leaf:  blake3::derive_key("MerkleForge 2026 leaf v1",          data)
// Node:  blake3::derive_key("MerkleForge 2026 internal-node v1", left || right)
```

Because the context strings are distinct and fixed, leaf and internal-node
digests are guaranteed to never collide regardless of input.

**When to use:** Throughput-sensitive workloads where Ethereum compatibility
is not required — background indexers, proof batch generation, high-frequency
state updates.

---

## Choosing an adapter

| | `Sha256` | `Keccak256` | `Blake3` |
|---|---|---|---|
| **Algorithm** | SHA-256 | Keccak-256 | BLAKE3 |
| **Digest size** | 32 bytes | 32 bytes | 32 bytes |
| **Ethereum compatible** || ✅ Required for MPT ||
| **Hardware acceleration** | ✅ SHA/AVX2 extensions | ✗ software only | ✅ SIMD, multi-core |
| **Software throughput** | Moderate | Moderate | Fastest |
| **Domain separation method** | `0x00`/`0x01` prefix bytes | `0x00`/`0x01` prefix bytes | `derive_key` context strings |
| **Best for** | Production x86-64, Bitcoin-style SPV | Any Ethereum-compatible tree | Maximum throughput, non-Ethereum |

**Decision guide:**

- Building a `MerklePatriciaTrie` whose roots need to match Ethereum →
  **`Keccak256`**, no choice.
- Running on a modern server with SHA Extensions or on ARM with SHA
  instructions → **`Sha256`** will match or beat BLAKE3 on hardware paths.
- Building a binary or sparse tree where raw throughput matters and Ethereum
  compatibility is not required → **`Blake3`**.
- Not sure? Start with **`Sha256`** — it's the most battle-tested, has the
  widest hardware support, and will be easy to reason about in security
  reviews.

---

## Benchmark comparison

> **Note:** The numbers below are from published literature (AITCS, 2024;
> Drake, 2019). The `merkle-bench` crate will produce project-specific
> numbers on standardised hardware in Phase 5 of the implementation roadmap.
> Those results will replace this table.

### Single-block throughput (software path, ~64 bytes)

| Algorithm | Approx. latency | Notes |
|-----------|----------------|-------|
| BLAKE3 | ~100–120 ns | Fastest on software path across all input sizes |
| SHA-256 (software) | ~250–300 ns | ~2× slower than BLAKE3 without hardware extensions |
| SHA-256 (hardware) | ~120–150 ns | SHA Extensions close the gap significantly |
| Keccak-256 | ~300–400 ns | No hardware acceleration; consistently slowest |

### Sustained throughput (large buffers, MB/s)

| Algorithm | Typical throughput | Scales across cores? |
|-----------|-------------------|----------------------|
| BLAKE3 | 1–4 GB/s | ✅ Internal tree parallelism |
| SHA-256 (hardware) | 500 MB/s–1 GB/s | ✗ Single-core |
| SHA-256 (software) | 200–400 MB/s | ✗ Single-core |
| Keccak-256 | 150–300 MB/s | ✗ Single-core |

These gaps directly determine tree construction speed. A tree with 1,000,000
leaves requires roughly 2,000,000 hash calls (one per leaf plus one per
internal node). At 64 bytes per input:

| Algorithm | Estimated construction time (1M leaves) |
|-----------|----------------------------------------|
| BLAKE3 | ~200–400 ms |
| SHA-256 (hardware) | ~300–600 ms |
| SHA-256 (software) | ~500 ms–1 s |
| Keccak-256 | ~600 ms–1.2 s |

The `merkle-bench` Criterion suite (`cargo bench --bench hash_throughput`)
measures your specific hardware so you can make an informed decision rather
than relying on generalised figures.

---

## Domain separation

All three adapters enforce domain separation between leaf hashes and
internal-node hashes. This prevents a class of second-preimage attacks where
an attacker constructs a proof by substituting an internal node for a leaf.

**The attack without domain separation:**

```
Suppose H(A || B) == H(leaf_data).
An attacker could present [A, B] as a "leaf" and fool a verifier
that checks only the final root, not whether the proof path is valid.
```

**How each adapter prevents it:**

| Adapter | Leaf | Internal node |
|---------|------|---------------|
| `Sha256` | `SHA-256(0x00 \|\| data)` | `SHA-256(0x01 \|\| left \|\| right)` |
| `Keccak256` | `Keccak-256(0x00 \|\| data)` | `Keccak-256(0x01 \|\| left \|\| right)` |
| `Blake3` | `derive_key("MerkleForge 2026 leaf v1", data)` | `derive_key("MerkleForge 2026 internal-node v1", left \|\| right)` |

The `0x00`/`0x01` byte prefix used by `Sha256` and `Keccak256` follows
RFC 6962 (Certificate Transparency). BLAKE3's `derive_key` mode is
equivalent but uses full context strings instead of a single byte, which is
both more readable and more collision-resistant at the domain boundary.

If you implement a custom `HashFunction`, you **must** apply the same
separation. The `ProofVerifier` in `merkleforge-core` assumes it.

---

## Implementing a custom `HashFunction`

If none of the three adapters fit your use case — say, you need BLAKE2b for
a specific protocol, or a truncated digest for a constrained environment —
implement the trait directly in your own crate.

### Minimal implementation

```rust
use merkle_core::traits::HashFunction;

pub struct MyHash;

impl HashFunction for MyHash {
    type Digest = [u8; 32];

    fn hash(data: &[u8]) -> [u8; 32] {
        // REQUIRED: leaf domain separation
        // Prefix 0x00 before hashing so leaf digests can never
        // collide with internal-node digests.
        todo!("your_hash_crate::hash([0x00, data].concat())")
    }

    fn hash_nodes(left: &[u8; 32], right: &[u8; 32]) -> [u8; 32] {
        // REQUIRED: internal-node domain separation
        // Prefix 0x01 so this can never equal a leaf hash.
        todo!("your_hash_crate::hash([0x01, left, right].concat())")
    }

    fn algorithm_name() -> &'static str { "MyHash-256" }
    fn digest_size() -> usize { 32 }
}
```

### Full example — BLAKE2b-256

```rust
use merkle_core::traits::HashFunction;
use blake2::{Blake2b, Digest};
use blake2::digest::consts::U32;

pub struct Blake2b256;

impl HashFunction for Blake2b256 {
    type Digest = [u8; 32];

    fn hash(data: &[u8]) -> [u8; 32] {
        let mut h = Blake2b::<U32>::new();
        h.update([0x00]); // leaf prefix
        h.update(data);
        h.finalize().into()
    }

    fn hash_nodes(left: &[u8; 32], right: &[u8; 32]) -> [u8; 32] {
        let mut h = Blake2b::<U32>::new();
        h.update([0x01]); // internal-node prefix
        h.update(left);
        h.update(right);
        h.finalize().into()
    }

    fn algorithm_name() -> &'static str { "BLAKE2b-256" }
    fn digest_size() -> usize { 32 }
}
```

Once implemented, `Blake2b256` drops straight into any `merkle-variants` tree:

```rust
let mut tree = BinaryMerkleTree::<Blake2b256>::new();
tree.insert(b"leaf data")?;
```

### Checklist before shipping a custom adapter

- [ ] `hash` applies a distinct domain prefix or context to leaf inputs
- [ ] `hash_nodes` applies a different domain prefix or context to node inputs
- [ ] `hash_nodes` is **non-commutative**`H(A, B) ≠ H(B, A)` for most inputs
- [ ] `Digest` implements `AsRef<[u8]> + Clone + Debug + PartialEq + Eq + Send + Sync + 'static`
- [ ] The implementation is deterministic — same input always produces same output
- [ ] `digest_size()` returns the correct byte length of `Digest`
- [ ] If you override `empty()`, it equals `hash(&[])` or an equally valid sentinel

### Using a non-32-byte digest

The `Digest` associated type is not constrained to `[u8; 32]`. You can use
any fixed-size array, or a custom newtype, as long as it satisfies the bounds:

```rust
pub struct Truncated128;

impl HashFunction for Truncated128 {
    type Digest = [u8; 16];   // 128-bit output

    fn hash(data: &[u8]) -> [u8; 16] {
        let full = Sha256::hash(data); // compute full SHA-256
        full[..16].try_into().unwrap() // truncate to 128 bits
    }

    fn hash_nodes(left: &[u8; 16], right: &[u8; 16]) -> [u8; 16] {
        let mut buf = [0u8; 33]; // 0x01 + 16 + 16
        buf[0] = 0x01;
        buf[1..17].copy_from_slice(left);
        buf[17..33].copy_from_slice(right);
        let full = Sha256::hash(&buf);
        full[..16].try_into().unwrap()
    }

    fn algorithm_name() -> &'static str { "SHA-256/128" }
    fn digest_size() -> usize { 16 }
}
```

> **Warning:** Truncating a digest reduces collision resistance. A 128-bit
> digest has a birthday-bound collision probability of ~2⁻⁶⁴. Use a
> full-width digest for any production security context.

---

## Safety

`#[forbid(unsafe_code)]` is set at the crate root. `merkleforge-hash`
contains no unsafe blocks. All three upstream crates (`sha2`, `tiny-keccak`,
`blake3`) are widely audited and used in production blockchain infrastructure.

---

## License

Licensed under either of [MIT](../LICENSE-MIT) or
[Apache-2.0](../LICENSE-APACHE) at your option.