QKDSimkit.core package

alice module

This module simulates Alice’s operations

QKDSimkit.core.alice.import_key(channel_address: str, ID: str, size: int = 256)[source]

Alice’s procedure to agree on a shared key

Parameters
  • channel_address (str) – channel address

  • ID (str) – identifier of alice-bob pair

  • size (int) – size of key in bits

bob module

This module simulates Bob’s operations

QKDSimkit.core.bob.import_key(channel_address: str, ID: str, size: int = 256)[source]

Bob’s procedure to agree on a shared key

Parameters
  • channel_address (str) – channel address

  • ID (str) – identifier of alice-bob pair

  • size (int) – size of key in bits

channel module

This module simulates channel’s operations

class QKDSimkit.core.channel.public_channel(address: str, noise: float, eve: bool)[source]

Bases: object

initiate_channel(*port)[source]

Start channel

initiate_connection(conn, addr)[source]

Listen for messages and broadcast them

channel_features module

This module contains methods to simulate some feature of a channel

core.channel_features.eavesdropper(photon_stream) str[source]

Method to simulate an eavesdropper in a quantum channel

Parameters

photon_stream (str) – message containing polarizations

Returns

message with new (eavesdropped) polarizations

core.channel_features.random_errors(photon_stream, rate: float) str[source]

Method to simulate random errors in a quantum channel

Parameters
  • photon_stream (str) – message containing polarizations

  • rate (float) – decimal number from 0 to 1, it sets the error rate

Returns

message with errors

models module

This module simulates the behaviour of a photon

class QKDSimkit.core.models.Photon[source]

Bases: object

Photon, it has bit, basis and polarization and some methods to manipulate them

create_random_bit() int[source]

Assign a random bit to the photon

measure(polarization: int) int[source]

Give back a polarization according to the basis of the photon

Parameters

polarization (int) – polarization

Returns

polarization

select_random_basis()[source]

Assign a random basis to the photon

set_bit_from_measurement() int[source]

Set bits according to settings of polarization and basis.

Returns

return a bit for each case

Return type

bit

set_polarization()[source]

Assign the right polarization to the photon according to basis and bit

node module

class QKDSimkit.core.node.Node(ID, size)[source]

Bases: object

Father class for receiver and sender

Parameters
  • ID (str) – identifier of alice-bob pair

  • size (int) – size of key in bits

min_shared[source]
Type

int

buffer_size[source]
Type

int

timeout_in_seconds[source]
Type

int

connection_attempts[source]
Type

int

max_repetitions[source]
Type

int

min_shared_percent[source]
Type

float

socket[source]

socket

ID[source]

identifier of alice-bob pair

Type

str

photon_pulse[source]

list of photons

Type

list

bases[source]

list of bases

Type

list

other_bases[source]

list of bases of the other node

Type

list

reconciled_key[source]

key with only common values and “”

Type

list

shared_key[source]

key with only common values

Type

list

sub_shared_key[source]

first half of the key

Type

list

other_sub_key[source]

first half of the key of the other node

Type

list

decision[source]

result of comparison between shared part of the key

Type

int

other_decision[source]

result of comparison between ke ys

Type

int

not_shared_key[source]
Type

list

key[source]

non-revealed part of the key

Type

list

fragments[source]
Type

list

regex[source]

utility regex

photon_pulse_size[source]

number of photons exchanged photons

Type

int

connect_to_channel(address: str, port: int)[source]

It starts the connection with the channel

Parameters
  • address (str) – address

  • port (int) – port

create_keys()[source]

It calls two functions to elaborate th photon pulse in actual keys

create_shared_key()[source]

Converts the whole photon pulse in a list of bit

create_sub_shared_key()[source]

Create the part of the key that will be sent to the other node (first half)

get_key()[source]

Create the part of the key that will not be sent and it will be used as symmetric key (second half)

listen_for(sender: str, attr: str)[source]

Receive a message and store it in the right place

Parameters
  • sender (str) – name of the node from which we expect to receive

  • attr (str) – name of the attribute that will store the content of the received message

ownMessage(addr: str) bool[source]

Check if a message comes from this node

Parameters

addr (str) – address of the received message

abstract recv(header: str)[source]

abstract method

recv_all() list[source]

receive a message receive from socket, sum every part of the message, check if the message is commplete, check if the ID of the sender corresponds to the ID of the receiver, return a list whit header and payload, please note that the ID is not returned

Returns

list made by header and payload

Return type

fragments (list)

reset_socket()[source]

