Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: split crates into multiple to isolate breaking changes #272

Merged
merged 39 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
7f3c30d
Initial split of crates
thomaseizinger Jan 31, 2023
3932079
Deprecate export of `Multihash`
thomaseizinger Jan 31, 2023
8ca3071
inline synstructure macro
thomaseizinger Jan 31, 2023
91f96c6
Deprecate old macro name in favor of new one
thomaseizinger Jan 31, 2023
d0e8215
Remove `derive` feature flag
thomaseizinger Jan 31, 2023
bcf8135
Remove unnecessary `pub` qualifier
thomaseizinger Jan 31, 2023
9cee44e
Remove unnecessary feature-gated import
thomaseizinger Jan 31, 2023
c1e5355
Deprecate `serde-codec`
thomaseizinger Jan 31, 2023
2dbeded
Remove git blame file
thomaseizinger Jan 31, 2023
f3a4c8b
Run rustfmt
thomaseizinger Mar 21, 2023
d9aa346
Reduce number of `cfg`s
thomaseizinger Mar 21, 2023
2bc4101
Remove `Result` type
thomaseizinger Mar 21, 2023
2a96940
Make `Error` opaque
thomaseizinger Mar 21, 2023
aab78df
Introduce `multihash-derive-impl` crate
thomaseizinger Mar 21, 2023
e2e2229
Fix clippy lint
thomaseizinger Mar 21, 2023
c32ce92
Only build multihash for no_std
thomaseizinger Mar 21, 2023
cb0d728
Update docs
thomaseizinger Mar 21, 2023
abbb7eb
Update docs for multihash-codetable
thomaseizinger Mar 21, 2023
c502465
Remove outdated docs
thomaseizinger Mar 21, 2023
e67be12
Replace macro tests with trybuild
thomaseizinger Mar 21, 2023
4d3048c
Remove unused feature `std` from `multihash`
thomaseizinger Mar 21, 2023
5a97fdc
Export deprecated macro name
thomaseizinger Mar 21, 2023
0b85ce9
Reduce diff
thomaseizinger Mar 21, 2023
960ec0e
Minimize diff and fix cargo metadata
thomaseizinger Mar 21, 2023
72601b1
Minimize diff
thomaseizinger Mar 21, 2023
b25bfcb
Remove `extern crate core`
thomaseizinger Mar 22, 2023
c0925a8
Use `dep:` consistently
thomaseizinger Mar 22, 2023
0cb5c77
Use uniform way of referring to error type
thomaseizinger Mar 22, 2023
55759e5
Make everything work on `no_std` again
thomaseizinger Mar 22, 2023
9a679cc
Add back serde support for `Code`
thomaseizinger Mar 22, 2023
c44bfb2
Reintroduce feature-flags in `multihash-codetable`
thomaseizinger Mar 22, 2023
40ea00c
Only use `serde` feature flag
thomaseizinger Mar 23, 2023
eb955f2
Restore old feature flags
thomaseizinger Mar 23, 2023
4f4e34d
Merge imports
thomaseizinger Mar 23, 2023
65b2ebc
Remove unnecessary qualification
thomaseizinger Mar 23, 2023
efc5caa
Restore identity hasher
thomaseizinger Mar 29, 2023
d48ac2c
Correctly feature-gate export
thomaseizinger Mar 29, 2023
b73894c
Replace imports with full-qualified references to avoid `cfg`s
thomaseizinger Mar 30, 2023
f5e46ca
Fix test
thomaseizinger Mar 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 9 additions & 36 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = ["derive", "."]
members = ["derive", "derive-impl", ".", "codetable"]
resolver = "2"

[package]
name = "multihash"
Expand All @@ -15,56 +16,28 @@ 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"]
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"]
default = ["std"]
std = ["unsigned-varint/std", "alloc"]
alloc = []
arb = ["dep:quickcheck", "dep:rand", "dep:arbitrary"]
scale-codec = ["dep:parity-scale-codec"]
serde-codec = ["serde"] # Deprecated, don't use.
serde = ["dep:serde", "dep:serde-big-array"]

[dependencies]
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"] }
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
45 changes: 45 additions & 0 deletions codetable/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
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

