qnect 0.3.0

⚛ Quantum computing in Rust: from Bell pairs to distributed quantum networks.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use crate::builder::BackendType;
use crate::network::chat_protocol::{NetworkMessage, NodeType};
use crate::network::network::{LinkType, QuantumNetwork};
use crate::network::quantum_client::QuantumClient;
use std::collections::HashMap;
use std::io::{BufRead, BufReader};
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader as AsyncBufReader};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{Mutex, mpsc};

pub const QUANTUM_SERVICE: &str = "127.0.0.1:6666";

pub struct QuantumChatNode {
    name: String,
    node_type: NodeType,
    location: (f64, f64),
    network: Arc<Mutex<QuantumNetwork>>,
    quantum_client: Arc<Mutex<QuantumClient>>,
    message_rx: mpsc::Receiver<(String, NetworkMessage)>,
    message_tx: mpsc::Sender<(String, NetworkMessage)>,
    peers: Arc<Mutex<HashMap<String, mpsc::Sender<NetworkMessage>>>>,
}

impl QuantumChatNode {
    pub async fn new(
        name: &str,
        node_type: NodeType,
        location: (f64, f64),
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let mut network = QuantumNetwork::new_distributed();
        network.add_distributed_node("Alice", 50, BackendType::Stabilizer)?;
        network.add_distributed_node("Repeater", 16, BackendType::Stabilizer)?;
        network.add_distributed_node("Bob", 50, BackendType::Stabilizer)?;

        network.add_quantum_link(
            "Alice",
            "Repeater",
            LinkType::Fiber {
                length_km: 500.0,
                loss_db_per_km: 0.2,
            },
            0.85,
            100.0,
        )?;

        network.add_quantum_link(
            "Repeater",
            "Bob",
            LinkType::Fiber {
                length_km: 500.0,
                loss_db_per_km: 0.2,
            },
            0.85,
            100.0,
        )?;

        log::debug!("[{}] Connecting to quantum service...", name);
        let quantum_client = QuantumClient::connect().await?;
        log::debug!("[{}] ✓ Connected to quantum backend!", name);

        let (tx, rx) = mpsc::channel(100);

        Ok(QuantumChatNode {
            name: name.to_string(),
            node_type,
            location,
            network: Arc::new(Mutex::new(network)),
            quantum_client: Arc::new(Mutex::new(quantum_client)),
            message_rx: rx,
            message_tx: tx,
            peers: Arc::new(Mutex::new(HashMap::new())),
        })
    }

    // EXACT listen method
    pub async fn listen(&self, port: u16) -> Result<(), Box<dyn std::error::Error>> {
        let listener = TcpListener::bind(format!("127.0.0.1:{}", port)).await?;
        log::debug!("[{}] Listening on port {}", self.name, port);

        let name = self.name.clone();
        let peers = self.peers.clone();
        let tx = self.message_tx.clone();

        tokio::spawn(async move {
            loop {
                if let Ok((stream, addr)) = listener.accept().await {
                    log::debug!("[{}] Connection from {}", name, addr);
                    let name_clone = name.clone();
                    let peers_clone = peers.clone();
                    let tx_clone = tx.clone();

                    tokio::spawn(async move {
                        handle_connection(stream, name_clone, peers_clone, tx_clone).await;
                    });
                }
            }
        });

        Ok(())
    }

    pub async fn connect_to(
        &self,
        peer_name: &str,
        addr: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let stream = TcpStream::connect(addr).await?;
        log::debug!("[{}] Connected to {} at {}", self.name, peer_name, addr);

        let (reader, mut writer) = stream.into_split();

        // Send our info
        let msg = NetworkMessage::NodeJoin {
            name: self.name.clone(),
            node_type: self.node_type.clone(),
            location: self.location,
        };
        let json = serde_json::to_string(&msg)?;
        writer.write_all(format!("{}\n", json).as_bytes()).await?;

        // Create channel for this peer
        let (peer_tx, mut peer_rx) = mpsc::channel(100);
        {
            let mut peers = self.peers.lock().await;
            peers.insert(peer_name.to_string(), peer_tx);
        }

        // Spawn writer
        tokio::spawn(async move {
            while let Some(msg) = peer_rx.recv().await {
                let json = serde_json::to_string(&msg).unwrap();
                let _ = writer.write_all(format!("{}\n", json).as_bytes()).await;
            }
        });

        // Spawn reader
        let tx = self.message_tx.clone();
        let peer_name_clone = peer_name.to_string();
        tokio::spawn(async move {
            let reader = AsyncBufReader::new(reader);
            let mut lines = reader.lines();

            while let Ok(Some(line)) = lines.next_line().await {
                if let Ok(msg) = serde_json::from_str::<NetworkMessage>(&line) {
                    let _ = tx.send((peer_name_clone.clone(), msg)).await;
                }
            }
        });

        Ok(())
    }