Reset the previously started socket

abstract send(header: str, message: str)[source]

abstract method

validate() int[source]

Wrapper of utils.validate it manages different outputs, it also gives info of eventual errors

Returns

1: keys are equals, 0: error rate is below a given percent, -1: error rate too high

Return type

int

qexceptions module

exception QKDSimkit.core.qexceptions.aliceerror[source]

Bases: Exception

exception QKDSimkit.core.qexceptions.boberror[source]

Bases: Exception

exception QKDSimkit.core.qexceptions.cacheerror[source]

Bases: Exception

exception QKDSimkit.core.qexceptions.qnoiseerror[source]

Bases: Exception

exception QKDSimkit.core.qexceptions.qobjecterror[source]

Bases: Exception

exception QKDSimkit.core.qexceptions.qsocketerror[source]

Bases: Exception

receiver module

class QKDSimkit.core.receiver.Receiver(ID, size)[source]

Bases: Node

Receiver class, it expands node, it contains methods to communicate a sender node, it can’t take action but it has to wait for the sender node for sending data, it can answer to a request with a message or to a message with an acknowledgement

listen_quantum()[source]

Listen method to receive photon pulse it behaves like a wrapper for recv for photon pulses

measure_photon_pulse()[source]

Measure photon pulse

given the vector that stores polarizations of received photons it creates a list of photons each polarization, basis and bit will be determined byt the measure method according to physical properties, a list with the basis of every photon will be stored

recv(header: str) str[source]

Receive function for receiver node it listen for a message, in case the header of the received message doesn’t match it checks if an acknowledgment for the received message has been already sent or if the received message is an acknowledgement itself for a previously sent message, it sends a new acknowledgment in the first case and it sends again the message in the other case, if the message is new and the header is correct it sends an acknowledgement and it saves it

Parameters

header (str) – unique identifier of the message that has to be received

Returns

the payload of the received data, the header and some other infos are not returned

Return type

message (str)

send(header: str, message: str)[source]

Send method for receiver it listens for the request from the sender node, in case the header of the received message doesn’t match it checks if an acknowledgment for the received header has been already sent or if the message for the requested header has been already sent, it sends a new acknowledgement in the first case and it sends again the message in the other case, it sends the expected message if the header is correct, the sent message is saved in a list

Parameters
  • header (str) – unique identifier

  • message (str) – message

sender module

class QKDSimkit.core.sender.Sender(ID, size: int)[source]

Bases: Node

Sender class, it expands Node, it contains methods to communicate a receiver node, the general idea is that this node dictate the communication and the receiver can just answer

create_photon_pulse() list[source]

Create a list of photons given a size :returns: list of photons

generate_reconciled_key()[source]

Generate a common key between the two parties

it checks for every photon if the chosen basis is common, if it is not common the basis is discarded

recv(header: str)[source]

Receiver method for sender node It will send a request message with the given header and it will wait for the response for a time timeout_in_seconds it may try multiple times depending on the variable connection_attempts, every received message with a different header will be discarded

Parameters

header (str) – unique identifier

send(header: str, message: str)[source]

Sender method for sender node it sends a message and wait for an acknowledgment if it doesn’t receive the ack in the given time timeout_in_seconds it may try multiple times depending on the variable connection_attempts

Parameters
  • header (str) – unique identifier

  • message (str) – string to be sent

send_photon_pulse(pulse: list)[source]

Send an already created photon pulse it takes the polarization from each photon

Parameters

pulse (list) – photon pulse to be sent

utils module

QKDSimkit.core.utils.decrypt(token: bytes, message: str) str[source]

Decrypts a message using a given key

Parameters
  • token (bytes) – key (32 url-safe base64-encoded bytes)

  • message (str) – encrypted message

Returns

clear text message (str)

QKDSimkit.core.utils.encrypt(token: bytes, message: str) str[source]

Encrypts a message using a given key

Parameters
  • token (bytes) – key (32 url-safe base64-encoded bytes)

  • message (str) – clear text message

Returns

encrypted message (str)

QKDSimkit.core.utils.generate_token(password)[source]

Generates a common token

QKDSimkit.core.utils.hash_token(token: str)[source]

Hash a string

Parameters

token (str) – message

Returns: hash (str)

QKDSimkit.core.utils.validate(shared_key: list, other_shared_key: list) float[source]

It compares two keys to find differences

Parameters
  • shared_key (str) – first key

  • other_shared_key (str) – second key

Returns

percent of equal elements in the two keys

Module contents