1use crate::error::{MLError, Result};
2use ndarray::{Array1, Array2};
3use quantrs2_circuit::prelude::Circuit;
4use quantrs2_sim::statevector::StateVectorSimulator;
5use std::collections::HashMap;
6use std::fmt;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub enum ProtocolType {
11 BB84,
13
14 E91,
16
17 B92,
19
20 BBM92,
22
23 SARG04,
25}
26
27#[derive(Debug, Clone)]
29pub struct Party {
30 pub name: String,
32
33 pub key: Option<Vec<u8>>,
35
36 pub bases: Option<Vec<usize>>,
38
39 pub state: Option<Vec<f64>>,
41}
42
43#[derive(Debug, Clone)]
45pub struct QuantumKeyDistribution {
46 pub protocol: ProtocolType,
48
49 pub num_qubits: usize,
51
52 pub alice: Party,
54
55 pub bob: Party,
57
58 pub error_rate: f64,
60
61 pub security_bits: usize,
63}
64
65impl QuantumKeyDistribution {
66 pub fn new(protocol: ProtocolType, num_qubits: usize) -> Self {
68 QuantumKeyDistribution {
69 protocol,
70 num_qubits,
71 alice: Party {
72 name: "Alice".to_string(),
73 key: None,
74 bases: None,
75 state: None,
76 },
77 bob: Party {
78 name: "Bob".to_string(),
79 key: None,
80 bases: None,
81 state: None,
82 },
83 error_rate: 0.0,
84 security_bits: num_qubits / 10,
85 }
86 }
87
88 pub fn with_error_rate(mut self, error_rate: f64) -> Self {
90 self.error_rate = error_rate;
91 self
92 }
93
94 pub fn with_security_bits(mut self, security_bits: usize) -> Self {
96 self.security_bits = security_bits;
97 self
98 }
99
100 pub fn distribute_key(&mut self) -> Result<usize> {
102 match self.protocol {
103 ProtocolType::BB84 => self.bb84_protocol(),
104 ProtocolType::E91 => self.e91_protocol(),
105 ProtocolType::B92 => self.b92_protocol(),
106 ProtocolType::BBM92 => Err(MLError::NotImplemented(
107 "BBM92 protocol not implemented yet".to_string(),
108 )),
109 ProtocolType::SARG04 => Err(MLError::NotImplemented(
110 "SARG04 protocol not implemented yet".to_string(),
111 )),
112 }
113 }
114
115 fn bb84_protocol(&mut self) -> Result<usize> {
117 let alice_bits = (0..self.num_qubits)
122 .map(|_| {
123 if rand::random::<f64>() > 0.5 {
124 1u8
125 } else {
126 0u8
127 }
128 })
129 .collect::<Vec<_>>();
130
131 let alice_bases = (0..self.num_qubits)
133 .map(|_| {
134 if rand::random::<f64>() > 0.5 {
135 1usize
136 } else {
137 0usize
138 }
139 })
140 .collect::<Vec<_>>();
141
142 let bob_bases = (0..self.num_qubits)
143 .map(|_| {
144 if rand::random::<f64>() > 0.5 {
145 1usize
146 } else {
147 0usize
148 }
149 })
150 .collect::<Vec<_>>();
151
152 let matching_bases = alice_bases
154 .iter()
155 .zip(bob_bases.iter())
156 .enumerate()
157 .filter_map(|(i, (a, b))| if a == b { Some(i) } else { None })
158 .collect::<Vec<_>>();
159
160 let mut key_bits = Vec::new();
162 for &i in &matching_bases {
163 if rand::random::<f64>() > self.error_rate {
165 key_bits.push(alice_bits[i]);
166 } else {
167 key_bits.push(alice_bits[i] ^ 1);
169 }
170 }
171
172 let mut key_bytes = Vec::new();
174 for chunk in key_bits.chunks(8) {
175 let byte = chunk
176 .iter()
177 .enumerate()
178 .fold(0u8, |acc, (i, &bit)| acc | (bit << i));
179 key_bytes.push(byte);
180 }
181
182 self.alice.key = Some(key_bytes.clone());
184 self.bob.key = Some(key_bytes);
185
186 self.alice.bases = Some(alice_bases);
188 self.bob.bases = Some(bob_bases);
189
190 Ok(matching_bases.len())
191 }
192
193 fn e91_protocol(&mut self) -> Result<usize> {
195 let key_length = self.num_qubits / 3; let key_bytes = (0..key_length / 8 + 1)
201 .map(|_| rand::random::<u8>())
202 .collect::<Vec<_>>();
203
204 self.alice.key = Some(key_bytes.clone());
206 self.bob.key = Some(key_bytes);
207
208 Ok(key_length)
209 }
210
211 fn b92_protocol(&mut self) -> Result<usize> {
213 let key_length = self.num_qubits / 4; let key_bytes = (0..key_length / 8 + 1)
219 .map(|_| rand::random::<u8>())
220 .collect::<Vec<_>>();
221
222 self.alice.key = Some(key_bytes.clone());
224 self.bob.key = Some(key_bytes);
225
226 Ok(key_length)
227 }
228
229 pub fn verify_keys(&self) -> bool {
231 match (&self.alice.key, &self.bob.key) {
232 (Some(alice_key), Some(bob_key)) => alice_key == bob_key,
233 _ => false,
234 }
235 }
236
237 pub fn get_alice_key(&self) -> Option<Vec<u8>> {
239 self.alice.key.clone()
240 }
241
242 pub fn get_bob_key(&self) -> Option<Vec<u8>> {
244 self.bob.key.clone()
245 }
246}
247
248#[derive(Debug, Clone)]
250pub struct QuantumSignature {
251 security_bits: usize,
253
254 algorithm: String,
256
257 public_key: Vec<u8>,
259
260 private_key: Vec<u8>,
262}
263
264impl QuantumSignature {
265 pub fn new(security_bits: usize, algorithm: &str) -> Result<Self> {
267 let public_key = (0..security_bits / 8 + 1)
272 .map(|_| rand::random::<u8>())
273 .collect::<Vec<_>>();
274
275 let private_key = (0..security_bits / 8 + 1)
276 .map(|_| rand::random::<u8>())
277 .collect::<Vec<_>>();
278
279 Ok(QuantumSignature {
280 security_bits,
281 algorithm: algorithm.to_string(),
282 public_key,
283 private_key,
284 })
285 }
286
287 pub fn sign(&self, message: &[u8]) -> Result<Vec<u8>> {
289 let mut signature = self.private_key.clone();
294
295 for (i, &byte) in message.iter().enumerate() {
297 if i < signature.len() {
298 signature[i] ^= byte;
299 }
300 }
301
302 Ok(signature)
303 }
304
305 pub fn verify(&self, message: &[u8], signature: &[u8]) -> Result<bool> {
307 let expected_signature = self.sign(message)?;
312
313 let is_valid = signature.len() == expected_signature.len()
315 && signature
316 .iter()
317 .zip(expected_signature.iter())
318 .all(|(a, b)| a == b);
319
320 Ok(is_valid)
321 }
322}
323
324#[derive(Debug, Clone)]
326pub struct QuantumAuthentication {
327 protocol: String,
329
330 security_bits: usize,
332
333 keys: HashMap<String, Vec<u8>>,
335}
336
337impl QuantumAuthentication {
338 pub fn new(protocol: &str, security_bits: usize) -> Self {
340 QuantumAuthentication {
341 protocol: protocol.to_string(),
342 security_bits,
343 keys: HashMap::new(),
344 }
345 }
346
347 pub fn add_party(&mut self, party_name: &str) -> Result<()> {
349 let key = (0..self.security_bits / 8 + 1)
351 .map(|_| rand::random::<u8>())
352 .collect::<Vec<_>>();
353
354 self.keys.insert(party_name.to_string(), key);
355
356 Ok(())
357 }
358
359 pub fn authenticate(&self, party_name: &str, message: &[u8]) -> Result<Vec<u8>> {
361 let key = self
363 .keys
364 .get(party_name)
365 .ok_or_else(|| MLError::InvalidParameter(format!("Party {} not found", party_name)))?;
366
367 let mut tag = key.clone();
369
370 for (i, &byte) in message.iter().enumerate() {
372 if i < tag.len() {
373 tag[i] ^= byte;
374 }
375 }
376
377 Ok(tag)
378 }
379
380 pub fn verify(&self, party_name: &str, message: &[u8], tag: &[u8]) -> Result<bool> {
382 let expected_tag = self.authenticate(party_name, message)?;
384
385 let is_valid = tag.len() == expected_tag.len()
387 && tag.iter().zip(expected_tag.iter()).all(|(a, b)| a == b);
388
389 Ok(is_valid)
390 }
391}
392
393#[derive(Debug, Clone)]
395pub struct QSDC {
396 pub num_qubits: usize,
398
399 pub error_rate: f64,
401}
402
403impl QSDC {
404 pub fn new(num_qubits: usize) -> Self {
406 QSDC {
407 num_qubits,
408 error_rate: 0.01, }
410 }
411
412 pub fn with_error_rate(mut self, error_rate: f64) -> Self {
414 self.error_rate = error_rate;
415 self
416 }
417
418 pub fn transmit_message(&self, message: &[u8]) -> Result<Vec<u8>> {
420 let mut received = message.to_vec();
426
427 for byte in &mut received {
429 for bit_pos in 0..8 {
430 if rand::random::<f64>() < self.error_rate {
431 *byte ^= 1 << bit_pos;
433 }
434 }
435 }
436
437 Ok(received)
438 }
439}
440
441pub fn encrypt_with_qkd(message: &[u8], key: Vec<u8>) -> Vec<u8> {
443 message
445 .iter()
446 .enumerate()
447 .map(|(i, &byte)| byte ^ key[i % key.len()])
448 .collect()
449}
450
451pub fn decrypt_with_qkd(encrypted: &[u8], key: Vec<u8>) -> Vec<u8> {
453 encrypt_with_qkd(encrypted, key)
455}
456
457impl fmt::Display for ProtocolType {
458 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
459 match self {
460 ProtocolType::BB84 => write!(f, "BB84"),
461 ProtocolType::E91 => write!(f, "E91"),
462 ProtocolType::B92 => write!(f, "B92"),
463 ProtocolType::BBM92 => write!(f, "BBM92"),
464 ProtocolType::SARG04 => write!(f, "SARG04"),
465 }
466 }
467}