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.

30 lines
1.1 KiB

3 years ago
from dataclasses import dataclass, field
from channel import Channel, ChannelSym
3 years ago
import random as r
import typing
from utils import bin_encode, bin_decode
3 years ago
@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)
correctness: typing.List[bool] = field(default_factory=list)
my_bases: typing.List[bool] = field(default_factory=list)
3 years ago
def recv_photons(self, n: int = 100):
self.my_bases = [bool(r.randint(0, 1)) for _ in range(n)]
self.my_results = self.channel.get_photon_results(self.my_bases)
3 years ago
def send_bases_and_correctness(self):
self.channel.send_classical(bin_encode(self.my_bases))
self.channel.send_classical(bin_encode([left + right >= 1 for left, right in self.my_results]))
3 years ago
def generate_key(self, n: int = 100):
self.recv_photons(n)
self.send_bases_and_correctness()
self.correctness = bin_decode(self.channel.recv_classical(n))
self.key = [(False if k[0] else True) for k, c in zip(self.my_results, self.correctness) if c]