neuralkey
Neural Key Exchange using Tree Parity Machine in Python
Hi! This is an implementaion of the Neural Key Exchange Protocol in python written with simplicity, understandability and code readability in mind. Suiltable for academic and researh purposes. The code is documented line by line, so lets get straight to theory.
Diffie–Hellman key exchange
If Alice and Bob wish to communicate with each other by exchanging encrypted messages, each of them should be able to decrypt the messages received from the other. And to do so, they have to exchange the keys with which the messages will be encrypted, and should be carefull not to let Eve get hold of the keys, or else she too would be able to decrypt the messages. So a secure protocol should be used for exchanging the keys, or else nosy Eve would be able to read all of Bob's and Alice's messages. Neural or not, the most used protocol for key exchange between two parties A and B is the Diffie-Hellman protocol. Diffie–Hellman Key Exchange establishes a shared secret between two parties that can be used for secret communication for exchanging data over a public network. The following conceptual diagram illustrates the general idea of the key exchange by using colors instead of very large numbers:
Neural Key Exchange Protocol
The Diffe-Hellman protocol can be implemented using a neural neural network with a single hidden layer (also called a tree parity machine). The number neurons in the hidden layer is denoted as
K
and the number of input neurons per hidden neuron is denoted as N
. There is a weight matrix W
between the input and hidden layers of dimensions K
xN
and the range of each weight is {-L, ..., -2, -1, 0, 1, 2, ..., +L}
where L
is a parameter of the tree.
Both Bob and Alice posses tree parity machines with same set of parameters (
K
, N
and L
). The weights of their machines are different, as they are randomly initialized. If they could synchronize the weights of their machines (without transferring the actual weights, ofcourse) they could then use those weights as a cryptographic key for the rest of their communication. The following algorithm is used to synchronize the weight matrices of Bob's and Alice's machines:
For a given random input vector x (of dimensions [
K
,N
]), the output (tau) of a tree parity machine is computed as follows:
And if the outputs of both Alice's and Bob's machines are equal, their weights are update using any of the following rules:
- Hebbian rule:
- Anti-hebbian rule:
- Random-walk:
The Code
The code is heavily documented. It consists of 4 files,
machine.py
, which defines a tree parity machine, update_rules.py
, which contains functions for all the three update rules, run.py
which simple creates 2 machines with default parameters and try to sync them and eve.py
which is similar to run.py
, except that nosy Eve will be trying to sync her machine with Alice's and Bob's. Will she get what she wants?
Installation
sudo git clone http://www.github.com/farizrahman4u/neuralkey.git
cd neuralkey/neuralkey
python run.py
python eve.py
Requirements
TODO
- Simulate Eve and man in the middle attack
- Improve visualization
#Bye!!