├── README.md ├── images ├── 0-300.PNG ├── 2700-3000.PNG └── medium-symm-crypto.jpg ├── train.py └── weights ├── alice_weights ├── checkpoint ├── model.ckpt.data-00000-of-00001 ├── model.ckpt.index └── model.ckpt.meta ├── bob_weights ├── checkpoint ├── model.ckpt.data-00000-of-00001 ├── model.ckpt.index └── model.ckpt.meta └── eve_weights ├── checkpoint ├── model.ckpt.data-00000-of-00001 ├── model.ckpt.index └── model.ckpt.meta /README.md: -------------------------------------------------------------------------------- 1 | # Adversarial Neural Cryptography 2 | 3 | ![alt text](https://github.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/blob/master/images/medium-symm-crypto.jpg) 4 | 5 | orginal paper : [Learning to protect communications with adversarial neural cryptography](https://arxiv.org/abs/1610.06918) 6 | 7 | [medium article](https://towardsdatascience.com/life-of-alice-bob-and-eve-with-neural-net-6df0ad1d6077) 8 | 9 | ### Training parameters: 10 | ```python 11 | 12 | learning_rate = 0.0008 13 | batch_size = 4096 14 | sample_size = 4096*5 # 4096 according to the paper 15 | epochs = 10000 # 850000 according to the paper 16 | steps_per_epoch = int(sample_size/batch_size) 17 | 18 | 19 | # Input and output configuration. 20 | TEXT_SIZE = 16 21 | KEY_SIZE = 16 22 | 23 | # training iterations per actors. 24 | ITERS_PER_ACTOR = 1 25 | EVE_MULTIPLIER = 2 # Train Eve 2x for every step of Alice/Bob 26 | 27 | ``` 28 | 29 | 30 | ### Training Loss: 31 | First 300 epochs 32 | 33 | ![alt text](https://github.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/blob/master/images/0-300.PNG) 34 | 35 | Last 300 epochs 36 | 37 | ![alt text](https://github.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/blob/master/images/2700-3000.PNG) 38 | -------------------------------------------------------------------------------- /images/0-300.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/bcf6526d95bf36bc6c3b75d27cdf52963d1c7271/images/0-300.PNG -------------------------------------------------------------------------------- /images/2700-3000.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/bcf6526d95bf36bc6c3b75d27cdf52963d1c7271/images/2700-3000.PNG -------------------------------------------------------------------------------- /images/medium-symm-crypto.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/bcf6526d95bf36bc6c3b75d27cdf52963d1c7271/images/medium-symm-crypto.jpg -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 1 | 2 | import tensorflow as tf 3 | import numpy as np 4 | 5 | learning_rate = 0.0008 6 | batch_size = 4096 7 | sample_size = 4096*5 # 4096 according to the paper 8 | epochs = 10000 # 850000 according to the paper 9 | steps_per_epoch = int(sample_size/batch_size) 10 | 11 | # BOB_LOSS_THRESH = 0.02 # Exit when Bob loss < 0.02 and Eve > 7.7 bits 12 | # EVE_LOSS_THRESH = 7.7 13 | 14 | 15 | # Input and output configuration. 16 | TEXT_SIZE = 16 17 | KEY_SIZE = 16 18 | 19 | # Training parameters. 20 | ITERS_PER_ACTOR = 1 21 | EVE_MULTIPLIER = 2 # Train Eve 2x for every step of Alice/Bob 22 | 23 | # Set a random seed to help reproduce the output 24 | seed = 7919 25 | tf.set_random_seed(seed) 26 | np.random.seed(seed) 27 | 28 | # False if we want to train from scratch and true to contiune training a already trained model 29 | restore_trained_model = False 30 | 31 | 32 | def random_bools(sample_size, n): 33 | 34 | temp = np.random.random_integers(0, high=1, size=[sample_size, n]) 35 | temp = temp*2 - 1 36 | return temp.astype(np.float32) 37 | 38 | 39 | def model(collection, message, key=None): 40 | 41 | if key is not None: 42 | combined_message = tf.concat(axis=1, 43 | values=[message, key]) 44 | else: 45 | combined_message = message 46 | 47 | with tf.variable_scope(collection): 48 | fc = tf.layers.dense( combined_message, TEXT_SIZE + KEY_SIZE, activation=tf.nn.relu) 49 | fc = tf.expand_dims(fc, 2) 50 | 51 | # tf.contrib.layers.conv1d( input, filters, kernel_size, stride, padding, activation_fn) 52 | conv1 = tf.layers.conv1d( fc, filters= 2, kernel_size= 4, strides= 1, padding='SAME', activation=tf.nn.sigmoid) 53 | conv2 = tf.layers.conv1d( conv1, filters= 4, kernel_size= 2, strides=2, padding='VALID', activation=tf.nn.sigmoid) 54 | conv3 = tf.layers.conv1d( conv2, filters= 4, kernel_size= 1, strides=1, padding='SAME', activation=tf.nn.sigmoid) 55 | 56 | # output 57 | conv4 = tf.layers.conv1d( conv3, filters= 1, kernel_size= 1, strides=1, padding='SAME', activation=tf.nn.tanh) 58 | 59 | 60 | out = tf.squeeze(conv4, 2) 61 | return out 62 | 63 | 64 | 65 | Alice_input_message = tf.placeholder(tf.float32, shape=(batch_size, TEXT_SIZE), name='Alice_input_message') 66 | Alice_input_key = tf.placeholder(tf.float32, shape=(batch_size, KEY_SIZE), name='Alice_input_key') 67 | 68 | 69 | 70 | Alice_out_cipher = model('Alice', Alice_input_message, Alice_input_key) 71 | Bob_out_message = model('Bob', Alice_out_cipher, Alice_input_key) 72 | Eve_out_message = model('Eve', Alice_out_cipher) 73 | 74 | 75 | ## Eves LOSS 76 | Eves_loss = (1/batch_size)*tf.reduce_sum( tf.abs( Eve_out_message - Alice_input_message )) 77 | 78 | ## ALICE AND BOB LOSS 79 | Bob_loss = (1/batch_size)*tf.reduce_sum( tf.abs( Bob_out_message - Alice_input_message )) 80 | Eve_evadropping_loss = tf.reduce_sum( tf.square(float(TEXT_SIZE) / 2.0 - Eves_loss) / ((TEXT_SIZE / 2)**2) ) 81 | 82 | Alice_bob_loss = Bob_loss + Eve_evadropping_loss 83 | 84 | 85 | 86 | # Get tensors to train 87 | Alice_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,scope='Alice') 88 | Bob_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,scope='Bob') 89 | Eve_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Eve') 90 | 91 | Eve_opt = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.9, epsilon=1e-08).minimize(Eves_loss, var_list=[Eve_vars]) 92 | bob_opt = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.9, epsilon=1e-08).minimize(Alice_bob_loss, var_list=[Alice_vars + Bob_vars]) 93 | 94 | 95 | sess = tf.Session() 96 | init = tf.global_variables_initializer() 97 | sess.run(init) 98 | 99 | alice_saver = tf.train.Saver(Alice_vars) 100 | bob_saver = tf.train.Saver(Bob_vars) 101 | eve_saver = tf.train.Saver(Eve_vars) 102 | 103 | 104 | if restore_trained_model: 105 | alice_saver.restore(sess, "weights/alice_weights/model.ckpt") 106 | bob_saver.restore(sess, "weights/bob_weights/model.ckpt") 107 | eve_saver.restore(sess, "weights/eve_weights/model.ckpt") 108 | 109 | 110 | # DATASET 111 | messages = random_bools(sample_size, TEXT_SIZE) 112 | keys = random_bools(sample_size, KEY_SIZE) 113 | 114 | 115 | # Training begins 116 | for i in range(epochs): 117 | 118 | for j in range(steps_per_epoch): 119 | 120 | # get batch dataset to train 121 | batch_messages = messages[j*batch_size: (j+1)*batch_size] 122 | batch_keys = keys[j*batch_size: (j+1)*batch_size] 123 | 124 | # Train Alice and Bob 125 | for _ in range(ITERS_PER_ACTOR): 126 | temp = sess.run([bob_opt, Bob_loss, Eve_evadropping_loss, Bob_out_message],feed_dict={Alice_input_message:batch_messages , Alice_input_key:batch_keys }) 127 | 128 | temp_alice_bob_loss = temp[1] 129 | temp_eve_evs_loss = temp[2] 130 | temp_bob_msg = temp[3] 131 | 132 | # train Eve 133 | for _ in range(ITERS_PER_ACTOR*EVE_MULTIPLIER): 134 | temp = sess.run([Eve_opt, Eves_loss, Eve_out_message], feed_dict={Alice_input_message:batch_messages , Alice_input_key:batch_keys }) 135 | 136 | temp_eve_loss = temp[1] 137 | temp_eve_msg = temp[2] 138 | 139 | # save after every 500 epochs 140 | if i%500 == 0 and i!=0: 141 | alice_saver.save(sess, "weights/alice_weights/model.ckpt") 142 | bob_saver.save(sess, "weights/bob_weights/model.ckpt") 143 | eve_saver.save(sess, "weights/eve_weights/model.ckpt") 144 | 145 | 146 | # output bit error and loss after every 100 epochs 147 | if i%50 == 0: 148 | print(' epochs: ', i, ' bob bit error: ', temp_alice_bob_loss,' + ', temp_eve_evs_loss,' & eve bit error:', temp_eve_loss) 149 | 150 | sess.close() 151 | -------------------------------------------------------------------------------- /weights/alice_weights/checkpoint: -------------------------------------------------------------------------------- 1 | model_checkpoint_path: "model.ckpt" 2 | all_model_checkpoint_paths: "model.ckpt" 3 | -------------------------------------------------------------------------------- /weights/alice_weights/model.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/bcf6526d95bf36bc6c3b75d27cdf52963d1c7271/weights/alice_weights/model.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /weights/alice_weights/model.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/bcf6526d95bf36bc6c3b75d27cdf52963d1c7271/weights/alice_weights/model.ckpt.index -------------------------------------------------------------------------------- /weights/alice_weights/model.ckpt.meta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/bcf6526d95bf36bc6c3b75d27cdf52963d1c7271/weights/alice_weights/model.ckpt.meta -------------------------------------------------------------------------------- /weights/bob_weights/checkpoint: -------------------------------------------------------------------------------- 1 | model_checkpoint_path: "model.ckpt" 2 | all_model_checkpoint_paths: "model.ckpt" 3 | -------------------------------------------------------------------------------- /weights/bob_weights/model.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/bcf6526d95bf36bc6c3b75d27cdf52963d1c7271/weights/bob_weights/model.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /weights/bob_weights/model.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/bcf6526d95bf36bc6c3b75d27cdf52963d1c7271/weights/bob_weights/model.ckpt.index -------------------------------------------------------------------------------- /weights/bob_weights/model.ckpt.meta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/bcf6526d95bf36bc6c3b75d27cdf52963d1c7271/weights/bob_weights/model.ckpt.meta -------------------------------------------------------------------------------- /weights/eve_weights/checkpoint: -------------------------------------------------------------------------------- 1 | model_checkpoint_path: "model.ckpt" 2 | all_model_checkpoint_paths: "model.ckpt" 3 | -------------------------------------------------------------------------------- /weights/eve_weights/model.ckpt.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/bcf6526d95bf36bc6c3b75d27cdf52963d1c7271/weights/eve_weights/model.ckpt.data-00000-of-00001 -------------------------------------------------------------------------------- /weights/eve_weights/model.ckpt.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/bcf6526d95bf36bc6c3b75d27cdf52963d1c7271/weights/eve_weights/model.ckpt.index -------------------------------------------------------------------------------- /weights/eve_weights/model.ckpt.meta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/VamshikShetty/adversarial-neural-cryptography-tensorflow/bcf6526d95bf36bc6c3b75d27cdf52963d1c7271/weights/eve_weights/model.ckpt.meta --------------------------------------------------------------------------------