From 9b5d6162e5ad2f3dfadb06b731e7e30aa4694327 Mon Sep 17 00:00:00 2001 From: integritychain Date: Tue, 30 Jan 2024 18:57:53 -0600 Subject: [PATCH] shaking! --- Cargo.toml | 8 +- src/algs.rs | 6 +- src/hashers.rs | 72 ++++++++++-- src/lib.rs | 296 ++++++++++++++++++++++++++++++------------------- src/test.rs | 2 +- 5 files changed, 252 insertions(+), 132 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7b3fb75..fda517b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,10 +21,10 @@ rand_chacha = "0.3.1" [features] -default = ["default-rng", "slh_dsa_shake_128s", "slh_dsa_sha2_128s"] -#default = ["default-rng", "slh_dsa_sha2_128s"] #, "slh_dsa_shake_128s", "slh_dsa_sha2_128f", "slh_dsa_shake_128f", -# "slh_dsa_sha2_192s", "slh_dsa_shake_192s", "slh_dsa_sha2_192f", "slh_dsa_shake_192f", -# "slh_dsa_sha2_256s", "slh_dsa_shake_256s", "slh_dsa_sha2_256f", "slh_dsa_shake_256f"] +#default = ["default-rng", "slh_dsa_shake_128s", "slh_dsa_sha2_128s"] +default = ["default-rng", "slh_dsa_sha2_128s", "slh_dsa_shake_128s", "slh_dsa_sha2_128f", "slh_dsa_shake_128f", + "slh_dsa_sha2_192s", "slh_dsa_shake_192s", "slh_dsa_sha2_192f", "slh_dsa_shake_192f", + "slh_dsa_sha2_256s", "slh_dsa_shake_256s", "slh_dsa_sha2_256f", "slh_dsa_shake_256f"] default-rng = ["rand_core/getrandom"] slh_dsa_sha2_128s = [] slh_dsa_shake_128s = [] diff --git a/src/algs.rs b/src/algs.rs index a2ecf0a..9707eb9 100644 --- a/src/algs.rs +++ b/src/algs.rs @@ -1116,7 +1116,8 @@ pub(crate) fn slh_sign_with_rng< // 15: idx_tree ← toInt(tmp_idx_tree, ceil((h-h/d)/8)) mod 2^{h−h/d} let idx_tree = to_int(tmp_idx_tree, (H::to_usize() - H::to_usize() / D::to_usize()).div_ceil(8)) - % 2u64.pow(H::to_u32() - H::to_u32() / D::to_u32()); + & (u64::MAX >> (64 - (H::to_u32() - H::to_u32() / D::to_u32()))); + // % 2u64.pow(H::to_u32() - H::to_u32() / D::to_u32()); // Can be 2^64 // 16: idx_leaf ← toInt(tmp_idx_leaf, ceil(h/8d) mod 2^{h/d} let idx_leaf = to_int(tmp_idx_leaf, H::to_usize().div_ceil(8 * D::to_usize())) @@ -1214,7 +1215,8 @@ pub(crate) fn slh_verify< // 14: idx_tree ← toInt(tmp_idx_tree, ceil((h - h/d)/8)) mod 2^{h−h/d} let idx_tree = to_int(tmp_idx_tree, (H::to_usize() - H::to_usize() / D::to_usize()).div_ceil(8)) - % 2u64.pow(H::to_u32() - H::to_u32() / D::to_u32()); + & (u64::MAX >> (64 - (H::to_u32() - H::to_u32() / D::to_u32()))); + // % 2u64.pow(H::to_u32() - H::to_u32() / D::to_u32()); // Can be 2^64 // 15: idx_leaf ← toInt(tmp_idx_leaf, ceil(h/8d) mod 2^{h/d} let idx_leaf = to_int(tmp_idx_leaf, H::to_usize().div_ceil(8 * D::to_usize())) diff --git a/src/hashers.rs b/src/hashers.rs index 57782e3..d3dff9e 100644 --- a/src/hashers.rs +++ b/src/hashers.rs @@ -106,44 +106,45 @@ pub(crate) mod shake { } } -pub(crate) mod sha2_cat1 { +pub(crate) mod sha2_cat_1 { use crate::types::Adrs; use generic_array::{ArrayLength, GenericArray}; - pub(crate) fn h_msg( _r: &[u8], _pk_seed: &[u8], _pk_root: &[u8], _m: &[u8], ) -> GenericArray { - unimplemented!() + GenericArray::default() } pub(crate) fn prf( _pk_seed: &[u8], _sk_seed: &[u8], _adrs: &Adrs, ) -> GenericArray { - unimplemented!() + GenericArray::default() } pub(crate) fn prf_msg( _sk_prf: &[u8], _opt_rand: &[u8], _m: &[u8], ) -> GenericArray { - unimplemented!() + GenericArray::default() } - pub(crate) fn f(_pk_seed: &[u8], _adrs: &Adrs, _m1: &[u8]) -> GenericArray { - unimplemented!() + pub(crate) fn f( + _pk_seed: &[u8], _adrs: &Adrs, _m1: &[u8], + ) -> GenericArray { + GenericArray::default() } pub(crate) fn h( _pk_seed: &[u8], _adrs: &Adrs, _m1: &[u8], _m2: &[u8], ) -> GenericArray { - unimplemented!() + GenericArray::default() } // Until a more elegant way is found to covert ml into list of bytes pub(crate) fn t_l( _pk_seed: &[u8], _adrs: &Adrs, _ml: &GenericArray, LEN>, ) -> GenericArray { - unimplemented!() + GenericArray::default() } // TODO: Squash K and LEN versions @@ -151,6 +152,57 @@ pub(crate) mod sha2_cat1 { pub(crate) fn t_len( _pk_seed: &[u8], _adrs: &Adrs, _ml: &GenericArray, K>, ) -> GenericArray { - unimplemented!() + GenericArray::default() + } +} + + +pub(crate) mod sha2_cat_3_5 { + use crate::types::Adrs; + use generic_array::{ArrayLength, GenericArray}; + + pub(crate) fn h_msg( + _r: &[u8], _pk_seed: &[u8], _pk_root: &[u8], _m: &[u8], + ) -> GenericArray { + GenericArray::default() + } + + pub(crate) fn prf( + _pk_seed: &[u8], _sk_seed: &[u8], _adrs: &Adrs, + ) -> GenericArray { + GenericArray::default() + } + + pub(crate) fn prf_msg( + _sk_prf: &[u8], _opt_rand: &[u8], _m: &[u8], + ) -> GenericArray { + GenericArray::default() + } + + pub(crate) fn f( + _pk_seed: &[u8], _adrs: &Adrs, _m1: &[u8], + ) -> GenericArray { + GenericArray::default() + } + + pub(crate) fn h( + _pk_seed: &[u8], _adrs: &Adrs, _m1: &[u8], _m2: &[u8], + ) -> GenericArray { + GenericArray::default() + } + + // Until a more elegant way is found to covert ml into list of bytes + pub(crate) fn t_l( + _pk_seed: &[u8], _adrs: &Adrs, _ml: &GenericArray, LEN>, + ) -> GenericArray { + GenericArray::default() + } + + // TODO: Squash K and LEN versions + // Until a more elegant way is found to covert ml into list of bytes + pub(crate) fn t_len( + _pk_seed: &[u8], _adrs: &Adrs, _ml: &GenericArray, K>, + ) -> GenericArray { + GenericArray::default() } } diff --git a/src/lib.rs b/src/lib.rs index 0f0979e..6d902d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,8 +7,8 @@ /// crate doc? // TODO // 1. General clean-up -// 2. Placeholders for other hash functions -// 3. Implement all suites (with internal found-trip test function) +// 2. Implement remainder of hashers +// 3. Implment KATs // 4. Separate into proper files // 5. Doc, of course! mod algs; @@ -26,7 +26,7 @@ const LEN2: u32 = 3; macro_rules! functionality { () => { use crate::types::{SlhDsaSig, SlhPrivateKey, SlhPublicKey}; - use generic_array::typenum::{Prod, Sum, U2, U3}; + //use generic_array::typenum::{Prod, Sum, U2, U3}; use rand_core::CryptoRngCore; /// blah @@ -82,9 +82,9 @@ macro_rules! functionality { /// TKTK #[cfg(feature = "slh_dsa_sha2_128s")] pub mod slh_dsa_sha2_128s { - use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len}; + use crate::hashers::sha2_cat_1::{f, h, h_msg, prf, prf_msg, t_l, t_len}; use crate::hashers::Hashers; - use generic_array::typenum::{U12, U14, U16, U30, U63, U7, U9}; + use generic_array::typenum::{Prod, Sum, U12, U14, U16, U2, U3, U30, U63, U7, U9}; type N = U16; type H = U63; @@ -106,17 +106,23 @@ pub mod slh_dsa_sha2_128s { /// TKTK #[cfg(feature = "slh_dsa_shake_128s")] pub mod slh_dsa_shake_128s { - const N: usize = 16; - const H: u32 = 63; - const D: u32 = 7; - const H_PRIME: u32 = 9; - const A: u32 = 12; - const K: u32 = 14; - const LGW: u32 = 4; - const M: u32 = 30; - const PK_LEN: usize = 32; + use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len}; + use crate::hashers::Hashers; + use generic_array::typenum::{Prod, Sum, U12, U14, U16, U2, U3, U30, U63, U7, U9}; + + type N = U16; + type H = U63; + type D = U7; + type HP = U9; + type A = U12; + type K = U14; + type M = U30; + type LEN = Sum, U3>; + //const PK_LEN: usize = 32; const SIG_LEN: usize = 7856; - const SK_LEN: usize = 0000; + //const SK_LEN: usize = 0000; + static HASHERS: Hashers = + Hashers:: { h_msg, prf, prf_msg, f, h, t_l, t_len }; functionality!(); } @@ -124,17 +130,23 @@ pub mod slh_dsa_shake_128s { /// TKTK #[cfg(feature = "slh_dsa_sha2_128f")] pub mod slh_dsa_sha2_128f { - const N: usize = 16; - const H: u32 = 66; - const D: u32 = 22; - const H_PRIME: u32 = 3; - const A: u32 = 6; - const K: u32 = 33; - const LGW: u32 = 4; - const M: u32 = 34; - const PK_LEN: usize = 32; + use crate::hashers::sha2_cat_1::{f, h, h_msg, prf, prf_msg, t_l, t_len}; + use crate::hashers::Hashers; + use generic_array::typenum::{Prod, Sum, U16, U2, U22, U3, U33, U34, U6, U66}; + + type N = U16; + type H = U66; + type D = U22; + type HP = U3; + type A = U6; + type K = U33; + type M = U34; + type LEN = Sum, U3>; + //const PK_LEN: usize = 32; const SIG_LEN: usize = 17088; - const SK_LEN: usize = 0000; + //const SK_LEN: usize = 0000; + static HASHERS: Hashers = + Hashers:: { h_msg, prf, prf_msg, f, h, t_l, t_len }; functionality!(); } @@ -142,17 +154,23 @@ pub mod slh_dsa_sha2_128f { /// TKTK #[cfg(feature = "slh_dsa_shake_128f")] pub mod slh_dsa_shake_128f { - const N: usize = 16; - const H: u32 = 66; - const D: u32 = 22; - const H_PRIME: u32 = 3; - const A: u32 = 6; - const K: u32 = 33; - const LGW: u32 = 4; - const M: u32 = 34; - const PK_LEN: usize = 32; + use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len}; + use crate::hashers::Hashers; + use generic_array::typenum::{Prod, Sum, U16, U2, U22, U3, U33, U34, U6, U66}; + + type N = U16; + type H = U66; + type D = U22; + type HP = U3; + type A = U6; + type K = U33; + type M = U34; + type LEN = Sum, U3>; + //const PK_LEN: usize = 32; const SIG_LEN: usize = 17088; - const SK_LEN: usize = 0000; + //const SK_LEN: usize = 0000; + static HASHERS: Hashers = + Hashers:: { h_msg, prf, prf_msg, f, h, t_l, t_len }; functionality!(); } @@ -160,17 +178,23 @@ pub mod slh_dsa_shake_128f { /// TKTK #[cfg(feature = "slh_dsa_sha2_192s")] pub mod slh_dsa_sha2_192s { - const N: usize = 24; - const H: u32 = 63; - const D: u32 = 7; - const H_PRIME: u32 = 9; - const A: u32 = 14; - const K: u32 = 17; - const LGW: u32 = 4; - const M: u32 = 39; - const PK_LEN: usize = 48; + use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l, t_len}; + use crate::hashers::Hashers; + use generic_array::typenum::{Prod, Sum, U14, U17, U2, U24, U3, U39, U63, U7, U9}; + + type N = U24; + type H = U63; + type D = U7; + type HP = U9; + type A = U14; + type K = U17; + type M = U39; + type LEN = Sum, U3>; + //const PK_LEN: usize = 32; const SIG_LEN: usize = 16224; - const SK_LEN: usize = 00000; + //const SK_LEN: usize = 0000; + static HASHERS: Hashers = + Hashers:: { h_msg, prf, prf_msg, f, h, t_l, t_len }; functionality!(); } @@ -178,17 +202,23 @@ pub mod slh_dsa_sha2_192s { /// TKTK #[cfg(feature = "slh_dsa_shake_192s")] pub mod slh_dsa_shake_192s { - const N: usize = 24; - const H: u32 = 63; - const D: u32 = 7; - const H_PRIME: u32 = 9; - const A: u32 = 14; - const K: u32 = 17; - const LGW: u32 = 4; - const M: u32 = 39; - const PK_LEN: usize = 48; + use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len}; + use crate::hashers::Hashers; + use generic_array::typenum::{Prod, Sum, U14, U17, U2, U24, U3, U39, U63, U7, U9}; + + type N = U24; + type H = U63; + type D = U7; + type HP = U9; + type A = U14; + type K = U17; + type M = U39; + type LEN = Sum, U3>; + //const PK_LEN: usize = 32; const SIG_LEN: usize = 16224; - const SK_LEN: usize = 00000; + //const SK_LEN: usize = 0000; + static HASHERS: Hashers = + Hashers:: { h_msg, prf, prf_msg, f, h, t_l, t_len }; functionality!(); } @@ -196,17 +226,23 @@ pub mod slh_dsa_shake_192s { /// TKTK #[cfg(feature = "slh_dsa_sha2_192f")] pub mod slh_dsa_sha2_192f { - const N: usize = 24; - const H: u32 = 66; - const D: u32 = 22; - const H_PRIME: u32 = 3; - const A: u32 = 8; - const K: u32 = 33; - const LGW: u32 = 4; - const M: u32 = 42; - const PK_LEN: usize = 48; + use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l, t_len}; + use crate::hashers::Hashers; + use generic_array::typenum::{Prod, Sum, U2, U22, U24, U3, U33, U42, U66, U8}; + + type N = U24; + type H = U66; + type D = U22; + type HP = U3; + type A = U8; + type K = U33; + type M = U42; + type LEN = Sum, U3>; + //const PK_LEN: usize = 32; const SIG_LEN: usize = 35664; - const SK_LEN: usize = 0000; + //const SK_LEN: usize = 0000; + static HASHERS: Hashers = + Hashers:: { h_msg, prf, prf_msg, f, h, t_l, t_len }; functionality!(); } @@ -214,17 +250,23 @@ pub mod slh_dsa_sha2_192f { /// TKTK #[cfg(feature = "slh_dsa_shake_192f")] pub mod slh_dsa_shake_192f { - const N: usize = 24; - const H: u32 = 66; - const D: u32 = 22; - const H_PRIME: u32 = 3; - const A: u32 = 8; - const K: u32 = 33; - const LGW: u32 = 4; - const M: u32 = 42; - const PK_LEN: usize = 48; + use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len}; + use crate::hashers::Hashers; + use generic_array::typenum::{Prod, Sum, U2, U22, U24, U3, U33, U42, U66, U8}; + + type N = U24; + type H = U66; + type D = U22; + type HP = U3; + type A = U8; + type K = U33; + type M = U42; + type LEN = Sum, U3>; + //const PK_LEN: usize = 32; const SIG_LEN: usize = 35664; - const SK_LEN: usize = 0000; + //const SK_LEN: usize = 0000; + static HASHERS: Hashers = + Hashers:: { h_msg, prf, prf_msg, f, h, t_l, t_len }; functionality!(); } @@ -232,17 +274,23 @@ pub mod slh_dsa_shake_192f { /// TKTK #[cfg(feature = "slh_dsa_sha2_256s")] pub mod slh_dsa_sha2_256s { - const N: usize = 32; - const H: u32 = 64; - const D: u32 = 8; - const H_PRIME: u32 = 8; - const A: u32 = 14; - const K: u32 = 22; - const LGW: u32 = 4; - const M: u32 = 47; - const PK_LEN: usize = 64; + use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l, t_len}; + use crate::hashers::Hashers; + use generic_array::typenum::{Prod, Sum, U14, U2, U22, U3, U32, U47, U64, U8}; + + type N = U32; + type H = U64; + type D = U8; + type HP = U8; + type A = U14; + type K = U22; + type M = U47; + type LEN = Sum, U3>; + //const PK_LEN: usize = 32; const SIG_LEN: usize = 29792; - const SK_LEN: usize = 0000; + //const SK_LEN: usize = 0000; + static HASHERS: Hashers = + Hashers:: { h_msg, prf, prf_msg, f, h, t_l, t_len }; functionality!(); } @@ -250,17 +298,23 @@ pub mod slh_dsa_sha2_256s { /// TKTK #[cfg(feature = "slh_dsa_shake_256s")] pub mod slh_dsa_shake_256s { - const N: usize = 32; - const H: u32 = 64; - const D: u32 = 8; - const H_PRIME: u32 = 8; - const A: u32 = 14; - const K: u32 = 22; - const LGW: u32 = 4; - const M: u32 = 47; - const PK_LEN: usize = 64; + use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len}; + use crate::hashers::Hashers; + use generic_array::typenum::{Prod, Sum, U14, U2, U22, U3, U32, U47, U64, U8}; + + type N = U32; + type H = U64; + type D = U8; + type HP = U8; + type A = U14; + type K = U22; + type M = U47; + type LEN = Sum, U3>; + //const PK_LEN: usize = 32; const SIG_LEN: usize = 29792; - const SK_LEN: usize = 0000; + //const SK_LEN: usize = 0000; + static HASHERS: Hashers = + Hashers:: { h_msg, prf, prf_msg, f, h, t_l, t_len }; functionality!(); } @@ -268,17 +322,23 @@ pub mod slh_dsa_shake_256s { /// TKTK #[cfg(feature = "slh_dsa_sha2_256f")] pub mod slh_dsa_sha2_256f { - const N: usize = 32; - const H: u32 = 68; - const D: u32 = 17; - const H_PRIME: u32 = 4; - const A: u32 = 9; - const K: u32 = 35; - const LGW: u32 = 4; - const M: u32 = 49; - const PK_LEN: usize = 64; + use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l, t_len}; + use crate::hashers::Hashers; + use generic_array::typenum::{Prod, Sum, U17, U2, U3, U32, U35, U4, U49, U68, U9}; + + type N = U32; + type H = U68; + type D = U17; + type HP = U4; + type A = U9; + type K = U35; + type M = U49; + type LEN = Sum, U3>; + //const PK_LEN: usize = 32; const SIG_LEN: usize = 49856; - const SK_LEN: usize = 0000; + //const SK_LEN: usize = 0000; + static HASHERS: Hashers = + Hashers:: { h_msg, prf, prf_msg, f, h, t_l, t_len }; functionality!(); } @@ -286,17 +346,23 @@ pub mod slh_dsa_sha2_256f { /// TKTK #[cfg(feature = "slh_dsa_shake_256f")] pub mod slh_dsa_shake_256f { - const N: usize = 32; - const H: u32 = 68; - const D: u32 = 17; - const H_PRIME: u32 = 4; - const A: u32 = 9; - const K: u32 = 35; - const LGW: u32 = 4; - const M: u32 = 49; - const PK_LEN: usize = 64; + use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len}; + use crate::hashers::Hashers; + use generic_array::typenum::{Prod, Sum, U17, U2, U3, U32, U35, U4, U49, U68, U9}; + + type N = U32; + type H = U68; + type D = U17; + type HP = U4; + type A = U9; + type K = U35; + type M = U49; + type LEN = Sum, U3>; + //const PK_LEN: usize = 32; const SIG_LEN: usize = 49856; - const SK_LEN: usize = 0000; + //const SK_LEN: usize = 0000; + static HASHERS: Hashers = + Hashers:: { h_msg, prf, prf_msg, f, h, t_l, t_len }; functionality!(); } diff --git a/src/test.rs b/src/test.rs index b8a83bc..babf2ec 100644 --- a/src/test.rs +++ b/src/test.rs @@ -40,7 +40,7 @@ mod tests { } - use crate::slh_dsa_sha2_128s::{slh_keygen_with_rng, slh_sign_with_rng, slh_verify}; + use crate::slh_dsa_shake_128s::{slh_keygen_with_rng, slh_sign_with_rng, slh_verify}; #[test] fn vector_debug() {