origin_crypto_sdk/signing/
postquantum.rs1impl std::fmt::Debug for Falcon1024Signer {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 f.debug_struct("Falcon1024Signer")
44 .field("public_key", &"<present, 1793 bytes>")
45 .finish_non_exhaustive()
46 }
47}
48
49pub struct Falcon1024Signer {
51 key: crate::pqc::falcon1024::FalconPrivateKey,
52 public_key: crate::pqc::falcon1024::FalconPublicKey,
53}
54
55impl Falcon1024Signer {
56 pub fn from_seed(seed: &[u8; 32]) -> Result<Self, crate::error::CryptoError> {
61 let (pk, sk) = crate::pqc::falcon1024::generate_keypair_from_seed(seed).map_err(|e| {
62 crate::error::CryptoError::InvalidKey(format!("Falcon-1024 keygen: {e}"))
63 })?;
64 Ok(Self {
65 key: sk,
66 public_key: pk,
67 })
68 }
69
70 pub fn from_keys(
72 sk: crate::pqc::falcon1024::FalconPrivateKey,
73 pk: crate::pqc::falcon1024::FalconPublicKey,
74 ) -> Self {
75 Self {
76 key: sk,
77 public_key: pk,
78 }
79 }
80
81 pub fn sign(&self, message: &[u8]) -> Result<Vec<u8>, crate::error::CryptoError> {
83 crate::pqc::falcon1024::sign(message, &self.key)
84 .map(|s| s.as_bytes().to_vec())
85 .map_err(|e| crate::error::CryptoError::InvalidKey(format!("Falcon-1024 sign: {e}")))
86 }
87
88 pub fn verify(&self, message: &[u8], signature: &[u8]) -> bool {
90 let Ok(sig) = crate::pqc::falcon1024::FalconSignature::from_bytes(signature) else {
91 return false;
92 };
93 crate::pqc::falcon1024::verify(message, &sig, &self.public_key).is_ok()
94 }
95
96 pub fn verify_with_pubkey(pubkey: &[u8], message: &[u8], signature: &[u8]) -> bool {
98 let Ok(pk) = crate::pqc::falcon1024::FalconPublicKey::from_bytes(pubkey) else {
99 return false;
100 };
101 let Ok(sig) = crate::pqc::falcon1024::FalconSignature::from_bytes(signature) else {
102 return false;
103 };
104 crate::pqc::falcon1024::verify(message, &sig, &pk).is_ok()
105 }
106
107 pub fn public_key_bytes(&self) -> Vec<u8> {
109 self.public_key.as_bytes().to_vec()
110 }
111}
112
113impl std::fmt::Debug for Falcon512Signer {
122 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123 f.debug_struct("Falcon512Signer")
126 .field("pk", &"<present, public bytes>")
127 .finish_non_exhaustive()
128 }
129}
130
131pub struct Falcon512Signer {
132 sk: crate::pqc::falcon512::Falcon512PrivateKey,
133 pk: crate::pqc::falcon512::Falcon512PublicKey,
134}
135
136impl Falcon512Signer {
137 pub fn from_seed(seed: &[u8; 32]) -> Self {
139 let (sk, pk) = crate::pqc::falcon512::generate_keypair(*seed);
140 Self { sk, pk }
141 }
142
143 pub fn sign(&self, message: &[u8]) -> Result<Vec<u8>, crate::error::CryptoError> {
145 crate::pqc::falcon512::sign(message, &self.sk)
146 .map(|s| s.0)
147 .map_err(|e| crate::error::CryptoError::InvalidKey(format!("Falcon-512 sign: {e}")))
148 }
149
150 pub fn verify(&self, message: &[u8], signature: &[u8]) -> bool {
152 let sig = crate::pqc::falcon512::Falcon512Signature(signature.to_vec());
153 crate::pqc::falcon512::verify(message, &sig, &self.pk)
154 }
155}
156
157#[cfg(test)]
158mod tests {
159 use super::*;
160
161 #[test]
162 fn falcon1024_roundtrip() {
163 let s = Falcon1024Signer::from_seed(&[1u8; 32]).unwrap();
164 let msg = b"hello";
165 let sig = s.sign(msg).unwrap();
166 assert!(s.verify(msg, &sig));
167 }
168
169 #[test]
170 fn falcon1024_rejects_tampered() {
171 let s = Falcon1024Signer::from_seed(&[2u8; 32]).unwrap();
172 let sig = s.sign(b"original").unwrap();
173 assert!(!s.verify(b"tampered", &sig));
174 }
175
176 #[test]
177 fn falcon512_roundtrip() {
178 let s = Falcon512Signer::from_seed(&[3u8; 32]);
179 let msg = b"hello";
180 let sig = s.sign(msg).unwrap();
181 assert!(s.verify(msg, &sig));
182 }
183}