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