Skip to content

Commit 964e4f3

Browse files
committed
feat: Add no_std support by migrating from std to core and alloc and updating dependencies.
1 parent 25349eb commit 964e4f3

11 files changed

Lines changed: 56 additions & 38 deletions

File tree

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ license = "MIT"
1414

1515
[dependencies]
1616
base16ct = "0.3.0"
17-
base64ct = { version = "1.8.0", features = ["alloc"] }
18-
ed25519-dalek = "2.2.0"
19-
sha2 = "0.10.8"
20-
thiserror = "2.0.17"
17+
base64ct = { version = "1.8.0", default-features = false, features = ["alloc"] }
18+
ed25519-dalek = { version = "2.2.0", default-features = false, features = ["alloc"] }
19+
sha2 = { version = "0.10.8", default-features = false }
20+
spin = "0.9"
21+
thiserror = { version = "2.0.17", default-features = false }
2122

2223
[dev-dependencies]
2324
hex-literal = "1.0.0"

src/crypto.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt;
1+
use core::fmt;
22

33
use ed25519_dalek::ed25519::signature::Verifier as _;
44
use sha2::{Digest, Sha256};
@@ -31,7 +31,7 @@ pub struct Signature {
3131
}
3232

3333
/// A SHA256 digest.
34-
#[derive(Clone, Eq, Hash, PartialEq)]
34+
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
3535
pub struct Hash {
3636
bytes: [u8; HASH_SIZE],
3737
}
@@ -89,6 +89,7 @@ boilerplate!(Signature, 64);
8989