    pub async fn run_bb84_alice(
        &mut self,
        bob: &str,
        rounds: usize,
    ) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        log::debug!(
            "\n[{}] Starting BB84 with {} ({} rounds)",
            self.name,
            bob,
            rounds
        );

        // Verify path exists
        let network = self.network.lock().await;
        let path = network
            .find_shortest_path(&self.name, bob)
            .ok_or("No path to Bob")?;
        log::debug!("[{}] Path found: {:?}", self.name, path);
        drop(network);

        // Send start signal
        self.send_to(bob, NetworkMessage::BB84Start { rounds })
            .await?;

        let mut alice_bits = Vec::new();
        let mut alice_bases = Vec::new();
        let mut bob_results = Vec::new();
        let mut shared_key = Vec::new();

        log::debug!("[{}] Preparing and sending quantum states...", self.name);

        for round in 0..rounds {
            // Alice's REAL quantum state preparation
            let bit = rand::random::<bool>();
            let alice_basis = rand::random::<bool>();
            alice_bits.push(bit);
            alice_bases.push(alice_basis);

            // REAL qubit allocation
            let mut quantum = self.quantum_client.lock().await;
            let alice_q = quantum.allocate_qubit(&self.name).await?;

            // REAL gate application
            if bit {
                quantum.apply_gate(&self.name, alice_q, "X").await?;
            }
            if alice_basis {
                quantum.apply_gate(&self.name, alice_q, "H").await?;
            }

            // REAL quantum teleportation to Bob
            let bob_q = quantum.teleport(&self.name, bob, alice_q).await?;
            drop(quantum);

            // Tell Bob which qubit to measure
            self.send_to(bob, NetworkMessage::BB84Measure {
                round,
                qubit_id: bob_q,
            })
            .await?;

            // Wait for Bob's measurement
            match tokio::time::timeout(Duration::from_secs(5), self.message_rx.recv()).await {
                Ok(Some((
                    from,
                    NetworkMessage::BB84Basis {
                        round: r,
                        basis: bob_basis,
                        result,
                    },
                ))) if from == bob && r == round => {
                    bob_results.push((bob_basis, result));

                    if alice_basis == bob_basis {
                        shared_key.push(result);
                        print!("");
                    } else {
                        print!("");
                    }
                }
                _ => {
                    log::debug!("\n[{}] Timeout waiting for Bob's measurement", self.name);
                }
            }

            if (round + 1) % 32 == 0 {
                log::debug!(" {}/{}", shared_key.len(), round + 1);
            }
        }

        log::debug!("\n[{}] Sending key reconciliation...", self.name);
        self.send_to(bob, NetworkMessage::BB84KeyBits {
            count: shared_key.len(),
        })
        .await?;

        for round in 0..rounds {
            if round < alice_bases.len()
                && round < bob_results.len()
                && alice_bases[round] == bob_results[round].0
            {
                self.send_to(bob, NetworkMessage::BB84Use { round }).await?;
            }
        }
        self.send_to(bob, NetworkMessage::BB84EndKey).await?;

        log::debug!(
            "[{}] ✅ Key established: {} bits",
            self.name,
            shared_key.len()
        );

