Skip to content

Commit 093267c

Browse files
authored
ml-dsa: use MaybeBox from module-lattice crate (#1350)
The `MaybeBox` type was moved to `module-lattice` so it could also be used with the `ml-kem` crate. See RustCrypto/KEMs#309
1 parent fb08f19 commit 093267c

5 files changed

Lines changed: 16 additions & 57 deletions

File tree

Cargo.lock

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

ml-dsa/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ zeroize = ["dep:zeroize", "hybrid-array/zeroize", "module-lattice/zeroize"]
3737
common = { package = "crypto-common", version = "0.2", default-features = false }
3838
ctutils = { version = "0.4", default-features = false }
3939
hybrid-array = { version = "0.4", features = ["extra-sizes"] }
40-
module-lattice = { version = "0.2.2", features = ["ctutils"] }
40+
module-lattice = { version = "0.2.3", features = ["ctutils"] }
4141
sha3 = { version = "0.11", default-features = false }
4242
signature = { version = "3", default-features = false, features = ["digest"] }
4343

ml-dsa/src/lib.rs

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,19 @@ pub use signature::{self, Error, Keypair, SignatureEncoding, Signer, Verifier};
6767
#[cfg(feature = "rand_core")]
6868
pub use common::Generate;
6969

70-
use crate::algebra::{AlgebraExt, Vector};
71-
use crate::crypto::H;
72-
use crate::hint::Hint;
73-
use crate::param::{ParameterSet, QMinus1};
74-
use core::{
75-
convert::{TryFrom, TryInto},
76-
ops::{Deref, DerefMut},
70+
use crate::{
71+
algebra::{AlgebraExt, Vector},
72+
crypto::H,
73+
hint::Hint,
74+
param::{ParameterSet, QMinus1},
7775
};
76+
use core::convert::{TryFrom, TryInto};
7877
use hybrid_array::{
7978
Array,
80-
typenum::{
81-
Diff, Length, Prod, Quot, Shleft, U1, U2, U4, U5, U6, U7, U8, U17, U19, U32, U48, U55, U64,
82-
U75, U80, U88,
83-
},
79+
sizes::{U1, U2, U4, U5, U6, U7, U8, U17, U19, U32, U48, U55, U64, U75, U80, U88},
80+
typenum::{Diff, Length, Prod, Quot, Shleft},
8481
};
85-
use module_lattice::Truncate;
82+
use module_lattice::{MaybeBox, Truncate};
8683
use sha3::Shake256;
8784

8885
/// A 32-byte array, defined here for brevity because it is used several times
@@ -255,45 +252,6 @@ impl ParameterSet for MlDsa87 {
255252
const TAU: usize = 60;
256253
}
257254

258-
/// Type which opportunistically uses `Box` when the `alloc` feature is available but falls back to
259-
/// a stack-allocated type when it's unavailable.
260-
#[derive(Clone, Debug, PartialEq)]
261-
pub(crate) struct MaybeBox<T> {
262-
#[cfg(not(feature = "alloc"))]
263-
inner: T,
264-
#[cfg(feature = "alloc")]
265-
inner: alloc::boxed::Box<T>,
266-
}
267-
268-
impl<T> MaybeBox<T> {
269-
/// Create a new `MaybeBox`, using `Box` if `alloc` is available.
270-
#[inline]
271-
pub(crate) fn new(inner: T) -> Self {
272-
#[cfg(not(feature = "alloc"))]
273-
{
274-
Self { inner }
275-
}
276-
#[cfg(feature = "alloc")]
277-
Self {
278-
inner: alloc::boxed::Box::new(inner),
279-
}
280-
}
281-
}
282-
283-
impl<T> Deref for MaybeBox<T> {
284-
type Target = T;
285-
286-
fn deref(&self) -> &Self::Target {
287-
&self.inner
288-
}
289-
}
290-
291-
impl<T> DerefMut for MaybeBox<T> {
292-
fn deref_mut(&mut self) -> &mut Self::Target {
293-
&mut self.inner
294-
}
295-
}
296-
297255
#[cfg(test)]
298256
mod test {
299257
use super::*;

ml-dsa/src/signing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
//! These types implement signature generation.
44
55
use crate::{
6-
B32, B64, ExpandedSigningKeyBytes, MaybeBox, MlDsaParams, MuBuilder, Seed, Signature,
7-
VerifyingKey,
6+
B32, B64, ExpandedSigningKeyBytes, MlDsaParams, MuBuilder, Seed, Signature, VerifyingKey,
87
algebra::{AlgebraExt, NttMatrix, NttVector, Vector},
98
crypto::H,
109
hint::Hint,
@@ -16,6 +15,7 @@ use common::{KeyExport, KeyInit, KeySizeUser, typenum::U32};
1615
use core::fmt;
1716
use ctutils::{Choice, CtEq};
1817
use hybrid_array::typenum::Unsigned;
18+
use module_lattice::MaybeBox;
1919
use sha3::Shake256;
2020
use signature::{DigestSigner, Error, MultipartSigner, Signer};
2121

ml-dsa/src/verifying.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! ML-DSA signature verification.
22
33
use crate::{
4-
B32, B64, EncodedVerifyingKey, MaybeBox, MlDsaParams, MuBuilder, Signature,
4+
B32, B64, EncodedVerifyingKey, MlDsaParams, MuBuilder, Signature,
55
algebra::{Elem, NttMatrix, NttVector, Vector},
66
crypto::H,
77
ntt::{Ntt, NttInverse},
@@ -10,6 +10,7 @@ use crate::{
1010
sampling::{expand_a, sample_in_ball},
1111
};
1212
use common::{Key, KeyExport, KeyInit, KeySizeUser};
13+
use module_lattice::MaybeBox;
1314
use sha3::Shake256;
1415
use signature::{DigestVerifier, Error, MultipartVerifier};
1516

0 commit comments

Comments
 (0)