Skip to content

Commit 5d604e9

Browse files
Feature gate ring for Miri support (#15)
* Feature gate ring for Miri support * Feature maxxing * Fix macOS CI * Quoting
1 parent 64c47ad commit 5d604e9

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

.github/workflows/test-suite.yml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,22 @@ jobs:
2323
strategy:
2424
matrix:
2525
os: [ubuntu-latest, macos-latest, windows-latest]
26+
features: ["", "ring", "zero_hash_cache", "ring,zero_hash_cache"]
27+
# Skip the ring-less builds on macOS ARM where ring is mandatory.
28+
exclude:
29+
- os: macos-latest
30+
features: ""
31+
- os: macos-latest
32+
features: "zero_hash_cache"
2633
runs-on: ${{ matrix.os }}
27-
name: test-${{ matrix.os }}
34+
name: test-${{ matrix.os }}-feat-${{ matrix.features }}
2835
steps:
2936
- uses: actions/checkout@v3
3037
- name: Get latest version of stable Rust
3138
run: rustup update stable
3239
- name: Run tests
33-
run: cargo test --release
40+
run: cargo test --release --no-default-features --features "${{ matrix.features }}"
41+
# We use `--skip-clean` to aggregate the coverage from runs with different features.
3442
coverage:
3543
runs-on: ubuntu-latest
3644
name: cargo-tarpaulin
@@ -40,8 +48,14 @@ jobs:
4048
run: rustup update stable
4149
- name: Install cargo-tarpaulin
4250
uses: taiki-e/install-action@cargo-tarpaulin
43-
- name: Check code coverage with cargo-tarpaulin
44-
run: cargo-tarpaulin --workspace --all-features --out xml
51+
- name: Check code coverage with cargo-tarpaulin (no features)
52+
run: cargo-tarpaulin --workspace --out xml --no-default-features
53+
- name: Check code coverage with cargo-tarpaulin (just `ring` feature)
54+
run: cargo-tarpaulin --workspace --out xml --skip-clean --no-default-features --features "ring"
55+
- name: Check code coverage with cargo-tarpaulin (just `zero_hash_cache` feature)
56+
run: cargo-tarpaulin --workspace --out xml --skip-clean --no-default-features --features "zero_hash_cache"
57+
- name: Check code coverage with cargo-tarpaulin (all features)
58+
run: cargo-tarpaulin --workspace --out xml --skip-clean --all-features
4559
- name: Upload to codecov.io
4660
uses: codecov/codecov-action@v3
4761
with:

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ categories = ["cryptography::cryptocurrencies"]
1212
rust-version = "1.80.0"
1313

1414
[dependencies]
15-
ring = "0.17"
15+
ring = { version = "0.17", optional = true }
1616

1717
[target.'cfg(target_arch = "x86_64")'.dependencies]
1818
cpufeatures = "0.2"
@@ -25,5 +25,6 @@ rustc-hex = "2"
2525
wasm-bindgen-test = "0.3.33"
2626

2727
[features]
28-
default = ["zero_hash_cache"]
28+
default = ["zero_hash_cache", "ring"]
2929
zero_hash_cache = []
30+
ring = ["dep:ring"]

src/lib.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ pub trait Sha256 {
5959
}
6060

6161
/// Implementation of SHA256 using the `ring` crate (fastest on CPUs without SHA extensions).
62+
#[cfg(feature = "ring")]
6263
pub struct RingImpl;
6364

65+
#[cfg(feature = "ring")]
6466
impl Sha256Context for ring::digest::Context {
6567
fn new() -> Self {
6668
Self::new(&ring::digest::SHA256)
@@ -77,6 +79,7 @@ impl Sha256Context for ring::digest::Context {
7779
}
7880
}
7981

82+
#[cfg(feature = "ring")]
8083
impl Sha256 for RingImpl {
8184
type Context = ring::digest::Context;
8285

@@ -97,6 +100,7 @@ impl Sha256 for RingImpl {
97100
pub enum DynamicImpl {
98101
#[cfg(target_arch = "x86_64")]
99102
Sha2,
103+
#[cfg(feature = "ring")]
100104
Ring,
101105
}
102106

@@ -119,15 +123,27 @@ impl DynamicImpl {
119123
/// Choose the best available implementation based on the currently executing CPU.
120124
#[inline(always)]
121125
pub fn best() -> Self {
122-
#[cfg(target_arch = "x86_64")]
126+
#[cfg(all(not(feature = "ring"), not(target_arch = "x86_64")))]
127+
{
128+
compile_error!("Ring must be enabled on non-x86_64 architectures");
129+
}
130+
131+
#[cfg(all(not(feature = "ring"), target_arch = "x86_64"))]
132+
{
133+
Self::Sha2
134+
}
135+
136+
#[cfg(all(feature = "ring", target_arch = "x86_64"))]
123137
if have_sha_extensions() {
124138
Self::Sha2
125139
} else {
126140
Self::Ring
127141
}
128142

129-
#[cfg(not(target_arch = "x86_64"))]
130-
Self::Ring
143+
#[cfg(all(feature = "ring", not(target_arch = "x86_64")))]
144+
{
145+
Self::Ring
146+
}
131147
}
132148
}
133149

@@ -139,6 +155,7 @@ impl Sha256 for DynamicImpl {
139155
match self {
140156
#[cfg(target_arch = "x86_64")]
141157
Self::Sha2 => Sha2CrateImpl.hash(input),
158+
#[cfg(feature = "ring")]
142159
Self::Ring => RingImpl.hash(input),
143160
}
144161
}
@@ -148,6 +165,7 @@ impl Sha256 for DynamicImpl {
148165
match self {
149166
#[cfg(target_arch = "x86_64")]
150167
Self::Sha2 => Sha2CrateImpl.hash_fixed(input),
168+
#[cfg(feature = "ring")]
151169
Self::Ring => RingImpl.hash_fixed(input),
152170
}
153171
}
@@ -159,6 +177,7 @@ impl Sha256 for DynamicImpl {
159177
pub enum DynamicContext {
160178
#[cfg(target_arch = "x86_64")]
161179
Sha2(sha2::Sha256),
180+
#[cfg(feature = "ring")]
162181
Ring(ring::digest::Context),
163182
}
164183

@@ -167,6 +186,7 @@ impl Sha256Context for DynamicContext {
167186
match DynamicImpl::best() {
168187
#[cfg(target_arch = "x86_64")]
169188
DynamicImpl::Sha2 => Self::Sha2(Sha256Context::new()),
189+
#[cfg(feature = "ring")]
170190
DynamicImpl::Ring => Self::Ring(Sha256Context::new()),
171191
}
172192
}
@@ -175,6 +195,7 @@ impl Sha256Context for DynamicContext {
175195
match self {
176196
#[cfg(target_arch = "x86_64")]
177197
Self::Sha2(ctxt) => Sha256Context::update(ctxt, bytes),
198+
#[cfg(feature = "ring")]
178199
Self::Ring(ctxt) => Sha256Context::update(ctxt, bytes),
179200
}
180201
}
@@ -183,6 +204,7 @@ impl Sha256Context for DynamicContext {
183204
match self {
184205
#[cfg(target_arch = "x86_64")]
185206
Self::Sha2(ctxt) => Sha256Context::finalize(ctxt),
207+
#[cfg(feature = "ring")]
186208
Self::Ring(ctxt) => Sha256Context::finalize(ctxt),
187209
}
188210
}

0 commit comments

Comments
 (0)