9090
#[cfg(test)]
9191
mod tests {
92+
use alloc::format;
9293
use hex_literal::hex;
9394

9495
use super::*;

src/io/ascii/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
mod parser;
22

33
// This needs to be in scope to write!() to a String
4-
use std::fmt::Write as _;
4+
use alloc::{format, string::String, vec::Vec};
5+
use core::fmt::Write as _;
56

67
use crate::{
78
Hash, InclusionProof, Protoleaf, Signature, SignedTreeHead, SigsumSignature, WitnessCosignature,
@@ -175,6 +176,7 @@ impl SigsumSignature {
175176

176177
#[cfg(test)]
177178
mod tests {
179+
use alloc::{string::ToString, vec};
178180
use super::*;
179181
use hex_literal::hex;
180182
use lazy_static::lazy_static;

src/io/ascii/parser.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::error::Error;
2-
use std::fmt;
3-
use std::iter::Peekable;
4-
use std::str::Lines;
1+
use alloc::{format, string::String, vec::Vec};
2+
use core::error::Error;
3+
use core::fmt;
4+
use core::iter::Peekable;
5+
use core::str::Lines;
56

67
use crate::{
78
crypto::{HASH_SIZE, PUBKEY_SIZE, SIGNATURE_SIZE},
@@ -28,7 +29,7 @@ macro_rules! bail {
2829
};
2930
}
3031

31-
pub(crate) type Result<T> = std::result::Result<T, ParseAsciiError>;
32+
pub(crate) type Result<T> = core::result::Result<T, ParseAsciiError>;
3233

3334
pub(crate) struct Parser<'a> {
3435
lines: Peekable<Lines<'a>>,
@@ -150,6 +151,7 @@ fn hexchar(ch: u8) -> Result<u8> {
150151

151152
#[cfg(test)]
152153
mod tests {
154+
use alloc::string::ToString;
153155
use super::*;
154156

155157
#[test]

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
//!
6161
//! Please make sure to update tests as appropriate.
6262
63+
#![no_std]
64+
65+
extern crate alloc;
66+
6367
mod io {
6468
pub(crate) mod ascii;
6569
}

src/log.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Implements types related to the sigsum log
22

3+
use alloc::vec::Vec;
4+
35
use crate::crypto::{Hash, PublicKey, Signature};
46

57
/// A signed tree head, as returned by the get-tree-head endpoint.

src/merkle/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// We want to keep the code as close as possible as the spec. So we ignore clippy warningcs.
22
#![allow(clippy::all)]
33

4+
use alloc::vec::Vec;
45
use sha2::{Digest, Sha256};
56

67
type Hash = [u8; 32];
@@ -61,6 +62,7 @@ pub fn lsb(n: u64) -> bool {
6162

6263
#[cfg(test)]
6364
mod tests {
65+
use alloc::{string::String, vec::Vec};
6466
use super::*;
6567

6668
use base64ct::{Base64, Encoding};

src/policy/builtin.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//! and used directly as public statics, or looked up at runtime by name via
55
//! [`Policy::builtin`].
66
7-
use std::ops::Deref;
8-
use std::sync::LazyLock;
7+
use core::ops::Deref;
8+
use spin::Lazy;
99

1010
use super::Policy;
1111

@@ -20,7 +20,7 @@ pub struct BuiltInPolicy {
2020
/// The user-friendly name of the built-in policy.
2121
pub name: &'static str,
2222

23-
policy: LazyLock<Policy>,
23+
policy: Lazy<Policy>,
2424
}
2525

2626
impl BuiltInPolicy {
@@ -62,7 +62,7 @@ macro_rules! define_builtin_policies {
6262
#[doc = "```"]
6363
pub static $const_name: BuiltInPolicy = BuiltInPolicy {
6464
name: $policy_name,
65-
policy: LazyLock::new(|| {
65+
policy: Lazy::new(|| {
6666
Policy::parse(include_str!(concat!(
6767
"../../builtin-policies/",
6868
$policy_name,

src/policy/mod.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
//! let policy = builder.build();
7373
//! ```
7474
75-
use std::collections::HashMap;
75+
use alloc::{collections::BTreeMap, string::String, vec::Vec};
7676

7777
use crate::crypto::{Hash, PublicKey};
7878

@@ -93,13 +93,13 @@ pub use parsing::ParsePolicyError;
9393
#[derive(Debug, Eq, PartialEq)]
9494
pub struct Policy {
9595
// logs keeps the list of log keys and URLs indexed by keyhash.
96-
logs: HashMap<Hash, Entity>,
96+
logs: BTreeMap<Hash, Entity>,
9797

9898
// witnesses keeps the list of witness keys and URLs indexed by keyhash.
99-
witnesses: HashMap<Hash, Entity>,
99+
witnesses: BTreeMap<Hash, Entity>,
100100

101101
// quorums keeps the quorum entries (witnesses and groups), indexed by name.
102-
quorums: HashMap<String, Quorum>,
102+
quorums: BTreeMap<String, Quorum>,
103103

104104
// quorum is the quorum required by the policy. Invariant: if present, quorum must be an
105105
// existing key in quorums.
@@ -120,7 +120,7 @@ pub struct Witness {
120120
}
121121

122122
pub struct Logs<'a> {
123-
inner: std::collections::hash_map::Iter<'a, Hash, Entity>,
123+
inner: alloc::collections::btree_map::Iter<'a, Hash, Entity>,
124124
}
125125

126126
impl Iterator for Logs<'_> {
@@ -252,9 +252,9 @@ impl Default for PolicyBuilder {
252252
impl PolicyBuilder {
253253
pub fn new() -> Self {
254254
Self(Policy {
255-
logs: HashMap::new(),
256-
witnesses: HashMap::new(),
257-
quorums: HashMap::new(),
255+
logs: BTreeMap::new(),
256+
witnesses: BTreeMap::new(),
257+
quorums: BTreeMap::new(),
258258
quorum: None,
259259
})
260260
}
@@ -325,6 +325,7 @@ impl PolicyBuilder {
325325

326326
#[cfg(test)]
327327
mod tests {
328+
use alloc::{string::String, string::ToString, vec};
328329
use hex_literal::hex;
329330

330331
use super::*;
@@ -333,9 +334,9 @@ mod tests {
333334
fn build_empty_policy() {
334335
let builder = PolicyBuilder::new();
335336
let expected = Policy {
336-
logs: HashMap::new(),
337-
witnesses: HashMap::new(),
338-
quorums: HashMap::new(),
337+
logs: BTreeMap::new(),
338+
witnesses: BTreeMap::new(),
339+
quorums: BTreeMap::new(),
339340
quorum: None,
340341
};
341342
assert_eq!(expected, builder.build());
@@ -379,7 +380,7 @@ mod tests {
379380
.unwrap();
380381
builder.set_quorum(String::from("mygroup")).unwrap();
381382
let expected = Policy {
382-
logs: HashMap::from([
383+
logs: BTreeMap::from([
383384
(
384385
hex!("e919506c3a798f2030f14046e39f03773c12b390e1010c95d2256d0ae594354e").into(),
385386
Entity(
@@ -397,7 +398,7 @@ mod tests {
397398
),
398399
),
399400
]),
400-
witnesses: HashMap::from([
401+
witnesses: BTreeMap::from([
401402
(
402403
hex!("d9440882ae2bd57076d4da2e7a12d4b26e137d56116419a69f8d6969709ed747").into(),
403404
Entity(
@@ -415,7 +416,7 @@ mod tests {
415416
),
416417
),
417418
]),
418-
quorums: HashMap::from([
419+
quorums: BTreeMap::from([
419420
(
420421
"witness01".into(),
421422
Quorum::Witness(

src/policy/parsing.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use std::iter::Enumerate;
2-
use std::str::Lines;
1+
use alloc::{format, string::String, string::ToString, vec::Vec};
2+
use core::iter::Enumerate;
3+
use core::str::Lines;
34

45
use super::{Policy, PolicyBuilder};
56
use crate::crypto::PublicKey;
@@ -17,7 +18,7 @@ macro_rules! bail {
1718
};
1819
}
1920

20-
type Result<T> = std::result::Result<T, ParsePolicyError>;
21+
type Result<T> = core::result::Result<T, ParsePolicyError>;
2122

2223
impl Policy {
2324
/// Parse a policy from its string representation according to the
@@ -161,6 +162,7 @@ impl<'a> Iterator for PolicyLines<'a> {
161162

162163
#[cfg(test)]
163164
mod tests {
165+
use alloc::{vec, vec::Vec};
164166
use hex_literal::hex;
165167

166168
use super::*;

0 commit comments

Comments
 (0)