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 Alice: channel: Channel sent: typing.List[bool] = field(default_factory=list) my_bases: typing.List[bool] = field(default_factory=list) key: typing.List[bool] = field(default_factory=list) correctness: typing.List[bool] = field(default_factory=list) def generate_key_and_send(self, n: int = 100): self.sent = [bool(r.randint(0, 1)) for _ in range(n)] self.my_bases = [bool(r.randint(0, 1)) for _ in range(n)] for key, basis in zip(self.sent, self.my_bases): self.channel.send_bit(key, basis) def generate_key(self, n: int = 100): self.generate_key_and_send(n) bobs_bases, bobs_correctness = bin_decode(self.channel.recv_classical(n, is_bob=False)), bin_decode( self.channel.recv_classical(n, is_bob=False)) self.correctness = [ my_basis == bobs_basis and bobs_correctness for my_basis, bobs_basis, bobs_correctness in zip(self.my_bases, bobs_bases, bobs_correctness)] self.channel.send_classical(bin_encode(self.correctness), is_bob=False) self.key = [k for k, c in zip(self.sent, self.correctness) if c]