Skip to content

Commit 4afed0e

Browse files
committed
sha1-checked: implement hardware acceleration on arm64 and x86_64
Fixes #909.
1 parent f6c786d commit 4afed0e

8 files changed

Lines changed: 1152 additions & 667 deletions

File tree

‎Cargo.lock‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎sha1-checked/Cargo.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ exclude = [
1818
]
1919

2020
[dependencies]
21+
cfg-if = "1.0"
2122
digest = "0.11"
2223
sha1 = { version = "0.11.0-rc.5", default-features = false }
2324
zeroize = { version = "1.8", default-features = false, optional = true }
2425

26+
[target.'cfg(any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64"))'.dependencies]
27+
cpufeatures = "0.3"
28+
2529
[dev-dependencies]
2630
digest = { version = "0.11", features = ["dev"] }
2731
hex-literal = "1"

‎sha1-checked/README.md‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,20 @@ unsuitable for further use in any security critical capacity, as it is
1818
But, this crate provides the detection [algorithm] pioneered by git, to detect hash collisions when they
1919
occur and prevent them. The [paper] has more details on how this works.
2020

21-
This implementation will be slower to use than the pure SHA-1 implementation, as it has to do more computations and
22-
it can not rely on hardware acceleration available on some CPUs.
21+
## Performance
22+
23+
This implementation is slower than plain SHA-1, since it does extra work per block to detect collisions.
24+
Measured against this crate's own benchmarks, at throughput relative to plain, undetected SHA-1 on the same backend:
25+
26+
| architecture | scalar | hardware-accelerated |
27+
|--------------|--------|----------------------|
28+
| `aarch64` | 64% | 63% |
29+
| `x86_64` | 55% | 38% |
30+
31+
Where the CPU's SHA-1 instructions are available, most blocks run through them, falling back to scalar
32+
compression only when a potential collision is flagged. On `aarch64` this keeps detection roughly the same
33+
fraction of plain SHA-1's speed as without hardware acceleration. On `x86_64`, `sha1`'s own hardware backend
34+
speeds up by more than `sha1-checked`'s fixed per-block bookkeeping does, so the gap widens.
2335

2436
## Examples
2537

0 commit comments

Comments
 (0)