Quantum Cryptography BB84 simulation
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
from dataclasses import dataclass, field
|
|
|
|
from channel import Channel, ChannelSym
|
|
|
|
import random as r
|
|
|
|
import typing
|
|
|
|
|
|
|
|
from utils import bin_encode, bin_decode
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Bob:
|
|
|
|
channel: Channel
|
|
|
|
my_results: typing.List[typing.Tuple[bool, bool]] = field(default_factory=list)
|
|
|
|
key: typing.List[bool] = field(default_factory=list)
|
|
|
|
true_basises: typing.List[bool] = field(default_factory=list)
|
|
|
|
my_basises: typing.List[bool] = field(default_factory=list)
|
|
|
|
|
|
|
|
def recv_photons(self, n: int = 100):
|
|
|
|
self.my_basises = [bool(r.randint(0, 1)) for _ in range(n)]
|
|
|
|
self.my_results = self.channel.get_photon_results(self.my_basises)
|
|
|
|
|
|
|
|
def send_basises_and_correctness(self):
|
|
|
|
self.channel.send_classical(bin_encode(self.my_basises))
|
|
|
|
self.channel.send_classical(bin_encode([left + right == 1 for left, right in self.my_results]))
|
|
|
|
|
|
|
|
def generate_key(self, n: int = 100):
|
|
|
|
self.recv_photons(n)
|
|
|
|
self.send_basises_and_correctness()
|
|
|
|
correctness = bin_decode(self.channel.recv_classical(n))
|
|
|
|
self.key = [(False if k[0] else True) for k, c in zip(self.my_results, correctness) if c]
|