Skip to content

Commit 0437180

Browse files
committed
Smaller dep surface
1 parent ecd5bed commit 0437180

4 files changed

Lines changed: 46 additions & 34 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ pqc = []
1313
warnings = "deny"
1414

1515
[dependencies]
16-
hex = "0.4"
1716
openssl-sys = "0.9"
18-
typed-arena = "2"
19-
cborrs = { git = "https://github.com/maxtropets/everparse.git", branch = "f/unique-cargo-crates" }
20-
cborrs-nondet = { git = "https://github.com/maxtropets/everparse.git", branch = "f/unique-cargo-crates" }
17+
cborrs = { git = "https://github.com/project-everest/everparse.git", tag = "v2026.02.25" }
18+
cborrs-nondet = { git = "https://github.com/project-everest/everparse.git", tag = "v2026.02.25" }

src/cbor.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
use cborrs::cbordet::*;
22
use cborrs_nondet::cbornondet::*;
3-
use typed_arena::Arena;
3+
4+
struct SimpleArena<T>(std::cell::RefCell<Vec<Box<[T]>>>);
5+
6+
impl<T> SimpleArena<T> {
7+
fn new() -> Self {
8+
Self(std::cell::RefCell::new(Vec::new()))
9+
}
10+
11+
fn alloc(&self, val: T) -> &mut T {
12+
self.alloc_extend(std::iter::once(val)).first_mut().unwrap()
13+
}
14+
15+
fn alloc_extend(&self, vals: impl IntoIterator<Item = T>) -> &mut [T] {
16+
let boxed: Box<[T]> = vals.into_iter().collect();
17+
let mut store = self.0.borrow_mut();
18+
store.push(boxed);
19+
let slot = store.last_mut().unwrap();
20+
// SAFETY: The returned reference borrows `self`, which owns the
21+
// backing storage. Items are never moved or removed, so the
22+
// reference remains valid for the lifetime of the arena.
23+
unsafe { &mut *(slot.as_mut() as *mut [T]) }
24+
}
25+
}
426

