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