From e7ac076f1cd649b1133276a99f0a24cb74c4814d Mon Sep 17 00:00:00 2001 From: Sam Leonard Date: Mon, 29 Jul 2024 13:41:27 +0100 Subject: [PATCH 1/6] Update class module to xoflib --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4a8d27b..bf96258 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ use sha3::{ macro_rules! impl_sponge_shaker_classes { // hasher is tt so we can pick the right kind of methods to generate ($hasher:tt, $xof_reader:ident, $shaker_name:ident, $sponge_name:ident) => { - #[pyclass(module="xof")] + #[pyclass(module="xoflib")] #[doc=concat!(stringify!($shaker_name), " implements absorption and finalization for the ", stringify!($hasher), " XOF")] struct $shaker_name { hasher: $hasher, @@ -19,7 +19,7 @@ macro_rules! impl_sponge_shaker_classes { impl_sponge_shaker_classes!(@shaker_methods $hasher, $shaker_name, $sponge_name); - #[pyclass(module="xof")] + #[pyclass(module="xoflib")] #[doc=concat!(stringify!($sponge_name), " implements sponge expansion for the ", stringify!($hasher), " XOF")] struct $sponge_name { xof: XofReaderCoreWrapper<$xof_reader>, From 32203ca7a113df5d93fc4e17016806ef2e4d5edb Mon Sep 17 00:00:00 2001 From: tritoke Date: Mon, 29 Jul 2024 13:46:35 +0100 Subject: [PATCH 2/6] Add asm feature for sha3 --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 898cc1f..d83f836 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,5 +9,5 @@ name = "xoflib" crate-type = ["cdylib"] [dependencies] -pyo3 = { version = "0.21.0" } -sha3 = "0.10.8" +pyo3 = "0.21.0" +sha3 = { version = "0.10.8", features = ["asm"] } From 533c243f88fdee5c93cfd61df1a5b3e5a8c39a21 Mon Sep 17 00:00:00 2001 From: tritoke Date: Mon, 29 Jul 2024 13:48:33 +0100 Subject: [PATCH 3/6] Add class name argument to impl macro --- src/lib.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bf96258..93e6842 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,8 +10,8 @@ use sha3::{ macro_rules! impl_sponge_shaker_classes { // hasher is tt so we can pick the right kind of methods to generate - ($hasher:tt, $xof_reader:ident, $shaker_name:ident, $sponge_name:ident) => { - #[pyclass(module="xoflib")] + ($hasher:tt, $class_name:literal, $xof_reader:ident, $shaker_name:ident, $sponge_name:ident) => { + #[pyclass(module="xoflib", name=$class_name)] #[doc=concat!(stringify!($shaker_name), " implements absorption and finalization for the ", stringify!($hasher), " XOF")] struct $shaker_name { hasher: $hasher, @@ -140,16 +140,30 @@ macro_rules! impl_sponge_shaker_classes { }; } -impl_sponge_shaker_classes!(Shake128, Shake128ReaderCore, Shaker128, Sponge128); -impl_sponge_shaker_classes!(Shake256, Shake256ReaderCore, Shaker256, Sponge256); +impl_sponge_shaker_classes!( + Shake128, + "Shake128", + Shake128ReaderCore, + Shaker128, + Sponge128 +); +impl_sponge_shaker_classes!( + Shake256, + "Shake256", + Shake256ReaderCore, + Shaker256, + Sponge256 +); impl_sponge_shaker_classes!( TurboShake128, + "TurboShake128", TurboShake128ReaderCore, TurboShaker128, TurboSponge128 ); impl_sponge_shaker_classes!( TurboShake256, + "TurboShake256", TurboShake256ReaderCore, TurboShaker256, TurboSponge256 From de22a942a606477bc48825aaeee99dc67a206d94 Mon Sep 17 00:00:00 2001 From: tritoke Date: Mon, 29 Jul 2024 13:53:07 +0100 Subject: [PATCH 4/6] Name macro arguments to improve readability --- src/lib.rs | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 93e6842..bd3df4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ use sha3::{ macro_rules! impl_sponge_shaker_classes { // hasher is tt so we can pick the right kind of methods to generate - ($hasher:tt, $class_name:literal, $xof_reader:ident, $shaker_name:ident, $sponge_name:ident) => { + (hasher_name = $hasher:tt, pyclass_name = $class_name:literal, reader_name = $xof_reader:ident, rust_shaker_name = $shaker_name:ident, rust_sponge_name = $sponge_name:ident) => { #[pyclass(module="xoflib", name=$class_name)] #[doc=concat!(stringify!($shaker_name), " implements absorption and finalization for the ", stringify!($hasher), " XOF")] struct $shaker_name { @@ -140,33 +140,37 @@ macro_rules! impl_sponge_shaker_classes { }; } +#[rustfmt::skip] impl_sponge_shaker_classes!( - Shake128, - "Shake128", - Shake128ReaderCore, - Shaker128, - Sponge128 + hasher_name = Shake128, + pyclass_name = "Shake128", + reader_name = Shake128ReaderCore, + rust_shaker_name = Shaker128, + rust_sponge_name = Sponge128 ); +#[rustfmt::skip] impl_sponge_shaker_classes!( - Shake256, - "Shake256", - Shake256ReaderCore, - Shaker256, - Sponge256 + hasher_name = Shake256, + pyclass_name = "Shake256", + reader_name = Shake256ReaderCore, + rust_shaker_name = Shaker256, + rust_sponge_name = Sponge256 ); +#[rustfmt::skip] impl_sponge_shaker_classes!( - TurboShake128, - "TurboShake128", - TurboShake128ReaderCore, - TurboShaker128, - TurboSponge128 + hasher_name = TurboShake128, + pyclass_name = "TurboShake128", + reader_name = TurboShake128ReaderCore, + rust_shaker_name = TurboShaker128, + rust_sponge_name = TurboSponge128 ); +#[rustfmt::skip] impl_sponge_shaker_classes!( - TurboShake256, - "TurboShake256", - TurboShake256ReaderCore, - TurboShaker256, - TurboSponge256 + hasher_name = TurboShake256, + pyclass_name = "TurboShake256", + reader_name = TurboShake256ReaderCore, + rust_shaker_name = TurboShaker256, + rust_sponge_name = TurboSponge256 ); /// A Python package for the Shake extendable-output functions (XOFs): Shake128, From 6b0587be970bd1a2ff008deb01fa07de731e1ed5 Mon Sep 17 00:00:00 2001 From: tritoke Date: Mon, 29 Jul 2024 13:55:22 +0100 Subject: [PATCH 5/6] Update xoflib.pyi for new class names --- xoflib.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xoflib.pyi b/xoflib.pyi index 4c68be3..d134021 100644 --- a/xoflib.pyi +++ b/xoflib.pyi @@ -1,4 +1,4 @@ -class Shaker128: +class Shake128: def __init__(self, input_bytes: bytes | None = None): ... @@ -12,7 +12,7 @@ class Sponge128: def read(self, n: int) -> bytes: ... -class Shaker256: +class Shake256: def __init__(self, input_bytes: bytes | None = None): ... @@ -26,7 +26,7 @@ class Sponge256: def read(self, n: int) -> bytes: ... -class TurboShaker128: +class TurboShake128: def __init__(self, domain_sep: int, input_bytes: bytes | None = None): ... @@ -40,7 +40,7 @@ class TurboSponge128: def read(self, n: int) -> bytes: ... -class TurboShaker256: +class TurboShake256: def __init__(self, domain_sep: int, input_bytes: bytes | None = None): ... From 0af2a0bfd0fc38e7d10fede74412e3753b7ee018 Mon Sep 17 00:00:00 2001 From: tritoke Date: Mon, 29 Jul 2024 13:56:11 +0100 Subject: [PATCH 6/6] Update class names in tests --- tests/test_xoflib.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_xoflib.py b/tests/test_xoflib.py index a51c847..6ba03ce 100644 --- a/tests/test_xoflib.py +++ b/tests/test_xoflib.py @@ -1,5 +1,5 @@ from hashlib import shake_128, shake_256 -from xoflib import Shaker128, Shaker256 +from xoflib import Shake128, Shake256 import unittest @@ -18,9 +18,9 @@ def hashlib_test_many_calls(self, Shake, shake_hashlib): self.assertEqual(shake_hashlib(absorb_bytes).digest(l), output) def test_hashlib_shake128(self): - self.hashlib_test_long_calls(Shaker128, shake_128) - self.hashlib_test_many_calls(Shaker128, shake_128) + self.hashlib_test_long_calls(Shake128, shake_128) + self.hashlib_test_many_calls(Shake128, shake_128) def test_hashlib_shake256(self): - self.hashlib_test_long_calls(Shaker256, shake_256) - self.hashlib_test_many_calls(Shaker256, shake_256) + self.hashlib_test_long_calls(Shake256, shake_256) + self.hashlib_test_many_calls(Shake256, shake_256)