From 5563c11d4c438a9ce87d39b4fb60c58a48183dbf Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 17:41:29 +0000 Subject: [PATCH 1/2] Hash tables: choose with the generated table dispatchers `TableHash`'s `hash_table`, `md_hash_table` and `hmac_table` return the tables that `vg_hash__auto`, `vg_md_hash__auto` and `vg_hmac__auto` (`crate::arch::dispatch::fn_tables`) choose, instead of those of the implementation each hash's `Backend::select` chose in Rust. RSAES-OAEP's and RSASSA-PSS's `table::table` and `table::md_table`, the `HmacHash` trait and `ecdsa::table::sign_with` no longer take the CPU's features. `streaming_hash!` loses its `tables:` and the checks of their features, and `hashes::tables` its `Tables`. The choice is today's: the dispatchers rank the tables as the backends' `select`s did (SHA-NI, then AVX2 or AVX+BMI on x86-64; SHA2 or SHA3 on AArch64; SHA-NI on x86). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01TKUFHFuGsz7F32Tm9DLkuP --- src/ec/ecdsa/brainpoolp256r1.rs | 2 +- src/ec/ecdsa/brainpoolp384r1.rs | 2 +- src/ec/ecdsa/brainpoolp512r1.rs | 2 +- src/ec/ecdsa/p192.rs | 2 +- src/ec/ecdsa/p224.rs | 2 +- src/ec/ecdsa/p256.rs | 2 +- src/ec/ecdsa/p384.rs | 2 +- src/ec/ecdsa/p521.rs | 2 +- src/ec/ecdsa/secp256k1.rs | 2 +- src/ec/ecdsa/table.rs | 55 ++++++------- src/hashes/md5.rs | 7 -- src/hashes/mod.rs | 52 +----------- src/hashes/sha1.rs | 25 ------ src/hashes/sha224.rs | 25 ------ src/hashes/sha256.rs | 25 ------ src/hashes/sha384.rs | 25 ------ src/hashes/sha512.rs | 25 ------ src/hashes/sha512_224.rs | 25 ------ src/hashes/sha512_256.rs | 25 ------ src/hashes/tables.rs | 136 +++++++++++++------------------- src/rsa/oaep.rs | 20 ++--- src/rsa/oaep/table.rs | 14 ++-- src/rsa/pss.rs | 22 +++--- src/rsa/pss/table.rs | 18 ++--- 24 files changed, 124 insertions(+), 393 deletions(-) diff --git a/src/ec/ecdsa/brainpoolp256r1.rs b/src/ec/ecdsa/brainpoolp256r1.rs index b4869e4aec5..3f90304fd63 100644 --- a/src/ec/ecdsa/brainpoolp256r1.rs +++ b/src/ec/ecdsa/brainpoolp256r1.rs @@ -55,7 +55,7 @@ impl sealed::Functions for H { }; // SAFETY: `sign` needs no CPU feature that the multiplications // selected for this CPU do not. - unsafe { sign_with::(sign, f, d, digest) } + unsafe { sign_with::(sign, d, digest) } } /// The verified function takes the hash itself, of any length, and diff --git a/src/ec/ecdsa/brainpoolp384r1.rs b/src/ec/ecdsa/brainpoolp384r1.rs index 80dd0db38e0..6f1bb91ff11 100644 --- a/src/ec/ecdsa/brainpoolp384r1.rs +++ b/src/ec/ecdsa/brainpoolp384r1.rs @@ -55,7 +55,7 @@ impl sealed::Functions for H { }; // SAFETY: `sign` needs no CPU feature that the multiplications // selected for this CPU do not. - unsafe { sign_with::(sign, f, d, digest) } + unsafe { sign_with::(sign, d, digest) } } /// The verified function takes the hash itself, of any length, and diff --git a/src/ec/ecdsa/brainpoolp512r1.rs b/src/ec/ecdsa/brainpoolp512r1.rs index 197c7c416f3..ff8bfcacbd2 100644 --- a/src/ec/ecdsa/brainpoolp512r1.rs +++ b/src/ec/ecdsa/brainpoolp512r1.rs @@ -59,7 +59,7 @@ impl sealed::Functions for H { }; // SAFETY: `sign` needs no CPU feature that the multiplications // selected for this CPU do not. - unsafe { sign_with::(sign, f, d, digest) } + unsafe { sign_with::(sign, d, digest) } } /// The verified function takes the hash itself, of any length, and diff --git a/src/ec/ecdsa/p192.rs b/src/ec/ecdsa/p192.rs index 55fecb5c042..a418b7297e3 100644 --- a/src/ec/ecdsa/p192.rs +++ b/src/ec/ecdsa/p192.rs @@ -36,7 +36,7 @@ fn verify_with(q: &[u8; 49], digest: &[u8], signature: &[u8; 48]) -> Result<(), impl sealed::Functions for H { fn sign(d: &[u8; 24], digest: &H::Output) -> Result<[u8; 48], InvalidKey> { // SAFETY: `vg_ecdsa_p192_sign` needs no CPU feature. - unsafe { sign_with::(vg_ecdsa_p192_sign, crate::cpu::detected(), d, digest) } + unsafe { sign_with::(vg_ecdsa_p192_sign, d, digest) } } /// The verified function takes the hash itself, of any length, and diff --git a/src/ec/ecdsa/p224.rs b/src/ec/ecdsa/p224.rs index e1b0a7cdb1a..eef7531efa0 100644 --- a/src/ec/ecdsa/p224.rs +++ b/src/ec/ecdsa/p224.rs @@ -49,7 +49,7 @@ impl sealed::Functions for H { }; // SAFETY: `sign` needs no CPU feature that the multiplications // selected for this CPU do not. - unsafe { sign_with::(sign, f, d, digest) } + unsafe { sign_with::(sign, d, digest) } } /// The verified function takes the hash itself, of any length, and diff --git a/src/ec/ecdsa/p256.rs b/src/ec/ecdsa/p256.rs index 565976c4dd9..294f6ff1e65 100644 --- a/src/ec/ecdsa/p256.rs +++ b/src/ec/ecdsa/p256.rs @@ -57,7 +57,7 @@ impl sealed::Functions for H { }; // SAFETY: `sign` needs no CPU feature that the multiplications // selected for this CPU do not. - unsafe { sign_with::(sign, f, d, digest) } + unsafe { sign_with::(sign, d, digest) } } /// The verified function takes the hash itself, of any length, and diff --git a/src/ec/ecdsa/p384.rs b/src/ec/ecdsa/p384.rs index 6a19e8d0421..09d505d1e36 100644 --- a/src/ec/ecdsa/p384.rs +++ b/src/ec/ecdsa/p384.rs @@ -53,7 +53,7 @@ impl sealed::Functions for H { }; // SAFETY: `sign` needs no CPU feature that the multiplications // selected for this CPU do not. - unsafe { sign_with::(sign, f, d, digest) } + unsafe { sign_with::(sign, d, digest) } } /// The verified function takes the hash itself, of any length, and diff --git a/src/ec/ecdsa/p521.rs b/src/ec/ecdsa/p521.rs index 4f70e924533..b38025d4f20 100644 --- a/src/ec/ecdsa/p521.rs +++ b/src/ec/ecdsa/p521.rs @@ -53,7 +53,7 @@ impl sealed::Functions for H { }; // SAFETY: `sign` needs no CPU feature that the multiplications // selected for this CPU do not. - unsafe { sign_with::(sign, f, d, digest) } + unsafe { sign_with::(sign, d, digest) } } /// The verified function takes the hash itself, of any length, and diff --git a/src/ec/ecdsa/secp256k1.rs b/src/ec/ecdsa/secp256k1.rs index d3dc6bf24be..02ba9d9f4f1 100644 --- a/src/ec/ecdsa/secp256k1.rs +++ b/src/ec/ecdsa/secp256k1.rs @@ -49,7 +49,7 @@ impl sealed::Functions for H { }; // SAFETY: `sign` needs no CPU feature that the multiplications // selected for this CPU do not. - unsafe { sign_with::(sign, f, d, digest) } + unsafe { sign_with::(sign, d, digest) } } /// The verified function takes the hash itself, of any length, and diff --git a/src/ec/ecdsa/table.rs b/src/ec/ecdsa/table.rs index 8cdf99a3794..94dcb2a4f33 100644 --- a/src/ec/ecdsa/table.rs +++ b/src/ec/ecdsa/table.rs @@ -13,62 +13,60 @@ use crate::hashes::sha512_224::Sha512_224; use crate::hashes::sha512_256::Sha512_256; use crate::hashes::tables::TableHash; use crate::zeroize::zeroize; -use verified_garbage_cpu::Features; /// A hash function that the verified signatures take HMAC over as a table: /// one with HMAC (`VG.Spec.HmacTable.hmacOf`). pub trait HmacHash: HashFunction { - /// Its table of `Hmac`, as the verified functions take it, for the - /// implementation that a CPU with the features `f` runs best - /// (`TableHash::hmac_table`). - fn hmac_table(f: Features) -> *const [usize; 6]; + /// The best of its tables of `Hmac` that this CPU runs, as the verified + /// functions take it (`TableHash::hmac_table`). + fn hmac_table() -> *const [usize; 6]; } impl HmacHash for Md5 { - fn hmac_table(f: Features) -> *const [usize; 6] { - TableHash::Md5.hmac_table(f) + fn hmac_table() -> *const [usize; 6] { + TableHash::Md5.hmac_table() } } impl HmacHash for Sha1 { - fn hmac_table(f: Features) -> *const [usize; 6] { - TableHash::Sha1.hmac_table(f) + fn hmac_table() -> *const [usize; 6] { + TableHash::Sha1.hmac_table() } } impl HmacHash for Sha224 { - fn hmac_table(f: Features) -> *const [usize; 6] { - TableHash::Sha224.hmac_table(f) + fn hmac_table() -> *const [usize; 6] { + TableHash::Sha224.hmac_table() } } impl HmacHash for Sha256 { - fn hmac_table(f: Features) -> *const [usize; 6] { - TableHash::Sha256.hmac_table(f) + fn hmac_table() -> *const [usize; 6] { + TableHash::Sha256.hmac_table() } } impl HmacHash for Sha384 { - fn hmac_table(f: Features) -> *const [usize; 6] { - TableHash::Sha384.hmac_table(f) + fn hmac_table() -> *const [usize; 6] { + TableHash::Sha384.hmac_table() } } impl HmacHash for Sha512 { - fn hmac_table(f: Features) -> *const [usize; 6] { - TableHash::Sha512.hmac_table(f) + fn hmac_table() -> *const [usize; 6] { + TableHash::Sha512.hmac_table() } } impl HmacHash for Sha512_224 { - fn hmac_table(f: Features) -> *const [usize; 6] { - TableHash::Sha512_224.hmac_table(f) + fn hmac_table() -> *const [usize; 6] { + TableHash::Sha512_224.hmac_table() } } impl HmacHash for Sha512_256 { - fn hmac_table(f: Features) -> *const [usize; 6] { - TableHash::Sha512_256.hmac_table(f) + fn hmac_table() -> *const [usize; 6] { + TableHash::Sha512_256.hmac_table() } } @@ -97,19 +95,18 @@ pub(super) type SignFn = unsafe extern "C" fn( ) -> u32; /// The signature `r ‖ s` of `digest`, the hash of a message with `H`, with -/// the key `d`, by `sign` with `H`'s table of `Hmac` for this CPU (`f`, its -/// features). +/// the key `d`, by `sign` with the best of `H`'s tables of `Hmac` that this +/// CPU runs. /// /// # Safety /// /// `sign` needs no CPU feature that this CPU lacks. pub(super) unsafe fn sign_with( sign: SignFn, - f: Features, d: &[u8; Q], digest: &H::Output, ) -> Result<[u8; S], InvalidKey> { - let table = H::hmac_table(f); + let table = H::hmac_table(); let digest = digest.as_ref(); let mut out = [0; S]; let mut scratch = [0u64; 1024]; @@ -120,8 +117,9 @@ pub(super) unsafe fn sign_with( // others, so none overlaps another or the call's stack frame, and, as // Rust objects, none wraps around the address space. `table` is one of // the tables of the interface `Hmac`, for `H`, whose functions need no - // CPU feature that `f`, this CPU's, lacks (`TableHash::hmac_table`); the - // caller guarantees the features `sign` needs. + // CPU feature that this CPU lacks (its dispatcher chose it, + // `TableHash::hmac_table`); the caller guarantees the features `sign` + // needs. let ok = unsafe { sign( &mut out, @@ -139,14 +137,13 @@ pub(super) unsafe fn sign_with( #[cfg(test)] mod tests { use super::{HmacHash, Md5, Sha1, Sha224, Sha256, Sha384, Sha512, Sha512_224, Sha512_256}; - use verified_garbage_cpu::Features; /// `H`'s table of `Hmac` names `H`'s digest length and HMAC's block /// size (its words 4 and 5). fn check() { // SAFETY: `hmac_table` points to the table's six words, of an // immutable `'static` `repr(C)` value of six pointer-sized words. - let t = unsafe { *H::hmac_table(Features(0)) }; + let t = unsafe { *H::hmac_table() }; assert_eq!((t[4], t[5]), (H::OUTPUT_SIZE, H::BLOCK_SIZE)); } diff --git a/src/hashes/md5.rs b/src/hashes/md5.rs index 960e995773d..8d9585f4e6c 100644 --- a/src/hashes/md5.rs +++ b/src/hashes/md5.rs @@ -24,13 +24,6 @@ super::streaming_hash!( backends: Md5Backend { Scalar => (vg_md5_update, vg_md5_finalize), }, - tables: { - Scalar => { - hash: crate::arch::fn_tables::VG_HASH_MD5 [], - md_hash: crate::arch::fn_tables::VG_MD_HASH_MD5 [], - hmac: crate::arch::fn_tables::VG_HMAC_MD5 [], - }, - }, } ); diff --git a/src/hashes/mod.rs b/src/hashes/mod.rs index b5df66d16e8..aa86987304a 100644 --- a/src/hashes/mod.rs +++ b/src/hashes/mod.rs @@ -71,16 +71,6 @@ pub trait HashFunction: Clone { /// `digest` calls: the match is exhaustive, so an implementation without one /// does not compile. Without it, `digest` uses the streaming functions. /// -/// `tables`, if given, is every implementation's tables of functions -/// (`crate::arch::fn_tables`), each with the generated constants of the CPU -/// features it needs (none for the baseline ISA's: it fails to compile if -/// they are not all among those `select` requires to choose that -/// implementation), which `tables` returns for the verified functions that take a hash function as a table -/// (`tables::TableHash`): its table of `Hash`, its table of `MdHash` and, -/// on the architectures with tables of `Hmac` (`tables::Tables`), its table -/// of `Hmac`. The match is exhaustive, so an implementation without its -/// tables does not compile. -/// /// Every region is a distinct Rust object, so none overlaps another or the /// return address, and none wraps around the end of the address space; the /// length is kept exactly (`update` panics rather than let it reach 2⁶⁴ @@ -127,13 +117,7 @@ macro_rules! streaming_hash { $(, digests: { $($(#[$dattr:meta])* $dvariant:ident => $digest:path),* $(,)? })? - $(, tables: { - $($(#[$tattr:meta])* $tvariant:ident => { - hash: $thash:path [$($threq:path),*], - md_hash: $tmd:path [$($tmreq:path),*] - $(, hmac: $thmac:path [$($thmreq:path),*])? $(,)? - }),* $(,)? - })? $(,)? + $(,)? } ) => { /// The implementations of the streaming primitives. @@ -154,8 +138,7 @@ macro_rules! streaming_hash { /// every CPU it is chosen for has them, so a function chosen /// with it may need no other, which its caller checks when it /// compiles (`assert!` in a `const`). (A hash function with no - /// implementation but the baseline, and no tables, does not use - /// it.) + /// implementation but the baseline does not use it.) #[allow(dead_code)] pub(crate) const REQUIRES: &[verified_garbage_cpu::Features] = &[ verified_garbage_cpu::Features(0), @@ -240,37 +223,6 @@ macro_rules! streaming_hash { )? } - $( - // Each implementation's tables need no CPU feature that `select` - // does not require to choose it. - $( - $(#[$tattr])* - const _: () = assert!( - $backend::REQUIRES[$backend::$tvariant as usize].contains( - verified_garbage_cpu::Features::all(&[$($threq,)* $($tmreq,)* $($($thmreq),*)?]) - ), - "a table needs a CPU feature its implementation is not chosen for" - ); - )* - - impl $backend { - /// The tables of functions of this implementation. - pub(crate) fn tables(self) -> $crate::hashes::tables::Tables { - match self { - $($(#[$tattr])* Self::$tvariant => $crate::hashes::tables::Tables { - #[cfg(feature = "alloc")] - hash: &$thash, - #[cfg(feature = "alloc")] - md_hash: &$tmd, - $( - hmac: &$thmac, - )? - },)* - } - } - } - )? - $(#[$doc])* /// /// Messages are limited to 2⁶⁴ − 1 bytes. diff --git a/src/hashes/sha1.rs b/src/hashes/sha1.rs index d750f9c0fdb..f31c3c52f32 100644 --- a/src/hashes/sha1.rs +++ b/src/hashes/sha1.rs @@ -65,31 +65,6 @@ super::streaming_hash!( #[cfg(target_arch = "x86_64")] AvxBmi => vg_sha1_digest_avx_bmi, }, - tables: { - Scalar => { - hash: crate::arch::fn_tables::VG_HASH_SHA1 [], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA1 [], - hmac: crate::arch::fn_tables::VG_HMAC_SHA1 [], - }, - #[cfg(target_arch = "aarch64")] - Sha2 => { - hash: crate::arch::fn_tables::VG_HASH_SHA1_SHA2 [crate::arch::fn_tables::VG_HASH_SHA1_SHA2_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA1_SHA2 [crate::arch::fn_tables::VG_MD_HASH_SHA1_SHA2_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA1_SHA2 [crate::arch::fn_tables::VG_HMAC_SHA1_SHA2_FEATURES], - }, - #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] - ShaNi => { - hash: crate::arch::fn_tables::VG_HASH_SHA1_SHANI [crate::arch::fn_tables::VG_HASH_SHA1_SHANI_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA1_SHANI [crate::arch::fn_tables::VG_MD_HASH_SHA1_SHANI_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA1_SHANI [crate::arch::fn_tables::VG_HMAC_SHA1_SHANI_FEATURES], - }, - #[cfg(target_arch = "x86_64")] - AvxBmi => { - hash: crate::arch::fn_tables::VG_HASH_SHA1_AVX_BMI [crate::arch::fn_tables::VG_HASH_SHA1_AVX_BMI_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA1_AVX_BMI [crate::arch::fn_tables::VG_MD_HASH_SHA1_AVX_BMI_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA1_AVX_BMI [crate::arch::fn_tables::VG_HMAC_SHA1_AVX_BMI_FEATURES], - }, - }, } ); diff --git a/src/hashes/sha224.rs b/src/hashes/sha224.rs index 31debc1ad23..81b3f70f8a0 100644 --- a/src/hashes/sha224.rs +++ b/src/hashes/sha224.rs @@ -59,31 +59,6 @@ super::streaming_hash!( #[cfg(target_arch = "x86_64")] Avx2 => vg_sha224_digest_avx2, }, - tables: { - Scalar => { - hash: crate::arch::fn_tables::VG_HASH_SHA224 [], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA224 [], - hmac: crate::arch::fn_tables::VG_HMAC_SHA224 [], - }, - #[cfg(target_arch = "aarch64")] - Sha2 => { - hash: crate::arch::fn_tables::VG_HASH_SHA224_SHA2 [crate::arch::fn_tables::VG_HASH_SHA224_SHA2_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA224_SHA2 [crate::arch::fn_tables::VG_MD_HASH_SHA224_SHA2_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA224_SHA2 [crate::arch::fn_tables::VG_HMAC_SHA224_SHA2_FEATURES], - }, - #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] - ShaNi => { - hash: crate::arch::fn_tables::VG_HASH_SHA224_SHANI [crate::arch::fn_tables::VG_HASH_SHA224_SHANI_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA224_SHANI [crate::arch::fn_tables::VG_MD_HASH_SHA224_SHANI_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA224_SHANI [crate::arch::fn_tables::VG_HMAC_SHA224_SHANI_FEATURES], - }, - #[cfg(target_arch = "x86_64")] - Avx2 => { - hash: crate::arch::fn_tables::VG_HASH_SHA224_AVX2 [crate::arch::fn_tables::VG_HASH_SHA224_AVX2_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA224_AVX2 [crate::arch::fn_tables::VG_MD_HASH_SHA224_AVX2_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA224_AVX2 [crate::arch::fn_tables::VG_HMAC_SHA224_AVX2_FEATURES], - }, - }, } ); diff --git a/src/hashes/sha256.rs b/src/hashes/sha256.rs index 52b05892ef5..8dc20237790 100644 --- a/src/hashes/sha256.rs +++ b/src/hashes/sha256.rs @@ -62,31 +62,6 @@ super::streaming_hash!( #[cfg(target_arch = "x86_64")] Avx2 => vg_sha256_digest_avx2, }, - tables: { - Scalar => { - hash: crate::arch::fn_tables::VG_HASH_SHA256 [], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA256 [], - hmac: crate::arch::fn_tables::VG_HMAC_SHA256 [], - }, - #[cfg(target_arch = "aarch64")] - Sha2 => { - hash: crate::arch::fn_tables::VG_HASH_SHA256_SHA2 [crate::arch::fn_tables::VG_HASH_SHA256_SHA2_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA256_SHA2 [crate::arch::fn_tables::VG_MD_HASH_SHA256_SHA2_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA256_SHA2 [crate::arch::fn_tables::VG_HMAC_SHA256_SHA2_FEATURES], - }, - #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] - ShaNi => { - hash: crate::arch::fn_tables::VG_HASH_SHA256_SHANI [crate::arch::fn_tables::VG_HASH_SHA256_SHANI_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA256_SHANI [crate::arch::fn_tables::VG_MD_HASH_SHA256_SHANI_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA256_SHANI [crate::arch::fn_tables::VG_HMAC_SHA256_SHANI_FEATURES], - }, - #[cfg(target_arch = "x86_64")] - Avx2 => { - hash: crate::arch::fn_tables::VG_HASH_SHA256_AVX2 [crate::arch::fn_tables::VG_HASH_SHA256_AVX2_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA256_AVX2 [crate::arch::fn_tables::VG_MD_HASH_SHA256_AVX2_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA256_AVX2 [crate::arch::fn_tables::VG_HMAC_SHA256_AVX2_FEATURES], - }, - }, } ); diff --git a/src/hashes/sha384.rs b/src/hashes/sha384.rs index 6fd08f3ede0..4bda49d3ccc 100644 --- a/src/hashes/sha384.rs +++ b/src/hashes/sha384.rs @@ -59,31 +59,6 @@ super::streaming_hash!( #[cfg(target_arch = "x86_64")] Avx2 => vg_sha384_digest_avx2, }, - tables: { - Scalar => { - hash: crate::arch::fn_tables::VG_HASH_SHA384 [], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA384 [], - hmac: crate::arch::fn_tables::VG_HMAC_SHA384 [], - }, - #[cfg(target_arch = "aarch64")] - Sha3 => { - hash: crate::arch::fn_tables::VG_HASH_SHA384_SHA3 [crate::arch::fn_tables::VG_HASH_SHA384_SHA3_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA384_SHA3 [crate::arch::fn_tables::VG_MD_HASH_SHA384_SHA3_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA384_SHA3 [crate::arch::fn_tables::VG_HMAC_SHA384_SHA3_FEATURES], - }, - #[cfg(target_arch = "x86_64")] - ShaNi => { - hash: crate::arch::fn_tables::VG_HASH_SHA384_SHANI [crate::arch::fn_tables::VG_HASH_SHA384_SHANI_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA384_SHANI [crate::arch::fn_tables::VG_MD_HASH_SHA384_SHANI_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA384_SHANI [crate::arch::fn_tables::VG_HMAC_SHA384_SHANI_FEATURES], - }, - #[cfg(target_arch = "x86_64")] - Avx2 => { - hash: crate::arch::fn_tables::VG_HASH_SHA384_AVX2 [crate::arch::fn_tables::VG_HASH_SHA384_AVX2_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA384_AVX2 [crate::arch::fn_tables::VG_MD_HASH_SHA384_AVX2_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA384_AVX2 [crate::arch::fn_tables::VG_HMAC_SHA384_AVX2_FEATURES], - }, - }, } ); diff --git a/src/hashes/sha512.rs b/src/hashes/sha512.rs index bd3dbc29f29..858530f72fc 100644 --- a/src/hashes/sha512.rs +++ b/src/hashes/sha512.rs @@ -55,31 +55,6 @@ super::streaming_hash!( #[cfg(target_arch = "x86_64")] Avx2 => vg_sha512_digest_avx2, }, - tables: { - Scalar => { - hash: crate::arch::fn_tables::VG_HASH_SHA512 [], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA512 [], - hmac: crate::arch::fn_tables::VG_HMAC_SHA512 [], - }, - #[cfg(target_arch = "aarch64")] - Sha3 => { - hash: crate::arch::fn_tables::VG_HASH_SHA512_SHA3 [crate::arch::fn_tables::VG_HASH_SHA512_SHA3_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA512_SHA3 [crate::arch::fn_tables::VG_MD_HASH_SHA512_SHA3_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA512_SHA3 [crate::arch::fn_tables::VG_HMAC_SHA512_SHA3_FEATURES], - }, - #[cfg(target_arch = "x86_64")] - ShaNi => { - hash: crate::arch::fn_tables::VG_HASH_SHA512_SHANI [crate::arch::fn_tables::VG_HASH_SHA512_SHANI_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA512_SHANI [crate::arch::fn_tables::VG_MD_HASH_SHA512_SHANI_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA512_SHANI [crate::arch::fn_tables::VG_HMAC_SHA512_SHANI_FEATURES], - }, - #[cfg(target_arch = "x86_64")] - Avx2 => { - hash: crate::arch::fn_tables::VG_HASH_SHA512_AVX2 [crate::arch::fn_tables::VG_HASH_SHA512_AVX2_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA512_AVX2 [crate::arch::fn_tables::VG_MD_HASH_SHA512_AVX2_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA512_AVX2 [crate::arch::fn_tables::VG_HMAC_SHA512_AVX2_FEATURES], - }, - }, } ); diff --git a/src/hashes/sha512_224.rs b/src/hashes/sha512_224.rs index c9bbe0cef3e..b58a8aebb8f 100644 --- a/src/hashes/sha512_224.rs +++ b/src/hashes/sha512_224.rs @@ -59,31 +59,6 @@ super::streaming_hash!( #[cfg(target_arch = "x86_64")] Avx2 => vg_sha512_224_digest_avx2, }, - tables: { - Scalar => { - hash: crate::arch::fn_tables::VG_HASH_SHA512_224 [], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA512_224 [], - hmac: crate::arch::fn_tables::VG_HMAC_SHA512_224 [], - }, - #[cfg(target_arch = "aarch64")] - Sha3 => { - hash: crate::arch::fn_tables::VG_HASH_SHA512_224_SHA3 [crate::arch::fn_tables::VG_HASH_SHA512_224_SHA3_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA512_224_SHA3 [crate::arch::fn_tables::VG_MD_HASH_SHA512_224_SHA3_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA512_224_SHA3 [crate::arch::fn_tables::VG_HMAC_SHA512_224_SHA3_FEATURES], - }, - #[cfg(target_arch = "x86_64")] - ShaNi => { - hash: crate::arch::fn_tables::VG_HASH_SHA512_224_SHANI [crate::arch::fn_tables::VG_HASH_SHA512_224_SHANI_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA512_224_SHANI [crate::arch::fn_tables::VG_MD_HASH_SHA512_224_SHANI_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA512_224_SHANI [crate::arch::fn_tables::VG_HMAC_SHA512_224_SHANI_FEATURES], - }, - #[cfg(target_arch = "x86_64")] - Avx2 => { - hash: crate::arch::fn_tables::VG_HASH_SHA512_224_AVX2 [crate::arch::fn_tables::VG_HASH_SHA512_224_AVX2_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA512_224_AVX2 [crate::arch::fn_tables::VG_MD_HASH_SHA512_224_AVX2_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA512_224_AVX2 [crate::arch::fn_tables::VG_HMAC_SHA512_224_AVX2_FEATURES], - }, - }, } ); diff --git a/src/hashes/sha512_256.rs b/src/hashes/sha512_256.rs index 8dbb9a2d4a8..512cf8de7cf 100644 --- a/src/hashes/sha512_256.rs +++ b/src/hashes/sha512_256.rs @@ -59,31 +59,6 @@ super::streaming_hash!( #[cfg(target_arch = "x86_64")] Avx2 => vg_sha512_256_digest_avx2, }, - tables: { - Scalar => { - hash: crate::arch::fn_tables::VG_HASH_SHA512_256 [], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA512_256 [], - hmac: crate::arch::fn_tables::VG_HMAC_SHA512_256 [], - }, - #[cfg(target_arch = "aarch64")] - Sha3 => { - hash: crate::arch::fn_tables::VG_HASH_SHA512_256_SHA3 [crate::arch::fn_tables::VG_HASH_SHA512_256_SHA3_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA512_256_SHA3 [crate::arch::fn_tables::VG_MD_HASH_SHA512_256_SHA3_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA512_256_SHA3 [crate::arch::fn_tables::VG_HMAC_SHA512_256_SHA3_FEATURES], - }, - #[cfg(target_arch = "x86_64")] - ShaNi => { - hash: crate::arch::fn_tables::VG_HASH_SHA512_256_SHANI [crate::arch::fn_tables::VG_HASH_SHA512_256_SHANI_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA512_256_SHANI [crate::arch::fn_tables::VG_MD_HASH_SHA512_256_SHANI_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA512_256_SHANI [crate::arch::fn_tables::VG_HMAC_SHA512_256_SHANI_FEATURES], - }, - #[cfg(target_arch = "x86_64")] - Avx2 => { - hash: crate::arch::fn_tables::VG_HASH_SHA512_256_AVX2 [crate::arch::fn_tables::VG_HASH_SHA512_256_AVX2_FEATURES], - md_hash: crate::arch::fn_tables::VG_MD_HASH_SHA512_256_AVX2 [crate::arch::fn_tables::VG_MD_HASH_SHA512_256_AVX2_FEATURES], - hmac: crate::arch::fn_tables::VG_HMAC_SHA512_256_AVX2 [crate::arch::fn_tables::VG_HMAC_SHA512_256_AVX2_FEATURES], - }, - }, } ); diff --git a/src/hashes/tables.rs b/src/hashes/tables.rs index 4d1f38a9f90..6388ee9aa94 100644 --- a/src/hashes/tables.rs +++ b/src/hashes/tables.rs @@ -2,33 +2,8 @@ //! take a hash function at run time (RSAES-OAEP, RSASSA-PSS, deterministic //! ECDSA). +use crate::arch::dispatch::fn_tables as auto; use crate::arch::fn_tables; -use crate::hashes::md5::Md5Backend; -use crate::hashes::sha1::Sha1Backend; -use crate::hashes::sha224::Sha224Backend; -use crate::hashes::sha256::Sha256Backend; -use crate::hashes::sha384::Sha384Backend; -use crate::hashes::sha512::Sha512Backend; -use crate::hashes::sha512_224::Sha512_224Backend; -use crate::hashes::sha512_256::Sha512_256Backend; -use verified_garbage_cpu::Features; - -/// The tables of functions of one implementation of a hash function, which -/// need no CPU feature that its backend's `select` does not require to -/// choose it (`streaming_hash!` checks it when it compiles). -#[derive(Clone, Copy)] -pub(crate) struct Tables { - /// Its table of `Hash`: `init`, `update` and `finalize`. - #[cfg(feature = "alloc")] - pub(crate) hash: &'static fn_tables::Hash, - /// Its table of `MdHash`: those, its compression function and its - /// digest of a state. - #[cfg(feature = "alloc")] - pub(crate) md_hash: &'static fn_tables::MdHash, - /// Its table of `Hmac`: HMAC's `init`, the streaming `update` and HMAC's - /// `finalize`, with its index, digest length and block size. - pub(crate) hmac: &'static fn_tables::Hmac, -} /// A hash function that has tables of functions (`VG.Spec.Mgf1.hashes`' /// Merkle–Damgård ones). @@ -53,54 +28,65 @@ pub(crate) enum TableHash { } impl TableHash { - /// The tables of the implementation that a CPU with the features `f` - /// runs best. - pub(crate) fn tables(self, f: Features) -> Tables { - match self { - Self::Md5 => Md5Backend::select(f).tables(), - Self::Sha1 => Sha1Backend::select(f).tables(), - Self::Sha224 => Sha224Backend::select(f).tables(), - Self::Sha256 => Sha256Backend::select(f).tables(), - Self::Sha384 => Sha384Backend::select(f).tables(), - Self::Sha512 => Sha512Backend::select(f).tables(), - Self::Sha512_224 => Sha512_224Backend::select(f).tables(), - Self::Sha512_256 => Sha512_256Backend::select(f).tables(), - } - } - /// Its table of `Hash`, as the verified functions take it: the words of - /// the table of the implementation that a CPU with the features `f` - /// runs best. + /// the best table this CPU runs (its dispatcher's, + /// `crate::arch::dispatch::fn_tables`). #[cfg(feature = "alloc")] - pub(crate) fn hash_table(self, f: Features) -> *const [usize; 5] { - self.tables(f).hash.as_ptr() + pub(crate) fn hash_table(self) -> *const [usize; 5] { + let t: &'static fn_tables::Hash = match self { + Self::Md5 => auto::vg_hash_md5_auto(), + Self::Sha1 => auto::vg_hash_sha1_auto(), + Self::Sha224 => auto::vg_hash_sha224_auto(), + Self::Sha256 => auto::vg_hash_sha256_auto(), + Self::Sha384 => auto::vg_hash_sha384_auto(), + Self::Sha512 => auto::vg_hash_sha512_auto(), + Self::Sha512_224 => auto::vg_hash_sha512_224_auto(), + Self::Sha512_256 => auto::vg_hash_sha512_256_auto(), + }; + t.as_ptr() } /// Its table of `MdHash`, as the verified functions take it. #[cfg(feature = "alloc")] - pub(crate) fn md_hash_table(self, f: Features) -> *const [usize; 11] { - self.tables(f).md_hash.as_ptr() + pub(crate) fn md_hash_table(self) -> *const [usize; 11] { + let t: &'static fn_tables::MdHash = match self { + Self::Md5 => auto::vg_md_hash_md5_auto(), + Self::Sha1 => auto::vg_md_hash_sha1_auto(), + Self::Sha224 => auto::vg_md_hash_sha224_auto(), + Self::Sha256 => auto::vg_md_hash_sha256_auto(), + Self::Sha384 => auto::vg_md_hash_sha384_auto(), + Self::Sha512 => auto::vg_md_hash_sha512_auto(), + Self::Sha512_224 => auto::vg_md_hash_sha512_224_auto(), + Self::Sha512_256 => auto::vg_md_hash_sha512_256_auto(), + }; + t.as_ptr() } /// Its table of `Hmac`, as the verified functions take it. - pub(crate) fn hmac_table(self, f: Features) -> *const [usize; 6] { - self.tables(f).hmac.as_ptr() + pub(crate) fn hmac_table(self) -> *const [usize; 6] { + let t: &'static fn_tables::Hmac = match self { + Self::Md5 => auto::vg_hmac_md5_auto(), + Self::Sha1 => auto::vg_hmac_sha1_auto(), + Self::Sha224 => auto::vg_hmac_sha224_auto(), + Self::Sha256 => auto::vg_hmac_sha256_auto(), + Self::Sha384 => auto::vg_hmac_sha384_auto(), + Self::Sha512 => auto::vg_hmac_sha512_auto(), + Self::Sha512_224 => auto::vg_hmac_sha512_224_auto(), + Self::Sha512_256 => auto::vg_hmac_sha512_256_auto(), + }; + t.as_ptr() } } #[cfg(test)] mod tests { - use super::{ - Md5Backend, Sha1Backend, Sha224Backend, Sha256Backend, Sha384Backend, Sha512_224Backend, - Sha512_256Backend, Sha512Backend, TableHash, - }; + use super::TableHash; /// A table of `Hash` holds the hash function's index and digest length /// (its words 3 and 4), as its table of `MdHash` does (words 5 and 6), /// and its table of `Hmac` holds them and HMAC's block size (words 3, 4 /// and 5); and the indices are those of `VG.Spec.Mgf1.hashes`, in this - /// order: for the implementations a CPU with exactly the features any - /// implementation's `select` requires runs, so for each of them. + /// order: for the tables this CPU runs. #[test] fn table_words() { let all = [ @@ -113,33 +99,21 @@ mod tests { (TableHash::Sha512_224, 28, 128), (TableHash::Sha512_256, 32, 128), ]; - let sets = [ - Md5Backend::REQUIRES, - Sha1Backend::REQUIRES, - Sha224Backend::REQUIRES, - Sha256Backend::REQUIRES, - Sha384Backend::REQUIRES, - Sha512Backend::REQUIRES, - Sha512_224Backend::REQUIRES, - Sha512_256Backend::REQUIRES, - ]; - for &f in sets.into_iter().flatten() { - for (i, (h, len, block)) in all.into_iter().enumerate() { - #[cfg(feature = "alloc")] - { - // SAFETY: `as_ptr` points to the tables' words, of - // immutable `'static` `repr(C)` values of 5 and 11 - // pointer-sized words. - let (g, md) = unsafe { (*h.hash_table(f), *h.md_hash_table(f)) }; - assert_eq!((g[3], g[4]), (i, len), "{h:?} {f:?}"); - assert_eq!((md[5], md[6]), (i, len), "{h:?} {f:?}"); - } - // SAFETY: `as_ptr` points to the table's words, of an - // immutable `'static` `repr(C)` value of 6 pointer-sized - // words. - let m = unsafe { *h.hmac_table(f) }; - assert_eq!((m[3], m[4], m[5]), (i, len, block), "{h:?} {f:?}"); + for (i, (h, len, block)) in all.into_iter().enumerate() { + #[cfg(feature = "alloc")] + { + // SAFETY: `as_ptr` points to the tables' words, of + // immutable `'static` `repr(C)` values of 5 and 11 + // pointer-sized words. + let (g, md) = unsafe { (*h.hash_table(), *h.md_hash_table()) }; + assert_eq!((g[3], g[4]), (i, len), "{h:?}"); + assert_eq!((md[5], md[6]), (i, len), "{h:?}"); } + // SAFETY: `as_ptr` points to the table's words, of an + // immutable `'static` `repr(C)` value of 6 pointer-sized + // words. + let m = unsafe { *h.hmac_table() }; + assert_eq!((m[3], m[4], m[5]), (i, len, block), "{h:?}"); } } } diff --git a/src/rsa/oaep.rs b/src/rsa/oaep.rs index 3f88b6edd26..eaf785691b9 100644 --- a/src/rsa/oaep.rs +++ b/src/rsa/oaep.rs @@ -186,8 +186,8 @@ fn with_tables(hash: Hash, mgf1_hash: Hash) -> Option<(EncryptFn, DecryptFn, Tab Some(( encrypt, decrypt, - table::table(hash, f)?, - table::table(mgf1_hash, f)?, + table::table(hash)?, + table::table(mgf1_hash)?, )) } @@ -259,10 +259,10 @@ fn encrypt_with( // precomputation for `key.n`, with `2 * ceil(n_len / 8)` words; `hash` // and `mgf1` are the words of tables of the crate's of `Hash`, and // `seed_len` is the digest length of `hash`'s, its word 4 - // (`hashes::tables::tests::table_words` checks it); and the CPU has the features of `f` and - // of the tables' functions, which `functions` and `table::table` chose - // for the features detected (which fails to compile if they - // need others). + // (`hashes::tables::tests::table_words` checks it); and the CPU has the + // features of `f`, which `functions` chose for the features detected + // (which fails to compile if it needs others), and of the tables' + // functions, which their dispatchers chose (`table::table`). let r = unsafe { f( out.as_mut_ptr(), @@ -339,10 +339,10 @@ fn decrypt_with( // `1 ≤ e_len ≤ 5 ≤ n_len`, `1 ≤ p_len < n_len`, `1 ≤ q_len < n_len`, // `dp_len = qinv_len = p_len`, `dq_len = q_len` and // `scratch_len = 16 n_len + 1056`; `hash` and `mgf1` are the words of - // tables of the crate's of `Hash`; and the CPU has the features of `f` - // and of the tables' functions, which `functions` and `table::table` - // chose for the features detected (which fails to compile if they - // need others). + // tables of the crate's of `Hash`; and the CPU has the features of `f`, + // which `functions` chose for the features detected (which fails to + // compile if it needs others), and of the tables' functions, which + // their dispatchers chose (`table::table`). let r = unsafe { f( out.as_mut_ptr(), diff --git a/src/rsa/oaep/table.rs b/src/rsa/oaep/table.rs index ce7963f5815..04d6ee4b19f 100644 --- a/src/rsa/oaep/table.rs +++ b/src/rsa/oaep/table.rs @@ -3,13 +3,11 @@ use crate::hashes::tables::TableHash; use crate::rsa::pkcs1_sig::Hash; -use verified_garbage_cpu::Features; /// The hash function `hash` as the verified functions take it: the words of -/// its table of `Hash` for the implementation that a CPU with the features -/// `f` runs best, or `None` if `hash` is not supported (MD5, which RSAES-OAEP +/// the best of its tables of `Hash` that this CPU runs, or `None` if `hash` is not supported (MD5, which RSAES-OAEP /// does not use, and SHA-3's, which have no table). -pub(super) fn table(hash: Hash, f: Features) -> Option { +pub(super) fn table(hash: Hash) -> Option { let h = match hash { Hash::Sha1 => TableHash::Sha1, Hash::Sha224 => TableHash::Sha224, @@ -20,7 +18,7 @@ pub(super) fn table(hash: Hash, f: Features) -> Option { Hash::Sha512_256 => TableHash::Sha512_256, _ => return None, }; - Some(h.hash_table(f)) + Some(h.hash_table()) } /// The verified functions: `functions`, which chooses, for a CPU with the @@ -82,8 +80,7 @@ mod tests { /// Every hash function but MD5 and SHA-3's has a table, whose word 4 is /// the hash function's digest length, which encryption takes as the - /// seed's. (`streaming_hash!` checks the tables' CPU features when it - /// compiles.) + /// seed's. #[test] fn tables() { let all = [ @@ -100,10 +97,9 @@ mod tests { Hash::Sha3_384, Hash::Sha3_512, ]; - let f = crate::cpu::detected(); let mut n = 0; for h in all { - if let Some(t) = table(h, f) { + if let Some(t) = table(h) { // SAFETY: `t` points to a table's five words, of an // immutable `'static` `repr(C)` value. assert_eq!(unsafe { (*t)[4] }, h.digest_len(), "{h:?}"); diff --git a/src/rsa/pss.rs b/src/rsa/pss.rs index 1d0c4d66ab9..1be0368c5eb 100644 --- a/src/rsa/pss.rs +++ b/src/rsa/pss.rs @@ -204,12 +204,7 @@ verified_fn_types! { fn with_tables(hash: Hash, mgf1_hash: Hash) -> (SignFn, VerifyFn, MdTable, Table) { let f = detected(); let (sign, verify) = arch::functions(f); - ( - sign, - verify, - table::md_table(hash, f), - table::table(mgf1_hash, f), - ) + (sign, verify, table::md_table(hash), table::table(mgf1_hash)) } /// The words of working space the functions need for an `n_len`-byte @@ -271,10 +266,10 @@ fn sign_with( // `Hash`, and `digest_len` is the digest length of `hash`'s, its word 6 // (`hashes::tables::tests::table_words` checks it, and `sign_pss` // the length); and the - // CPU has the features of `f` and of the tables' functions, which - // `functions`, `table::md_table` and `table::table` chose for the - // features detected (which fails to compile if they need - // others). + // CPU has the features of `f`, which `functions` chose for the + // features detected (which fails to compile if it needs others), and + // of the tables' functions, which their dispatchers chose + // (`table::md_table`, `table::table`). let r = unsafe { f( out.as_mut_ptr(), @@ -349,9 +344,10 @@ impl PublicKey { // the words of a table of the crate's of `MdHash` and `mgf1` of one of // `Hash`, and `digest_len` is the digest length of `hash`'s, its word 6 // (`hashes::tables::tests::table_words` checks it); and the CPU has the - // features of `f` and of the tables' functions, which `functions`, - // `table::md_table` and `table::table` chose for the features detected - // (which fails to compile if they need others). + // features of `f`, which `functions` chose for the features detected + // (which fails to compile if it needs others), and of the tables' + // functions, which their dispatchers chose (`table::md_table`, + // `table::table`). let r = unsafe { f( self.n.as_ptr(), diff --git a/src/rsa/pss/table.rs b/src/rsa/pss/table.rs index d982417e847..904a005ef8c 100644 --- a/src/rsa/pss/table.rs +++ b/src/rsa/pss/table.rs @@ -3,7 +3,6 @@ use super::Hash; use crate::hashes::tables::TableHash; -use verified_garbage_cpu::Features; /// The hash function with tables that `hash` is. fn table_hash(hash: Hash) -> TableHash { @@ -20,16 +19,16 @@ fn table_hash(hash: Hash) -> TableHash { } /// The hash function `hash` as the verified functions take it, their -/// argument `hash`: the words of its table of `MdHash` for the -/// implementation that a CPU with the features `f` runs best. -pub(super) fn md_table(hash: Hash, f: Features) -> super::MdTable { - table_hash(hash).md_hash_table(f) +/// argument `hash`: the words of the best of its tables of `MdHash` that +/// this CPU runs. +pub(super) fn md_table(hash: Hash) -> super::MdTable { + table_hash(hash).md_hash_table() } /// MGF1's hash function `hash` as the verified functions take it, their /// argument `mgf1`: the words of its table of `Hash`. -pub(super) fn table(hash: Hash, f: Features) -> super::Table { - table_hash(hash).hash_table(f) +pub(super) fn table(hash: Hash) -> super::Table { + table_hash(hash).hash_table() } /// The verified functions: `functions`, which chooses, for a CPU with the @@ -92,7 +91,7 @@ mod tests { /// The tables of each hash function hold its digest length, the one the /// verified functions take the hash value's to be: a table of `MdHash` /// in its word 6, one of `Hash` in its word 4. (`crate::hashes::tables` - /// checks the tables' CPU features and indices.) + /// checks the tables' indices.) #[test] fn tables() { let all = [ @@ -105,11 +104,10 @@ mod tests { Hash::Sha512_224, Hash::Sha512_256, ]; - let f = crate::cpu::detected(); for h in all { // SAFETY: the pointers point to the tables' words, of immutable // `'static` `repr(C)` values of 11 and 5 pointer-sized words. - let (md, g) = unsafe { (*md_table(h, f), *table(h, f)) }; + let (md, g) = unsafe { (*md_table(h), *table(h)) }; assert_eq!(md[6], h.digest_len(), "{h:?}"); assert_eq!(g[4], h.digest_len(), "{h:?}"); } From 62e6825457476fddd0d0b468b8c2ab2120f8a759 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 18:16:56 +0000 Subject: [PATCH 2/2] CPU tests: the hash tables' test on the SHA-512 lines `hashes::tables::tests::table_words` now reads the tables the dispatchers choose, so the lines that choose among SHA-512's implementations (SHA512, and none) run it, to cover the choices of the SHA-512 family's tables of `Hmac` (SHA-512/224's and SHA-512/256's), which nothing else there reaches. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01TKUFHFuGsz7F32Tm9DLkuP --- ci/cpu_tests.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ci/cpu_tests.py b/ci/cpu_tests.py index 38e7baa32b1..d47b26b15a2 100644 --- a/ci/cpu_tests.py +++ b/ci/cpu_tests.py @@ -427,6 +427,9 @@ def runs_in(runner, test): }, "sha512": { "lib": [ + # Every hash's tables of functions: their dispatchers' choices + # of the SHA-512 family's tables, which only these lines make. + "hashes::tables", "hashes::sha512", "hashes::sha512_224", "hashes::sha512_256",