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.
 
 

29 lines
1.1 KiB

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)
correctness: typing.List[bool] = field(default_factory=list)
my_bases: typing.List[bool] = field(default_factory=list)
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)
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]))
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]