pub struct QuantumKeyDistribution {
pub protocol: ProtocolType,
pub num_qubits: usize,
pub alice: Party,
pub bob: Party,
pub error_rate: f64,
pub security_bits: usize,
}Expand description
Quantum key distribution protocol
Fields§
§protocol: ProtocolTypeType of QKD protocol
num_qubits: usizeNumber of qubits to use in the protocol
alice: PartyAlice party
bob: PartyBob party
error_rate: f64Error rate for the quantum channel
security_bits: usizeSecurity parameter (number of bits to use for security checks)
Implementations§
Source§impl QuantumKeyDistribution
impl QuantumKeyDistribution
Sourcepub fn new(protocol: ProtocolType, num_qubits: usize) -> Self
pub fn new(protocol: ProtocolType, num_qubits: usize) -> Self
Creates a new QKD protocol instance
Examples found in repository?
examples/quantum_crypto.rs (line 35)
28fn run_bb84_example() -> Result<()> {
29 println!("\nBB84 Quantum Key Distribution");
30 println!("----------------------------");
31
32 // Create BB84 QKD with 1000 qubits
33 let num_qubits = 1000;
34 println!("Creating BB84 protocol with {num_qubits} qubits");
35 let mut qkd = QuantumKeyDistribution::new(ProtocolType::BB84, num_qubits);
36
37 // Optional: set error rate
38 qkd = qkd.with_error_rate(0.03);
39 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
40
41 // Distribute key
42 println!("Performing quantum key distribution...");
43 let start = Instant::now();
44 let key_length = qkd.distribute_key()?;
45 println!("Key distribution completed in {:.2?}", start.elapsed());
46 println!("Final key length: {key_length} bits");
47
48 // Verify keys match
49 println!("Verifying Alice and Bob have identical keys...");
50 if qkd.verify_keys() {
51 println!("✓ Key verification successful!");
52
53 // Display part of the key (first 8 bytes)
54 if let Some(key) = qkd.get_alice_key() {
55 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
56 }
57 } else {
58 println!("✗ Key verification failed!");
59 }
60
61 // Use the key for encryption
62 if let Some(key) = qkd.get_alice_key() {
63 let message = b"Hello, quantum world!";
64
65 println!(
66 "Encrypting message: '{}'",
67 std::str::from_utf8(message).unwrap()
68 );
69 let encrypted = quantrs2_ml::crypto::encrypt_with_qkd(message, key);
70
71 println!("Encrypted data: {:?}", &encrypted);
72
73 // Decrypt with Bob's key
74 if let Some(bob_key) = qkd.get_bob_key() {
75 let decrypted = quantrs2_ml::crypto::decrypt_with_qkd(&encrypted, bob_key);
76 println!(
77 "Decrypted message: '{}'",
78 std::str::from_utf8(&decrypted).unwrap()
79 );
80 }
81 }
82
83 Ok(())
84}
85
86fn run_e91_example() -> Result<()> {
87 println!("\nE91 Entanglement-Based Protocol");
88 println!("------------------------------");
89
90 // Create E91 QKD with 800 qubits
91 let num_qubits = 800;
92 println!("Creating E91 protocol with {num_qubits} qubits");
93 let mut qkd = QuantumKeyDistribution::new(ProtocolType::E91, num_qubits);
94
95 // Set error rate
96 qkd = qkd.with_error_rate(0.02);
97 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
98
99 // Distribute key
100 println!("Performing quantum key distribution with entangled pairs...");
101 let start = Instant::now();
102 let key_length = qkd.distribute_key()?;
103 println!("Key distribution completed in {:.2?}", start.elapsed());
104 println!("Final key length: {key_length} bits");
105
106 // Verify keys match
107 println!("Verifying Alice and Bob have identical keys...");
108 if qkd.verify_keys() {
109 println!("✓ Key verification successful!");
110
111 // Display part of the key
112 if let Some(key) = qkd.get_alice_key() {
113 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
114 }
115 } else {
116 println!("✗ Key verification failed!");
117 }
118
119 Ok(())
120}Sourcepub fn with_error_rate(self, error_rate: f64) -> Self
pub fn with_error_rate(self, error_rate: f64) -> Self
Sets the error rate for the quantum channel
Examples found in repository?
examples/quantum_crypto.rs (line 38)
28fn run_bb84_example() -> Result<()> {
29 println!("\nBB84 Quantum Key Distribution");
30 println!("----------------------------");
31
32 // Create BB84 QKD with 1000 qubits
33 let num_qubits = 1000;
34 println!("Creating BB84 protocol with {num_qubits} qubits");
35 let mut qkd = QuantumKeyDistribution::new(ProtocolType::BB84, num_qubits);
36
37 // Optional: set error rate
38 qkd = qkd.with_error_rate(0.03);
39 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
40
41 // Distribute key
42 println!("Performing quantum key distribution...");
43 let start = Instant::now();
44 let key_length = qkd.distribute_key()?;
45 println!("Key distribution completed in {:.2?}", start.elapsed());
46 println!("Final key length: {key_length} bits");
47
48 // Verify keys match
49 println!("Verifying Alice and Bob have identical keys...");
50 if qkd.verify_keys() {
51 println!("✓ Key verification successful!");
52
53 // Display part of the key (first 8 bytes)
54 if let Some(key) = qkd.get_alice_key() {
55 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
56 }
57 } else {
58 println!("✗ Key verification failed!");
59 }
60
61 // Use the key for encryption
62 if let Some(key) = qkd.get_alice_key() {
63 let message = b"Hello, quantum world!";
64
65 println!(
66 "Encrypting message: '{}'",
67 std::str::from_utf8(message).unwrap()
68 );
69 let encrypted = quantrs2_ml::crypto::encrypt_with_qkd(message, key);
70
71 println!("Encrypted data: {:?}", &encrypted);
72
73 // Decrypt with Bob's key
74 if let Some(bob_key) = qkd.get_bob_key() {
75 let decrypted = quantrs2_ml::crypto::decrypt_with_qkd(&encrypted, bob_key);
76 println!(
77 "Decrypted message: '{}'",
78 std::str::from_utf8(&decrypted).unwrap()
79 );
80 }
81 }
82
83 Ok(())
84}
85
86fn run_e91_example() -> Result<()> {
87 println!("\nE91 Entanglement-Based Protocol");
88 println!("------------------------------");
89
90 // Create E91 QKD with 800 qubits
91 let num_qubits = 800;
92 println!("Creating E91 protocol with {num_qubits} qubits");
93 let mut qkd = QuantumKeyDistribution::new(ProtocolType::E91, num_qubits);
94
95 // Set error rate
96 qkd = qkd.with_error_rate(0.02);
97 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
98
99 // Distribute key
100 println!("Performing quantum key distribution with entangled pairs...");
101 let start = Instant::now();
102 let key_length = qkd.distribute_key()?;
103 println!("Key distribution completed in {:.2?}", start.elapsed());
104 println!("Final key length: {key_length} bits");
105
106 // Verify keys match
107 println!("Verifying Alice and Bob have identical keys...");
108 if qkd.verify_keys() {
109 println!("✓ Key verification successful!");
110
111 // Display part of the key
112 if let Some(key) = qkd.get_alice_key() {
113 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
114 }
115 } else {
116 println!("✗ Key verification failed!");
117 }
118
119 Ok(())
120}Sourcepub fn with_security_bits(self, security_bits: usize) -> Self
pub fn with_security_bits(self, security_bits: usize) -> Self
Sets the security parameter
Sourcepub fn distribute_key(&mut self) -> Result<usize>
pub fn distribute_key(&mut self) -> Result<usize>
Distributes a key using the specified QKD protocol
Examples found in repository?
examples/quantum_crypto.rs (line 44)
28fn run_bb84_example() -> Result<()> {
29 println!("\nBB84 Quantum Key Distribution");
30 println!("----------------------------");
31
32 // Create BB84 QKD with 1000 qubits
33 let num_qubits = 1000;
34 println!("Creating BB84 protocol with {num_qubits} qubits");
35 let mut qkd = QuantumKeyDistribution::new(ProtocolType::BB84, num_qubits);
36
37 // Optional: set error rate
38 qkd = qkd.with_error_rate(0.03);
39 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
40
41 // Distribute key
42 println!("Performing quantum key distribution...");
43 let start = Instant::now();
44 let key_length = qkd.distribute_key()?;
45 println!("Key distribution completed in {:.2?}", start.elapsed());
46 println!("Final key length: {key_length} bits");
47
48 // Verify keys match
49 println!("Verifying Alice and Bob have identical keys...");
50 if qkd.verify_keys() {
51 println!("✓ Key verification successful!");
52
53 // Display part of the key (first 8 bytes)
54 if let Some(key) = qkd.get_alice_key() {
55 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
56 }
57 } else {
58 println!("✗ Key verification failed!");
59 }
60
61 // Use the key for encryption
62 if let Some(key) = qkd.get_alice_key() {
63 let message = b"Hello, quantum world!";
64
65 println!(
66 "Encrypting message: '{}'",
67 std::str::from_utf8(message).unwrap()
68 );
69 let encrypted = quantrs2_ml::crypto::encrypt_with_qkd(message, key);
70
71 println!("Encrypted data: {:?}", &encrypted);
72
73 // Decrypt with Bob's key
74 if let Some(bob_key) = qkd.get_bob_key() {
75 let decrypted = quantrs2_ml::crypto::decrypt_with_qkd(&encrypted, bob_key);
76 println!(
77 "Decrypted message: '{}'",
78 std::str::from_utf8(&decrypted).unwrap()
79 );
80 }
81 }
82
83 Ok(())
84}
85
86fn run_e91_example() -> Result<()> {
87 println!("\nE91 Entanglement-Based Protocol");
88 println!("------------------------------");
89
90 // Create E91 QKD with 800 qubits
91 let num_qubits = 800;
92 println!("Creating E91 protocol with {num_qubits} qubits");
93 let mut qkd = QuantumKeyDistribution::new(ProtocolType::E91, num_qubits);
94
95 // Set error rate
96 qkd = qkd.with_error_rate(0.02);
97 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
98
99 // Distribute key
100 println!("Performing quantum key distribution with entangled pairs...");
101 let start = Instant::now();
102 let key_length = qkd.distribute_key()?;
103 println!("Key distribution completed in {:.2?}", start.elapsed());
104 println!("Final key length: {key_length} bits");
105
106 // Verify keys match
107 println!("Verifying Alice and Bob have identical keys...");
108 if qkd.verify_keys() {
109 println!("✓ Key verification successful!");
110
111 // Display part of the key
112 if let Some(key) = qkd.get_alice_key() {
113 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
114 }
115 } else {
116 println!("✗ Key verification failed!");
117 }
118
119 Ok(())
120}Sourcepub fn verify_keys(&self) -> bool
pub fn verify_keys(&self) -> bool
Verifies that Alice and Bob have identical keys
Examples found in repository?
examples/quantum_crypto.rs (line 50)
28fn run_bb84_example() -> Result<()> {
29 println!("\nBB84 Quantum Key Distribution");
30 println!("----------------------------");
31
32 // Create BB84 QKD with 1000 qubits
33 let num_qubits = 1000;
34 println!("Creating BB84 protocol with {num_qubits} qubits");
35 let mut qkd = QuantumKeyDistribution::new(ProtocolType::BB84, num_qubits);
36
37 // Optional: set error rate
38 qkd = qkd.with_error_rate(0.03);
39 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
40
41 // Distribute key
42 println!("Performing quantum key distribution...");
43 let start = Instant::now();
44 let key_length = qkd.distribute_key()?;
45 println!("Key distribution completed in {:.2?}", start.elapsed());
46 println!("Final key length: {key_length} bits");
47
48 // Verify keys match
49 println!("Verifying Alice and Bob have identical keys...");
50 if qkd.verify_keys() {
51 println!("✓ Key verification successful!");
52
53 // Display part of the key (first 8 bytes)
54 if let Some(key) = qkd.get_alice_key() {
55 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
56 }
57 } else {
58 println!("✗ Key verification failed!");
59 }
60
61 // Use the key for encryption
62 if let Some(key) = qkd.get_alice_key() {
63 let message = b"Hello, quantum world!";
64
65 println!(
66 "Encrypting message: '{}'",
67 std::str::from_utf8(message).unwrap()
68 );
69 let encrypted = quantrs2_ml::crypto::encrypt_with_qkd(message, key);
70
71 println!("Encrypted data: {:?}", &encrypted);
72
73 // Decrypt with Bob's key
74 if let Some(bob_key) = qkd.get_bob_key() {
75 let decrypted = quantrs2_ml::crypto::decrypt_with_qkd(&encrypted, bob_key);
76 println!(
77 "Decrypted message: '{}'",
78 std::str::from_utf8(&decrypted).unwrap()
79 );
80 }
81 }
82
83 Ok(())
84}
85
86fn run_e91_example() -> Result<()> {
87 println!("\nE91 Entanglement-Based Protocol");
88 println!("------------------------------");
89
90 // Create E91 QKD with 800 qubits
91 let num_qubits = 800;
92 println!("Creating E91 protocol with {num_qubits} qubits");
93 let mut qkd = QuantumKeyDistribution::new(ProtocolType::E91, num_qubits);
94
95 // Set error rate
96 qkd = qkd.with_error_rate(0.02);
97 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
98
99 // Distribute key
100 println!("Performing quantum key distribution with entangled pairs...");
101 let start = Instant::now();
102 let key_length = qkd.distribute_key()?;
103 println!("Key distribution completed in {:.2?}", start.elapsed());
104 println!("Final key length: {key_length} bits");
105
106 // Verify keys match
107 println!("Verifying Alice and Bob have identical keys...");
108 if qkd.verify_keys() {
109 println!("✓ Key verification successful!");
110
111 // Display part of the key
112 if let Some(key) = qkd.get_alice_key() {
113 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
114 }
115 } else {
116 println!("✗ Key verification failed!");
117 }
118
119 Ok(())
120}Sourcepub fn get_alice_key(&self) -> Option<Vec<u8>>
pub fn get_alice_key(&self) -> Option<Vec<u8>>
Gets Alice’s key (if generated)
Examples found in repository?
examples/quantum_crypto.rs (line 54)
28fn run_bb84_example() -> Result<()> {
29 println!("\nBB84 Quantum Key Distribution");
30 println!("----------------------------");
31
32 // Create BB84 QKD with 1000 qubits
33 let num_qubits = 1000;
34 println!("Creating BB84 protocol with {num_qubits} qubits");
35 let mut qkd = QuantumKeyDistribution::new(ProtocolType::BB84, num_qubits);
36
37 // Optional: set error rate
38 qkd = qkd.with_error_rate(0.03);
39 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
40
41 // Distribute key
42 println!("Performing quantum key distribution...");
43 let start = Instant::now();
44 let key_length = qkd.distribute_key()?;
45 println!("Key distribution completed in {:.2?}", start.elapsed());
46 println!("Final key length: {key_length} bits");
47
48 // Verify keys match
49 println!("Verifying Alice and Bob have identical keys...");
50 if qkd.verify_keys() {
51 println!("✓ Key verification successful!");
52
53 // Display part of the key (first 8 bytes)
54 if let Some(key) = qkd.get_alice_key() {
55 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
56 }
57 } else {
58 println!("✗ Key verification failed!");
59 }
60
61 // Use the key for encryption
62 if let Some(key) = qkd.get_alice_key() {
63 let message = b"Hello, quantum world!";
64
65 println!(
66 "Encrypting message: '{}'",
67 std::str::from_utf8(message).unwrap()
68 );
69 let encrypted = quantrs2_ml::crypto::encrypt_with_qkd(message, key);
70
71 println!("Encrypted data: {:?}", &encrypted);
72
73 // Decrypt with Bob's key
74 if let Some(bob_key) = qkd.get_bob_key() {
75 let decrypted = quantrs2_ml::crypto::decrypt_with_qkd(&encrypted, bob_key);
76 println!(
77 "Decrypted message: '{}'",
78 std::str::from_utf8(&decrypted).unwrap()
79 );
80 }
81 }
82
83 Ok(())
84}
85
86fn run_e91_example() -> Result<()> {
87 println!("\nE91 Entanglement-Based Protocol");
88 println!("------------------------------");
89
90 // Create E91 QKD with 800 qubits
91 let num_qubits = 800;
92 println!("Creating E91 protocol with {num_qubits} qubits");
93 let mut qkd = QuantumKeyDistribution::new(ProtocolType::E91, num_qubits);
94
95 // Set error rate
96 qkd = qkd.with_error_rate(0.02);
97 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
98
99 // Distribute key
100 println!("Performing quantum key distribution with entangled pairs...");
101 let start = Instant::now();
102 let key_length = qkd.distribute_key()?;
103 println!("Key distribution completed in {:.2?}", start.elapsed());
104 println!("Final key length: {key_length} bits");
105
106 // Verify keys match
107 println!("Verifying Alice and Bob have identical keys...");
108 if qkd.verify_keys() {
109 println!("✓ Key verification successful!");
110
111 // Display part of the key
112 if let Some(key) = qkd.get_alice_key() {
113 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
114 }
115 } else {
116 println!("✗ Key verification failed!");
117 }
118
119 Ok(())
120}Sourcepub fn get_bob_key(&self) -> Option<Vec<u8>>
pub fn get_bob_key(&self) -> Option<Vec<u8>>
Gets Bob’s key (if generated)
Examples found in repository?
examples/quantum_crypto.rs (line 74)
28fn run_bb84_example() -> Result<()> {
29 println!("\nBB84 Quantum Key Distribution");
30 println!("----------------------------");
31
32 // Create BB84 QKD with 1000 qubits
33 let num_qubits = 1000;
34 println!("Creating BB84 protocol with {num_qubits} qubits");
35 let mut qkd = QuantumKeyDistribution::new(ProtocolType::BB84, num_qubits);
36
37 // Optional: set error rate
38 qkd = qkd.with_error_rate(0.03);
39 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
40
41 // Distribute key
42 println!("Performing quantum key distribution...");
43 let start = Instant::now();
44 let key_length = qkd.distribute_key()?;
45 println!("Key distribution completed in {:.2?}", start.elapsed());
46 println!("Final key length: {key_length} bits");
47
48 // Verify keys match
49 println!("Verifying Alice and Bob have identical keys...");
50 if qkd.verify_keys() {
51 println!("✓ Key verification successful!");
52
53 // Display part of the key (first 8 bytes)
54 if let Some(key) = qkd.get_alice_key() {
55 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
56 }
57 } else {
58 println!("✗ Key verification failed!");
59 }
60
61 // Use the key for encryption
62 if let Some(key) = qkd.get_alice_key() {
63 let message = b"Hello, quantum world!";
64
65 println!(
66 "Encrypting message: '{}'",
67 std::str::from_utf8(message).unwrap()
68 );
69 let encrypted = quantrs2_ml::crypto::encrypt_with_qkd(message, key);
70
71 println!("Encrypted data: {:?}", &encrypted);
72
73 // Decrypt with Bob's key
74 if let Some(bob_key) = qkd.get_bob_key() {
75 let decrypted = quantrs2_ml::crypto::decrypt_with_qkd(&encrypted, bob_key);
76 println!(
77 "Decrypted message: '{}'",
78 std::str::from_utf8(&decrypted).unwrap()
79 );
80 }
81 }
82
83 Ok(())
84}Trait Implementations§
Source§impl Clone for QuantumKeyDistribution
impl Clone for QuantumKeyDistribution
Source§fn clone(&self) -> QuantumKeyDistribution
fn clone(&self) -> QuantumKeyDistribution
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for QuantumKeyDistribution
impl RefUnwindSafe for QuantumKeyDistribution
impl Send for QuantumKeyDistribution
impl Sync for QuantumKeyDistribution
impl Unpin for QuantumKeyDistribution
impl UnwindSafe for QuantumKeyDistribution
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.