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 34)
27fn run_bb84_example() -> Result<()> {
28 println!("\nBB84 Quantum Key Distribution");
29 println!("----------------------------");
30
31 // Create BB84 QKD with 1000 qubits
32 let num_qubits = 1000;
33 println!("Creating BB84 protocol with {} qubits", num_qubits);
34 let mut qkd = QuantumKeyDistribution::new(ProtocolType::BB84, num_qubits);
35
36 // Optional: set error rate
37 qkd = qkd.with_error_rate(0.03);
38 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
39
40 // Distribute key
41 println!("Performing quantum key distribution...");
42 let start = Instant::now();
43 let key_length = qkd.distribute_key()?;
44 println!("Key distribution completed in {:.2?}", start.elapsed());
45 println!("Final key length: {} bits", key_length);
46
47 // Verify keys match
48 println!("Verifying Alice and Bob have identical keys...");
49 if qkd.verify_keys() {
50 println!("✓ Key verification successful!");
51
52 // Display part of the key (first 8 bytes)
53 if let Some(key) = qkd.get_alice_key() {
54 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
55 }
56 } else {
57 println!("✗ Key verification failed!");
58 }
59
60 // Use the key for encryption
61 if let Some(key) = qkd.get_alice_key() {
62 let message = b"Hello, quantum world!";
63
64 println!(
65 "Encrypting message: '{}'",
66 std::str::from_utf8(message).unwrap()
67 );
68 let encrypted = quantrs2_ml::crypto::encrypt_with_qkd(message, key);
69
70 println!("Encrypted data: {:?}", &encrypted);
71
72 // Decrypt with Bob's key
73 if let Some(bob_key) = qkd.get_bob_key() {
74 let decrypted = quantrs2_ml::crypto::decrypt_with_qkd(&encrypted, bob_key);
75 println!(
76 "Decrypted message: '{}'",
77 std::str::from_utf8(&decrypted).unwrap()
78 );
79 }
80 }
81
82 Ok(())
83}
84
85fn run_e91_example() -> Result<()> {
86 println!("\nE91 Entanglement-Based Protocol");
87 println!("------------------------------");
88
89 // Create E91 QKD with 800 qubits
90 let num_qubits = 800;
91 println!("Creating E91 protocol with {} qubits", num_qubits);
92 let mut qkd = QuantumKeyDistribution::new(ProtocolType::E91, num_qubits);
93
94 // Set error rate
95 qkd = qkd.with_error_rate(0.02);
96 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
97
98 // Distribute key
99 println!("Performing quantum key distribution with entangled pairs...");
100 let start = Instant::now();
101 let key_length = qkd.distribute_key()?;
102 println!("Key distribution completed in {:.2?}", start.elapsed());
103 println!("Final key length: {} bits", key_length);
104
105 // Verify keys match
106 println!("Verifying Alice and Bob have identical keys...");
107 if qkd.verify_keys() {
108 println!("✓ Key verification successful!");
109
110 // Display part of the key
111 if let Some(key) = qkd.get_alice_key() {
112 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
113 }
114 } else {
115 println!("✗ Key verification failed!");
116 }
117
118 Ok(())
119}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 37)
27fn run_bb84_example() -> Result<()> {
28 println!("\nBB84 Quantum Key Distribution");
29 println!("----------------------------");
30
31 // Create BB84 QKD with 1000 qubits
32 let num_qubits = 1000;
33 println!("Creating BB84 protocol with {} qubits", num_qubits);
34 let mut qkd = QuantumKeyDistribution::new(ProtocolType::BB84, num_qubits);
35
36 // Optional: set error rate
37 qkd = qkd.with_error_rate(0.03);
38 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
39
40 // Distribute key
41 println!("Performing quantum key distribution...");
42 let start = Instant::now();
43 let key_length = qkd.distribute_key()?;
44 println!("Key distribution completed in {:.2?}", start.elapsed());
45 println!("Final key length: {} bits", key_length);
46
47 // Verify keys match
48 println!("Verifying Alice and Bob have identical keys...");
49 if qkd.verify_keys() {
50 println!("✓ Key verification successful!");
51
52 // Display part of the key (first 8 bytes)
53 if let Some(key) = qkd.get_alice_key() {
54 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
55 }
56 } else {
57 println!("✗ Key verification failed!");
58 }
59
60 // Use the key for encryption
61 if let Some(key) = qkd.get_alice_key() {
62 let message = b"Hello, quantum world!";
63
64 println!(
65 "Encrypting message: '{}'",
66 std::str::from_utf8(message).unwrap()
67 );
68 let encrypted = quantrs2_ml::crypto::encrypt_with_qkd(message, key);
69
70 println!("Encrypted data: {:?}", &encrypted);
71
72 // Decrypt with Bob's key
73 if let Some(bob_key) = qkd.get_bob_key() {
74 let decrypted = quantrs2_ml::crypto::decrypt_with_qkd(&encrypted, bob_key);
75 println!(
76 "Decrypted message: '{}'",
77 std::str::from_utf8(&decrypted).unwrap()
78 );
79 }
80 }
81
82 Ok(())
83}
84
85fn run_e91_example() -> Result<()> {
86 println!("\nE91 Entanglement-Based Protocol");
87 println!("------------------------------");
88
89 // Create E91 QKD with 800 qubits
90 let num_qubits = 800;
91 println!("Creating E91 protocol with {} qubits", num_qubits);
92 let mut qkd = QuantumKeyDistribution::new(ProtocolType::E91, num_qubits);
93
94 // Set error rate
95 qkd = qkd.with_error_rate(0.02);
96 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
97
98 // Distribute key
99 println!("Performing quantum key distribution with entangled pairs...");
100 let start = Instant::now();
101 let key_length = qkd.distribute_key()?;
102 println!("Key distribution completed in {:.2?}", start.elapsed());
103 println!("Final key length: {} bits", key_length);
104
105 // Verify keys match
106 println!("Verifying Alice and Bob have identical keys...");
107 if qkd.verify_keys() {
108 println!("✓ Key verification successful!");
109
110 // Display part of the key
111 if let Some(key) = qkd.get_alice_key() {
112 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
113 }
114 } else {
115 println!("✗ Key verification failed!");
116 }
117
118 Ok(())
119}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 43)
27fn run_bb84_example() -> Result<()> {
28 println!("\nBB84 Quantum Key Distribution");
29 println!("----------------------------");
30
31 // Create BB84 QKD with 1000 qubits
32 let num_qubits = 1000;
33 println!("Creating BB84 protocol with {} qubits", num_qubits);
34 let mut qkd = QuantumKeyDistribution::new(ProtocolType::BB84, num_qubits);
35
36 // Optional: set error rate
37 qkd = qkd.with_error_rate(0.03);
38 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
39
40 // Distribute key
41 println!("Performing quantum key distribution...");
42 let start = Instant::now();
43 let key_length = qkd.distribute_key()?;
44 println!("Key distribution completed in {:.2?}", start.elapsed());
45 println!("Final key length: {} bits", key_length);
46
47 // Verify keys match
48 println!("Verifying Alice and Bob have identical keys...");
49 if qkd.verify_keys() {
50 println!("✓ Key verification successful!");
51
52 // Display part of the key (first 8 bytes)
53 if let Some(key) = qkd.get_alice_key() {
54 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
55 }
56 } else {
57 println!("✗ Key verification failed!");
58 }
59
60 // Use the key for encryption
61 if let Some(key) = qkd.get_alice_key() {
62 let message = b"Hello, quantum world!";
63
64 println!(
65 "Encrypting message: '{}'",
66 std::str::from_utf8(message).unwrap()
67 );
68 let encrypted = quantrs2_ml::crypto::encrypt_with_qkd(message, key);
69
70 println!("Encrypted data: {:?}", &encrypted);
71
72 // Decrypt with Bob's key
73 if let Some(bob_key) = qkd.get_bob_key() {
74 let decrypted = quantrs2_ml::crypto::decrypt_with_qkd(&encrypted, bob_key);
75 println!(
76 "Decrypted message: '{}'",
77 std::str::from_utf8(&decrypted).unwrap()
78 );
79 }
80 }
81
82 Ok(())
83}
84
85fn run_e91_example() -> Result<()> {
86 println!("\nE91 Entanglement-Based Protocol");
87 println!("------------------------------");
88
89 // Create E91 QKD with 800 qubits
90 let num_qubits = 800;
91 println!("Creating E91 protocol with {} qubits", num_qubits);
92 let mut qkd = QuantumKeyDistribution::new(ProtocolType::E91, num_qubits);
93
94 // Set error rate
95 qkd = qkd.with_error_rate(0.02);
96 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
97
98 // Distribute key
99 println!("Performing quantum key distribution with entangled pairs...");
100 let start = Instant::now();
101 let key_length = qkd.distribute_key()?;
102 println!("Key distribution completed in {:.2?}", start.elapsed());
103 println!("Final key length: {} bits", key_length);
104
105 // Verify keys match
106 println!("Verifying Alice and Bob have identical keys...");
107 if qkd.verify_keys() {
108 println!("✓ Key verification successful!");
109
110 // Display part of the key
111 if let Some(key) = qkd.get_alice_key() {
112 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
113 }
114 } else {
115 println!("✗ Key verification failed!");
116 }
117
118 Ok(())
119}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 49)
27fn run_bb84_example() -> Result<()> {
28 println!("\nBB84 Quantum Key Distribution");
29 println!("----------------------------");
30
31 // Create BB84 QKD with 1000 qubits
32 let num_qubits = 1000;
33 println!("Creating BB84 protocol with {} qubits", num_qubits);
34 let mut qkd = QuantumKeyDistribution::new(ProtocolType::BB84, num_qubits);
35
36 // Optional: set error rate
37 qkd = qkd.with_error_rate(0.03);
38 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
39
40 // Distribute key
41 println!("Performing quantum key distribution...");
42 let start = Instant::now();
43 let key_length = qkd.distribute_key()?;
44 println!("Key distribution completed in {:.2?}", start.elapsed());
45 println!("Final key length: {} bits", key_length);
46
47 // Verify keys match
48 println!("Verifying Alice and Bob have identical keys...");
49 if qkd.verify_keys() {
50 println!("✓ Key verification successful!");
51
52 // Display part of the key (first 8 bytes)
53 if let Some(key) = qkd.get_alice_key() {
54 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
55 }
56 } else {
57 println!("✗ Key verification failed!");
58 }
59
60 // Use the key for encryption
61 if let Some(key) = qkd.get_alice_key() {
62 let message = b"Hello, quantum world!";
63
64 println!(
65 "Encrypting message: '{}'",
66 std::str::from_utf8(message).unwrap()
67 );
68 let encrypted = quantrs2_ml::crypto::encrypt_with_qkd(message, key);
69
70 println!("Encrypted data: {:?}", &encrypted);
71
72 // Decrypt with Bob's key
73 if let Some(bob_key) = qkd.get_bob_key() {
74 let decrypted = quantrs2_ml::crypto::decrypt_with_qkd(&encrypted, bob_key);
75 println!(
76 "Decrypted message: '{}'",
77 std::str::from_utf8(&decrypted).unwrap()
78 );
79 }
80 }
81
82 Ok(())
83}
84
85fn run_e91_example() -> Result<()> {
86 println!("\nE91 Entanglement-Based Protocol");
87 println!("------------------------------");
88
89 // Create E91 QKD with 800 qubits
90 let num_qubits = 800;
91 println!("Creating E91 protocol with {} qubits", num_qubits);
92 let mut qkd = QuantumKeyDistribution::new(ProtocolType::E91, num_qubits);
93
94 // Set error rate
95 qkd = qkd.with_error_rate(0.02);
96 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
97
98 // Distribute key
99 println!("Performing quantum key distribution with entangled pairs...");
100 let start = Instant::now();
101 let key_length = qkd.distribute_key()?;
102 println!("Key distribution completed in {:.2?}", start.elapsed());
103 println!("Final key length: {} bits", key_length);
104
105 // Verify keys match
106 println!("Verifying Alice and Bob have identical keys...");
107 if qkd.verify_keys() {
108 println!("✓ Key verification successful!");
109
110 // Display part of the key
111 if let Some(key) = qkd.get_alice_key() {
112 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
113 }
114 } else {
115 println!("✗ Key verification failed!");
116 }
117
118 Ok(())
119}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 53)
27fn run_bb84_example() -> Result<()> {
28 println!("\nBB84 Quantum Key Distribution");
29 println!("----------------------------");
30
31 // Create BB84 QKD with 1000 qubits
32 let num_qubits = 1000;
33 println!("Creating BB84 protocol with {} qubits", num_qubits);
34 let mut qkd = QuantumKeyDistribution::new(ProtocolType::BB84, num_qubits);
35
36 // Optional: set error rate
37 qkd = qkd.with_error_rate(0.03);
38 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
39
40 // Distribute key
41 println!("Performing quantum key distribution...");
42 let start = Instant::now();
43 let key_length = qkd.distribute_key()?;
44 println!("Key distribution completed in {:.2?}", start.elapsed());
45 println!("Final key length: {} bits", key_length);
46
47 // Verify keys match
48 println!("Verifying Alice and Bob have identical keys...");
49 if qkd.verify_keys() {
50 println!("✓ Key verification successful!");
51
52 // Display part of the key (first 8 bytes)
53 if let Some(key) = qkd.get_alice_key() {
54 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
55 }
56 } else {
57 println!("✗ Key verification failed!");
58 }
59
60 // Use the key for encryption
61 if let Some(key) = qkd.get_alice_key() {
62 let message = b"Hello, quantum world!";
63
64 println!(
65 "Encrypting message: '{}'",
66 std::str::from_utf8(message).unwrap()
67 );
68 let encrypted = quantrs2_ml::crypto::encrypt_with_qkd(message, key);
69
70 println!("Encrypted data: {:?}", &encrypted);
71
72 // Decrypt with Bob's key
73 if let Some(bob_key) = qkd.get_bob_key() {
74 let decrypted = quantrs2_ml::crypto::decrypt_with_qkd(&encrypted, bob_key);
75 println!(
76 "Decrypted message: '{}'",
77 std::str::from_utf8(&decrypted).unwrap()
78 );
79 }
80 }
81
82 Ok(())
83}
84
85fn run_e91_example() -> Result<()> {
86 println!("\nE91 Entanglement-Based Protocol");
87 println!("------------------------------");
88
89 // Create E91 QKD with 800 qubits
90 let num_qubits = 800;
91 println!("Creating E91 protocol with {} qubits", num_qubits);
92 let mut qkd = QuantumKeyDistribution::new(ProtocolType::E91, num_qubits);
93
94 // Set error rate
95 qkd = qkd.with_error_rate(0.02);
96 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
97
98 // Distribute key
99 println!("Performing quantum key distribution with entangled pairs...");
100 let start = Instant::now();
101 let key_length = qkd.distribute_key()?;
102 println!("Key distribution completed in {:.2?}", start.elapsed());
103 println!("Final key length: {} bits", key_length);
104
105 // Verify keys match
106 println!("Verifying Alice and Bob have identical keys...");
107 if qkd.verify_keys() {
108 println!("✓ Key verification successful!");
109
110 // Display part of the key
111 if let Some(key) = qkd.get_alice_key() {
112 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
113 }
114 } else {
115 println!("✗ Key verification failed!");
116 }
117
118 Ok(())
119}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 73)
27fn run_bb84_example() -> Result<()> {
28 println!("\nBB84 Quantum Key Distribution");
29 println!("----------------------------");
30
31 // Create BB84 QKD with 1000 qubits
32 let num_qubits = 1000;
33 println!("Creating BB84 protocol with {} qubits", num_qubits);
34 let mut qkd = QuantumKeyDistribution::new(ProtocolType::BB84, num_qubits);
35
36 // Optional: set error rate
37 qkd = qkd.with_error_rate(0.03);
38 println!("Simulated error rate: {:.1}%", qkd.error_rate * 100.0);
39
40 // Distribute key
41 println!("Performing quantum key distribution...");
42 let start = Instant::now();
43 let key_length = qkd.distribute_key()?;
44 println!("Key distribution completed in {:.2?}", start.elapsed());
45 println!("Final key length: {} bits", key_length);
46
47 // Verify keys match
48 println!("Verifying Alice and Bob have identical keys...");
49 if qkd.verify_keys() {
50 println!("✓ Key verification successful!");
51
52 // Display part of the key (first 8 bytes)
53 if let Some(key) = qkd.get_alice_key() {
54 println!("First 8 bytes of key: {:?}", &key[0..8.min(key.len())]);
55 }
56 } else {
57 println!("✗ Key verification failed!");
58 }
59
60 // Use the key for encryption
61 if let Some(key) = qkd.get_alice_key() {
62 let message = b"Hello, quantum world!";
63
64 println!(
65 "Encrypting message: '{}'",
66 std::str::from_utf8(message).unwrap()
67 );
68 let encrypted = quantrs2_ml::crypto::encrypt_with_qkd(message, key);
69
70 println!("Encrypted data: {:?}", &encrypted);
71
72 // Decrypt with Bob's key
73 if let Some(bob_key) = qkd.get_bob_key() {
74 let decrypted = quantrs2_ml::crypto::decrypt_with_qkd(&encrypted, bob_key);
75 println!(
76 "Decrypted message: '{}'",
77 std::str::from_utf8(&decrypted).unwrap()
78 );
79 }
80 }
81
82 Ok(())
83}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.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.