Skip to content

Commit 692353d

Browse files
authored
refactor: improve no_std support for slh-dsa (#956)
1 parent 2f10b1b commit 692353d

File tree

8 files changed

+19
-16
lines changed

8 files changed

+19
-16
lines changed

slh-dsa/src/fors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ impl<P: ForsParams> ForsSignature<P> {
105105
}
106106

107107
#[cfg(feature = "alloc")]
108-
pub fn to_vec(&self) -> Vec<u8> {
109-
let mut v = vec![0u8; Self::SIZE];
108+
pub fn to_vec(&self) -> alloc::vec::Vec<u8> {
109+
let mut v = alloc::vec![0u8; Self::SIZE];
110110
self.write_to(&mut v);
111111
v
112112
}

slh-dsa/src/hypertree.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ impl<P: HypertreeParams> HypertreeSig<P> {
2828
}
2929

3030
#[cfg(feature = "alloc")]
31-
pub fn to_vec(&self) -> Vec<u8> {
32-
let mut buf = vec![0u8; Self::SIZE];
31+
pub fn to_vec(&self) -> alloc::vec::Vec<u8> {
32+
let mut buf = alloc::vec![0u8; Self::SIZE];
3333
self.write_to(&mut buf);
3434
buf
3535
}

slh-dsa/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg_attr(not(feature = "alloc"), no_std)]
1+
#![no_std]
22
#![doc = include_str!("../README.md")]
33
#![warn(clippy::pedantic)] // Be pedantic by default
44
//#![allow(non_snake_case)] // Allow notation matching the spec
@@ -46,6 +46,9 @@
4646
//! assert!(vk_deserialized.verify(message, &sig).is_ok())
4747
//! ```
4848
49+
#[cfg(feature = "alloc")]
50+
extern crate alloc;
51+
4952
pub use signature;
5053

5154
mod address;

slh-dsa/src/signature_encoding.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ pub struct Signature<P: ParameterSet> {
3333
impl<P: ParameterSet> Signature<P> {
3434
#[cfg(feature = "alloc")]
3535
/// Serialize the signature to a `Vec<u8>` of length `P::SigLen`.
36-
pub fn to_vec(&self) -> Vec<u8> {
37-
let mut bytes = Vec::with_capacity(P::SigLen::USIZE);
36+
pub fn to_vec(&self) -> alloc::vec::Vec<u8> {
37+
let mut bytes = alloc::vec::Vec::with_capacity(P::SigLen::USIZE);
3838
bytes.extend_from_slice(&self.randomizer);
3939
bytes.extend_from_slice(&self.fors_sig.to_vec());
4040
bytes.extend_from_slice(&self.ht_sig.to_vec());
@@ -81,8 +81,8 @@ impl<P: ParameterSet> TryFrom<&[u8]> for Signature<P> {
8181
}
8282

8383
#[cfg(feature = "alloc")]
84-
impl<P: ParameterSet> From<&Signature<P>> for Vec<u8> {
85-
fn from(sig: &Signature<P>) -> Vec<u8> {
84+
impl<P: ParameterSet> From<&Signature<P>> for alloc::vec::Vec<u8> {
85+
fn from(sig: &Signature<P>) -> alloc::vec::Vec<u8> {
8686
sig.to_vec()
8787
}
8888
}
@@ -209,7 +209,7 @@ mod tests {
209209
let sk = SigningKey::<P>::new(&mut rng);
210210
let msg = b"Hello, world!";
211211
let sig = sk.try_sign(msg).unwrap();
212-
let sig_vec: Vec<u8> = (&sig).into();
212+
let sig_vec: alloc::vec::Vec<u8> = (&sig).into();
213213
assert_eq!(
214214
sig.encoded_len(),
215215
sig_vec.len(),

slh-dsa/src/signing_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl<P: ParameterSet> SigningKey<P> {
188188

189189
/// Serialize the signing key to a new heap-allocated vector
190190
#[cfg(feature = "alloc")]
191-
pub fn to_vec(&self) -> Vec<u8>
191+
pub fn to_vec(&self) -> alloc::vec::Vec<u8>
192192
where
193193
P: VerifyingKeyLen,
194194
{

slh-dsa/src/verifying_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<P: ParameterSet + VerifyingKeyLen> VerifyingKey<P> {
105105

106106
/// Serialize the verifying key to a new heap-allocated vector
107107
#[cfg(feature = "alloc")]
108-
pub fn to_vec(&self) -> Vec<u8> {
108+
pub fn to_vec(&self) -> alloc::vec::Vec<u8> {
109109
self.to_bytes().to_vec()
110110
}
111111
}

slh-dsa/src/wots.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ impl<P: WotsParams> WotsSig<P> {
3030

3131
#[cfg(feature = "alloc")]
3232
#[cfg(test)]
33-
pub fn to_vec(&self) -> Vec<u8> {
34-
let mut vec = vec![0u8; Self::SIZE];
33+
pub fn to_vec(&self) -> alloc::vec::Vec<u8> {
34+
let mut vec = alloc::vec![0u8; Self::SIZE];
3535
self.write_to(&mut vec);
3636
vec
3737
}

slh-dsa/src/xmss.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ impl<P: XmssParams> XmssSig<P> {
2727

2828
#[cfg(feature = "alloc")]
2929
#[cfg(test)]
30-
pub fn to_vec(&self) -> Vec<u8> {
31-
let mut buf = vec![0u8; Self::SIZE];
30+
pub fn to_vec(&self) -> alloc::vec::Vec<u8> {
31+
let mut buf = alloc::vec![0u8; Self::SIZE];
3232
self.write_to(&mut buf);
3333
buf
3434
}

0 commit comments

Comments
 (0)