bc_components/mldsa/mldsa_public_key.rs
1use dcbor::prelude::*;
2use pqcrypto_mldsa::*;
3use pqcrypto_traits::sign::*;
4
5use super::{MLDSA, MLDSASignature};
6use crate::{Digest, Error, Reference, ReferenceProvider, Result, tags};
7
8/// A public key for the ML-DSA post-quantum digital signature algorithm.
9///
10/// `MLDSAPublicKey` represents a public key that can be used to verify digital
11/// signatures created with the ML-DSA (Module Lattice-based Digital Signature
12/// Algorithm) post-quantum algorithm. It supports multiple security levels
13/// through the variants:
14///
15/// - `MLDSA44`: NIST security level 2 (roughly equivalent to AES-128)
16/// - `MLDSA65`: NIST security level 3 (roughly equivalent to AES-192)
17/// - `MLDSA87`: NIST security level 5 (roughly equivalent to AES-256)
18///
19/// # Examples
20///
21/// ```
22/// use bc_components::MLDSA;
23///
24/// // Generate a keypair
25/// let (private_key, public_key) = MLDSA::MLDSA44.keypair();
26///
27/// // Sign a message
28/// let message = b"Hello, post-quantum world!";
29/// let signature = private_key.sign(message);
30///
31/// // Verify the signature
32/// assert!(public_key.verify(&signature, message).unwrap());
33/// ```
34#[derive(Clone)]
35pub enum MLDSAPublicKey {
36 /// An ML-DSA44 public key (NIST security level 2)
37 MLDSA44(Box<mldsa44::PublicKey>),
38 /// An ML-DSA65 public key (NIST security level 3)
39 MLDSA65(Box<mldsa65::PublicKey>),
40 /// An ML-DSA87 public key (NIST security level 5)
41 MLDSA87(Box<mldsa87::PublicKey>),
42}
43
44/// Implements equality comparison for ML-DSA public keys.
45impl PartialEq for MLDSAPublicKey {
46 /// Compares two ML-DSA public keys for equality.
47 ///
48 /// Two ML-DSA public keys are equal if they have the same security level
49 /// and the same raw byte representation.
50 fn eq(&self, other: &Self) -> bool {
51 self.level() == other.level() && self.as_bytes() == other.as_bytes()
52 }
53}
54
55impl Eq for MLDSAPublicKey {}
56
57/// Implements hashing for ML-DSA public keys.
58impl std::hash::Hash for MLDSAPublicKey {
59 /// Hashes both the security level and the raw bytes of the public key.
60 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
61 self.level().hash(state);
62 self.as_bytes().hash(state);
63 }
64}
65
66impl MLDSAPublicKey {
67 /// Verifies an ML-DSA signature for a message using this public key.
68 ///
69 /// # Parameters
70 ///
71 /// * `signature` - The signature to verify.
72 /// * `message` - The message that was signed.
73 ///
74 /// # Returns
75 ///
76 /// `Ok(true)` if the signature is valid for the message and this public
77 /// key, `Ok(false)` if the signature is invalid, or an error if the
78 /// security levels of the signature and public key don't match.
79 ///
80 /// # Errors
81 ///
82 /// Returns an error if the security level of the signature doesn't match
83 /// the security level of this public key.
84 ///
85 /// # Examples
86 ///
87 /// ```
88 /// use bc_components::MLDSA;
89 ///
90 /// let (private_key, public_key) = MLDSA::MLDSA44.keypair();
91 /// let message = b"Hello, world!";
92 /// let signature = private_key.sign(message);
93 ///
94 /// assert!(public_key.verify(&signature, message).unwrap());
95 /// ```
96 pub fn verify(
97 &self,
98 signature: &MLDSASignature,
99 message: impl AsRef<[u8]>,
100 ) -> Result<bool> {
101 if signature.level() != self.level() {
102 return Err(Error::LevelMismatch);
103 }
104
105 let verifies = match (self, signature) {
106 (MLDSAPublicKey::MLDSA44(pk), MLDSASignature::MLDSA44(sig)) => {
107 mldsa44::verify_detached_signature(sig, message.as_ref(), pk)
108 .is_ok()
109 }
110 (MLDSAPublicKey::MLDSA65(pk), MLDSASignature::MLDSA65(sig)) => {
111 mldsa65::verify_detached_signature(sig, message.as_ref(), pk)
112 .is_ok()
113 }
114 (MLDSAPublicKey::MLDSA87(pk), MLDSASignature::MLDSA87(sig)) => {
115 mldsa87::verify_detached_signature(sig, message.as_ref(), pk)
116 .is_ok()
117 }
118 _ => false,
119 };
120
121 Ok(verifies)
122 }
123
124 /// Returns the security level of this ML-DSA public key.
125 pub fn level(&self) -> MLDSA {
126 match self {
127 MLDSAPublicKey::MLDSA44(_) => MLDSA::MLDSA44,
128 MLDSAPublicKey::MLDSA65(_) => MLDSA::MLDSA65,
129 MLDSAPublicKey::MLDSA87(_) => MLDSA::MLDSA87,
130 }
131 }
132
133 /// Returns the size of this ML-DSA public key in bytes.
134 pub fn size(&self) -> usize { self.level().public_key_size() }
135
136 /// Returns the raw bytes of this ML-DSA public key.
137 pub fn as_bytes(&self) -> &[u8] { self.as_ref() }
138
139 /// Creates an ML-DSA public key from raw bytes and a security level.
140 ///
141 /// # Parameters
142 ///
143 /// * `level` - The security level of the key.
144 /// * `bytes` - The raw bytes of the key.
145 ///
146 /// # Returns
147 ///
148 /// An `MLDSAPublicKey` if the bytes represent a valid key for the given
149 /// level, or an error otherwise.
150 ///
151 /// # Errors
152 ///
153 /// Returns an error if the bytes do not represent a valid ML-DSA public key
154 /// for the specified security level.
155 pub fn from_bytes(level: MLDSA, bytes: &[u8]) -> Result<Self> {
156 match level {
157 MLDSA::MLDSA44 => Ok(MLDSAPublicKey::MLDSA44(Box::new(
158 mldsa44::PublicKey::from_bytes(bytes).map_err(|e| {
159 Error::post_quantum(format!(
160 "MLDSA44 public key error: {}",
161 e
162 ))
163 })?,
164 ))),
165 MLDSA::MLDSA65 => Ok(MLDSAPublicKey::MLDSA65(Box::new(
166 mldsa65::PublicKey::from_bytes(bytes).map_err(|e| {
167 Error::post_quantum(format!(
168 "MLDSA65 public key error: {}",
169 e
170 ))
171 })?,
172 ))),
173 MLDSA::MLDSA87 => Ok(MLDSAPublicKey::MLDSA87(Box::new(
174 mldsa87::PublicKey::from_bytes(bytes).map_err(|e| {
175 Error::post_quantum(format!(
176 "MLDSA87 public key error: {}",
177 e
178 ))
179 })?,
180 ))),
181 }
182 }
183}
184
185impl AsRef<[u8]> for MLDSAPublicKey {
186 /// Returns the public key as a byte slice.
187 fn as_ref(&self) -> &[u8] {
188 match self {
189 MLDSAPublicKey::MLDSA44(key) => key.as_bytes(),
190 MLDSAPublicKey::MLDSA65(key) => key.as_bytes(),
191 MLDSAPublicKey::MLDSA87(key) => key.as_bytes(),
192 }
193 }
194}
195
196/// Provides debug formatting for ML-DSA public keys.
197impl std::fmt::Debug for MLDSAPublicKey {
198 /// Formats the public key as a string for debugging purposes.
199 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
200 match self {
201 MLDSAPublicKey::MLDSA44(_) => f.write_str("MLDSA442PublicKey"),
202 MLDSAPublicKey::MLDSA65(_) => f.write_str("MLDSA65PublicKey"),
203 MLDSAPublicKey::MLDSA87(_) => f.write_str("MLDSA87PublicKey"),
204 }
205 }
206}
207
208/// Defines CBOR tags for ML-DSA public keys.
209impl CBORTagged for MLDSAPublicKey {
210 /// Returns the CBOR tag for ML-DSA public keys.
211 fn cbor_tags() -> Vec<Tag> {
212 tags_for_values(&[tags::TAG_MLDSA_PUBLIC_KEY])
213 }
214}
215
216/// Converts an `MLDSAPublicKey` to CBOR.
217impl From<MLDSAPublicKey> for CBOR {
218 /// Converts to tagged CBOR.
219 fn from(value: MLDSAPublicKey) -> Self { value.tagged_cbor() }
220}
221
222/// Implements CBOR encoding for ML-DSA public keys.
223impl CBORTaggedEncodable for MLDSAPublicKey {
224 /// Creates the untagged CBOR representation as an array with level and key
225 /// bytes.
226 fn untagged_cbor(&self) -> CBOR {
227 vec![self.level().into(), CBOR::to_byte_string(self.as_bytes())].into()
228 }
229}
230
231/// Attempts to convert CBOR to an `MLDSAPublicKey`.
232impl TryFrom<CBOR> for MLDSAPublicKey {
233 type Error = dcbor::Error;
234
235 /// Converts from tagged CBOR.
236 fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
237 Self::from_tagged_cbor(cbor)
238 }
239}
240
241/// Implements CBOR decoding for ML-DSA public keys.
242impl CBORTaggedDecodable for MLDSAPublicKey {
243 /// Creates an `MLDSAPublicKey` from untagged CBOR.
244 ///
245 /// # Errors
246 /// Returns an error if the CBOR value doesn't represent a valid ML-DSA
247 /// public key.
248 fn from_untagged_cbor(untagged_cbor: CBOR) -> dcbor::Result<Self> {
249 match untagged_cbor.as_case() {
250 CBORCase::Array(elements) => {
251 if elements.len() != 2 {
252 return Err("MLDSAPublicKey must have two elements".into());
253 }
254
255 let level = MLDSA::try_from(elements[0].clone())?;
256 let data = CBOR::try_into_byte_string(elements[1].clone())?;
257 Ok(MLDSAPublicKey::from_bytes(level, &data)?)
258 }
259 _ => Err("MLDSAPublicKey must be an array".into()),
260 }
261 }
262}
263
264impl ReferenceProvider for MLDSAPublicKey {
265 fn reference(&self) -> Reference {
266 Reference::from_digest(Digest::from_image(
267 self.tagged_cbor().to_cbor_data(),
268 ))
269 }
270}
271
272impl std::fmt::Display for MLDSAPublicKey {
273 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
274 match self {
275 MLDSAPublicKey::MLDSA44(_) => {
276 write!(f, "MLDSA44PublicKey({})", self.ref_hex_short())
277 }
278 MLDSAPublicKey::MLDSA65(_) => {
279 write!(f, "MLDSA65PublicKey({})", self.ref_hex_short())
280 }
281 MLDSAPublicKey::MLDSA87(_) => {
282 write!(f, "MLDSA87PublicKey({})", self.ref_hex_short())
283 }
284 }
285 }
286}