        Ok(shared_key)
    }

    async fn send_to(
        &self,
        peer: &str,
        msg: NetworkMessage,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let peers = self.peers.lock().await;
        if let Some(tx) = peers.get(peer) {
            tx.send(msg).await?;
        }
        Ok(())
    }

    // BOB's BB84 - using REAL quantum measurements!
    pub async fn run_bb84_bob(
        &mut self,
        alice: &str,
    ) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
        log::debug!("\n[{}] Waiting for BB84 from {}", self.name, alice);

        // Wait for start
        loop {
            match self.message_rx.recv().await {
                Some((from, NetworkMessage::BB84Start { rounds })) if from == alice => {
                    log::debug!("[{}] BB84 started: {} rounds", self.name, rounds);
                    break;
                }
                _ => {}
            }
        }

        let mut all_measurements = Vec::new();

        log::debug!("[{}] Receiving and measuring quantum states...", self.name);

        // Phase 1: Measure all qubits (like example 29)
        loop {
            match tokio::time::timeout(Duration::from_secs(10), self.message_rx.recv()).await {
                Ok(Some((from, NetworkMessage::BB84Measure { round, qubit_id })))
                    if from == alice =>
                {
                    // Bob's REAL quantum measurement
                    let bob_basis = rand::random::<bool>();

                    let mut quantum = self.quantum_client.lock().await;

                    // Apply basis if H
                    if bob_basis {
                        quantum.apply_gate(&self.name, qubit_id, "H").await?;
                    }

                    // REAL measurement
                    let result = quantum.measure(&self.name, qubit_id).await?;
                    drop(quantum);

                    all_measurements.push(result);

                    // Send basis and result back to Alice
                    self.send_to(alice, NetworkMessage::BB84Basis {
                        round,
                        basis: bob_basis,
                        result,
                    })
                    .await?;
                }
                Ok(Some((from, NetworkMessage::BB84KeyBits { .. }))) if from == alice => {
                    // Key reconciliation phase started
                    self.message_tx
                        .send((from, NetworkMessage::BB84KeyBits { count: 0 }))
                        .await?;
                    break;
                }
                _ => {}
            }
        }

        // Phase 2: Alice tells us which bits to keep
        let mut shared_key = Vec::new();

        loop {
            match self.message_rx.recv().await {
                Some((from, NetworkMessage::BB84Use { round })) if from == alice => {
                    if round < all_measurements.len() {
                        shared_key.push(all_measurements[round]);
                    }
                }
                Some((from, NetworkMessage::BB84EndKey)) if from == alice => {
                    break;
                }
                _ => {}
            }
        }

        log::debug!(
            "[{}] ✅ Received {} key bits from Alice",
            self.name,
            shared_key.len()
        );

        Ok(shared_key)
    }

    // Chat function using the quantum-derived key
    pub async fn run_chat(
        &mut self,
        peer: &str,
        key: Vec<u8>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        log::debug!("\n💬 Quantum-Secured Chat Active!");
        log::debug!("Type 'quit' to exit\n");

        let key_clone = key.clone();
        let peer_name = peer.to_string();
        let my_name = self.name.clone();

        // Spawn message receiver
        let (decrypt_tx, decrypt_rx) = mpsc::channel::<String>(10);
        // let msg_tx = self.message_tx.clone();

        tokio::spawn(async move {
            let mut msg_rx = decrypt_rx;
            while let Some(encrypted_hex) = msg_rx.recv().await {
                if let Ok(encrypted_bytes) = hex::decode(&encrypted_hex) {
                    let decrypted: Vec<u8> = encrypted_bytes
                        .iter()
                        .zip(key_clone.iter().cycle())
                        .map(|(e, k)| e ^ k)
                        .collect();

                    let msg = String::from_utf8_lossy(&decrypted);
                    log::debug!("\n{}: {}", peer_name, msg);
                    print!("{} > ", my_name);
                    std::io::Write::flush(&mut std::io::stdout()).unwrap();
                }
            }
        });

        // Handle stdin
        let (input_tx, mut input_rx) = mpsc::channel::<String>(10);

        std::thread::spawn(move || {
            let stdin = std::io::stdin();
            let reader = BufReader::new(stdin);

            for line in reader.lines() {
                if let Ok(line) = line {
                    let _ = input_tx.blocking_send(line);
                }
            }
        });

        print!("{} > ", self.name);
        std::io::Write::flush(&mut std::io::stdout())?;

        loop {
            tokio::select! {
                // Handle incoming messages
                Some((from, msg)) = self.message_rx.recv() => {
                    if from == peer {
                        if let NetworkMessage::EncryptedMessage { data } = msg {
                            decrypt_tx.send(data).await?;
                        }
                    }
                }

                // Handle user input
                Some(line) = input_rx.recv() => {
                    let message = line.trim();

                    if message == "quit" {
                        break;
                    }

                    if !message.is_empty() {
                        // Encrypt with quantum key
                        let encrypted: Vec<u8> = message.bytes()
                            .zip(key.iter().cycle())
                            .map(|(m, k)| m ^ k)
                            .collect();

                        self.send_to(peer, NetworkMessage::EncryptedMessage {
                            data: hex::encode(&encrypted)
                        }).await?;
                    }

                    print!("{} > ", self.name);
                    std::io::Write::flush(&mut std::io::stdout())?;
                }
            }
        }

        Ok(())
    }
}

async fn handle_connection(
    stream: TcpStream,
    our_name: String,
    peers: Arc<Mutex<HashMap<String, mpsc::Sender<NetworkMessage>>>>,
    message_tx: mpsc::Sender<(String, NetworkMessage)>,
) {
    let (reader, mut writer) = stream.into_split();
    let mut lines = AsyncBufReader::new(reader).lines();

    // Read peer info
    if let Ok(Some(line)) = lines.next_line().await {
        if let Ok(NetworkMessage::NodeJoin { name, .. }) =
            serde_json::from_str::<NetworkMessage>(&line)
        {
            log::debug!("[{}] Peer identified as {}", our_name, name);

            // Create channel for this peer
            let (peer_tx, mut peer_rx) = mpsc::channel(100);
            {
                let mut p = peers.lock().await;
                p.insert(name.clone(), peer_tx);
            }

            // Spawn writer
            tokio::spawn(async move {
                while let Some(msg) = peer_rx.recv().await {
                    let json = serde_json::to_string(&msg).unwrap();
                    let _ = writer.write_all(format!("{}\n", json).as_bytes()).await;
                }
            });

            // Continue reading
            while let Ok(Some(line)) = lines.next_line().await {
                if let Ok(msg) = serde_json::from_str::<NetworkMessage>(&line) {
                    let _ = message_tx.send((name.clone(), msg)).await;
                }
            }
        }
    }
}