Skip to content

Commit df4c41d

Browse files
authored
Merge pull request #1 from cryptidtech/sync-from-bettersign
sync from bettersign
2 parents cade433 + 6ec2d93 commit df4c41d

19 files changed

Lines changed: 3836 additions & 649 deletions

‎CHANGELOG.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [1.0.0] - 2026-07-13
9+
10+
### Changed
11+
- Synced from bettersign workspace (bs-multicodec 0.7.0)
12+
- Renamed crate from `bs-multicodec` to `multi-codec`
13+
- Added `types.rs` module with type-safe codec wrappers
14+
- Added comprehensive test suite (edge cases, error tests, integration, proptest, security)
15+
- Initial published release on crates.io as `multi-codec`

‎Cargo.toml‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
11
[package]
2-
name = "multicodec"
3-
version = "1.0.10"
2+
name = "multi-codec"
3+
version = "1.0.2"
44
edition = "2021"
55
authors = ["Benjamin Kampmann <ben@gnunicorn.org>", "Dave Grantham <dwg@linuxprogrammer.org>"]
66
description = "Implementation of the Multicodec specification"
7-
repository = "https://github.com/cryptidtech/rust-multicodec"
8-
license = "MIT OR Apache-2.0"
7+
repository = "https://github.com/cryptidtech/multi-codec.git"
98
readme = "README.md"
9+
license = "MIT OR Apache-2.0"
1010
keywords = ["multiformats", "multicodec", "serde"]
11+
categories = ["encoding"]
1112

1213
[features]
1314
default = ["serde"]
1415

1516
[dependencies]
16-
multitrait = { version = "1.0", git="https://github.com/cryptidtech/multitrait.git" }
17-
serde = { version = "1.0", default-features = false, optional = true, features = ['serde_derive'] }
18-
thiserror = "1.0"
17+
multi-trait = "1.0"
18+
serde = { version = "1.0", default-features = false, optional = true, features = ["serde_derive"] }
19+
thiserror = { version = "2.0" }
1920

2021
[dev-dependencies]
22+
criterion = { version = "0.8", features = ["html_reports"] }
23+
hex = "0.4"
24+
proptest = "1.4"
2125
serde_cbor = "0.11"
2226
serde_json = "1.0"
2327
serde_test = "1.0"
28+
unsigned-varint = { version = "0.8", features = ["std"] }
29+
30+
[[bench]]
31+
name = "codec_bench"
32+
harness = false
33+
path = "benches/codec_bench.rs"
34+
35+
[[example]]
36+
name = "uvi"
37+
path = "examples/uvi.rs"
2438

2539
[build-dependencies]
2640
convert_case = "0.6"
2741
csv = "1.3"
28-
serde = "1.0"
42+
serde = { version = "1.0", features = ["serde_derive"] }
2943
serde_derive = "1.0"