[features]
default = ["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, 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 }

[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

[[test]]
name = "lib"
required-features = ["sha1", "sha2", "sha3", "ripemd", "strobe", "blake2b", "blake2s", "blake3"]
9 changes: 5 additions & 4 deletions benches/multihash.rs → codetable/benches/multihash.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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_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;

macro_rules! group_digest {
($criterion:ident, $( $id:expr => $hash:ident, $input:expr)* ) => {{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::convert::TryFrom;

use multihash::derive::Multihash;
use multihash::{Error, Hasher, MultihashDigest, MultihashGeneric, Sha2_256};
use multihash_codetable::Sha2_256;
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.
Expand All @@ -19,14 +19,14 @@ 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
#[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,
}

Expand Down
3 changes: 2 additions & 1 deletion examples/manual_mh.rs → codetable/examples/manual_mh.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use multihash::{Code, MultihashDigest};
use multihash_codetable::Code;
use multihash_derive::MultihashDigest;

/// prefix/multihash generating tool to aid when adding new tests
fn prefix_util() {
Expand Down
43 changes: 22 additions & 21 deletions src/hasher_impl.rs → codetable/src/hasher_impl.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::hasher::Hasher;
use multihash_derive::Hasher;

#[cfg(feature = "std")]
use std::io;

#[cfg(not(feature = "std"))]
use core2::io;

macro_rules! derive_write {
Expand All @@ -21,7 +17,7 @@ macro_rules! derive_write {
};
}

#[cfg(any(feature = "blake2b", feature = "blake2s"))]
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(any(feature = "blake2b", feature = "blake2s", feature = "blake3"))]
macro_rules! derive_hasher_blake {
($module:ident, $name:ident) => {
/// Multihash hasher.
Expand Down Expand Up @@ -143,7 +139,12 @@ pub mod blake3 {
pub type Blake3_256 = Blake3Hasher<32>;
}

#[cfg(feature = "digest")]
#[cfg(any(
feature = "sha1",
feature = "sha2",
feature = "sha3",
feature = "ripemd"
))]
macro_rules! derive_rustcrypto_hasher {
($module:ty, $name:ident, $size:expr) => {
/// Multihash hasher.
Expand All @@ -162,7 +163,7 @@ macro_rules! derive_rustcrypto_hasher {
}
}

impl $crate::hasher::Hasher for $name {
impl ::multihash_derive::Hasher for $name {
fn update(&mut self, input: &[u8]) {
use digest::Digest;
self.state.update(input)
Expand Down Expand Up @@ -207,33 +208,33 @@ pub mod sha1 {
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 {
Expand Down
90 changes: 80 additions & 10 deletions src/multihash_impl.rs → codetable/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
pub use multihash_derive::Multihash;
#![cfg_attr(not(feature = "std"), no_std)]

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};
#[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.
///
/// 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))]
#[derive(Copy, Clone, Debug, Eq, Multihash, PartialEq)]
/// [`multihash-derive`]: https://docs.rs/multihash-derive
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, Debug, Eq, MultihashDigest, PartialEq)]
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
#[mh(alloc_size = 64)]
pub enum Code {
/// SHA-256 (32-byte hash size)
Expand Down Expand Up @@ -83,22 +106,20 @@ 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")]
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
#[mh(code = 0x00, hasher = crate::IdentityHasher::<64>)]
Identity,
}

#[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_derive::{Hasher, Multihash, MultihashDigest};

#[test]
#[cfg(feature = "sha3")]
fn test_hasher_256() {
let mut hasher = Sha3_256::default();
hasher.update(b"hello world");
Expand All @@ -112,6 +133,7 @@ mod tests {
}

#[test]
#[cfg(feature = "sha3")]
fn test_hasher_512() {
let mut hasher = Sha3_512::default();
hasher.update(b"hello world");
Expand All @@ -123,4 +145,52 @@ mod tests {
assert_eq!(hash.digest(), digest);
assert_eq!(hash, hash2);
}

#[test]
#[cfg(feature = "sha2")]
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]
#[cfg(feature = "sha2")]
fn test_truncate_down() {
let hash = Code::Sha2_256.digest(b"hello world");
let small = hash.truncate(20);
assert_eq!(small.size(), 20);
}

#[test]
#[cfg(feature = "sha2")]
fn test_truncate_up() {
let hash = Code::Sha2_256.digest(b"hello world");
let small = hash.truncate(100);
assert_eq!(small.size(), 32);
}

#[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();
}
}
Loading