From 7f3c30d82ed82f6d1f83b655e51313d51203091b Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 31 Jan 2023 16:21:53 +1100 Subject: [PATCH 01/39] Initial split of crates --- .git-blame-ignore-revs | 0 Cargo.toml | 47 +++-------- codetable/Cargo.toml | 29 +++++++ {benches => codetable/benches}/multihash.rs | 12 +-- .../examples}/custom_table.rs | 5 +- {examples => codetable/examples}/manual_mh.rs | 3 +- {src => codetable/src}/hasher_impl.rs | 18 +---- src/multihash_impl.rs => codetable/src/lib.rs | 81 +++++++++++++------ {tests => codetable/tests}/lib.rs | 21 ++--- derive/Cargo.toml | 2 +- src/lib.rs | 28 +------ src/multihash.rs | 47 +---------- 12 files changed, 124 insertions(+), 169 deletions(-) create mode 100644 .git-blame-ignore-revs create mode 100644 codetable/Cargo.toml rename {benches => codetable/benches}/multihash.rs (94%) rename {examples => codetable/examples}/custom_table.rs (92%) rename {examples => codetable/examples}/manual_mh.rs (91%) rename {src => codetable/src}/hasher_impl.rs (94%) rename src/multihash_impl.rs => codetable/src/lib.rs (64%) rename {tests => codetable/tests}/lib.rs (96%) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 00000000..e69de29b diff --git a/Cargo.toml b/Cargo.toml index 4600b694..d767b023 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["derive", "."] +members = ["derive", ".", "codetable"] [package] name = "multihash" @@ -15,26 +15,17 @@ edition = "2021" rust-version = "1.59" [features] -default = ["std", "derive", "multihash-impl", "secure-hashes"] -std = ["unsigned-varint/std", "multihash-derive/std", "alloc"] -alloc = ["core2/alloc"] -multihash-impl = ["derive"] -derive = ["multihash-derive"] +default = ["std", "derive"] +std = ["unsigned-varint/std", "multihash-derive?/std", "alloc"] +alloc = [] +derive = ["dep:multihash-derive"] arb = ["quickcheck", "rand", "arbitrary"] -secure-hashes = ["blake2b", "blake2s", "blake3", "sha2", "sha3"] -scale-codec = ["parity-scale-codec"] -serde-codec = ["serde", "serde-big-array"] - -blake2b = ["blake2b_simd"] -blake2s = ["blake2s_simd"] -identity = [] -sha1 = ["digest", "sha-1"] -sha2 = ["digest", "sha-2"] -sha3 = ["digest", "sha-3"] -strobe = ["strobe-rs"] -ripemd = ["ripemd-rs"] +scale-codec = ["dep:parity-scale-codec"] +serde-codec = ["dep:serde", "dep:serde-big-array"] [dependencies] +arbitrary = {version = "1.1.0", optional = true } +core2 = { version = "0.4.0", default-features = false } parity-scale-codec = { version = "3.0.0", default-features = false, features = ["derive"], optional = true } quickcheck = { version = "1.0.3", optional = true } rand = { version = "0.8.5", optional = true, features = ["small_rng"] } @@ -42,29 +33,9 @@ serde = { version = "1.0.116", optional = true, default-features = false, featur serde-big-array = { version = "0.3.2", optional = true, features = ["const-generics"] } multihash-derive = { version = "0.8.0", path = "derive", default-features = false, optional = true } unsigned-varint = { version = "0.7.1", default-features = false } -arbitrary = {version = "1.1.0", optional = true } - -blake2b_simd = { version = "1.0.0", default-features = false, optional = true } -blake2s_simd = { version = "1.0.0", default-features = false, optional = true } -blake3 = { version = "1.2.0", default-features = false, optional = true } -digest = { version = "0.10.1", default-features = false, optional = true } -sha-1 = { version = "0.10.0", default-features = false, optional = true } -sha-2 = { version = "0.10.0", default-features = false, optional = true, package = "sha2" } -sha-3 = { version = "0.10.0", default-features = false, optional = true, package = "sha3" } -strobe-rs = { version = "0.7.0", default-features = false, optional = true } -ripemd-rs = { package = "ripemd", version = "0.1.1", optional = true} - -core2 = { version = "0.4.0", default-features = false } [dev-dependencies] -criterion = "0.3.3" hex = "0.4.2" serde_json = "1.0.58" quickcheck = "1.0.3" -rand = "0.8.5" arbitrary = "1.1.0" -multihash = { path = ".", features = ["sha1", "strobe"] } - -[[bench]] -name = "multihash" -harness = false diff --git a/codetable/Cargo.toml b/codetable/Cargo.toml new file mode 100644 index 00000000..86107080 --- /dev/null +++ b/codetable/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "multihash-codetable" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +blake2b_simd = { version = "1.0.0", default-features = false } +blake2s_simd = { version = "1.0.0", default-features = false } +blake3 = { version = "1.2.0", default-features = false } +digest = { version = "0.10.1", default-features = false } +sha-1 = { version = "0.10.0", default-features = false } +sha-2 = { version = "0.10.0", default-features = false, package = "sha2" } +sha-3 = { version = "0.10.0", default-features = false, package = "sha3" } +strobe-rs = { version = "0.7.0", default-features = false } +ripemd-rs = { package = "ripemd", version = "0.1.1"} +multihash-derive = { version = "0.8.0", path = "../derive", default-features = false } +multihash = { version = "0.18.0", path = "../" } + +[dev-dependencies] +hex = "0.4.2" +unsigned-varint = { version = "0.7.1", default-features = false } +criterion = "0.3.3" +rand = "0.8.5" + +[[bench]] +name = "multihash" +harness = false diff --git a/benches/multihash.rs b/codetable/benches/multihash.rs similarity index 94% rename from benches/multihash.rs rename to codetable/benches/multihash.rs index 0b75ea15..ee160d6c 100644 --- a/benches/multihash.rs +++ b/codetable/benches/multihash.rs @@ -1,11 +1,11 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use rand::Rng; - -use multihash::{ - Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3_256, Hasher, Keccak224, Keccak256, - Keccak384, Keccak512, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384, Sha3_512, - Strobe256, Strobe512, +use multihash::Hasher; +use multihash_codetable::{ + Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3_256, Keccak224, Keccak256, Keccak384, + Keccak512, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384, Sha3_512, Strobe256, + Strobe512, }; +use rand::Rng; macro_rules! group_digest { ($criterion:ident, $( $id:expr => $hash:ident, $input:expr)* ) => {{ diff --git a/examples/custom_table.rs b/codetable/examples/custom_table.rs similarity index 92% rename from examples/custom_table.rs rename to codetable/examples/custom_table.rs index a7f6a92f..32c62a72 100644 --- a/examples/custom_table.rs +++ b/codetable/examples/custom_table.rs @@ -1,7 +1,8 @@ use std::convert::TryFrom; use multihash::derive::Multihash; -use multihash::{Error, Hasher, MultihashDigest, MultihashGeneric, Sha2_256}; +use multihash::{Hasher, MultihashDigest as _}; +use multihash_codetable::Sha2_256; // You can implement a custom hasher. This is a SHA2 256-bit hasher that returns a hash that is // truncated to 160 bits. @@ -26,7 +27,7 @@ pub enum Code { #[mh(code = 0x12, hasher = Sha2_256Truncated20)] Sha2_256Truncated20, /// Example for using a hasher with a bit size that is not exported by default - #[mh(code = 0xb219, hasher = multihash::Blake2bHasher::<25>)] + #[mh(code = 0xb219, hasher = multihash_codetable::Blake2bHasher::<25>)] Blake2b200, } diff --git a/examples/manual_mh.rs b/codetable/examples/manual_mh.rs similarity index 91% rename from examples/manual_mh.rs rename to codetable/examples/manual_mh.rs index 95414b85..11f35f52 100644 --- a/examples/manual_mh.rs +++ b/codetable/examples/manual_mh.rs @@ -1,4 +1,5 @@ -use multihash::{Code, MultihashDigest}; +use multihash::MultihashDigest; +use multihash_codetable::Code; /// prefix/multihash generating tool to aid when adding new tests fn prefix_util() { diff --git a/src/hasher_impl.rs b/codetable/src/hasher_impl.rs similarity index 94% rename from src/hasher_impl.rs rename to codetable/src/hasher_impl.rs index 4e24439a..8fd204bd 100644 --- a/src/hasher_impl.rs +++ b/codetable/src/hasher_impl.rs @@ -1,11 +1,7 @@ -use crate::hasher::Hasher; +use multihash::Hasher; -#[cfg(feature = "std")] use std::io; -#[cfg(not(feature = "std"))] -use core2::io; - macro_rules! derive_write { ($name:ident) => { impl io::Write for $name { @@ -21,7 +17,6 @@ macro_rules! derive_write { }; } -#[cfg(any(feature = "blake2b", feature = "blake2s"))] macro_rules! derive_hasher_blake { ($module:ident, $name:ident) => { /// Multihash hasher. @@ -65,7 +60,6 @@ macro_rules! derive_hasher_blake { }; } -#[cfg(feature = "blake2b")] pub mod blake2b { use super::*; @@ -78,7 +72,6 @@ pub mod blake2b { pub type Blake2b512 = Blake2bHasher<64>; } -#[cfg(feature = "blake2s")] pub mod blake2s { use super::*; @@ -91,7 +84,6 @@ pub mod blake2s { pub type Blake2s256 = Blake2sHasher<32>; } -#[cfg(feature = "blake3")] pub mod blake3 { use super::*; @@ -143,7 +135,6 @@ pub mod blake3 { pub type Blake3_256 = Blake3Hasher<32>; } -#[cfg(feature = "digest")] macro_rules! derive_rustcrypto_hasher { ($module:ty, $name:ident, $size:expr) => { /// Multihash hasher. @@ -162,7 +153,7 @@ macro_rules! derive_rustcrypto_hasher { } } - impl $crate::hasher::Hasher for $name { + impl ::multihash::Hasher for $name { fn update(&mut self, input: &[u8]) { use digest::Digest; self.state.update(input) @@ -196,14 +187,12 @@ macro_rules! derive_rustcrypto_hasher { }; } -#[cfg(feature = "sha1")] pub mod sha1 { use super::*; derive_rustcrypto_hasher!(::sha1::Sha1, Sha1, 20); } -#[cfg(feature = "sha2")] pub mod sha2 { use super::*; @@ -211,7 +200,6 @@ pub mod sha2 { derive_rustcrypto_hasher!(sha_2::Sha512, Sha2_512, 64); } -#[cfg(feature = "sha3")] pub mod sha3 { use super::*; @@ -226,7 +214,6 @@ pub mod sha3 { derive_rustcrypto_hasher!(sha_3::Keccak512, Keccak512, 64); } -#[cfg(feature = "ripemd")] pub mod ripemd { use super::*; @@ -286,7 +273,6 @@ pub mod identity { pub type Identity256 = IdentityHasher<32>; } -#[cfg(feature = "strobe")] pub mod strobe { use super::*; use strobe_rs::{SecParam, Strobe}; diff --git a/src/multihash_impl.rs b/codetable/src/lib.rs similarity index 64% rename from src/multihash_impl.rs rename to codetable/src/lib.rs index 29face0b..52b60db7 100644 --- a/src/multihash_impl.rs +++ b/codetable/src/lib.rs @@ -1,4 +1,17 @@ -pub use multihash_derive::Multihash; +mod hasher_impl; + +use multihash_derive::Multihash; + +pub use crate::hasher_impl::blake2b::{Blake2b256, Blake2b512, Blake2bHasher}; +pub use crate::hasher_impl::blake2s::{Blake2s128, Blake2s256, Blake2sHasher}; +pub use crate::hasher_impl::blake3::{Blake3Hasher, Blake3_256}; +pub use crate::hasher_impl::identity::{Identity256, IdentityHasher}; +pub use crate::hasher_impl::ripemd::{Ripemd160, Ripemd256, Ripemd320}; +pub use crate::hasher_impl::sha1::Sha1; +pub use crate::hasher_impl::sha2::{Sha2_256, Sha2_512}; +pub use crate::hasher_impl::sha3::{Keccak224, Keccak256, Keccak384, Keccak512}; +pub use crate::hasher_impl::sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512}; +pub use crate::hasher_impl::strobe::{Strobe256, Strobe512, StrobeHasher}; /// Default (cryptographically secure) Multihash implementation. /// @@ -12,81 +25,61 @@ pub use multihash_derive::Multihash; #[mh(alloc_size = 64)] pub enum Code { /// SHA-256 (32-byte hash size) - #[cfg(feature = "sha2")] #[mh(code = 0x12, hasher = crate::Sha2_256)] Sha2_256, /// SHA-512 (64-byte hash size) - #[cfg(feature = "sha2")] #[mh(code = 0x13, hasher = crate::Sha2_512)] Sha2_512, /// SHA3-224 (28-byte hash size) - #[cfg(feature = "sha3")] #[mh(code = 0x17, hasher = crate::Sha3_224)] Sha3_224, /// SHA3-256 (32-byte hash size) - #[cfg(feature = "sha3")] #[mh(code = 0x16, hasher = crate::Sha3_256)] Sha3_256, /// SHA3-384 (48-byte hash size) - #[cfg(feature = "sha3")] #[mh(code = 0x15, hasher = crate::Sha3_384)] Sha3_384, /// SHA3-512 (64-byte hash size) - #[cfg(feature = "sha3")] #[mh(code = 0x14, hasher = crate::Sha3_512)] Sha3_512, /// Keccak-224 (28-byte hash size) - #[cfg(feature = "sha3")] #[mh(code = 0x1a, hasher = crate::Keccak224)] Keccak224, /// Keccak-256 (32-byte hash size) - #[cfg(feature = "sha3")] #[mh(code = 0x1b, hasher = crate::Keccak256)] Keccak256, /// Keccak-384 (48-byte hash size) - #[cfg(feature = "sha3")] #[mh(code = 0x1c, hasher = crate::Keccak384)] Keccak384, /// Keccak-512 (64-byte hash size) - #[cfg(feature = "sha3")] #[mh(code = 0x1d, hasher = crate::Keccak512)] Keccak512, /// BLAKE2b-256 (32-byte hash size) - #[cfg(feature = "blake2b")] #[mh(code = 0xb220, hasher = crate::Blake2b256)] Blake2b256, /// BLAKE2b-512 (64-byte hash size) - #[cfg(feature = "blake2b")] #[mh(code = 0xb240, hasher = crate::Blake2b512)] Blake2b512, /// BLAKE2s-128 (16-byte hash size) - #[cfg(feature = "blake2s")] #[mh(code = 0xb250, hasher = crate::Blake2s128)] Blake2s128, /// BLAKE2s-256 (32-byte hash size) - #[cfg(feature = "blake2s")] #[mh(code = 0xb260, hasher = crate::Blake2s256)] Blake2s256, /// BLAKE3-256 (32-byte hash size) - #[cfg(feature = "blake3")] #[mh(code = 0x1e, hasher = crate::Blake3_256)] Blake3_256, /// RIPEMD-160 (20-byte hash size) - #[cfg(feature = "ripemd")] #[mh(code = 0x1053, hasher = crate::Ripemd160)] Ripemd160, /// RIPEMD-256 (32-byte hash size) - #[cfg(feature = "ripemd")] #[mh(code = 0x1054, hasher = crate::Ripemd256)] Ripemd256, /// RIPEMD-320 (40-byte hash size) - #[cfg(feature = "ripemd")] #[mh(code = 0x1055, hasher = crate::Ripemd320)] Ripemd320, - // The following hashes are not cryptographically secure hashes and are not enabled by default /// Identity hash (max. 64 bytes) - #[cfg(feature = "identity")] #[mh(code = 0x00, hasher = crate::IdentityHasher::<64>)] Identity, } @@ -94,9 +87,9 @@ pub enum Code { #[cfg(test)] mod tests { use super::*; - use crate::hasher::Hasher; use crate::hasher_impl::sha3::{Sha3_256, Sha3_512}; - use crate::multihash::MultihashDigest; + use multihash::MultihashDigest; + use multihash::{Hasher, MultihashGeneric}; #[test] fn test_hasher_256() { @@ -123,4 +116,46 @@ mod tests { assert_eq!(hash.digest(), digest); assert_eq!(hash, hash2); } + + #[test] + fn roundtrip() { + let hash = Code::Sha2_256.digest(b"hello world"); + let mut buf = [0u8; 35]; + let written = hash.write(&mut buf[..]).unwrap(); + let hash2 = MultihashGeneric::<32>::read(&buf[..]).unwrap(); + assert_eq!(hash, hash2); + assert_eq!(hash.encoded_len(), written); + } + + #[test] + fn test_truncate_down() { + let hash = Code::Sha2_256.digest(b"hello world"); + let small = hash.truncate(20); + assert_eq!(small.size(), 20); + } + + #[test] + fn test_truncate_up() { + let hash = Code::Sha2_256.digest(b"hello world"); + let small = hash.truncate(100); + assert_eq!(small.size(), 32); + } + + #[test] + fn test_resize_fits() { + let hash = Code::Sha2_256.digest(b"hello world"); + let _: MultihashGeneric<32> = hash.resize().unwrap(); + } + + #[test] + fn test_resize_up() { + let hash = Code::Sha2_256.digest(b"hello world"); + let _: MultihashGeneric<100> = hash.resize().unwrap(); + } + + #[test] + fn test_resize_truncate() { + let hash = Code::Sha2_256.digest(b"hello world"); + hash.resize::<20>().unwrap_err(); + } } diff --git a/tests/lib.rs b/codetable/tests/lib.rs similarity index 96% rename from tests/lib.rs rename to codetable/tests/lib.rs index 906d3d00..8d181b27 100644 --- a/tests/lib.rs +++ b/codetable/tests/lib.rs @@ -1,15 +1,13 @@ use std::io::{Cursor, Write}; -use multihash::{ - derive::Multihash, Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3_256, Hasher, - Identity256, Keccak224, Keccak256, Keccak384, Keccak512, MultihashDigest, Sha1, Sha2_256, - Sha2_512, Sha3_224, Sha3_256, Sha3_384, Sha3_512, Strobe256, Strobe512, +use multihash::{Hasher, MultihashDigest}; +use multihash_codetable::{ + Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3_256, Identity256, Keccak224, Keccak256, + Keccak384, Keccak512, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384, Sha3_512, + Strobe256, Strobe512, }; -#[cfg(feature = "ripemd")] -use multihash::{Ripemd160, Ripemd256, Ripemd320}; - -#[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, multihash_derive::Multihash, PartialEq)] #[mh(alloc_size = 64)] pub enum Code { #[mh(code = 0x00, hasher = Identity256)] @@ -388,7 +386,8 @@ fn multihash_errors() { #[test] fn blak3_non_default_digest() { - use multihash::Blake3Hasher; + use multihash::MultihashDigest; + use multihash_codetable::Blake3Hasher; const DIGEST_SIZE: usize = 16; pub struct ContentHasher(Blake3Hasher); @@ -404,7 +403,9 @@ fn blak3_non_default_digest() { } fn finish(&mut self) -> ContentHash { - let hash = multihash::Code::Blake3_256.wrap(self.0.finalize()).unwrap(); + let hash = multihash_codetable::Code::Blake3_256 + .wrap(self.0.finalize()) + .unwrap(); let resized_hash = hash.resize::().unwrap(); let mut content = ContentHash([0u8; DIGEST_SIZE]); diff --git a/derive/Cargo.toml b/derive/Cargo.toml index 4f3aa927..b59e8ba7 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -25,4 +25,4 @@ std = [] [dev-dependencies] pretty_assertions = "1.0.0" -multihash = { path = "..", default-features = false, features = ["derive", "sha2"] } +#multihash = { path = "..", default-features = false, features = ["derive", "sha2"] } diff --git a/src/lib.rs b/src/lib.rs index d561c27b..06a107eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,36 +59,10 @@ extern crate alloc; mod arb; mod error; mod hasher; -mod hasher_impl; mod multihash; -#[cfg(feature = "multihash-impl")] -mod multihash_impl; pub use crate::error::{Error, Result}; -pub use crate::hasher::Hasher; pub use crate::multihash::{Multihash as MultihashGeneric, MultihashDigest}; +pub use hasher::Hasher; #[cfg(feature = "derive")] pub use multihash_derive as derive; - -#[cfg(feature = "multihash-impl")] -pub use crate::multihash_impl::{Code, Multihash}; - -#[cfg(feature = "blake2b")] -pub use crate::hasher_impl::blake2b::{Blake2b256, Blake2b512, Blake2bHasher}; -#[cfg(feature = "blake2s")] -pub use crate::hasher_impl::blake2s::{Blake2s128, Blake2s256, Blake2sHasher}; -#[cfg(feature = "blake3")] -pub use crate::hasher_impl::blake3::{Blake3Hasher, Blake3_256}; -pub use crate::hasher_impl::identity::{Identity256, IdentityHasher}; -#[cfg(feature = "ripemd")] -pub use crate::hasher_impl::ripemd::{Ripemd160, Ripemd256, Ripemd320}; -#[cfg(feature = "sha1")] -pub use crate::hasher_impl::sha1::Sha1; -#[cfg(feature = "sha2")] -pub use crate::hasher_impl::sha2::{Sha2_256, Sha2_512}; -#[cfg(feature = "sha3")] -pub use crate::hasher_impl::sha3::{Keccak224, Keccak256, Keccak384, Keccak512}; -#[cfg(feature = "sha3")] -pub use crate::hasher_impl::sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512}; -#[cfg(feature = "strobe")] -pub use crate::hasher_impl::strobe::{Strobe256, Strobe512, StrobeHasher}; diff --git a/src/multihash.rs b/src/multihash.rs index e9aac9c5..d4c0d672 100644 --- a/src/multihash.rs +++ b/src/multihash.rs @@ -374,63 +374,20 @@ pub(crate) fn read_u64(mut r: R) -> Result { #[cfg(test)] mod tests { use super::*; - use crate::multihash_impl::Code; - - #[test] - fn roundtrip() { - let hash = Code::Sha2_256.digest(b"hello world"); - let mut buf = [0u8; 35]; - let written = hash.write(&mut buf[..]).unwrap(); - let hash2 = Multihash::<32>::read(&buf[..]).unwrap(); - assert_eq!(hash, hash2); - assert_eq!(hash.encoded_len(), written); - } - - #[test] - fn test_truncate_down() { - let hash = Code::Sha2_256.digest(b"hello world"); - let small = hash.truncate(20); - assert_eq!(small.size(), 20); - } - - #[test] - fn test_truncate_up() { - let hash = Code::Sha2_256.digest(b"hello world"); - let small = hash.truncate(100); - assert_eq!(small.size(), 32); - } - - #[test] - fn test_resize_fits() { - let hash = Code::Sha2_256.digest(b"hello world"); - let _: Multihash<32> = hash.resize().unwrap(); - } - - #[test] - fn test_resize_up() { - let hash = Code::Sha2_256.digest(b"hello world"); - let _: Multihash<100> = hash.resize().unwrap(); - } - - #[test] - fn test_resize_truncate() { - let hash = Code::Sha2_256.digest(b"hello world"); - hash.resize::<20>().unwrap_err(); - } #[test] #[cfg(feature = "scale-codec")] fn test_scale() { use parity_scale_codec::{Decode, Encode}; - let mh1 = Code::Sha2_256.digest(b"hello world"); + let mh1 = Multihash::<32>::wrap(0, b"hello world").unwrap(); // println!("mh1: code = {}, size = {}, digest = {:?}", mh1.code(), mh1.size(), mh1.digest()); let mh1_bytes = mh1.encode(); // println!("Multihash<32>: {}", hex::encode(&mh1_bytes)); let mh2: Multihash<32> = Decode::decode(&mut &mh1_bytes[..]).unwrap(); assert_eq!(mh1, mh2); - let mh3: Multihash<64> = Code::Sha2_256.digest(b"hello world"); + let mh3 = Multihash::<64>::wrap(0, b"hello world").unwrap(); // println!("mh3: code = {}, size = {}, digest = {:?}", mh3.code(), mh3.size(), mh3.digest()); let mh3_bytes = mh3.encode(); // println!("Multihash<64>: {}", hex::encode(&mh3_bytes)); From 393207903019e9d6eecb623d6d196b2ec4ddddc5 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 31 Jan 2023 16:24:57 +1100 Subject: [PATCH 02/39] Deprecate export of `Multihash` --- codetable/src/lib.rs | 8 ++++---- src/arb.rs | 19 +++++++++---------- src/lib.rs | 6 +++++- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/codetable/src/lib.rs b/codetable/src/lib.rs index 52b60db7..4669e04a 100644 --- a/codetable/src/lib.rs +++ b/codetable/src/lib.rs @@ -89,7 +89,7 @@ mod tests { use super::*; use crate::hasher_impl::sha3::{Sha3_256, Sha3_512}; use multihash::MultihashDigest; - use multihash::{Hasher, MultihashGeneric}; + use multihash::{Hasher, Multihash}; #[test] fn test_hasher_256() { @@ -122,7 +122,7 @@ mod tests { let hash = Code::Sha2_256.digest(b"hello world"); let mut buf = [0u8; 35]; let written = hash.write(&mut buf[..]).unwrap(); - let hash2 = MultihashGeneric::<32>::read(&buf[..]).unwrap(); + let hash2 = Multihash::<32>::read(&buf[..]).unwrap(); assert_eq!(hash, hash2); assert_eq!(hash.encoded_len(), written); } @@ -144,13 +144,13 @@ mod tests { #[test] fn test_resize_fits() { let hash = Code::Sha2_256.digest(b"hello world"); - let _: MultihashGeneric<32> = hash.resize().unwrap(); + let _: Multihash<32> = hash.resize().unwrap(); } #[test] fn test_resize_up() { let hash = Code::Sha2_256.digest(b"hello world"); - let _: MultihashGeneric<100> = hash.resize().unwrap(); + let _: Multihash<100> = hash.resize().unwrap(); } #[test] diff --git a/src/arb.rs b/src/arb.rs index 2768a778..a879388e 100644 --- a/src/arb.rs +++ b/src/arb.rs @@ -5,12 +5,11 @@ use rand::{ }; use arbitrary::{size_hint, Unstructured}; - -use crate::MultihashGeneric; +use crate::Multihash; /// Generates a random valid multihash. -impl quickcheck::Arbitrary for MultihashGeneric { - fn arbitrary(g: &mut Gen) -> MultihashGeneric { +impl quickcheck::Arbitrary for Multihash { + fn arbitrary(g: &mut Gen) -> Multihash { // In real world lower multihash codes are more likely to happen, hence distribute them // with bias towards smaller values. let weights = [128, 64, 32, 16, 8, 4, 2, 1]; @@ -32,11 +31,11 @@ impl quickcheck::Arbitrary for MultihashGeneric { let size = rng.gen_range(0..S); let mut data = [0; S]; rng.fill_bytes(&mut data); - MultihashGeneric::wrap(code, &data[..size]).unwrap() + Multihash::wrap(code, &data[..size]).unwrap() } } -impl<'a, const S: usize> arbitrary::Arbitrary<'a> for MultihashGeneric { +impl<'a, const S: usize> arbitrary::Arbitrary<'a> for Multihash { fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result { let mut code = 0u64; let mut len_choice = u.arbitrary::()? | 1; @@ -59,7 +58,7 @@ impl<'a, const S: usize> arbitrary::Arbitrary<'a> for MultihashGeneric { let size = u.int_in_range(0..=S)?; let data = u.bytes(size)?; - Ok(MultihashGeneric::wrap(code, data).unwrap()) + Ok(Multihash::wrap(code, data).unwrap()) } fn size_hint(depth: usize) -> (usize, Option) { @@ -69,15 +68,15 @@ impl<'a, const S: usize> arbitrary::Arbitrary<'a> for MultihashGeneric { #[cfg(test)] mod tests { - use crate::MultihashGeneric; + use crate::Multihash; use arbitrary::{Arbitrary, Unstructured}; #[test] fn arbitrary() { let mut u = Unstructured::new(&[2, 4, 13, 5, 6, 7, 8, 9, 6]); - let mh = as Arbitrary>::arbitrary(&mut u).unwrap(); - let mh2 = MultihashGeneric::<16>::wrap(1037, &[6, 7, 8, 9, 6]).unwrap(); + let mh = as Arbitrary>::arbitrary(&mut u).unwrap(); + let mh2 = Multihash::<16>::wrap(1037, &[6, 7, 8, 9, 6]).unwrap(); assert_eq!(mh, mh2); } } diff --git a/src/lib.rs b/src/lib.rs index 06a107eb..743960ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,11 @@ mod hasher; mod multihash; pub use crate::error::{Error, Result}; -pub use crate::multihash::{Multihash as MultihashGeneric, MultihashDigest}; +pub use crate::multihash::{Multihash, MultihashDigest}; pub use hasher::Hasher; #[cfg(feature = "derive")] pub use multihash_derive as derive; + +/// Deprecated type-alias for the [`Multihash`] type. +#[deprecated(since = "0.18.0", note = "Use `multihash::Multihash instead.")] +pub type MultihashGeneric = Multihash; From 8ca30717821c1cd7f35c85f651691929b4853b6e Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 31 Jan 2023 16:28:17 +1100 Subject: [PATCH 03/39] inline synstructure macro --- derive/src/lib.rs | 17 +++++++++++++---- src/arb.rs | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/derive/src/lib.rs b/derive/src/lib.rs index a9c633ec..47ed1819 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -40,9 +40,18 @@ mod utils; use proc_macro::TokenStream; use proc_macro_error::proc_macro_error; -use synstructure::{decl_derive, Structure}; +use synstructure::macros::{parse, DeriveInput}; +use synstructure::{MacroResult, Structure}; -decl_derive!([Multihash, attributes(mh)] => #[proc_macro_error] multihash); -fn multihash(s: Structure) -> TokenStream { - multihash::multihash(s).into() +#[proc_macro_derive(Multihash, attributes(mh))] +#[allow(non_snake_case)] +#[proc_macro_error] +pub fn Multihash(i: TokenStream) -> TokenStream { + match parse::(i) { + Ok(p) => match Structure::try_new(&p) { + Ok(s) => multihash::multihash(s).into_stream(), + Err(e) => e.to_compile_error().into(), + }, + Err(e) => e.to_compile_error().into(), + } } diff --git a/src/arb.rs b/src/arb.rs index a879388e..c279484a 100644 --- a/src/arb.rs +++ b/src/arb.rs @@ -4,8 +4,8 @@ use rand::{ Rng, RngCore, SeedableRng, }; -use arbitrary::{size_hint, Unstructured}; use crate::Multihash; +use arbitrary::{size_hint, Unstructured}; /// Generates a random valid multihash. impl quickcheck::Arbitrary for Multihash { From 91f96c6702e74e8dd4c0a07e91d0f7c7e8b1d1e3 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 31 Jan 2023 16:29:45 +1100 Subject: [PATCH 04/39] Deprecate old macro name in favor of new one --- codetable/examples/custom_table.rs | 4 ++-- codetable/src/lib.rs | 4 ++-- codetable/tests/lib.rs | 2 +- derive/src/lib.rs | 9 +++++++++ 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/codetable/examples/custom_table.rs b/codetable/examples/custom_table.rs index 32c62a72..99287d17 100644 --- a/codetable/examples/custom_table.rs +++ b/codetable/examples/custom_table.rs @@ -1,8 +1,8 @@ use std::convert::TryFrom; -use multihash::derive::Multihash; use multihash::{Hasher, MultihashDigest as _}; use multihash_codetable::Sha2_256; +use multihash_derive::MultihashDigest; // You can implement a custom hasher. This is a SHA2 256-bit hasher that returns a hash that is // truncated to 160 bits. @@ -20,7 +20,7 @@ impl Hasher for Sha2_256Truncated20 { } } -#[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, MultihashDigest, PartialEq)] #[mh(alloc_size = 64)] pub enum Code { /// Example for using a custom hasher which returns truncated hashes diff --git a/codetable/src/lib.rs b/codetable/src/lib.rs index 4669e04a..24abbcd2 100644 --- a/codetable/src/lib.rs +++ b/codetable/src/lib.rs @@ -1,6 +1,6 @@ mod hasher_impl; -use multihash_derive::Multihash; +use multihash_derive::MultihashDigest; pub use crate::hasher_impl::blake2b::{Blake2b256, Blake2b512, Blake2bHasher}; pub use crate::hasher_impl::blake2s::{Blake2s128, Blake2s256, Blake2sHasher}; @@ -21,7 +21,7 @@ pub use crate::hasher_impl::strobe::{Strobe256, Strobe512, StrobeHasher}; /// [`Multihash` derive]: crate::derive #[cfg_attr(feature = "serde-codec", derive(serde::Deserialize))] #[cfg_attr(feature = "serde-codec", derive(serde::Serialize))] -#[derive(Copy, Clone, Debug, Eq, Multihash, PartialEq)] +#[derive(Copy, Clone, Debug, Eq, MultihashDigest, PartialEq)] #[mh(alloc_size = 64)] pub enum Code { /// SHA-256 (32-byte hash size) diff --git a/codetable/tests/lib.rs b/codetable/tests/lib.rs index 8d181b27..7e388573 100644 --- a/codetable/tests/lib.rs +++ b/codetable/tests/lib.rs @@ -7,7 +7,7 @@ use multihash_codetable::{ Strobe256, Strobe512, }; -#[derive(Clone, Copy, Debug, Eq, multihash_derive::Multihash, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, multihash_derive::MultihashDigest, PartialEq)] #[mh(alloc_size = 64)] pub enum Code { #[mh(code = 0x00, hasher = Identity256)] diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 47ed1819..4c990ee3 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -46,6 +46,7 @@ use synstructure::{MacroResult, Structure}; #[proc_macro_derive(Multihash, attributes(mh))] #[allow(non_snake_case)] #[proc_macro_error] +#[deprecated(since = "0.8.1", note = "Use `MultihashDigest` derive instead.")] pub fn Multihash(i: TokenStream) -> TokenStream { match parse::(i) { Ok(p) => match Structure::try_new(&p) { @@ -55,3 +56,11 @@ pub fn Multihash(i: TokenStream) -> TokenStream { Err(e) => e.to_compile_error().into(), } } + +#[proc_macro_derive(MultihashDigest, attributes(mh))] +#[allow(non_snake_case)] +#[proc_macro_error] +pub fn MultihashDigest(i: TokenStream) -> TokenStream { + #[allow(deprecated)] + Multihash(i) +} From d0e82159500c5be18e0bfd3194cf2986b52adeee Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 31 Jan 2023 16:31:24 +1100 Subject: [PATCH 05/39] Remove `derive` feature flag We don't want this in our dependency tree. --- Cargo.toml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d767b023..94e892ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,10 +15,9 @@ edition = "2021" rust-version = "1.59" [features] -default = ["std", "derive"] -std = ["unsigned-varint/std", "multihash-derive?/std", "alloc"] +default = ["std"] +std = ["unsigned-varint/std", "alloc"] alloc = [] -derive = ["dep:multihash-derive"] arb = ["quickcheck", "rand", "arbitrary"] scale-codec = ["dep:parity-scale-codec"] serde-codec = ["dep:serde", "dep:serde-big-array"] @@ -31,7 +30,6 @@ quickcheck = { version = "1.0.3", optional = true } rand = { version = "0.8.5", optional = true, features = ["small_rng"] } serde = { version = "1.0.116", optional = true, default-features = false, features = ["derive"] } serde-big-array = { version = "0.3.2", optional = true, features = ["const-generics"] } -multihash-derive = { version = "0.8.0", path = "derive", default-features = false, optional = true } unsigned-varint = { version = "0.7.1", default-features = false } [dev-dependencies] From bcf8135fcb521955e2df9015ded9256b8bc5c503 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 31 Jan 2023 16:36:13 +1100 Subject: [PATCH 06/39] Remove unnecessary `pub` qualifier --- src/multihash.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/multihash.rs b/src/multihash.rs index d4c0d672..bf5de625 100644 --- a/src/multihash.rs +++ b/src/multihash.rs @@ -309,7 +309,7 @@ impl parity_scale_codec::Decode for Multihash { } /// Writes the multihash to a byte stream. -pub fn write_multihash(mut w: W, code: u64, size: u8, digest: &[u8]) -> Result +fn write_multihash(mut w: W, code: u64, size: u8, digest: &[u8]) -> Result where W: io::Write, { @@ -334,7 +334,7 @@ where /// maximum/allocated size of the digest. /// /// Currently the maximum size for a digest is 255 bytes. -pub fn read_multihash(mut r: R) -> Result<(u64, u8, [u8; S]), Error> +fn read_multihash(mut r: R) -> Result<(u64, u8, [u8; S]), Error> where R: io::Read, { From 9cee44ea6d912c072edb3b904ad26e4669004f80 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 31 Jan 2023 16:37:31 +1100 Subject: [PATCH 07/39] Remove unnecessary feature-gated import --- src/multihash.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/multihash.rs b/src/multihash.rs index bf5de625..52fd93fc 100644 --- a/src/multihash.rs +++ b/src/multihash.rs @@ -5,8 +5,6 @@ use core::convert::TryFrom; use core::convert::TryInto; use core::fmt::Debug; -#[cfg(feature = "serde-codec")] -use serde_big_array::BigArray; use unsigned_varint::encode as varint_encode; @@ -82,7 +80,7 @@ pub struct Multihash { /// The actual size of the digest in bytes (not the allocated size). size: u8, /// The digest. - #[cfg_attr(feature = "serde-codec", serde(with = "BigArray"))] + #[cfg_attr(feature = "serde-codec", serde(with = "serde_big_array::BigArray"))] digest: [u8; S], } From c1e5355f3e82b7b9811b89c52a0f73c80f190e15 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 31 Jan 2023 16:44:29 +1100 Subject: [PATCH 08/39] Deprecate `serde-codec` --- Cargo.toml | 3 ++- src/multihash.rs | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 94e892ec..3f187ce2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,8 @@ std = ["unsigned-varint/std", "alloc"] alloc = [] arb = ["quickcheck", "rand", "arbitrary"] scale-codec = ["dep:parity-scale-codec"] -serde-codec = ["dep:serde", "dep:serde-big-array"] +serde-codec = ["serde"] # Deprecated, don't use. +serde = ["dep:serde", "dep:serde-big-array"] [dependencies] arbitrary = {version = "1.1.0", optional = true } diff --git a/src/multihash.rs b/src/multihash.rs index 52fd93fc..3b9d4d6d 100644 --- a/src/multihash.rs +++ b/src/multihash.rs @@ -71,8 +71,8 @@ pub trait MultihashDigest: /// assert_eq!(mh.size(), 32); /// assert_eq!(mh.digest(), &digest_bytes[2..]); /// ``` -#[cfg_attr(feature = "serde-codec", derive(serde::Deserialize))] -#[cfg_attr(feature = "serde-codec", derive(serde::Serialize))] +#[cfg_attr(any(feature = "serde-codec", feature = "serde"), derive(serde::Deserialize))] +#[cfg_attr(any(feature = "serde-codec", feature = "serde"), derive(serde::Serialize))] #[derive(Clone, Copy, Debug, Eq, Ord, PartialOrd)] pub struct Multihash { /// The code of the Multihash. @@ -80,7 +80,7 @@ pub struct Multihash { /// The actual size of the digest in bytes (not the allocated size). size: u8, /// The digest. - #[cfg_attr(feature = "serde-codec", serde(with = "serde_big_array::BigArray"))] + #[cfg_attr(any(feature = "serde-codec", feature = "serde"), serde(with = "serde_big_array::BigArray"))] digest: [u8; S], } @@ -396,7 +396,7 @@ mod tests { } #[test] - #[cfg(feature = "serde-codec")] + #[cfg(any(feature = "serde-codec", feature = "serde"))] fn test_serde() { let mh = Multihash::<32>::default(); let bytes = serde_json::to_string(&mh).unwrap(); From 2dbeded4a0c7234834a7190cdec443bc7ac8d2a2 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 31 Jan 2023 17:09:58 +1100 Subject: [PATCH 09/39] Remove git blame file --- .git-blame-ignore-revs | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .git-blame-ignore-revs diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs deleted file mode 100644 index e69de29b..00000000 From f3a4c8bbb2b6134d4101e0b5183bbf5af8489b37 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 16:07:04 +0100 Subject: [PATCH 10/39] Run rustfmt --- src/multihash.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/multihash.rs b/src/multihash.rs index 3b9d4d6d..7fc88cee 100644 --- a/src/multihash.rs +++ b/src/multihash.rs @@ -71,8 +71,14 @@ pub trait MultihashDigest: /// assert_eq!(mh.size(), 32); /// assert_eq!(mh.digest(), &digest_bytes[2..]); /// ``` -#[cfg_attr(any(feature = "serde-codec", feature = "serde"), derive(serde::Deserialize))] -#[cfg_attr(any(feature = "serde-codec", feature = "serde"), derive(serde::Serialize))] +#[cfg_attr( + any(feature = "serde-codec", feature = "serde"), + derive(serde::Deserialize) +)] +#[cfg_attr( + any(feature = "serde-codec", feature = "serde"), + derive(serde::Serialize) +)] #[derive(Clone, Copy, Debug, Eq, Ord, PartialOrd)] pub struct Multihash { /// The code of the Multihash. @@ -80,7 +86,10 @@ pub struct Multihash { /// The actual size of the digest in bytes (not the allocated size). size: u8, /// The digest. - #[cfg_attr(any(feature = "serde-codec", feature = "serde"), serde(with = "serde_big_array::BigArray"))] + #[cfg_attr( + any(feature = "serde-codec", feature = "serde"), + serde(with = "serde_big_array::BigArray") + )] digest: [u8; S], } From d9aa346df609f562c4dcfb42aaa2cf210f9136e2 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 16:07:13 +0100 Subject: [PATCH 11/39] Reduce number of `cfg`s --- src/error.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/error.rs b/src/error.rs index ddf25fbd..9d03a989 100644 --- a/src/error.rs +++ b/src/error.rs @@ -4,8 +4,6 @@ use core2::{error::Error as StdError, io::Error as IoError}; use std::{error::Error as StdError, io::Error as IoError}; use unsigned_varint::decode::Error as DecodeError; -#[cfg(feature = "std")] -use unsigned_varint::io::ReadError; /// Multihash error. #[derive(Debug)] @@ -40,11 +38,11 @@ impl From for Error { } #[cfg(feature = "std")] -impl From for Error { - fn from(err: ReadError) -> Self { +impl From for Error { + fn from(err: unsigned_varint::io::ReadError) -> Self { match err { - ReadError::Io(err) => Self::Io(err), - ReadError::Decode(err) => Self::Varint(err), + unsigned_varint::io::ReadError::Io(err) => Self::Io(err), + unsigned_varint::io::ReadError::Decode(err) => Self::Varint(err), _ => unreachable!(), } } From 2bc410145a1d846662804a69905c8e0751b1f878 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 16:08:12 +0100 Subject: [PATCH 12/39] Remove `Result` type Not common enough to variant a type alias. --- src/error.rs | 3 --- src/lib.rs | 6 +++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index 9d03a989..da4db488 100644 --- a/src/error.rs +++ b/src/error.rs @@ -47,6 +47,3 @@ impl From for Error { } } } - -/// Multihash result. -pub type Result = core::result::Result; diff --git a/src/lib.rs b/src/lib.rs index 743960ee..61152594 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,7 +61,11 @@ mod error; mod hasher; mod multihash; -pub use crate::error::{Error, Result}; +/// Multihash result. +#[deprecated(note = "Use `Result` instead")] +pub type Result = core::result::Result; + +pub use crate::error::Error; pub use crate::multihash::{Multihash, MultihashDigest}; pub use hasher::Hasher; #[cfg(feature = "derive")] From 2a9694005d5b36a5e8eb93efe9ea1e6de23952f9 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 16:26:31 +0100 Subject: [PATCH 13/39] Make `Error` opaque --- derive/src/multihash.rs | 2 +- src/error.rs | 92 +++++++++++++++++++++++++++++++---------- src/lib.rs | 1 + src/multihash.rs | 32 ++++++++------ 4 files changed, 93 insertions(+), 34 deletions(-) diff --git a/derive/src/multihash.rs b/derive/src/multihash.rs index d03dcec2..b964e633 100644 --- a/derive/src/multihash.rs +++ b/derive/src/multihash.rs @@ -252,7 +252,7 @@ pub fn multihash(s: Structure) -> TokenStream { fn try_from(code: u64) -> Result { match code { #(#code_from_u64,)* - _ => Err(#mh_crate::Error::UnsupportedCode(code)) + _ => Err(#mh_crate::Error::unsupported_code(code)) } } } diff --git a/src/error.rs b/src/error.rs index da4db488..815ac775 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,15 +1,56 @@ #[cfg(not(feature = "std"))] -use core2::{error::Error as StdError, io::Error as IoError}; +use core2::{error::Error as StdError, io}; #[cfg(feature = "std")] -use std::{error::Error as StdError, io::Error as IoError}; +use std::{error::Error as StdError, io}; use unsigned_varint::decode::Error as DecodeError; -/// Multihash error. +/// Opaque error struct for operations involving a [`Multihash`](crate::Multihash). #[derive(Debug)] -pub enum Error { +pub struct Error { + kind: Kind, +} + +impl Error { + /// The specified code is not supported by code table. + /// FIXME: This should not be in our public API because it is only needed by the custom derive which we have no knowledge of in this crate. + pub fn unsupported_code(code: u64) -> Self { + Self { + kind: Kind::UnsupportedCode(code), + } + } + + pub(crate) const fn invalid_size(size: u64) -> Self { + Self { + kind: Kind::InvalidSize(size), + } + } + + #[cfg(not(feature = "std"))] + pub(crate) const fn insufficient_varint_bytes() -> Self { + Self { + kind: Kind::Varint(unsigned_varint::decode::Error::Insufficient), + } + } + + #[cfg(not(feature = "std"))] + pub(crate) const fn varint_overflow() -> Self { + Self { + kind: Kind::Varint(unsigned_varint::decode::Error::Overflow), + } + } +} + +impl core::fmt::Display for Error { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { + self.kind.fmt(f) + } +} + +#[derive(Debug)] +enum Kind { /// Io error. - Io(IoError), + Io(io::Error), /// Unsupported multihash code. UnsupportedCode(u64), /// Invalid multihash size. @@ -18,7 +59,24 @@ pub enum Error { Varint(DecodeError), } -impl core::fmt::Display for Error { +#[cfg(feature = "std")] +pub(crate) fn unsigned_variant_to_multihash_error(err: unsigned_varint::io::ReadError) -> Error { + match err { + unsigned_varint::io::ReadError::Io(err) => io_to_multihash_error(err), + unsigned_varint::io::ReadError::Decode(err) => Error { + kind: Kind::Varint(err), + }, + _ => unreachable!(), + } +} + +pub(crate) fn io_to_multihash_error(err: io::Error) -> Error { + Error { + kind: Kind::Io(err), + } +} + +impl core::fmt::Display for Kind { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { match self { Self::Io(err) => write!(f, "{err}"), @@ -29,21 +87,13 @@ impl core::fmt::Display for Error { } } -impl StdError for Error {} - -impl From for Error { - fn from(err: IoError) -> Self { - Self::Io(err) - } -} - -#[cfg(feature = "std")] -impl From for Error { - fn from(err: unsigned_varint::io::ReadError) -> Self { - match err { - unsigned_varint::io::ReadError::Io(err) => Self::Io(err), - unsigned_varint::io::ReadError::Decode(err) => Self::Varint(err), - _ => unreachable!(), +impl StdError for Error { + fn source(&self) -> Option<&(dyn StdError + 'static)> { + match &self.kind { + Kind::Io(inner) => Some(inner), + Kind::UnsupportedCode(_) => None, + Kind::InvalidSize(_) => None, + Kind::Varint(_) => None, // FIXME: Does not implement `core2::Error`. } } } diff --git a/src/lib.rs b/src/lib.rs index 61152594..42314b32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,6 +54,7 @@ #[cfg(feature = "alloc")] extern crate alloc; +extern crate core; #[cfg(any(test, feature = "arb"))] mod arb; diff --git a/src/multihash.rs b/src/multihash.rs index 7fc88cee..86491784 100644 --- a/src/multihash.rs +++ b/src/multihash.rs @@ -107,7 +107,7 @@ impl Multihash { /// Wraps the digest in a multihash. pub const fn wrap(code: u64, input_digest: &[u8]) -> Result { if input_digest.len() > S { - return Err(Error::InvalidSize(input_digest.len() as _)); + return Err(Error::invalid_size(input_digest.len() as _)); } let size = input_digest.len(); let mut digest = [0; S]; @@ -158,7 +158,7 @@ impl Multihash { let result = Self::read(&mut bytes)?; // There were more bytes supplied than read if !bytes.is_empty() { - return Err(Error::InvalidSize(bytes.len().try_into().expect( + return Err(Error::invalid_size(bytes.len().try_into().expect( "Currently the maximum size is 255, therefore always fits into usize", ))); } @@ -221,7 +221,7 @@ impl Multihash { pub fn resize(&self) -> Result, Error> { let size = self.size as usize; if size > R { - return Err(Error::InvalidSize(self.size as u64)); + return Err(Error::invalid_size(self.size as u64)); } let mut mh = Multihash { code: self.code, @@ -328,9 +328,12 @@ where let written = code.len() + size.len() + digest.len(); - w.write_all(code)?; - w.write_all(size)?; - w.write_all(digest)?; + w.write_all(code) + .map_err(crate::error::io_to_multihash_error)?; + w.write_all(size) + .map_err(crate::error::io_to_multihash_error)?; + w.write_all(digest) + .map_err(crate::error::io_to_multihash_error)?; Ok(written) } @@ -349,16 +352,19 @@ where let size = read_u64(&mut r)?; if size > S as u64 || size > u8::MAX as u64 { - return Err(Error::InvalidSize(size)); + return Err(Error::invalid_size(size)); } let mut digest = [0; S]; - r.read_exact(&mut digest[..size as usize])?; + r.read_exact(&mut digest[..size as usize]) + .map_err(crate::error::io_to_multihash_error)?; Ok((code, size as u8, digest)) } #[cfg(feature = "std")] -pub(crate) use unsigned_varint::io::read_u64; +pub(crate) fn read_u64(r: R) -> Result { + unsigned_varint::io::read_u64(r).map_err(crate::error::unsigned_variant_to_multihash_error) +} /// Reads 64 bits from a byte array into a u64 /// Adapted from unsigned-varint's generated read_u64 function at @@ -368,14 +374,16 @@ pub(crate) fn read_u64(mut r: R) -> Result { use unsigned_varint::decode; let mut b = varint_encode::u64_buffer(); for i in 0..b.len() { - let n = r.read(&mut (b[i..i + 1]))?; + let n = r + .read(&mut (b[i..i + 1])) + .map_err(crate::error::io_to_multihash_error)?; if n == 0 { - return Err(Error::Varint(decode::Error::Insufficient)); + return Err(Error::insufficient_varint_bytes()); } else if decode::is_last(b[i]) { return Ok(decode::u64(&b[..=i]).unwrap().0); } } - Err(Error::Varint(decode::Error::Overflow)) + Err(Error::varint_overflow()) } #[cfg(test)] From aab78df1fbc161ce89d0634dfc3af212deb3feb4 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 17:06:50 +0100 Subject: [PATCH 14/39] Introduce `multihash-derive-impl` crate --- Cargo.toml | 3 +- codetable/Cargo.toml | 1 - codetable/benches/multihash.rs | 2 +- codetable/examples/custom_table.rs | 3 +- codetable/examples/manual_mh.rs | 2 +- codetable/src/hasher_impl.rs | 4 +- codetable/src/lib.rs | 4 +- codetable/tests/lib.rs | 4 +- derive-impl/Cargo.toml | 28 ++++++++ derive-impl/src/lib.rs | 66 +++++++++++++++++++ {derive => derive-impl}/src/multihash.rs | 6 +- {derive => derive-impl}/src/utils.rs | 0 derive/Cargo.toml | 19 +----- {src => derive/src}/hasher.rs | 0 derive/src/lib.rs | 83 +++++++++++++++++------- src/error.rs | 12 ---- src/lib.rs | 6 +- src/multihash.rs | 37 ----------- 18 files changed, 170 insertions(+), 110 deletions(-) create mode 100644 derive-impl/Cargo.toml create mode 100644 derive-impl/src/lib.rs rename {derive => derive-impl}/src/multihash.rs (98%) rename {derive => derive-impl}/src/utils.rs (100%) rename {src => derive/src}/hasher.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 3f187ce2..dd1dc405 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["derive", ".", "codetable"] +members = ["derive", "derive-impl", ".", "codetable"] [package] name = "multihash" @@ -38,3 +38,4 @@ hex = "0.4.2" serde_json = "1.0.58" quickcheck = "1.0.3" arbitrary = "1.1.0" +rand = { version = "0.8.5", features = ["small_rng"] } diff --git a/codetable/Cargo.toml b/codetable/Cargo.toml index 86107080..25e53018 100644 --- a/codetable/Cargo.toml +++ b/codetable/Cargo.toml @@ -16,7 +16,6 @@ sha-3 = { version = "0.10.0", default-features = false, package = "sha3" } strobe-rs = { version = "0.7.0", default-features = false } ripemd-rs = { package = "ripemd", version = "0.1.1"} multihash-derive = { version = "0.8.0", path = "../derive", default-features = false } -multihash = { version = "0.18.0", path = "../" } [dev-dependencies] hex = "0.4.2" diff --git a/codetable/benches/multihash.rs b/codetable/benches/multihash.rs index ee160d6c..5d964a66 100644 --- a/codetable/benches/multihash.rs +++ b/codetable/benches/multihash.rs @@ -1,10 +1,10 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use multihash::Hasher; use multihash_codetable::{ Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3_256, Keccak224, Keccak256, Keccak384, Keccak512, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384, Sha3_512, Strobe256, Strobe512, }; +use multihash_derive::Hasher; use rand::Rng; macro_rules! group_digest { diff --git a/codetable/examples/custom_table.rs b/codetable/examples/custom_table.rs index 99287d17..40235d0d 100644 --- a/codetable/examples/custom_table.rs +++ b/codetable/examples/custom_table.rs @@ -1,8 +1,7 @@ use std::convert::TryFrom; -use multihash::{Hasher, MultihashDigest as _}; use multihash_codetable::Sha2_256; -use multihash_derive::MultihashDigest; +use multihash_derive::{Hasher, MultihashDigest}; // You can implement a custom hasher. This is a SHA2 256-bit hasher that returns a hash that is // truncated to 160 bits. diff --git a/codetable/examples/manual_mh.rs b/codetable/examples/manual_mh.rs index 11f35f52..c3905591 100644 --- a/codetable/examples/manual_mh.rs +++ b/codetable/examples/manual_mh.rs @@ -1,5 +1,5 @@ -use multihash::MultihashDigest; use multihash_codetable::Code; +use multihash_derive::MultihashDigest; /// prefix/multihash generating tool to aid when adding new tests fn prefix_util() { diff --git a/codetable/src/hasher_impl.rs b/codetable/src/hasher_impl.rs index 8fd204bd..5c67523a 100644 --- a/codetable/src/hasher_impl.rs +++ b/codetable/src/hasher_impl.rs @@ -1,4 +1,4 @@ -use multihash::Hasher; +use multihash_derive::Hasher; use std::io; @@ -153,7 +153,7 @@ macro_rules! derive_rustcrypto_hasher { } } - impl ::multihash::Hasher for $name { + impl ::multihash_derive::Hasher for $name { fn update(&mut self, input: &[u8]) { use digest::Digest; self.state.update(input) diff --git a/codetable/src/lib.rs b/codetable/src/lib.rs index 24abbcd2..d9329350 100644 --- a/codetable/src/lib.rs +++ b/codetable/src/lib.rs @@ -88,8 +88,8 @@ pub enum Code { mod tests { use super::*; use crate::hasher_impl::sha3::{Sha3_256, Sha3_512}; - use multihash::MultihashDigest; - use multihash::{Hasher, Multihash}; + use multihash_derive::MultihashDigest; + use multihash_derive::{Hasher, Multihash}; #[test] fn test_hasher_256() { diff --git a/codetable/tests/lib.rs b/codetable/tests/lib.rs index 7e388573..8e3f1e5f 100644 --- a/codetable/tests/lib.rs +++ b/codetable/tests/lib.rs @@ -1,11 +1,11 @@ use std::io::{Cursor, Write}; -use multihash::{Hasher, MultihashDigest}; use multihash_codetable::{ Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3_256, Identity256, Keccak224, Keccak256, Keccak384, Keccak512, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384, Sha3_512, Strobe256, Strobe512, }; +use multihash_derive::{Hasher, MultihashDigest}; #[derive(Clone, Copy, Debug, Eq, multihash_derive::MultihashDigest, PartialEq)] #[mh(alloc_size = 64)] @@ -386,8 +386,8 @@ fn multihash_errors() { #[test] fn blak3_non_default_digest() { - use multihash::MultihashDigest; use multihash_codetable::Blake3Hasher; + use multihash_derive::MultihashDigest; const DIGEST_SIZE: usize = 16; pub struct ContentHasher(Blake3Hasher); diff --git a/derive-impl/Cargo.toml b/derive-impl/Cargo.toml new file mode 100644 index 00000000..da935b8b --- /dev/null +++ b/derive-impl/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "multihash-derive-impl" +version = "0.1.0" +authors = ["David Craven "] +edition = "2018" +description = "Proc macro for deriving custom multihash tables." +license = "MIT" +repository = "https://github.com/multiformats/multihash" +resolver = "2" + +[lib] +proc-macro = true + +[dependencies] +proc-macro2 = { version = "1.0.24", features = ["span-locations"] } +proc-macro-crate = "~1.1.0" +proc-macro-error = "1.0.4" +quote = "1.0.7" +syn = "1.0.42" +synstructure = "0.12.4" + +[features] +default = ["std"] +std = [] + +[dev-dependencies] +pretty_assertions = "1.0.0" +#multihash = { path = "..", default-features = false, features = ["derive", "sha2"] } diff --git a/derive-impl/src/lib.rs b/derive-impl/src/lib.rs new file mode 100644 index 00000000..4c990ee3 --- /dev/null +++ b/derive-impl/src/lib.rs @@ -0,0 +1,66 @@ +//! This proc macro derives a custom Multihash code table from a list of hashers. It also +//! generates a public type called `Multihash` which corresponds to the specified `alloc_size`. +//! +//! The digests are stack allocated with a fixed size. That size needs to be big enough to hold any +//! of the specified hash digests. This cannot be determined reliably on compile-time, hence it +//! needs to set manually via the `alloc_size` attribute. Also you might want to set it to bigger +//! sizes then necessarily needed for backwards/forward compatibility. +//! +//! If you set `#mh(alloc_size = …)` to a too low value, you will get compiler errors. Please note +//! the the sizes are checked only on a syntactic level and *not* on the type level. This means +//! that digest need to have a size const generic, which is a valid `usize`, for example `32` or +//! `64`. +//! +//! You can disable those compiler errors with setting the `no_alloc_size_errors` attribute. This +//! can be useful if you e.g. have specified type aliases for your hash digests and you are sure +//! you use the correct value for `alloc_size`. +//! +//! # Example +//! +//! ``` +//! use multihash::derive::Multihash; +//! use multihash::MultihashDigest; +//! +//! #[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)] +//! #[mh(alloc_size = 64)] +//! pub enum Code { +//! #[mh(code = 0x01, hasher = multihash::Sha2_256)] +//! Foo, +//! #[mh(code = 0x02, hasher = multihash::Sha2_512)] +//! Bar, +//! } +//! +//! let hash = Code::Foo.digest(b"hello world!"); +//! println!("{:02x?}", hash); +//! ``` +extern crate proc_macro; + +mod multihash; +mod utils; + +use proc_macro::TokenStream; +use proc_macro_error::proc_macro_error; +use synstructure::macros::{parse, DeriveInput}; +use synstructure::{MacroResult, Structure}; + +#[proc_macro_derive(Multihash, attributes(mh))] +#[allow(non_snake_case)] +#[proc_macro_error] +#[deprecated(since = "0.8.1", note = "Use `MultihashDigest` derive instead.")] +pub fn Multihash(i: TokenStream) -> TokenStream { + match parse::(i) { + Ok(p) => match Structure::try_new(&p) { + Ok(s) => multihash::multihash(s).into_stream(), + Err(e) => e.to_compile_error().into(), + }, + Err(e) => e.to_compile_error().into(), + } +} + +#[proc_macro_derive(MultihashDigest, attributes(mh))] +#[allow(non_snake_case)] +#[proc_macro_error] +pub fn MultihashDigest(i: TokenStream) -> TokenStream { + #[allow(deprecated)] + Multihash(i) +} diff --git a/derive/src/multihash.rs b/derive-impl/src/multihash.rs similarity index 98% rename from derive/src/multihash.rs rename to derive-impl/src/multihash.rs index b964e633..237c57d0 100644 --- a/derive/src/multihash.rs +++ b/derive-impl/src/multihash.rs @@ -198,7 +198,7 @@ fn error_code_duplicates(hashes: &[Hash]) { struct ParseError(Span); pub fn multihash(s: Structure) -> TokenStream { - let mh_crate = match utils::use_crate("multihash") { + let mh_crate = match utils::use_crate("multihash-derive") { Ok(ident) => ident, Err(e) => { let err = syn::Error::new(Span::call_site(), e).to_compile_error(); @@ -247,12 +247,12 @@ pub fn multihash(s: Structure) -> TokenStream { } impl core::convert::TryFrom for #code_enum { - type Error = #mh_crate::Error; + type Error = #mh_crate::UnsupportedCode; fn try_from(code: u64) -> Result { match code { #(#code_from_u64,)* - _ => Err(#mh_crate::Error::unsupported_code(code)) + _ => Err(#mh_crate::UnsupportedCode(code)) } } } diff --git a/derive/src/utils.rs b/derive-impl/src/utils.rs similarity index 100% rename from derive/src/utils.rs rename to derive-impl/src/utils.rs diff --git a/derive/Cargo.toml b/derive/Cargo.toml index b59e8ba7..d2172fa2 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -8,21 +8,6 @@ license = "MIT" repository = "https://github.com/multiformats/multihash" resolver = "2" -[lib] -proc-macro = true - [dependencies] -proc-macro2 = { version = "1.0.24", features = ["span-locations"] } -proc-macro-crate = "~1.1.0" -proc-macro-error = "1.0.4" -quote = "1.0.7" -syn = "1.0.42" -synstructure = "0.12.4" - -[features] -default = ["std"] -std = [] - -[dev-dependencies] -pretty_assertions = "1.0.0" -#multihash = { path = "..", default-features = false, features = ["derive", "sha2"] } +multihash-derive-impl = { version = "0.1.0", path = "../derive-impl" } +multihash = { version = "0.18.0", path = "../" } diff --git a/src/hasher.rs b/derive/src/hasher.rs similarity index 100% rename from src/hasher.rs rename to derive/src/hasher.rs diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 4c990ee3..74c7560e 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -33,34 +33,69 @@ //! let hash = Code::Foo.digest(b"hello world!"); //! println!("{:02x?}", hash); //! ``` -extern crate proc_macro; -mod multihash; -mod utils; +mod hasher; -use proc_macro::TokenStream; -use proc_macro_error::proc_macro_error; -use synstructure::macros::{parse, DeriveInput}; -use synstructure::{MacroResult, Structure}; +use std::convert::TryFrom; +use std::fmt; -#[proc_macro_derive(Multihash, attributes(mh))] -#[allow(non_snake_case)] -#[proc_macro_error] -#[deprecated(since = "0.8.1", note = "Use `MultihashDigest` derive instead.")] -pub fn Multihash(i: TokenStream) -> TokenStream { - match parse::(i) { - Ok(p) => match Structure::try_new(&p) { - Ok(s) => multihash::multihash(s).into_stream(), - Err(e) => e.to_compile_error().into(), - }, - Err(e) => e.to_compile_error().into(), +pub use hasher::Hasher; +pub use multihash_derive_impl::MultihashDigest; + +pub use multihash::*; + +/// The given code is not supported by this codetable. +#[derive(Debug)] +pub struct UnsupportedCode(pub u64); + +impl fmt::Display for UnsupportedCode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "the code {} is not supported by this codetable", self.0) } } -#[proc_macro_derive(MultihashDigest, attributes(mh))] -#[allow(non_snake_case)] -#[proc_macro_error] -pub fn MultihashDigest(i: TokenStream) -> TokenStream { - #[allow(deprecated)] - Multihash(i) +impl std::error::Error for UnsupportedCode {} + +/// Trait that implements hashing. +/// +/// It is usually implemented by a custom code table enum that derives the [`Multihash` derive]. +/// +/// [`Multihash` derive]: crate::derive +pub trait MultihashDigest: + TryFrom + + Into + + Send + + Sync + + Unpin + + Copy + + Eq + + fmt::Debug + + 'static +{ + /// Calculate the hash of some input data. + /// + /// # Example + /// + /// ``` + /// // `Code` implements `MultihashDigest` + /// use multihash::{Code, MultihashDigest}; + /// + /// let hash = Code::Sha3_256.digest(b"Hello world!"); + /// println!("{:02x?}", hash); + /// ``` + fn digest(&self, input: &[u8]) -> Multihash; + + /// Create a multihash from an existing multihash digest. + /// + /// # Example + /// + /// ``` + /// use multihash::{Code, Hasher, MultihashDigest, Sha3_256}; + /// + /// let mut hasher = Sha3_256::default(); + /// hasher.update(b"Hello world!"); + /// let hash = Code::Sha3_256.wrap(&hasher.finalize()).unwrap(); + /// println!("{:02x?}", hash); + /// ``` + fn wrap(&self, digest: &[u8]) -> std::result::Result, multihash::Error>; } diff --git a/src/error.rs b/src/error.rs index 815ac775..f8df7bc6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -12,14 +12,6 @@ pub struct Error { } impl Error { - /// The specified code is not supported by code table. - /// FIXME: This should not be in our public API because it is only needed by the custom derive which we have no knowledge of in this crate. - pub fn unsupported_code(code: u64) -> Self { - Self { - kind: Kind::UnsupportedCode(code), - } - } - pub(crate) const fn invalid_size(size: u64) -> Self { Self { kind: Kind::InvalidSize(size), @@ -51,8 +43,6 @@ impl core::fmt::Display for Error { enum Kind { /// Io error. Io(io::Error), - /// Unsupported multihash code. - UnsupportedCode(u64), /// Invalid multihash size. InvalidSize(u64), /// Invalid varint. @@ -80,7 +70,6 @@ impl core::fmt::Display for Kind { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { match self { Self::Io(err) => write!(f, "{err}"), - Self::UnsupportedCode(code) => write!(f, "Unsupported multihash code {code}."), Self::InvalidSize(size) => write!(f, "Invalid multihash size {size}."), Self::Varint(err) => write!(f, "{err}"), } @@ -91,7 +80,6 @@ impl StdError for Error { fn source(&self) -> Option<&(dyn StdError + 'static)> { match &self.kind { Kind::Io(inner) => Some(inner), - Kind::UnsupportedCode(_) => None, Kind::InvalidSize(_) => None, Kind::Varint(_) => None, // FIXME: Does not implement `core2::Error`. } diff --git a/src/lib.rs b/src/lib.rs index 42314b32..7ebe7dfd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,7 +59,6 @@ extern crate core; #[cfg(any(test, feature = "arb"))] mod arb; mod error; -mod hasher; mod multihash; /// Multihash result. @@ -67,10 +66,7 @@ mod multihash; pub type Result = core::result::Result; pub use crate::error::Error; -pub use crate::multihash::{Multihash, MultihashDigest}; -pub use hasher::Hasher; -#[cfg(feature = "derive")] -pub use multihash_derive as derive; +pub use crate::multihash::Multihash; /// Deprecated type-alias for the [`Multihash`] type. #[deprecated(since = "0.18.0", note = "Use `multihash::Multihash instead.")] diff --git a/src/multihash.rs b/src/multihash.rs index 86491784..d90d1bdd 100644 --- a/src/multihash.rs +++ b/src/multihash.rs @@ -1,7 +1,6 @@ use crate::Error; #[cfg(feature = "alloc")] use alloc::vec::Vec; -use core::convert::TryFrom; use core::convert::TryInto; use core::fmt::Debug; @@ -14,42 +13,6 @@ use std::io; #[cfg(not(feature = "std"))] use core2::io; -/// Trait that implements hashing. -/// -/// It is usually implemented by a custom code table enum that derives the [`Multihash` derive]. -/// -/// [`Multihash` derive]: crate::derive -pub trait MultihashDigest: - TryFrom + Into + Send + Sync + Unpin + Copy + Eq + Debug + 'static -{ - /// Calculate the hash of some input data. - /// - /// # Example - /// - /// ``` - /// // `Code` implements `MultihashDigest` - /// use multihash::{Code, MultihashDigest}; - /// - /// let hash = Code::Sha3_256.digest(b"Hello world!"); - /// println!("{:02x?}", hash); - /// ``` - fn digest(&self, input: &[u8]) -> Multihash; - - /// Create a multihash from an existing multihash digest. - /// - /// # Example - /// - /// ``` - /// use multihash::{Code, Hasher, MultihashDigest, Sha3_256}; - /// - /// let mut hasher = Sha3_256::default(); - /// hasher.update(b"Hello world!"); - /// let hash = Code::Sha3_256.wrap(&hasher.finalize()).unwrap(); - /// println!("{:02x?}", hash); - /// ``` - fn wrap(&self, digest: &[u8]) -> Result, Error>; -} - /// A Multihash instance that only supports the basic functionality and no hashing. /// /// With this Multihash implementation you can operate on Multihashes in a generic way, but From e2e2229aeef4cf30da94249f4bf72ead433cc722 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 17:16:29 +0100 Subject: [PATCH 15/39] Fix clippy lint --- src/multihash.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/multihash.rs b/src/multihash.rs index d90d1bdd..09483d93 100644 --- a/src/multihash.rs +++ b/src/multihash.rs @@ -220,7 +220,7 @@ impl Multihash { } // Don't hash the whole allocated space, but just the actual digest -#[allow(clippy::derive_hash_xor_eq)] +#[allow(clippy::derived_hash_with_manual_eq)] impl core::hash::Hash for Multihash { fn hash(&self, state: &mut T) { self.code.hash(state); From c32ce92f205a3d65933c282b51b4bfe08e5932de Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 17:16:52 +0100 Subject: [PATCH 16/39] Only build multihash for no_std --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 23d582f9..03eaa79d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -73,7 +73,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: build - args: --no-default-features --workspace --target thumbv6m-none-eabi + args: --no-default-features --package multihash --target thumbv6m-none-eabi coverage: name: Code Coverage From cb0d72869f9a7f19525e966f7d973658bf49b065 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 17:30:57 +0100 Subject: [PATCH 17/39] Update docs --- derive-impl/Cargo.toml | 2 +- derive-impl/src/lib.rs | 40 ++++++------------------------------ derive/Cargo.toml | 1 - derive/src/lib.rs | 36 ++++++++++++++++++++++++--------- src/lib.rs | 46 ++++++++---------------------------------- 5 files changed, 41 insertions(+), 84 deletions(-) diff --git a/derive-impl/Cargo.toml b/derive-impl/Cargo.toml index da935b8b..503c24e9 100644 --- a/derive-impl/Cargo.toml +++ b/derive-impl/Cargo.toml @@ -3,7 +3,7 @@ name = "multihash-derive-impl" version = "0.1.0" authors = ["David Craven "] edition = "2018" -description = "Proc macro for deriving custom multihash tables." +description = "Internal proc-macro crate for the MultihashDigest derive" license = "MIT" repository = "https://github.com/multiformats/multihash" resolver = "2" diff --git a/derive-impl/src/lib.rs b/derive-impl/src/lib.rs index 4c990ee3..7fd1c3c4 100644 --- a/derive-impl/src/lib.rs +++ b/derive-impl/src/lib.rs @@ -1,38 +1,9 @@ -//! This proc macro derives a custom Multihash code table from a list of hashers. It also -//! generates a public type called `Multihash` which corresponds to the specified `alloc_size`. +//! This is an internal crate that implements the actual `MultihashDigest` derive. //! -//! The digests are stack allocated with a fixed size. That size needs to be big enough to hold any -//! of the specified hash digests. This cannot be determined reliably on compile-time, hence it -//! needs to set manually via the `alloc_size` attribute. Also you might want to set it to bigger -//! sizes then necessarily needed for backwards/forward compatibility. -//! -//! If you set `#mh(alloc_size = …)` to a too low value, you will get compiler errors. Please note -//! the the sizes are checked only on a syntactic level and *not* on the type level. This means -//! that digest need to have a size const generic, which is a valid `usize`, for example `32` or -//! `64`. -//! -//! You can disable those compiler errors with setting the `no_alloc_size_errors` attribute. This -//! can be useful if you e.g. have specified type aliases for your hash digests and you are sure -//! you use the correct value for `alloc_size`. -//! -//! # Example -//! -//! ``` -//! use multihash::derive::Multihash; -//! use multihash::MultihashDigest; -//! -//! #[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)] -//! #[mh(alloc_size = 64)] -//! pub enum Code { -//! #[mh(code = 0x01, hasher = multihash::Sha2_256)] -//! Foo, -//! #[mh(code = 0x02, hasher = multihash::Sha2_512)] -//! Bar, -//! } -//! -//! let hash = Code::Foo.digest(b"hello world!"); -//! println!("{:02x?}", hash); -//! ``` +//! The `multihash-derive` crate acts as a facade and defines additional symbols that our derive depends on. +//! For example, the actual trait that we are deriving `MultihashDigest`, as well as the `Hasher` trait and +//! the `UnsupportedCode` error type. + extern crate proc_macro; mod multihash; @@ -57,6 +28,7 @@ pub fn Multihash(i: TokenStream) -> TokenStream { } } +/// Custom derive for the `MultihashDigest` trait. #[proc_macro_derive(MultihashDigest, attributes(mh))] #[allow(non_snake_case)] #[proc_macro_error] diff --git a/derive/Cargo.toml b/derive/Cargo.toml index d2172fa2..94d81548 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -1,7 +1,6 @@ [package] name = "multihash-derive" version = "0.8.1" -authors = ["David Craven "] edition = "2018" description = "Proc macro for deriving custom multihash tables." license = "MIT" diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 74c7560e..3aaf05ec 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -1,3 +1,5 @@ +//! A procedural macro for custom Multihash code tables. +//! //! This proc macro derives a custom Multihash code table from a list of hashers. It also //! generates a public type called `Multihash` which corresponds to the specified `alloc_size`. //! @@ -18,19 +20,34 @@ //! # Example //! //! ``` -//! use multihash::derive::Multihash; -//! use multihash::MultihashDigest; +//! use multihash_derive::{Hasher, MultihashDigest}; +//! +//! struct FooHasher; +//! +//! impl Hasher for FooHasher { +//! // Implement hasher ... +//! # fn update(&mut self, input: &[u8]) { +//! # +//! # } +//! # +//! # fn finalize(&mut self) -> &[u8] { +//! # &[] +//! # } +//! # +//! # fn reset(&mut self) { +//! # +//! # } +//! } //! -//! #[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)] +//! #[derive(Clone, Copy, Debug, Eq, MultihashDigest, PartialEq)] //! #[mh(alloc_size = 64)] //! pub enum Code { -//! #[mh(code = 0x01, hasher = multihash::Sha2_256)] -//! Foo, -//! #[mh(code = 0x02, hasher = multihash::Sha2_512)] -//! Bar, +//! #[mh(code = 0x01, hasher = FooHasher)] +//! Foo //! } //! //! let hash = Code::Foo.digest(b"hello world!"); +//! //! println!("{:02x?}", hash); //! ``` @@ -40,6 +57,7 @@ use std::convert::TryFrom; use std::fmt; pub use hasher::Hasher; +#[doc(inline)] pub use multihash_derive_impl::MultihashDigest; pub use multihash::*; @@ -58,9 +76,7 @@ impl std::error::Error for UnsupportedCode {} /// Trait that implements hashing. /// -/// It is usually implemented by a custom code table enum that derives the [`Multihash` derive]. -/// -/// [`Multihash` derive]: crate::derive +/// Typically, you won't implement this yourself but use the [`MultihashDigest`](multihash_derive_impl::MultihashDigest) custom-derive. pub trait MultihashDigest: TryFrom + Into diff --git a/src/lib.rs b/src/lib.rs index 7ebe7dfd..f69d7f78 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,42 +1,12 @@ -//! Multihash implementation. +//! Bare-minimum multihash data structure. //! -//! Feature Flags -//! ------------- +//! This crate defines a `no_std` compatible data structures for representing a `Multihash`. //! -//! Multihash has lots of [feature flags], by default a table with cryptographically secure hashers -//! is created. +//! It does not offer any hashing, instead you are encouraged to either do the hashing yourself. +//! Alternatively, you can use an existing code table or make your own code table. //! -//! Some of the features are about specific hash functions, these are ("default" marks the hashers -//! that are enabled by default): -//! -//! - `blake2b`: (default) Enable Blake2b hashers -//! - `blake2s`: (default) Enable Blake2s hashers -//! - `identity`: Enable the Identity hashers (using it is discouraged as it's not a hash function -//! in the sense that it produces a fixed sized output independent of the input size) -//! - `sha1`: Enable SHA-1 hasher -//! - `sha2`: (default) Enable SHA-2 hashers -//! - `sha3`: (default) Enable SHA-3 hashers -//! - `strobe`: Enable Strobe hashers -//! -//! In order to enable all cryptographically secure hashers, you can set the `secure-hashes` -//! feature flag (enabled by default). -//! -//! The library has support for `no_std`, if you disable the `std` feature flag. -//! -//! The `multihash-impl` feature flag (enabled by default) enables a default Multihash -//! implementation that contains some of the bundled hashers. If you want a different set of hash -//! algorithms you can change this with enabled the corresponding features. -//! -//! For example if you only need SHA2 hasher, you could set the features in the `multihash` -//! dependency like this: -//! -//! ```toml -//! multihash = { version = …, default-features = false, features = ["std", "multihash-impl", "sha2"] } -//! ``` -//! -//! If you want to customize your code table even more, for example you want only one specific hash -//! digest size and not whole family, you would only enable the `derive` feature (enabled by -//! default), which enables the [`Multihash` derive], together with the hashers you want. +//! The [`multihash-codetable`] crate defines a set of hashes to get started quickly. +//! To make your own codetable, use the [`multihash-derive`] crate. //! //! The `arb` feature flag enables the quickcheck arbitrary implementation for property based //! testing. @@ -44,10 +14,10 @@ //! For serializing the multihash there is support for [Serde] via the `serde-codec` feature and //! the [SCALE Codec] via the `scale-codec` feature. //! -//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section -//! [`Multihash` derive]: crate::derive //! [Serde]: https://serde.rs //! [SCALE Codec]: https://github.com/paritytech/parity-scale-codec +//! [`multihash-derive`]: https://docs.rs/multihash-derive +//! [`multihash-codetable`]: https://docs.rs/multihash-codetable #![deny(missing_docs, unsafe_code)] #![cfg_attr(not(feature = "std"), no_std)] From abbb7ebc07e63907ccad4dbecc523aa014e45a02 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 17:33:34 +0100 Subject: [PATCH 18/39] Update docs for multihash-codetable --- codetable/Cargo.toml | 1 + codetable/src/lib.rs | 6 ++---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/codetable/Cargo.toml b/codetable/Cargo.toml index 25e53018..a5520c07 100644 --- a/codetable/Cargo.toml +++ b/codetable/Cargo.toml @@ -1,5 +1,6 @@ [package] name = "multihash-codetable" +description = "Default multihash code-table with cryptographically secure hash implementations" version = "0.1.0" edition = "2021" diff --git a/codetable/src/lib.rs b/codetable/src/lib.rs index d9329350..9b094f01 100644 --- a/codetable/src/lib.rs +++ b/codetable/src/lib.rs @@ -16,11 +16,9 @@ pub use crate::hasher_impl::strobe::{Strobe256, Strobe512, StrobeHasher}; /// Default (cryptographically secure) Multihash implementation. /// /// This is a default set of hashing algorithms. Usually applications would use their own subset of -/// algorithms. See the [`Multihash` derive] for more information. +/// algorithms. See the [`multihash-derive`] crate for more information. /// -/// [`Multihash` derive]: crate::derive -#[cfg_attr(feature = "serde-codec", derive(serde::Deserialize))] -#[cfg_attr(feature = "serde-codec", derive(serde::Serialize))] +/// [`multihash-derive`]: https://docs.rs/multihash-derive #[derive(Copy, Clone, Debug, Eq, MultihashDigest, PartialEq)] #[mh(alloc_size = 64)] pub enum Code { From c5024658fc83aea213f71a878ee428e329cd0715 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 17:47:54 +0100 Subject: [PATCH 19/39] Remove outdated docs --- derive/src/lib.rs | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 3aaf05ec..df2029c6 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -89,29 +89,8 @@ pub trait MultihashDigest: + 'static { /// Calculate the hash of some input data. - /// - /// # Example - /// - /// ``` - /// // `Code` implements `MultihashDigest` - /// use multihash::{Code, MultihashDigest}; - /// - /// let hash = Code::Sha3_256.digest(b"Hello world!"); - /// println!("{:02x?}", hash); - /// ``` fn digest(&self, input: &[u8]) -> Multihash; /// Create a multihash from an existing multihash digest. - /// - /// # Example - /// - /// ``` - /// use multihash::{Code, Hasher, MultihashDigest, Sha3_256}; - /// - /// let mut hasher = Sha3_256::default(); - /// hasher.update(b"Hello world!"); - /// let hash = Code::Sha3_256.wrap(&hasher.finalize()).unwrap(); - /// println!("{:02x?}", hash); - /// ``` - fn wrap(&self, digest: &[u8]) -> std::result::Result, multihash::Error>; + fn wrap(&self, digest: &[u8]) -> std::result::Result, Error>; } From e67be12fdae7bb4c2579a786bebd1a9342073c8e Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 18:10:17 +0100 Subject: [PATCH 20/39] Replace macro tests with trybuild --- derive-impl/Cargo.toml | 4 - derive-impl/src/multihash.rs | 113 +----------------- derive-impl/src/utils.rs | 10 -- derive/Cargo.toml | 4 + derive/src/lib.rs | 6 +- derive/tests/fail/no_allow_same_code_twice.rs | 27 +++++ .../fail/no_allow_same_code_twice.stderr | 8 ++ derive/tests/fail/no_allow_same_name_twice.rs | 27 +++++ .../fail/no_allow_same_name_twice.stderr | 10 ++ derive/tests/multihash.rs | 29 +++++ derive/tests/pass/derive.rs | 20 ++++ src/multihash.rs | 35 +----- 12 files changed, 133 insertions(+), 160 deletions(-) create mode 100644 derive/tests/fail/no_allow_same_code_twice.rs create mode 100644 derive/tests/fail/no_allow_same_code_twice.stderr create mode 100644 derive/tests/fail/no_allow_same_name_twice.rs create mode 100644 derive/tests/fail/no_allow_same_name_twice.stderr create mode 100644 derive/tests/multihash.rs create mode 100644 derive/tests/pass/derive.rs diff --git a/derive-impl/Cargo.toml b/derive-impl/Cargo.toml index 503c24e9..eec777e1 100644 --- a/derive-impl/Cargo.toml +++ b/derive-impl/Cargo.toml @@ -22,7 +22,3 @@ synstructure = "0.12.4" [features] default = ["std"] std = [] - -[dev-dependencies] -pretty_assertions = "1.0.0" -#multihash = { path = "..", default-features = false, features = ["derive", "sha2"] } diff --git a/derive-impl/src/multihash.rs b/derive-impl/src/multihash.rs index 237c57d0..57161c31 100644 --- a/derive-impl/src/multihash.rs +++ b/derive-impl/src/multihash.rs @@ -221,7 +221,7 @@ pub fn multihash(s: Structure) -> TokenStream { quote! { /// A Multihash with the same allocated size as the Multihashes produces by this derive. - pub type Multihash = #mh_crate::MultihashGeneric<#alloc_size>; + pub type Multihash = #mh_crate::Multihash<#alloc_size>; impl #mh_crate::MultihashDigest<#alloc_size> for #code_enum { fn digest(&self, input: &[u8]) -> Multihash { @@ -258,114 +258,3 @@ pub fn multihash(s: Structure) -> TokenStream { } } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_multihash_derive() { - let input = quote! { - #[derive(Clone, Multihash)] - #[mh(alloc_size = 32)] - pub enum Code { - #[mh(code = multihash::IDENTITY, hasher = multihash::Identity256)] - Identity256, - /// Multihash array for hash function. - #[mh(code = 0x38b64f, hasher = multihash::Strobe256)] - Strobe256, - } - }; - let expected = quote! { - /// A Multihash with the same allocated size as the Multihashes produces by this derive. - pub type Multihash = multihash::MultihashGeneric<32>; - - impl multihash::MultihashDigest<32> for Code { - fn digest(&self, input: &[u8]) -> Multihash { - use multihash::Hasher; - match self { - Self::Identity256 => { - let mut hasher = multihash::Identity256::default(); - hasher.update(input); - Multihash::wrap(multihash::IDENTITY, hasher.finalize()).unwrap() - }, - Self::Strobe256 => { - let mut hasher = multihash::Strobe256::default(); - hasher.update(input); - Multihash::wrap(0x38b64f, hasher.finalize()).unwrap() - }, - _ => unreachable!(), - } - } - - fn wrap(&self, digest: &[u8]) -> Result { - Multihash::wrap((*self).into(), digest) - } - } - - impl From for u64 { - fn from(code: Code) -> Self { - match code { - Code::Identity256 => multihash::IDENTITY, - Code::Strobe256 => 0x38b64f, - _ => unreachable!(), - } - } - } - - impl core::convert::TryFrom for Code { - type Error = multihash::Error; - - fn try_from(code: u64) -> Result { - match code { - multihash::IDENTITY => Ok(Self::Identity256), - 0x38b64f => Ok(Self::Strobe256), - _ => Err(multihash::Error::UnsupportedCode(code)) - } - } - } - }; - let derive_input = syn::parse2(input).unwrap(); - let s = Structure::new(&derive_input); - let result = multihash(s); - utils::assert_proc_macro(result, expected); - } - - #[test] - #[should_panic( - expected = "the #mh(code) attribute `multihash :: SHA2_256` is defined multiple times" - )] - fn test_multihash_error_code_duplicates() { - let input = quote! { - #[derive(Clone, Multihash)] - #[mh(alloc_size = 64)] - pub enum Multihash { - #[mh(code = multihash::SHA2_256, hasher = multihash::Sha2_256)] - Identity256, - #[mh(code = multihash::SHA2_256, hasher = multihash::Sha2_256)] - Identity256, - } - }; - let derive_input = syn::parse2(input).unwrap(); - let s = Structure::new(&derive_input); - multihash(s); - } - - #[test] - #[should_panic(expected = "the #mh(code) attribute `0x14` is defined multiple times")] - fn test_multihash_error_code_duplicates_numbers() { - let input = quote! { - #[derive(Clone, Multihash)] - #[mh(alloc_size = 32)] - pub enum Code { - #[mh(code = 0x14, hasher = multihash::Sha2_256)] - Identity256, - #[mh(code = 0x14, hasher = multihash::Sha2_256)] - Identity256, - } - }; - let derive_input = syn::parse2(input).unwrap(); - let s = Structure::new(&derive_input); - multihash(s); - } -} diff --git a/derive-impl/src/utils.rs b/derive-impl/src/utils.rs index b21edb8a..bf3cf427 100644 --- a/derive-impl/src/utils.rs +++ b/derive-impl/src/utils.rs @@ -43,13 +43,3 @@ impl Parse for Attr { }) } } - -#[cfg(test)] -pub(crate) fn assert_proc_macro( - result: proc_macro2::TokenStream, - expected: proc_macro2::TokenStream, -) { - let result = result.to_string(); - let expected = expected.to_string(); - pretty_assertions::assert_eq!(result, expected); -} diff --git a/derive/Cargo.toml b/derive/Cargo.toml index 94d81548..6ed025b5 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -10,3 +10,7 @@ resolver = "2" [dependencies] multihash-derive-impl = { version = "0.1.0", path = "../derive-impl" } multihash = { version = "0.18.0", path = "../" } + +[dev-dependencies] +trybuild = "1.0.80" +multihash-codetable = { path = "../codetable" } diff --git a/derive/src/lib.rs b/derive/src/lib.rs index df2029c6..18739da8 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -19,7 +19,7 @@ //! //! # Example //! -//! ``` +//! ```ignore : `proc-macro-crate` does not work in docs, see https://github.com/bkchr/proc-macro-crate/issues/14 //! use multihash_derive::{Hasher, MultihashDigest}; //! //! struct FooHasher; @@ -57,11 +57,11 @@ use std::convert::TryFrom; use std::fmt; pub use hasher::Hasher; +pub use multihash::Error; +pub use multihash::Multihash; #[doc(inline)] pub use multihash_derive_impl::MultihashDigest; -pub use multihash::*; - /// The given code is not supported by this codetable. #[derive(Debug)] pub struct UnsupportedCode(pub u64); diff --git a/derive/tests/fail/no_allow_same_code_twice.rs b/derive/tests/fail/no_allow_same_code_twice.rs new file mode 100644 index 00000000..866ecaa7 --- /dev/null +++ b/derive/tests/fail/no_allow_same_code_twice.rs @@ -0,0 +1,27 @@ +#[derive(Default)] +struct FooHasher { + +} + +impl multihash_derive::Hasher for FooHasher { + fn update(&mut self, input: &[u8]) { } + + fn finalize(&mut self) -> &[u8] { + todo!() + } + + fn reset(&mut self) { } +} + +#[derive(Clone, Debug, Eq, PartialEq, Copy, multihash_derive::MultihashDigest)] +#[mh(alloc_size = 32)] +pub enum Code { + #[mh(code = 0x0, hasher = FooHasher)] + Foo1, + #[mh(code = 0x0, hasher = FooHasher)] + Foo2, +} + +fn main() { + +} diff --git a/derive/tests/fail/no_allow_same_code_twice.stderr b/derive/tests/fail/no_allow_same_code_twice.stderr new file mode 100644 index 00000000..b4688a76 --- /dev/null +++ b/derive/tests/fail/no_allow_same_code_twice.stderr @@ -0,0 +1,8 @@ +error: the #mh(code) attribute `0x0` is defined multiple times + + = note: previous definition of `0x0` at line 0 + + --> tests/fail/no_allow_same_code_twice.rs:21:17 + | +21 | #[mh(code = 0x0, hasher = FooHasher)] + | ^^^ diff --git a/derive/tests/fail/no_allow_same_name_twice.rs b/derive/tests/fail/no_allow_same_name_twice.rs new file mode 100644 index 00000000..73a89858 --- /dev/null +++ b/derive/tests/fail/no_allow_same_name_twice.rs @@ -0,0 +1,27 @@ +#[derive(Default)] +struct FooHasher { + +} + +impl multihash_derive::Hasher for FooHasher { + fn update(&mut self, input: &[u8]) { } + + fn finalize(&mut self) -> &[u8] { + todo!() + } + + fn reset(&mut self) { } +} + +#[derive(Clone, Debug, Eq, PartialEq, Copy, multihash_derive::MultihashDigest)] +#[mh(alloc_size = 32)] +pub enum Code { + #[mh(code = 0x0, hasher = FooHasher)] + Foo, + #[mh(code = 0x1, hasher = FooHasher)] + Foo, +} + +fn main() { + +} diff --git a/derive/tests/fail/no_allow_same_name_twice.stderr b/derive/tests/fail/no_allow_same_name_twice.stderr new file mode 100644 index 00000000..9ba4e34b --- /dev/null +++ b/derive/tests/fail/no_allow_same_name_twice.stderr @@ -0,0 +1,10 @@ +error[E0428]: the name `Foo` is defined multiple times + --> tests/fail/no_allow_same_name_twice.rs:22:5 + | +20 | Foo, + | --- previous definition of the type `Foo` here +21 | #[mh(code = 0x1, hasher = FooHasher)] +22 | Foo, + | ^^^ `Foo` redefined here + | + = note: `Foo` must be defined only once in the type namespace of this enum diff --git a/derive/tests/multihash.rs b/derive/tests/multihash.rs new file mode 100644 index 00000000..f723ef5e --- /dev/null +++ b/derive/tests/multihash.rs @@ -0,0 +1,29 @@ +use multihash_derive::{Hasher, MultihashDigest}; + +#[test] +fn ui() { + let t = trybuild::TestCases::new(); + t.pass("tests/pass/*.rs"); + t.compile_fail("tests/fail/*.rs"); +} + +#[test] +fn uses_correct_hasher() { + #[derive(Clone, Debug, Eq, PartialEq, Copy, MultihashDigest)] + #[mh(alloc_size = 32)] + pub enum Code { + /// Multihash array for hash function. + #[mh(code = 0x38b64f, hasher = multihash_codetable::Strobe256)] + Strobe256, + } + + let multihash1 = Code::Strobe256.digest(b"foobar"); + + let mut hasher = multihash_codetable::Strobe256::default(); + hasher.update(b"foobar"); + let digest = hasher.finalize(); + + let multihash2 = Multihash::wrap(0x38b64f, digest).unwrap(); + + assert_eq!(multihash1, multihash2) +} diff --git a/derive/tests/pass/derive.rs b/derive/tests/pass/derive.rs new file mode 100644 index 00000000..48818b74 --- /dev/null +++ b/derive/tests/pass/derive.rs @@ -0,0 +1,20 @@ +use multihash_derive::MultihashDigest; + +#[derive(Clone, Debug, Eq, PartialEq, Copy, MultihashDigest)] +#[mh(alloc_size = 32)] +pub enum Code { + #[mh(code = 0x0, hasher = multihash_codetable::Identity256)] + Identity256, + /// Multihash array for hash function. + #[mh(code = 0x38b64f, hasher = multihash_codetable::Strobe256)] + Strobe256, +} + +fn main() { + assert_multihash_size_32(Code::Identity256.digest(&[])); + assert_multihash_size_32(Code::Strobe256.digest(&[])); +} + +fn assert_multihash_size_32(_mh: multihash_derive::Multihash<32>) { + +} diff --git a/src/multihash.rs b/src/multihash.rs index 09483d93..fb8617b8 100644 --- a/src/multihash.rs +++ b/src/multihash.rs @@ -29,7 +29,7 @@ use core2::io; /// 0x76, 0x22, 0xf3, 0xca, 0x71, 0xfb, 0xa1, 0xd9, 0x72, 0xfd, 0x94, 0xa3, 0x1c, 0x3b, 0xfb, /// 0xf2, 0x4e, 0x39, 0x38, /// ]; -/// let mh = Multihash::from_bytes(&digest_bytes).unwrap(); +/// let mh = Multihash::<32>::from_bytes(&digest_bytes).unwrap(); /// assert_eq!(mh.code(), Sha3_256); /// assert_eq!(mh.size(), 32); /// assert_eq!(mh.digest(), &digest_bytes[2..]); @@ -160,27 +160,15 @@ impl Multihash { /// is secure (cryptographically) to use. /// /// If the new size is larger than the current size, this method does nothing. - /// - /// ``` - /// use multihash::{Code, MultihashDigest}; - /// - /// let hash = Code::Sha3_256.digest(b"Hello world!").truncate(20); - /// ``` pub fn truncate(&self, size: u8) -> Self { let mut mh = *self; mh.size = mh.size.min(size); mh } - /// Resizes the backing multihash buffer. This function fails if the hash digest is larger than - /// the target size. - /// - /// ``` - /// use multihash::{Code, MultihashDigest, MultihashGeneric}; + /// Resizes the backing multihash buffer. /// - /// let hash = Code::Sha3_256.digest(b"Hello world!"); - /// let large_hash: MultihashGeneric<32> = hash.resize().unwrap(); - /// ``` + /// This function fails if the hash digest is larger than the target size. pub fn resize(&self) -> Result, Error> { let size = self.size as usize; if size > R { @@ -197,22 +185,7 @@ impl Multihash { /// Decomposes struct, useful when needing a `Sized` array or moving all the data into another type /// - /// It is recommended to use `digest()` `code()` and `size()` for most cases - /// - /// ``` - /// use multihash::{Code, MultihashDigest}; - /// struct Foo { - /// arr: [u8; S], - /// len: usize, - /// } - /// - /// let hash = Code::Sha3_256.digest(b"Hello world!"); - /// let (.., arr, size) = hash.into_inner(); - /// let foo = Foo { - /// arr, - /// len: size as usize, - /// }; - /// ``` + /// It is recommended to use `digest()` `code()` and `size()` for most cases. pub fn into_inner(self) -> (u64, [u8; S], u8) { let Self { code, digest, size } = self; (code, digest, size) From 4d3048cca02ad552e95595387ee10e331aba080f Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 18:16:35 +0100 Subject: [PATCH 21/39] Remove unused feature `std` from `multihash` --- derive-impl/Cargo.toml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/derive-impl/Cargo.toml b/derive-impl/Cargo.toml index eec777e1..7431f74b 100644 --- a/derive-impl/Cargo.toml +++ b/derive-impl/Cargo.toml @@ -18,7 +18,3 @@ proc-macro-error = "1.0.4" quote = "1.0.7" syn = "1.0.42" synstructure = "0.12.4" - -[features] -default = ["std"] -std = [] From 5a97fdc129d4a6dad94d2377d8640c55eb75b1c5 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 18:20:59 +0100 Subject: [PATCH 22/39] Export deprecated macro name --- derive/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 18739da8..abfa74ce 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -60,6 +60,7 @@ pub use hasher::Hasher; pub use multihash::Error; pub use multihash::Multihash; #[doc(inline)] +pub use multihash_derive_impl::Multihash; // This one is deprecated. pub use multihash_derive_impl::MultihashDigest; /// The given code is not supported by this codetable. From 0b85ce9058681209d7b91f0907c173441f91aea8 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 18:22:38 +0100 Subject: [PATCH 23/39] Reduce diff --- Cargo.toml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dd1dc405..756f588f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,18 +24,19 @@ serde-codec = ["serde"] # Deprecated, don't use. serde = ["dep:serde", "dep:serde-big-array"] [dependencies] -arbitrary = {version = "1.1.0", optional = true } -core2 = { version = "0.4.0", default-features = false } parity-scale-codec = { version = "3.0.0", default-features = false, features = ["derive"], optional = true } quickcheck = { version = "1.0.3", optional = true } rand = { version = "0.8.5", optional = true, features = ["small_rng"] } serde = { version = "1.0.116", optional = true, default-features = false, features = ["derive"] } serde-big-array = { version = "0.3.2", optional = true, features = ["const-generics"] } unsigned-varint = { version = "0.7.1", default-features = false } +arbitrary = {version = "1.1.0", optional = true } + +core2 = { version = "0.4.0", default-features = false } [dev-dependencies] hex = "0.4.2" serde_json = "1.0.58" quickcheck = "1.0.3" -arbitrary = "1.1.0" rand = { version = "0.8.5", features = ["small_rng"] } +arbitrary = "1.1.0" From 960ec0e64687655763085163cb3e8f209019c4de Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 18:26:46 +0100 Subject: [PATCH 24/39] Minimize diff and fix cargo metadata --- Cargo.toml | 1 + codetable/Cargo.toml | 2 ++ codetable/benches/multihash.rs | 3 ++- derive-impl/Cargo.toml | 3 +-- derive/Cargo.toml | 3 +-- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 756f588f..64a971f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = ["derive", "derive-impl", ".", "codetable"] +resolver = "2" [package] name = "multihash" diff --git a/codetable/Cargo.toml b/codetable/Cargo.toml index a5520c07..9967b5b5 100644 --- a/codetable/Cargo.toml +++ b/codetable/Cargo.toml @@ -2,6 +2,8 @@ name = "multihash-codetable" description = "Default multihash code-table with cryptographically secure hash implementations" version = "0.1.0" +repository = "https://github.com/multiformats/rust-multihash" +license = "MIT" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/codetable/benches/multihash.rs b/codetable/benches/multihash.rs index 5d964a66..735926ac 100644 --- a/codetable/benches/multihash.rs +++ b/codetable/benches/multihash.rs @@ -1,11 +1,12 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use rand::Rng; + use multihash_codetable::{ Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3_256, Keccak224, Keccak256, Keccak384, Keccak512, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384, Sha3_512, Strobe256, Strobe512, }; use multihash_derive::Hasher; -use rand::Rng; macro_rules! group_digest { ($criterion:ident, $( $id:expr => $hash:ident, $input:expr)* ) => {{ diff --git a/derive-impl/Cargo.toml b/derive-impl/Cargo.toml index 7431f74b..c8dd27a9 100644 --- a/derive-impl/Cargo.toml +++ b/derive-impl/Cargo.toml @@ -5,8 +5,7 @@ authors = ["David Craven "] edition = "2018" description = "Internal proc-macro crate for the MultihashDigest derive" license = "MIT" -repository = "https://github.com/multiformats/multihash" -resolver = "2" +repository = "https://github.com/multiformats/rust-multihash" [lib] proc-macro = true diff --git a/derive/Cargo.toml b/derive/Cargo.toml index 6ed025b5..71e83379 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -4,8 +4,7 @@ version = "0.8.1" edition = "2018" description = "Proc macro for deriving custom multihash tables." license = "MIT" -repository = "https://github.com/multiformats/multihash" -resolver = "2" +repository = "https://github.com/multiformats/rust-multihash" [dependencies] multihash-derive-impl = { version = "0.1.0", path = "../derive-impl" } From 72601b10b44c54b53ef8b4142a0b8107a87a90cd Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Mar 2023 18:29:04 +0100 Subject: [PATCH 25/39] Minimize diff --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 64a971f3..7ff67fbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,5 +39,5 @@ core2 = { version = "0.4.0", default-features = false } hex = "0.4.2" serde_json = "1.0.58" quickcheck = "1.0.3" -rand = { version = "0.8.5", features = ["small_rng"] } +rand = "0.8.5" arbitrary = "1.1.0" From b25bfcb56d810021d367128966db445f8e2e6bbd Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 22 Mar 2023 14:07:39 +0100 Subject: [PATCH 26/39] Remove `extern crate core` --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f69d7f78..3c2b53e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,6 @@ #[cfg(feature = "alloc")] extern crate alloc; -extern crate core; #[cfg(any(test, feature = "arb"))] mod arb; From c0925a855ecec43ed1e89c478cb66730e835c817 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 22 Mar 2023 14:08:22 +0100 Subject: [PATCH 27/39] Use `dep:` consistently --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7ff67fbe..bb07dfd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,9 +19,9 @@ rust-version = "1.59" default = ["std"] std = ["unsigned-varint/std", "alloc"] alloc = [] -arb = ["quickcheck", "rand", "arbitrary"] +arb = ["dep:quickcheck", "dep:rand", "dep:arbitrary"] scale-codec = ["dep:parity-scale-codec"] -serde-codec = ["serde"] # Deprecated, don't use. +serde-codec = ["dep:serde"] # Deprecated, don't use. serde = ["dep:serde", "dep:serde-big-array"] [dependencies] From 0cb5c77d18909eceec209aaf405c3ebc511d0654 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 22 Mar 2023 14:20:46 +0100 Subject: [PATCH 28/39] Use uniform way of referring to error type --- src/error.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index f8df7bc6..61dc29a9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,7 +3,7 @@ use core2::{error::Error as StdError, io}; #[cfg(feature = "std")] use std::{error::Error as StdError, io}; -use unsigned_varint::decode::Error as DecodeError; +use unsigned_varint::decode; /// Opaque error struct for operations involving a [`Multihash`](crate::Multihash). #[derive(Debug)] @@ -21,14 +21,14 @@ impl Error { #[cfg(not(feature = "std"))] pub(crate) const fn insufficient_varint_bytes() -> Self { Self { - kind: Kind::Varint(unsigned_varint::decode::Error::Insufficient), + kind: Kind::Varint(decode::Error::Insufficient), } } #[cfg(not(feature = "std"))] pub(crate) const fn varint_overflow() -> Self { Self { - kind: Kind::Varint(unsigned_varint::decode::Error::Overflow), + kind: Kind::Varint(decode::Error::Overflow), } } } @@ -46,7 +46,7 @@ enum Kind { /// Invalid multihash size. InvalidSize(u64), /// Invalid varint. - Varint(DecodeError), + Varint(decode::Error), } #[cfg(feature = "std")] From 55759e5e2ce23fea83149fa1b7054b44c848e775 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 22 Mar 2023 14:32:54 +0100 Subject: [PATCH 29/39] Make everything work on `no_std` again --- .github/workflows/build.yml | 2 +- codetable/Cargo.toml | 7 ++++++- codetable/src/hasher_impl.rs | 2 +- codetable/src/lib.rs | 2 ++ derive/Cargo.toml | 7 ++++++- derive/src/lib.rs | 10 ++++++---- 6 files changed, 22 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 03eaa79d..23d582f9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -73,7 +73,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: build - args: --no-default-features --package multihash --target thumbv6m-none-eabi + args: --no-default-features --workspace --target thumbv6m-none-eabi coverage: name: Code Coverage diff --git a/codetable/Cargo.toml b/codetable/Cargo.toml index 9967b5b5..f58f782f 100644 --- a/codetable/Cargo.toml +++ b/codetable/Cargo.toml @@ -8,6 +8,10 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = ["std"] +std = ["blake2b_simd/std", "blake2s_simd/std", "blake3/std", "digest/std", "sha-1/std", "sha-2/std", "sha-3/std", "strobe-rs/std", "ripemd-rs/std", "multihash-derive/std", "core2/std"] + [dependencies] blake2b_simd = { version = "1.0.0", default-features = false } blake2s_simd = { version = "1.0.0", default-features = false } @@ -17,8 +21,9 @@ sha-1 = { version = "0.10.0", default-features = false } sha-2 = { version = "0.10.0", default-features = false, package = "sha2" } sha-3 = { version = "0.10.0", default-features = false, package = "sha3" } strobe-rs = { version = "0.7.0", default-features = false } -ripemd-rs = { package = "ripemd", version = "0.1.1"} +ripemd-rs = { package = "ripemd", version = "0.1.1", default-features = false } multihash-derive = { version = "0.8.0", path = "../derive", default-features = false } +core2 = { version = "0.4.0", default-features = false } [dev-dependencies] hex = "0.4.2" diff --git a/codetable/src/hasher_impl.rs b/codetable/src/hasher_impl.rs index 5c67523a..796744dd 100644 --- a/codetable/src/hasher_impl.rs +++ b/codetable/src/hasher_impl.rs @@ -1,6 +1,6 @@ use multihash_derive::Hasher; -use std::io; +use core2::io; macro_rules! derive_write { ($name:ident) => { diff --git a/codetable/src/lib.rs b/codetable/src/lib.rs index 9b094f01..81d0d7a2 100644 --- a/codetable/src/lib.rs +++ b/codetable/src/lib.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(feature = "std"), no_std)] + mod hasher_impl; use multihash_derive::MultihashDigest; diff --git a/derive/Cargo.toml b/derive/Cargo.toml index 71e83379..fa5e0032 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -6,9 +6,14 @@ description = "Proc macro for deriving custom multihash tables." license = "MIT" repository = "https://github.com/multiformats/rust-multihash" +[features] +default = ["std"] +std = ["multihash/std", "core2/std"] + [dependencies] multihash-derive-impl = { version = "0.1.0", path = "../derive-impl" } -multihash = { version = "0.18.0", path = "../" } +multihash = { version = "0.18.0", path = "../", default-features = false } +core2 = { version = "0.4.0", default-features = false } [dev-dependencies] trybuild = "1.0.80" diff --git a/derive/src/lib.rs b/derive/src/lib.rs index abfa74ce..062ce43e 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(feature = "std"), no_std)] + //! A procedural macro for custom Multihash code tables. //! //! This proc macro derives a custom Multihash code table from a list of hashers. It also @@ -53,8 +55,8 @@ mod hasher; -use std::convert::TryFrom; -use std::fmt; +use core::convert::TryFrom; +use core::fmt; pub use hasher::Hasher; pub use multihash::Error; @@ -73,7 +75,7 @@ impl fmt::Display for UnsupportedCode { } } -impl std::error::Error for UnsupportedCode {} +impl core2::error::Error for UnsupportedCode {} /// Trait that implements hashing. /// @@ -93,5 +95,5 @@ pub trait MultihashDigest: fn digest(&self, input: &[u8]) -> Multihash; /// Create a multihash from an existing multihash digest. - fn wrap(&self, digest: &[u8]) -> std::result::Result, Error>; + fn wrap(&self, digest: &[u8]) -> Result, Error>; } From 9a679ccef98159203e61af40040424b80f8afc62 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 22 Mar 2023 14:46:40 +0100 Subject: [PATCH 30/39] Add back serde support for `Code` --- codetable/Cargo.toml | 1 + codetable/src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/codetable/Cargo.toml b/codetable/Cargo.toml index f58f782f..73a53f6f 100644 --- a/codetable/Cargo.toml +++ b/codetable/Cargo.toml @@ -24,6 +24,7 @@ strobe-rs = { version = "0.7.0", default-features = false } ripemd-rs = { package = "ripemd", version = "0.1.1", default-features = false } multihash-derive = { version = "0.8.0", path = "../derive", default-features = false } core2 = { version = "0.4.0", default-features = false } +serde = { version = "1.0.158", features = ["derive"], optional = true } [dev-dependencies] hex = "0.4.2" diff --git a/codetable/src/lib.rs b/codetable/src/lib.rs index 81d0d7a2..d72d9ebd 100644 --- a/codetable/src/lib.rs +++ b/codetable/src/lib.rs @@ -21,6 +21,7 @@ pub use crate::hasher_impl::strobe::{Strobe256, Strobe512, StrobeHasher}; /// algorithms. See the [`multihash-derive`] crate for more information. /// /// [`multihash-derive`]: https://docs.rs/multihash-derive +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Copy, Clone, Debug, Eq, MultihashDigest, PartialEq)] #[mh(alloc_size = 64)] pub enum Code { From c44bfb228299c1c21e87a2b0ec9b070aa6b9db5e Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 22 Mar 2023 15:02:38 +0100 Subject: [PATCH 31/39] Reintroduce feature-flags in `multihash-codetable` --- codetable/Cargo.toml | 28 +++++++++++++++--------- codetable/src/hasher_impl.rs | 41 ++++++++++++++++++++++++------------ codetable/src/lib.rs | 39 ++++++++++++++++++++++++++++++++-- codetable/tests/lib.rs | 4 ++-- derive/Cargo.toml | 2 +- 5 files changed, 86 insertions(+), 28 deletions(-) diff --git a/codetable/Cargo.toml b/codetable/Cargo.toml index 73a53f6f..91dc4363 100644 --- a/codetable/Cargo.toml +++ b/codetable/Cargo.toml @@ -10,18 +10,22 @@ edition = "2021" [features] default = ["std"] -std = ["blake2b_simd/std", "blake2s_simd/std", "blake3/std", "digest/std", "sha-1/std", "sha-2/std", "sha-3/std", "strobe-rs/std", "ripemd-rs/std", "multihash-derive/std", "core2/std"] +std = ["blake2b_simd?/std", "blake2s_simd?/std", "blake3?/std", "digest?/std", "sha-1?/std", "sha2?/std", "sha3?/std", "strobe-rs?/std", "ripemd?/std", "multihash-derive/std", "core2/std"] +sha1 = ["dep:sha-1"] +strobe = ["dep:strobe-rs"] +blake2b = ["dep:blake2b_simd"] +blake2s = ["dep:blake2s_simd"] [dependencies] -blake2b_simd = { version = "1.0.0", default-features = false } -blake2s_simd = { version = "1.0.0", default-features = false } -blake3 = { version = "1.2.0", default-features = false } -digest = { version = "0.10.1", default-features = false } -sha-1 = { version = "0.10.0", default-features = false } -sha-2 = { version = "0.10.0", default-features = false, package = "sha2" } -sha-3 = { version = "0.10.0", default-features = false, package = "sha3" } -strobe-rs = { version = "0.7.0", default-features = false } -ripemd-rs = { package = "ripemd", version = "0.1.1", default-features = false } +blake2b_simd = { version = "1.0.0", default-features = false, optional = true } +blake2s_simd = { version = "1.0.0", default-features = false, optional = true } +blake3 = { version = "1.2.0", default-features = false, optional = true } +digest = { version = "0.10.1", default-features = false, optional = true } +sha-1 = { version = "0.10.0", default-features = false, optional = true } +sha2 = { version = "0.10.0", default-features = false, optional = true } +sha3 = { version = "0.10.0", default-features = false, optional = true } +strobe-rs = { version = "0.7.0", default-features = false, optional = true } +ripemd = { version = "0.1.1", default-features = false, optional = true } multihash-derive = { version = "0.8.0", path = "../derive", default-features = false } core2 = { version = "0.4.0", default-features = false } serde = { version = "1.0.158", features = ["derive"], optional = true } @@ -35,3 +39,7 @@ rand = "0.8.5" [[bench]] name = "multihash" harness = false + +[[test]] +name = "lib" +required-features = ["sha1", "sha2", "sha3", "ripemd", "strobe", "blake2b", "blake2s", "blake3"] diff --git a/codetable/src/hasher_impl.rs b/codetable/src/hasher_impl.rs index 796744dd..bf6a55dd 100644 --- a/codetable/src/hasher_impl.rs +++ b/codetable/src/hasher_impl.rs @@ -17,6 +17,7 @@ macro_rules! derive_write { }; } +#[cfg(any(feature = "blake2b", feature = "blake2s", feature = "blake3"))] macro_rules! derive_hasher_blake { ($module:ident, $name:ident) => { /// Multihash hasher. @@ -60,6 +61,7 @@ macro_rules! derive_hasher_blake { }; } +#[cfg(feature = "blake2b")] pub mod blake2b { use super::*; @@ -72,6 +74,7 @@ pub mod blake2b { pub type Blake2b512 = Blake2bHasher<64>; } +#[cfg(feature = "blake2s")] pub mod blake2s { use super::*; @@ -84,6 +87,7 @@ pub mod blake2s { pub type Blake2s256 = Blake2sHasher<32>; } +#[cfg(feature = "blake3")] pub mod blake3 { use super::*; @@ -135,6 +139,12 @@ pub mod blake3 { pub type Blake3_256 = Blake3Hasher<32>; } +#[cfg(any( + feature = "sha1", + feature = "sha2", + feature = "sha3", + feature = "ripemd" +))] macro_rules! derive_rustcrypto_hasher { ($module:ty, $name:ident, $size:expr) => { /// Multihash hasher. @@ -187,40 +197,44 @@ macro_rules! derive_rustcrypto_hasher { }; } +#[cfg(feature = "sha1")] pub mod sha1 { use super::*; derive_rustcrypto_hasher!(::sha1::Sha1, Sha1, 20); } +#[cfg(feature = "sha2")] pub mod sha2 { use super::*; - derive_rustcrypto_hasher!(sha_2::Sha256, Sha2_256, 32); - derive_rustcrypto_hasher!(sha_2::Sha512, Sha2_512, 64); + derive_rustcrypto_hasher!(::sha2::Sha256, Sha2_256, 32); + derive_rustcrypto_hasher!(::sha2::Sha512, Sha2_512, 64); } +#[cfg(feature = "sha3")] pub mod sha3 { use super::*; - derive_rustcrypto_hasher!(sha_3::Sha3_224, Sha3_224, 28); - derive_rustcrypto_hasher!(sha_3::Sha3_256, Sha3_256, 32); - derive_rustcrypto_hasher!(sha_3::Sha3_384, Sha3_384, 48); - derive_rustcrypto_hasher!(sha_3::Sha3_512, Sha3_512, 64); + derive_rustcrypto_hasher!(::sha3::Sha3_224, Sha3_224, 28); + derive_rustcrypto_hasher!(::sha3::Sha3_256, Sha3_256, 32); + derive_rustcrypto_hasher!(::sha3::Sha3_384, Sha3_384, 48); + derive_rustcrypto_hasher!(::sha3::Sha3_512, Sha3_512, 64); - derive_rustcrypto_hasher!(sha_3::Keccak224, Keccak224, 28); - derive_rustcrypto_hasher!(sha_3::Keccak256, Keccak256, 32); - derive_rustcrypto_hasher!(sha_3::Keccak384, Keccak384, 48); - derive_rustcrypto_hasher!(sha_3::Keccak512, Keccak512, 64); + derive_rustcrypto_hasher!(::sha3::Keccak224, Keccak224, 28); + derive_rustcrypto_hasher!(::sha3::Keccak256, Keccak256, 32); + derive_rustcrypto_hasher!(::sha3::Keccak384, Keccak384, 48); + derive_rustcrypto_hasher!(::sha3::Keccak512, Keccak512, 64); } +#[cfg(feature = "ripemd")] pub mod ripemd { use super::*; - derive_rustcrypto_hasher!(ripemd_rs::Ripemd160, Ripemd160, 20); - derive_rustcrypto_hasher!(ripemd_rs::Ripemd256, Ripemd256, 32); - derive_rustcrypto_hasher!(ripemd_rs::Ripemd320, Ripemd320, 40); + derive_rustcrypto_hasher!(::ripemd::Ripemd160, Ripemd160, 20); + derive_rustcrypto_hasher!(::ripemd::Ripemd256, Ripemd256, 32); + derive_rustcrypto_hasher!(::ripemd::Ripemd320, Ripemd320, 40); } pub mod identity { @@ -273,6 +287,7 @@ pub mod identity { pub type Identity256 = IdentityHasher<32>; } +#[cfg(feature = "strobe")] pub mod strobe { use super::*; use strobe_rs::{SecParam, Strobe}; diff --git a/codetable/src/lib.rs b/codetable/src/lib.rs index d72d9ebd..2d56a19b 100644 --- a/codetable/src/lib.rs +++ b/codetable/src/lib.rs @@ -4,15 +4,24 @@ mod hasher_impl; use multihash_derive::MultihashDigest; +#[cfg(feature = "blake2b")] pub use crate::hasher_impl::blake2b::{Blake2b256, Blake2b512, Blake2bHasher}; +#[cfg(feature = "blake2s")] pub use crate::hasher_impl::blake2s::{Blake2s128, Blake2s256, Blake2sHasher}; +#[cfg(feature = "blake3")] pub use crate::hasher_impl::blake3::{Blake3Hasher, Blake3_256}; pub use crate::hasher_impl::identity::{Identity256, IdentityHasher}; +#[cfg(feature = "ripemd")] pub use crate::hasher_impl::ripemd::{Ripemd160, Ripemd256, Ripemd320}; +#[cfg(feature = "sha1")] pub use crate::hasher_impl::sha1::Sha1; +#[cfg(feature = "sha2")] pub use crate::hasher_impl::sha2::{Sha2_256, Sha2_512}; -pub use crate::hasher_impl::sha3::{Keccak224, Keccak256, Keccak384, Keccak512}; -pub use crate::hasher_impl::sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512}; +#[cfg(feature = "sha3")] +pub use crate::hasher_impl::sha3::{ + Keccak224, Keccak256, Keccak384, Keccak512, Sha3_224, Sha3_256, Sha3_384, Sha3_512, +}; +#[cfg(feature = "strobe")] pub use crate::hasher_impl::strobe::{Strobe256, Strobe512, StrobeHasher}; /// Default (cryptographically secure) Multihash implementation. @@ -26,57 +35,75 @@ pub use crate::hasher_impl::strobe::{Strobe256, Strobe512, StrobeHasher}; #[mh(alloc_size = 64)] pub enum Code { /// SHA-256 (32-byte hash size) + #[cfg(feature = "sha2")] #[mh(code = 0x12, hasher = crate::Sha2_256)] Sha2_256, /// SHA-512 (64-byte hash size) + #[cfg(feature = "sha2")] #[mh(code = 0x13, hasher = crate::Sha2_512)] Sha2_512, /// SHA3-224 (28-byte hash size) + #[cfg(feature = "sha3")] #[mh(code = 0x17, hasher = crate::Sha3_224)] Sha3_224, /// SHA3-256 (32-byte hash size) + #[cfg(feature = "sha3")] #[mh(code = 0x16, hasher = crate::Sha3_256)] Sha3_256, /// SHA3-384 (48-byte hash size) + #[cfg(feature = "sha3")] #[mh(code = 0x15, hasher = crate::Sha3_384)] Sha3_384, /// SHA3-512 (64-byte hash size) + #[cfg(feature = "sha3")] #[mh(code = 0x14, hasher = crate::Sha3_512)] Sha3_512, /// Keccak-224 (28-byte hash size) + #[cfg(feature = "sha2")] #[mh(code = 0x1a, hasher = crate::Keccak224)] Keccak224, /// Keccak-256 (32-byte hash size) + #[cfg(feature = "sha2")] #[mh(code = 0x1b, hasher = crate::Keccak256)] Keccak256, /// Keccak-384 (48-byte hash size) + #[cfg(feature = "sha3")] #[mh(code = 0x1c, hasher = crate::Keccak384)] Keccak384, /// Keccak-512 (64-byte hash size) + #[cfg(feature = "sha3")] #[mh(code = 0x1d, hasher = crate::Keccak512)] Keccak512, /// BLAKE2b-256 (32-byte hash size) + #[cfg(feature = "blake2b")] #[mh(code = 0xb220, hasher = crate::Blake2b256)] Blake2b256, /// BLAKE2b-512 (64-byte hash size) + #[cfg(feature = "blake2b")] #[mh(code = 0xb240, hasher = crate::Blake2b512)] Blake2b512, /// BLAKE2s-128 (16-byte hash size) + #[cfg(feature = "blake2s")] #[mh(code = 0xb250, hasher = crate::Blake2s128)] Blake2s128, /// BLAKE2s-256 (32-byte hash size) + #[cfg(feature = "blake2s")] #[mh(code = 0xb260, hasher = crate::Blake2s256)] Blake2s256, /// BLAKE3-256 (32-byte hash size) + #[cfg(feature = "blake2s")] #[mh(code = 0x1e, hasher = crate::Blake3_256)] Blake3_256, /// RIPEMD-160 (20-byte hash size) + #[cfg(feature = "ripemd")] #[mh(code = 0x1053, hasher = crate::Ripemd160)] Ripemd160, /// RIPEMD-256 (32-byte hash size) + #[cfg(feature = "ripemd")] #[mh(code = 0x1054, hasher = crate::Ripemd256)] Ripemd256, /// RIPEMD-320 (40-byte hash size) + #[cfg(feature = "ripemd")] #[mh(code = 0x1055, hasher = crate::Ripemd320)] Ripemd320, // The following hashes are not cryptographically secure hashes and are not enabled by default @@ -93,6 +120,7 @@ mod tests { use multihash_derive::{Hasher, Multihash}; #[test] + #[cfg(feature = "sha3")] fn test_hasher_256() { let mut hasher = Sha3_256::default(); hasher.update(b"hello world"); @@ -106,6 +134,7 @@ mod tests { } #[test] + #[cfg(feature = "sha3")] fn test_hasher_512() { let mut hasher = Sha3_512::default(); hasher.update(b"hello world"); @@ -119,6 +148,7 @@ mod tests { } #[test] + #[cfg(feature = "sha2")] fn roundtrip() { let hash = Code::Sha2_256.digest(b"hello world"); let mut buf = [0u8; 35]; @@ -129,6 +159,7 @@ mod tests { } #[test] + #[cfg(feature = "sha2")] fn test_truncate_down() { let hash = Code::Sha2_256.digest(b"hello world"); let small = hash.truncate(20); @@ -136,6 +167,7 @@ mod tests { } #[test] + #[cfg(feature = "sha2")] fn test_truncate_up() { let hash = Code::Sha2_256.digest(b"hello world"); let small = hash.truncate(100); @@ -143,18 +175,21 @@ mod tests { } #[test] + #[cfg(feature = "sha2")] fn test_resize_fits() { let hash = Code::Sha2_256.digest(b"hello world"); let _: Multihash<32> = hash.resize().unwrap(); } #[test] + #[cfg(feature = "sha2")] fn test_resize_up() { let hash = Code::Sha2_256.digest(b"hello world"); let _: Multihash<100> = hash.resize().unwrap(); } #[test] + #[cfg(feature = "sha2")] fn test_resize_truncate() { let hash = Code::Sha2_256.digest(b"hello world"); hash.resize::<20>().unwrap_err(); diff --git a/codetable/tests/lib.rs b/codetable/tests/lib.rs index 8e3f1e5f..096da8e1 100644 --- a/codetable/tests/lib.rs +++ b/codetable/tests/lib.rs @@ -2,8 +2,8 @@ use std::io::{Cursor, Write}; use multihash_codetable::{ Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3_256, Identity256, Keccak224, Keccak256, - Keccak384, Keccak512, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384, Sha3_512, - Strobe256, Strobe512, + Keccak384, Keccak512, Ripemd160, Ripemd256, Ripemd320, Sha1, Sha2_256, Sha2_512, Sha3_224, + Sha3_256, Sha3_384, Sha3_512, Strobe256, Strobe512, }; use multihash_derive::{Hasher, MultihashDigest}; diff --git a/derive/Cargo.toml b/derive/Cargo.toml index fa5e0032..df4a2ad7 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -17,4 +17,4 @@ core2 = { version = "0.4.0", default-features = false } [dev-dependencies] trybuild = "1.0.80" -multihash-codetable = { path = "../codetable" } +multihash-codetable = { path = "../codetable", features = ["strobe"] } From 40ea00c0d7968fb913696416d1cda6622f608eb3 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 23 Mar 2023 14:55:00 +0100 Subject: [PATCH 32/39] Only use `serde` feature flag --- Cargo.toml | 2 +- src/multihash.rs | 17 ++++------------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bb07dfd7..580f7fac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ std = ["unsigned-varint/std", "alloc"] alloc = [] arb = ["dep:quickcheck", "dep:rand", "dep:arbitrary"] scale-codec = ["dep:parity-scale-codec"] -serde-codec = ["dep:serde"] # Deprecated, don't use. +serde-codec = ["serde"] # Deprecated, don't use. serde = ["dep:serde", "dep:serde-big-array"] [dependencies] diff --git a/src/multihash.rs b/src/multihash.rs index fb8617b8..cd2b3ab0 100644 --- a/src/multihash.rs +++ b/src/multihash.rs @@ -34,14 +34,8 @@ use core2::io; /// assert_eq!(mh.size(), 32); /// assert_eq!(mh.digest(), &digest_bytes[2..]); /// ``` -#[cfg_attr( - any(feature = "serde-codec", feature = "serde"), - derive(serde::Deserialize) -)] -#[cfg_attr( - any(feature = "serde-codec", feature = "serde"), - derive(serde::Serialize) -)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize))] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] #[derive(Clone, Copy, Debug, Eq, Ord, PartialOrd)] pub struct Multihash { /// The code of the Multihash. @@ -49,10 +43,7 @@ pub struct Multihash { /// The actual size of the digest in bytes (not the allocated size). size: u8, /// The digest. - #[cfg_attr( - any(feature = "serde-codec", feature = "serde"), - serde(with = "serde_big_array::BigArray") - )] + #[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))] digest: [u8; S], } @@ -349,7 +340,7 @@ mod tests { } #[test] - #[cfg(any(feature = "serde-codec", feature = "serde"))] + #[cfg(feature = "serde")] fn test_serde() { let mh = Multihash::<32>::default(); let bytes = serde_json::to_string(&mh).unwrap(); From eb955f2ca8d3928354630eaa5c50fa59abb2e689 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 23 Mar 2023 14:57:05 +0100 Subject: [PATCH 33/39] Restore old feature flags --- codetable/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/codetable/src/lib.rs b/codetable/src/lib.rs index 2d56a19b..01db9466 100644 --- a/codetable/src/lib.rs +++ b/codetable/src/lib.rs @@ -59,11 +59,11 @@ pub enum Code { #[mh(code = 0x14, hasher = crate::Sha3_512)] Sha3_512, /// Keccak-224 (28-byte hash size) - #[cfg(feature = "sha2")] + #[cfg(feature = "sha3")] #[mh(code = 0x1a, hasher = crate::Keccak224)] Keccak224, /// Keccak-256 (32-byte hash size) - #[cfg(feature = "sha2")] + #[cfg(feature = "sha3")] #[mh(code = 0x1b, hasher = crate::Keccak256)] Keccak256, /// Keccak-384 (48-byte hash size) @@ -91,7 +91,7 @@ pub enum Code { #[mh(code = 0xb260, hasher = crate::Blake2s256)] Blake2s256, /// BLAKE3-256 (32-byte hash size) - #[cfg(feature = "blake2s")] + #[cfg(feature = "blake3")] #[mh(code = 0x1e, hasher = crate::Blake3_256)] Blake3_256, /// RIPEMD-160 (20-byte hash size) From 4f4e34d96c9e41f04f937eb30c713bc30697d13c Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 23 Mar 2023 14:57:13 +0100 Subject: [PATCH 34/39] Merge imports --- codetable/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/codetable/src/lib.rs b/codetable/src/lib.rs index 01db9466..d9333015 100644 --- a/codetable/src/lib.rs +++ b/codetable/src/lib.rs @@ -116,8 +116,7 @@ pub enum Code { mod tests { use super::*; use crate::hasher_impl::sha3::{Sha3_256, Sha3_512}; - use multihash_derive::MultihashDigest; - use multihash_derive::{Hasher, Multihash}; + use multihash_derive::{Hasher, Multihash, MultihashDigest}; #[test] #[cfg(feature = "sha3")] From 65b2ebcf16cb3b2f722e8a3cf1153ed953a20c35 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 23 Mar 2023 14:57:18 +0100 Subject: [PATCH 35/39] Remove unnecessary qualification --- codetable/tests/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codetable/tests/lib.rs b/codetable/tests/lib.rs index 096da8e1..7fc63b83 100644 --- a/codetable/tests/lib.rs +++ b/codetable/tests/lib.rs @@ -7,7 +7,7 @@ use multihash_codetable::{ }; use multihash_derive::{Hasher, MultihashDigest}; -#[derive(Clone, Copy, Debug, Eq, multihash_derive::MultihashDigest, PartialEq)] +#[derive(Clone, Copy, Debug, Eq, MultihashDigest, PartialEq)] #[mh(alloc_size = 64)] pub enum Code { #[mh(code = 0x00, hasher = Identity256)] From efc5caa0f55f2ed7e8979cfccef207ade9d1da48 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 29 Mar 2023 23:14:27 +0200 Subject: [PATCH 36/39] Restore identity hasher --- codetable/Cargo.toml | 1 + codetable/src/hasher_impl.rs | 1 + codetable/src/lib.rs | 2 ++ 3 files changed, 4 insertions(+) diff --git a/codetable/Cargo.toml b/codetable/Cargo.toml index 91dc4363..713747ba 100644 --- a/codetable/Cargo.toml +++ b/codetable/Cargo.toml @@ -15,6 +15,7 @@ sha1 = ["dep:sha-1"] strobe = ["dep:strobe-rs"] blake2b = ["dep:blake2b_simd"] blake2s = ["dep:blake2s_simd"] +identity = [] [dependencies] blake2b_simd = { version = "1.0.0", default-features = false, optional = true } diff --git a/codetable/src/hasher_impl.rs b/codetable/src/hasher_impl.rs index bf6a55dd..4cc4e3ab 100644 --- a/codetable/src/hasher_impl.rs +++ b/codetable/src/hasher_impl.rs @@ -237,6 +237,7 @@ pub mod ripemd { derive_rustcrypto_hasher!(::ripemd::Ripemd320, Ripemd320, 40); } +#[cfg(feature = "identity")] pub mod identity { use super::*; diff --git a/codetable/src/lib.rs b/codetable/src/lib.rs index d9333015..e084a4aa 100644 --- a/codetable/src/lib.rs +++ b/codetable/src/lib.rs @@ -106,8 +106,10 @@ pub enum Code { #[cfg(feature = "ripemd")] #[mh(code = 0x1055, hasher = crate::Ripemd320)] Ripemd320, + // The following hashes are not cryptographically secure hashes and are not enabled by default /// Identity hash (max. 64 bytes) + #[cfg(feature = "identity")] #[mh(code = 0x00, hasher = crate::IdentityHasher::<64>)] Identity, } From d48ac2c4fc97c4ce25c5623dc2de1e67cff40def Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 29 Mar 2023 23:16:32 +0200 Subject: [PATCH 37/39] Correctly feature-gate export --- codetable/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/codetable/src/lib.rs b/codetable/src/lib.rs index e084a4aa..8eade693 100644 --- a/codetable/src/lib.rs +++ b/codetable/src/lib.rs @@ -10,6 +10,7 @@ pub use crate::hasher_impl::blake2b::{Blake2b256, Blake2b512, Blake2bHasher}; pub use crate::hasher_impl::blake2s::{Blake2s128, Blake2s256, Blake2sHasher}; #[cfg(feature = "blake3")] pub use crate::hasher_impl::blake3::{Blake3Hasher, Blake3_256}; +#[cfg(feature = "identity")] pub use crate::hasher_impl::identity::{Identity256, IdentityHasher}; #[cfg(feature = "ripemd")] pub use crate::hasher_impl::ripemd::{Ripemd160, Ripemd256, Ripemd320}; From b73894cecffb42e29a2fb64348c5ee105fdf1019 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 30 Mar 2023 23:31:15 +0200 Subject: [PATCH 38/39] Replace imports with full-qualified references to avoid `cfg`s --- codetable/src/hasher_impl.rs | 47 ++++++++++++------------------------ 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/codetable/src/hasher_impl.rs b/codetable/src/hasher_impl.rs index 4cc4e3ab..d7e9bf36 100644 --- a/codetable/src/hasher_impl.rs +++ b/codetable/src/hasher_impl.rs @@ -1,16 +1,15 @@ -use multihash_derive::Hasher; - -use core2::io; - +#[cfg(any(feature = "strobe", feature = "identity", feature = "blake3"))] macro_rules! derive_write { ($name:ident) => { - impl io::Write for $name { - fn write(&mut self, buf: &[u8]) -> io::Result { + impl core2::io::Write for $name { + fn write(&mut self, buf: &[u8]) -> core2::io::Result { + use multihash_derive::Hasher as _; + self.update(buf); Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { + fn flush(&mut self) -> core2::io::Result<()> { Ok(()) } } @@ -38,7 +37,7 @@ macro_rules! derive_hasher_blake { } } - impl Hasher for $name { + impl multihash_derive::Hasher for $name { fn update(&mut self, input: &[u8]) { self.state.update(input); } @@ -63,8 +62,6 @@ macro_rules! derive_hasher_blake { #[cfg(feature = "blake2b")] pub mod blake2b { - use super::*; - derive_hasher_blake!(blake2b_simd, Blake2bHasher); /// 256 bit blake2b hasher. @@ -76,8 +73,6 @@ pub mod blake2b { #[cfg(feature = "blake2s")] pub mod blake2s { - use super::*; - derive_hasher_blake!(blake2s_simd, Blake2sHasher); /// 256 bit blake2b hasher. @@ -89,8 +84,6 @@ pub mod blake2s { #[cfg(feature = "blake3")] pub mod blake3 { - use super::*; - /// Multihash hasher. #[derive(Debug)] pub struct Blake3Hasher { @@ -117,7 +110,7 @@ pub mod blake3 { } } - impl Hasher for Blake3Hasher { + impl multihash_derive::Hasher for Blake3Hasher { fn update(&mut self, input: &[u8]) { self.hasher.update(input); } @@ -184,13 +177,15 @@ macro_rules! derive_rustcrypto_hasher { } } - impl io::Write for $name { - fn write(&mut self, buf: &[u8]) -> io::Result { + impl core2::io::Write for $name { + fn write(&mut self, buf: &[u8]) -> core2::io::Result { + use multihash_derive::Hasher as _; + self.update(buf); Ok(buf.len()) } - fn flush(&mut self) -> io::Result<()> { + fn flush(&mut self) -> core2::io::Result<()> { Ok(()) } } @@ -199,23 +194,17 @@ macro_rules! derive_rustcrypto_hasher { #[cfg(feature = "sha1")] pub mod sha1 { - use super::*; - derive_rustcrypto_hasher!(::sha1::Sha1, Sha1, 20); } #[cfg(feature = "sha2")] pub mod sha2 { - use super::*; - derive_rustcrypto_hasher!(::sha2::Sha256, Sha2_256, 32); derive_rustcrypto_hasher!(::sha2::Sha512, Sha2_512, 64); } #[cfg(feature = "sha3")] pub mod sha3 { - use super::*; - derive_rustcrypto_hasher!(::sha3::Sha3_224, Sha3_224, 28); derive_rustcrypto_hasher!(::sha3::Sha3_256, Sha3_256, 32); derive_rustcrypto_hasher!(::sha3::Sha3_384, Sha3_384, 48); @@ -229,9 +218,6 @@ pub mod sha3 { #[cfg(feature = "ripemd")] pub mod ripemd { - - use super::*; - derive_rustcrypto_hasher!(::ripemd::Ripemd160, Ripemd160, 20); derive_rustcrypto_hasher!(::ripemd::Ripemd256, Ripemd256, 32); derive_rustcrypto_hasher!(::ripemd::Ripemd320, Ripemd320, 40); @@ -239,8 +225,6 @@ pub mod ripemd { #[cfg(feature = "identity")] pub mod identity { - use super::*; - /// Identity hasher with a maximum size. /// /// # Panics @@ -261,7 +245,7 @@ pub mod identity { } } - impl Hasher for IdentityHasher { + impl multihash_derive::Hasher for IdentityHasher { fn update(&mut self, input: &[u8]) { let start = self.i.min(self.bytes.len()); let end = (self.i + input.len()).min(self.bytes.len()); @@ -290,7 +274,6 @@ pub mod identity { #[cfg(feature = "strobe")] pub mod strobe { - use super::*; use strobe_rs::{SecParam, Strobe}; /// Strobe hasher. @@ -310,7 +293,7 @@ pub mod strobe { } } - impl Hasher for StrobeHasher { + impl multihash_derive::Hasher for StrobeHasher { fn update(&mut self, input: &[u8]) { self.strobe.ad(input, self.initialized); self.initialized = true; From f5e46ca365e04aa4e849b8ac76c6da8b211a09ef Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 30 Mar 2023 23:38:13 +0200 Subject: [PATCH 39/39] Fix test --- derive/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/derive/Cargo.toml b/derive/Cargo.toml index df4a2ad7..983f072f 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -17,4 +17,4 @@ core2 = { version = "0.4.0", default-features = false } [dev-dependencies] trybuild = "1.0.80" -multihash-codetable = { path = "../codetable", features = ["strobe"] } +multihash-codetable = { path = "../codetable", features = ["strobe", "identity"] }