‎benches/codec_bench.rs‎

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
// SPDX-License-Identifier: MIT or Apache-2.0
2+
//! Performance benchmarks for multi-codec
3+
4+
use std::hint::black_box;
5+
6+
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
7+
use multi_codec::Codec;
8+
use multi_trait::TryDecodeFrom;
9+
10+
/// Benchmark conversions from u64
11+
fn bench_from_u64(c: &mut Criterion) {
12+
c.bench_function("codec_from_u64_valid", |b| {
13+
b.iter(|| Codec::try_from(black_box(0xEDu64)))
14+
});
15+
16+
c.bench_function("codec_from_u64_invalid", |b| {
17+
b.iter(|| Codec::try_from(black_box(0xDEADBEEFu64)))
18+
});
19+
}
20+
21+
/// Benchmark conversions to u64
22+
fn bench_to_u64(c: &mut Criterion) {
23+
let codec = Codec::Ed25519Pub;
24+
25+
c.bench_function("codec_to_u64", |b| {
26+
b.iter(|| {
27+
let _code: u64 = black_box(codec).into();
28+
})
29+
});
30+
31+
c.bench_function("codec_code_method", |b| b.iter(|| black_box(codec).code()));
32+
}
33+
34+
/// Benchmark conversions from string
35+
fn bench_from_str(c: &mut Criterion) {
36+
c.bench_function("codec_from_str_valid", |b| {
37+
b.iter(|| Codec::try_from(black_box("ed25519-pub")))
38+
});
39+
40+
c.bench_function("codec_from_str_invalid", |b| {
41+
b.iter(|| Codec::try_from(black_box("unknown-codec")))
42+
});
43+
}
44+
45+
/// Benchmark conversions to string
46+
fn bench_to_str(c: &mut Criterion) {
47+
c.bench_function("codec_as_str", |b| {
48+
let codec = Codec::Ed25519Pub;
49+
b.iter(|| {
50+
let s = codec.as_str();
51+
black_box(s.len())
52+
})
53+
});
54+
}
55+
56+
/// Benchmark encoding
57+
fn bench_encoding(c: &mut Criterion) {
58+
let mut group = c.benchmark_group("encoding");
59+
60+
let codecs = vec![
61+
("Identity", Codec::Identity),
62+
("Sha2256", Codec::Sha2256),
63+
("Ed25519Pub", Codec::Ed25519Pub),
64+
];
65+
66+
for (name, codec) in codecs {
67+
group.bench_with_input(BenchmarkId::new("into_vec", name), &codec, |b, &codec| {
68+
b.iter(|| {
69+
let _v: Vec<u8> = black_box(codec).into();
70+
})
71+
});
72+
}
73+
74+
group.finish();
75+
}
76+
77+
/// Benchmark decoding
78+
fn bench_decoding(c: &mut Criterion) {
79+
let mut group = c.benchmark_group("decoding");
80+
81+
let test_cases = vec![
82+
("Identity", vec![0x00]),
83+
("Sha2256", vec![0x12]),
84+
("Ed25519Pub", vec![0xED, 0x01]),
85+
];
86+
87+
for (name, bytes) in test_cases {
88+
group.bench_with_input(
89+
BenchmarkId::new("try_decode_from", name),
90+
&bytes,
91+
|b, bytes| b.iter(|| Codec::try_decode_from(black_box(bytes.as_slice()))),
92+
);
93+
}
94+
95+
group.finish();
96+
}
97+
98+
/// Benchmark hash operations
99+
fn bench_hash(c: &mut Criterion) {
100+
use std::collections::hash_map::DefaultHasher;
101+
use std::hash::{Hash, Hasher};
102+
103+
let codec = Codec::Ed25519Pub;
104+
105+
c.bench_function("codec_hash", |b| {
106+
b.iter(|| {
107+
let mut hasher = DefaultHasher::new();
108+
black_box(codec).hash(&mut hasher);
109+
hasher.finish()
110+
})
111+
});
112+
}
113+
114+
/// Benchmark roundtrip operations
115+
fn bench_roundtrip(c: &mut Criterion) {
116+
let mut group = c.benchmark_group("roundtrip");
117+
118+
group.bench_function("encode_decode", |b| {
119+
let codec = Codec::Ed25519Pub;
120+
b.iter(|| {
121+
let encoded: Vec<u8> = black_box(codec).into();
122+
let (decoded, _) = Codec::try_decode_from(black_box(&encoded)).unwrap();
123+
black_box(decoded);
124+
})
125+
});
126+
127+
group.bench_function("str_codec_str", |b| {
128+
b.iter(|| {
129+
let codec = Codec::try_from(black_box("ed25519-pub")).unwrap();
130+
let len = codec.as_str().len();
131+
black_box(len);
132+
})
133+
});
134+
135+
group.bench_function("u64_codec_u64", |b| {
136+
b.iter(|| {
137+
let codec = Codec::try_from(black_box(0xEDu64)).unwrap();
138+
let _code: u64 = codec.into();
139+
})
140+
});
141+
142+
group.finish();
143+
}
144+
145+
/// Benchmark serde operations
146+
#[cfg(feature = "serde")]
147+
fn bench_serde(c: &mut Criterion) {
148+
use serde_json;
149+
150+
let mut group = c.benchmark_group("serde");
151+
152+
let codec = Codec::Ed25519Pub;
153+
154+
group.bench_function("json_serialize", |b| {
155+
b.iter(|| serde_json::to_string(&black_box(codec)))
156+
});
157+
158+
let json = serde_json::to_string(&codec).unwrap();
159+
group.bench_function("json_deserialize", |b| {
160+
b.iter(|| serde_json::from_str::<Codec>(black_box(&json)))
161+
});
162+
163+
group.bench_function("cbor_serialize", |b| {
164+
b.iter(|| serde_cbor::to_vec(&black_box(codec)))
165+
});
166+
167+
let cbor = serde_cbor::to_vec(&codec).unwrap();
168+
group.bench_function("cbor_deserialize", |b| {
169+
b.iter(|| serde_cbor::from_slice::<Codec>(black_box(&cbor)))
170+
});
171+
172+
group.finish();
173+
}
174+
175+
#[cfg(feature = "serde")]
176+
criterion_group!(
177+
benches,
178+
bench_from_u64,
179+
bench_to_u64,
180+
bench_from_str,
181+
bench_to_str,
182+
bench_encoding,
183+
bench_decoding,
184+
bench_hash,
185+
bench_roundtrip,
186+
bench_serde
187+
);
188+
189+
#[cfg(not(feature = "serde"))]
190+
criterion_group!(
191+
benches,
192+
bench_from_u64,
193+
bench_to_u64,
194+
bench_from_str,
195+
bench_to_str,
196+
bench_encoding,
197+
bench_decoding,
198+
bench_hash,
199+
bench_roundtrip
200+
);
201+
202+
criterion_main!(benches);

0 commit comments

Comments
 (0)