527
/// An owned CBOR value supporting arbitrary nesting.
628
///
@@ -28,8 +50,8 @@ impl CborValue {
2850

2951
/// Serialize this value to deterministic CBOR bytes.
3052
pub fn to_bytes(&self) -> Result<Vec<u8>, String> {
31-
let item_arena: Arena<CborDet<'_>> = Arena::new();
32-
let entry_arena: Arena<CborDetMapEntry<'_>> = Arena::new();
53+
let item_arena: SimpleArena<CborDet<'_>> = SimpleArena::new();
54+
let entry_arena: SimpleArena<CborDetMapEntry<'_>> = SimpleArena::new();
3355
let raw = self.to_raw(&item_arena, &entry_arena)?;
3456
serialize_det(raw)
3557
}
@@ -41,8 +63,8 @@ impl CborValue {
4163
/// exactly once.
4264
fn to_raw<'a>(
4365
&'a self,
44-
items: &'a Arena<CborDet<'a>>,
45-
entries: &'a Arena<CborDetMapEntry<'a>>,
66+
items: &'a SimpleArena<CborDet<'a>>,
67+
entries: &'a SimpleArena<CborDetMapEntry<'a>>,
4668
) -> Result<CborDet<'a>, String> {
4769
match self {
4870
CborValue::Int(v) => {

src/cose.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,21 +217,27 @@ pub fn cose_verify1(
217217
#[cfg(test)]
218218
mod tests {
219219
use super::*;
220-
use hex;
220+
fn hex_decode(s: &str) -> Vec<u8> {
221+
assert!(s.len() % 2 == 0, "odd-length hex string");
222+
(0..s.len())
223+
.step_by(2)
224+
.map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap())
225+
.collect()
226+
}
221227

222228
const TEST_PHDR: &str = "A319018B020FA3061A698B72820173736572766963652E6578616D706C652E636F6D02706C65646765722E7369676E6174757265666363662E7631A1647478696465322E313334";
223229

224230
#[test]
225231
fn test_parse_cose() {
226232
let in_str = "d284588da50138220458406661363331386532666561643537313035326231383230393236653865653531313030623630633161383239393362333031353133383561623334343237303019018b020fa3061a698b72820173736572766963652e6578616d706c652e636f6d02706c65646765722e7369676e6174757265666363662e7631a1647478696465322e313334a119018ca12081590100a2018358204208b5b5378c253f49641ab2edb58b557c75cdbb85ae9327930362c84ebba694784963653a322e3133333a3066646666336265663338346237383231316363336434306463363333663336383364353963643930303864613037653030623266356464323734613365633758200000000000000000000000000000000000000000000000000000000000000000028382f5582081980abb4e161b2f3d306c185ef9f7ce84cf5a3b0c8978da82e049d761adfd0082f55820610e8b89721667f99305e7ce4befe0b3b393821a3f72713f89961ebc7e81de6382f55820cbe0d3307b00aa9f324e29c8fb26508404af81044c7adcd4f5b41043d92aff23f6586005784bfccce87452a35a0cd14df5ed8a38c8937f63fb6b522fb94a1551c0e061893bb35fba1fa6fea322b080a14c0894c3864bf4e76df04ffb0f7c350366f91c0d522652d8fa3ebad6ba0270b48e43a065312c759d8bc9a413d4270d5ba86182";
227-
let v = hex::decode(in_str).unwrap();
233+
let v = hex_decode(in_str);
228234
let (_phdr, _payload, _sig) = parse_cose_sign1(&v).unwrap();
229235
}
230236

231237
#[test]
232238
fn test_insert_alg() {
233239
let key = EvpKey::new(KeyType::EC(WhichEC::P256)).unwrap();
234-
let phdr_bytes = hex::decode(TEST_PHDR).unwrap();
240+
let phdr_bytes = hex_decode(TEST_PHDR);
235241
let phdr = CborValue::from_bytes(&phdr_bytes).unwrap();
236242
let phdr_with_alg = insert_alg_value(&key, phdr).unwrap();
237243

@@ -245,7 +251,7 @@ mod tests {
245251

246252
fn sign_verify_cose_encoded(key_type: KeyType) {
247253
let key = EvpKey::new(key_type).unwrap();
248-
let phdr = hex::decode(TEST_PHDR).unwrap();
254+
let phdr = hex_decode(TEST_PHDR);
249255
let uhdr = b"\xa0"; // empty map
250256
let payload = b"Good boy...";
251257

@@ -256,7 +262,7 @@ mod tests {
256262

257263
fn sign_verify_cose(key_type: KeyType) {
258264
let key = EvpKey::new(key_type).unwrap();
259-
let phdr_bytes = hex::decode(TEST_PHDR).unwrap();
265+
let phdr_bytes = hex_decode(TEST_PHDR);
260266
let phdr = CborValue::from_bytes(&phdr_bytes).unwrap();
261267
let uhdr = CborValue::Map(vec![]); // empty map
262268
let payload = b"Good boy...";
@@ -298,7 +304,7 @@ mod tests {
298304
#[test]
299305
fn cose_detached_payload() {
300306
let key = EvpKey::new(KeyType::EC(WhichEC::P256)).unwrap();
301-
let phdr_bytes = hex::decode(TEST_PHDR).unwrap();
307+
let phdr_bytes = hex_decode(TEST_PHDR);
302308
let phdr = CborValue::from_bytes(&phdr_bytes).unwrap();
303309
let uhdr = CborValue::Map(vec![]);
304310
let payload = b"Good boy...";
@@ -315,7 +321,7 @@ mod tests {
315321
#[test]
316322
fn cose_detached_payload_encoded() {
317323
let key = EvpKey::new(KeyType::EC(WhichEC::P256)).unwrap();
318-
let phdr = hex::decode(TEST_PHDR).unwrap();
324+
let phdr = hex_decode(TEST_PHDR);
319325
let uhdr = b"\xa0"; // empty map
320326
let payload = b"Good boy...";
321327

@@ -342,7 +348,7 @@ mod tests {
342348
let pub_der = original_key.to_der_public().unwrap();
343349
let verification_key = EvpKey::from_der_public(&pub_der).unwrap();
344350

345-
let phdr_bytes = hex::decode(TEST_PHDR).unwrap();
351+
let phdr_bytes = hex_decode(TEST_PHDR);
346352
let phdr = CborValue::from_bytes(&phdr_bytes).unwrap();
347353
let uhdr = CborValue::Map(vec![]);
348354
let payload = b"test with DER-imported key";
@@ -385,7 +391,7 @@ mod tests {
385391
let pub_der = original_key.to_der_public().unwrap();
386392
let verification_key = EvpKey::from_der_public(&pub_der).unwrap();
387393

388-
let phdr_bytes = hex::decode(TEST_PHDR).unwrap();
394+
let phdr_bytes = hex_decode(TEST_PHDR);
389395
let phdr = CborValue::from_bytes(&phdr_bytes).unwrap();
390396
let uhdr = CborValue::Map(vec![]);
391397
let payload = b"ML-DSA with DER-imported key";

0 commit comments

Comments
 (0)