{ "cells": [ { "cell_type": "code", "execution_count": 1, "outputs": [], "source": [ "from src.models import SubstitutionKey, OuterKey, Mask, ServerKeys, MutualKeys, ClientKeys\n", "from src.server_darc import resolve_message" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.803060Z", "start_time": "2024-07-18T21:01:01.280375Z" } } }, { "cell_type": "code", "execution_count": 2, "outputs": [], "source": [ "def create_random_message(message_len, height):\n", " rand_message = OuterKey.init_matrix(height).matrix[0]\n", " return rand_message[:message_len]" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.808394Z", "start_time": "2024-07-18T21:01:01.805877Z" } } }, { "cell_type": "code", "execution_count": 3, "outputs": [], "source": [ "height = 70\n", "width = 3\n", "message_len = 70\n", "message = create_random_message(message_len, height)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.822786Z", "start_time": "2024-07-18T21:01:01.809032Z" } } }, { "cell_type": "code", "execution_count": 4, "outputs": [], "source": [ "server_substitution_data = SubstitutionKey.init_matrix(width, height)\n", "client_substitution_data = SubstitutionKey.init_matrix(width, height)\n", "mutual_substitution_data = SubstitutionKey.init_matrix(width, height)\n", "\n", "server_substitution_medium = SubstitutionKey.init_matrix(width, height)\n", "client_substitution_medium = SubstitutionKey.init_matrix(width, height)\n", "mutual_substitution_medium = SubstitutionKey.init_matrix(width, height)\n", "\n", "server_keys = ServerKeys.random_init(height, width)\n", "mutual_keys = MutualKeys.random_init(height, width)\n", "client_keys = ClientKeys.random_init(height, width)\n", "\n", "server_keys.persistent.data.alpha_key = server_substitution_data ^ mutual_substitution_data\n", "server_keys.persistent.medium.alpha_key = server_substitution_medium ^ mutual_substitution_medium\n", "\n", "mutual_keys.persistent.data.alpha_key = mutual_substitution_data ^ client_substitution_data\n", "mutual_keys.persistent.medium.alpha_key = mutual_substitution_medium ^ client_substitution_medium\n", "\n", "client_keys.persistent.data.alpha_key = client_substitution_data\n", "client_keys.persistent.medium.alpha_key = client_substitution_medium\n", "\n", "alphabet = SubstitutionKey.init_matrix(width, height, 255)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.916064Z", "start_time": "2024-07-18T21:01:01.815030Z" } } }, { "cell_type": "code", "execution_count": 5, "outputs": [], "source": [ "sdi1 = server_keys.persistent.data.inner_key_1 << server_keys.ephemeral.data.inner_key_1\n", "sdo1 = server_keys.persistent.data.outer_key_1 << server_keys.ephemeral.data.outer_key_1\n", "spda = server_keys.persistent.data.alpha_key\n", "seda = server_keys.ephemeral.data.alpha_key\n", "spma = server_keys.persistent.medium.alpha_key\n", "sema = server_keys.ephemeral.medium.alpha_key\n", "\n", "cdi1 = client_keys.persistent.data.inner_key_1 << client_keys.ephemeral.data.inner_key_1\n", "cdo1 = client_keys.persistent.data.outer_key_1 << client_keys.ephemeral.data.outer_key_1\n", "cdo2 = client_keys.persistent.data.outer_key_2 << client_keys.ephemeral.data.outer_key_2\n", "ceda = client_keys.ephemeral.data.alpha_key\n", "cpda = client_keys.persistent.data.alpha_key\n", "cmi1 = client_keys.persistent.medium.inner_key_1 << client_keys.ephemeral.medium.inner_key_1\n", "cmo1 = client_keys.persistent.medium.outer_key_1 << client_keys.ephemeral.medium.outer_key_1\n", "cmo2 = client_keys.persistent.medium.outer_key_2 << client_keys.ephemeral.medium.outer_key_2\n", "cema = client_keys.ephemeral.medium.alpha_key\n", "cpma = client_keys.persistent.medium.alpha_key\n", "\n", "mdi1 = mutual_keys.persistent.data.inner_key_1 << mutual_keys.ephemeral.data.inner_key_1\n", "mpdi2 = mutual_keys.persistent.data.inner_key_2\n", "mdo1 = mutual_keys.persistent.data.outer_key_1 << mutual_keys.ephemeral.data.outer_key_1\n", "mdo2 = mutual_keys.persistent.data.outer_key_2 << mutual_keys.ephemeral.data.outer_key_2\n", "mdo3 = mutual_keys.persistent.data.outer_key_3 << mutual_keys.ephemeral.data.outer_key_3\n", "mmi1 = mutual_keys.persistent.medium.inner_key_1 << mutual_keys.ephemeral.medium.inner_key_1\n", "mpmi2 = mutual_keys.persistent.medium.inner_key_2\n", "mmi3 = mutual_keys.persistent.medium.inner_key_3 << mutual_keys.ephemeral.medium.inner_key_3\n", "mmi4 = mutual_keys.persistent.medium.inner_key_4 << mutual_keys.ephemeral.medium.inner_key_4\n", "mmo1 = mutual_keys.persistent.medium.outer_key_1 << mutual_keys.ephemeral.medium.outer_key_1\n", "mmo2 = mutual_keys.persistent.medium.outer_key_2 << mutual_keys.ephemeral.medium.outer_key_2\n", "mmo3 = mutual_keys.persistent.medium.outer_key_3 << mutual_keys.ephemeral.medium.outer_key_3\n", "mmo4 = mutual_keys.persistent.medium.outer_key_4 << mutual_keys.ephemeral.medium.outer_key_4\n", "mmo5 = mutual_keys.persistent.medium.outer_key_5 << mutual_keys.ephemeral.medium.outer_key_5\n", "mmo6 = mutual_keys.persistent.medium.outer_key_6 << mutual_keys.ephemeral.medium.outer_key_6\n", "mema = mutual_keys.ephemeral.medium.alpha_key\n", "mpma = mutual_keys.persistent.medium.alpha_key\n", "mpda = mutual_keys.persistent.data.alpha_key\n", "meda = mutual_keys.ephemeral.data.alpha_key" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.916423Z", "start_time": "2024-07-18T21:01:01.850796Z" } } }, { "cell_type": "code", "execution_count": 6, "outputs": [], "source": [ "cdo1_cdo2 = cdo1 << cdo2\n", "cmo1_cmo2 = cmo1 << cmo2\n", "\n", "nmmo6_nmmo4 = ~(mmo4 << mmo6)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.916508Z", "start_time": "2024-07-18T21:01:01.853430Z" } } }, { "cell_type": "code", "execution_count": 7, "outputs": [], "source": [ "m_func_1 = lambda x: (x < mdo1) << (mdi1 < mdo2)\n", "\n", "m_func_2 = lambda x: (x < mmo1) << (mmi1 < mmo2)\n", "\n", "s_func_1 = lambda x: (x < sdo1) << (sdi1 < mdo3)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.916576Z", "start_time": "2024-07-18T21:01:01.856185Z" } } }, { "cell_type": "code", "execution_count": 8, "outputs": [], "source": [ "medium_phase1 = m_func_2(spma ^ sema)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.916634Z", "start_time": "2024-07-18T21:01:01.859848Z" } } }, { "cell_type": "code", "execution_count": 9, "outputs": [ { "data": { "text/plain": "True" }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "medium_phase2 = (\n", " (\n", " medium_phase1 ^\n", " m_func_2(cema ^ cpma ^ mpma)\n", " ) << (cmi1 < mmo3)\n", " ) < cmo1_cmo2\n", "medium_phase2 == (\n", " (\n", " (\n", " m_func_2(server_substitution_medium ^ sema ^ cema)\n", " ) << (cmi1 < mmo3)\n", " ) < cmo1_cmo2\n", ")" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.916838Z", "start_time": "2024-07-18T21:01:01.867649Z" } } }, { "cell_type": "code", "execution_count": 10, "outputs": [ { "data": { "text/plain": "True" }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "medium_phase3 = (\n", " ((medium_phase2 < ~cmo1_cmo2) << (~cmi1 < mmo3)) ^\n", " m_func_2(cpma ^ cema ^ mema)\n", " ) << (mpmi2 < mmo3) < cmo1_cmo2\n", "\n", "medium_phase3 == ((\n", " m_func_2(server_substitution_medium ^ sema ^ cpma ^ mema)\n", " ) << (mpmi2 < mmo3) < cmo1_cmo2\n", ")" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.916949Z", "start_time": "2024-07-18T21:01:01.876383Z" } } }, { "cell_type": "code", "execution_count": 11, "outputs": [], "source": [ "translated_alphabet = (\n", " s_func_1(alphabet) ^\n", " m_func_1(spda ^ seda ^ mpda ^ meda)\n", ") << (mpdi2 < mdo3)\n", "\n", "alphabet_phase1 = (\n", " s_func_1(alphabet) ^\n", " m_func_1(spda ^ seda)\n", ")" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.917063Z", "start_time": "2024-07-18T21:01:01.883731Z" } } }, { "cell_type": "code", "execution_count": 12, "outputs": [ { "data": { "text/plain": "True" }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphabet_phase2 = ((((\n", " alphabet_phase1 ^\n", " m_func_1(ceda ^ cpda ^ mpda)\n", " ) < ~mdo3)) << cdi1) < (mdo3 << cdo1 << cdo2)\n", "\n", "alphabet_phase2 == (\n", " ((((\n", " s_func_1(alphabet) ^\n", " m_func_1(server_substitution_data ^ seda ^ ceda)\n", " ) < ~mdo3)) << cdi1) < (mdo3 << cdo1 << cdo2)\n", ")" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.917163Z", "start_time": "2024-07-18T21:01:01.892274Z" } } }, { "cell_type": "code", "execution_count": 13, "outputs": [ { "data": { "text/plain": "True" }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphabet_phase3 = (\n", " ((alphabet_phase2 < ~cdo1_cdo2) << (~cdi1 < mdo3)) ^\n", " m_func_1(ceda ^ cpda ^ meda)\n", " ) << (mpdi2 < mdo3) < cdo1_cdo2\n", "\n", "alphabet_phase3 == (\n", " (\n", " s_func_1(alphabet) ^\n", " m_func_1(server_substitution_data ^ seda ^ cpda ^ meda)\n", " ) << (mpdi2 < mdo3) < cdo1_cdo2\n", ")" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.917267Z", "start_time": "2024-07-18T21:01:01.901400Z" } } }, { "cell_type": "code", "execution_count": 14, "outputs": [], "source": [ "eof_msg_mask = Mask.init_matrix(width, height, message_len)\n", "\n", "padded_input_sequence = OuterKey.init_matrix(height)\n", "padded_input_sequence.matrix = [message + padded_input_sequence.matrix[0][message_len:]]" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.917359Z", "start_time": "2024-07-18T21:01:01.916301Z" } } }, { "cell_type": "code", "execution_count": 15, "outputs": [ { "data": { "text/plain": "True" }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "darc_message = ((\n", " (medium_phase3 < ~cmo1_cmo2) ^\n", " ((((alphabet_phase3 < ~cdo1_cdo2) < padded_input_sequence) ^ eof_msg_mask) << (mmi3 < nmmo6_nmmo4))\n", " ) << (mmi4 < (mmo5 << nmmo6_nmmo4))) < mmo4\n", "\n", "darc_message == (\n", " ((\n", " ((\n", " m_func_2(server_substitution_medium ^ sema ^ cpma ^ mema)\n", " ) << (mpmi2 < mmo3)) ^\n", " (((((\n", " s_func_1(alphabet) ^\n", " m_func_1(server_substitution_data ^ seda ^ cpda ^ meda)) << (mpdi2 < mdo3)\n", " ) < padded_input_sequence) ^ eof_msg_mask) << (mmi3 < nmmo6_nmmo4))\n", " ) << (mmi4 < (mmo5 << nmmo6_nmmo4))) < mmo4\n", ")" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.939417Z", "start_time": "2024-07-18T21:01:01.916475Z" } } }, { "cell_type": "code", "execution_count": 16, "outputs": [ { "data": { "text/plain": "True" }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unwrapped_medium = (\n", " ((darc_message < ~mmo4) << ~(mmi4 < (mmo5 << nmmo6_nmmo4))) ^\n", " (m_func_2(spma ^ sema ^ mpma ^ mema) << (mpmi2 < mmo3))\n", " ) << (~mmi3 < nmmo6_nmmo4)\n", "\n", "unwrapped_medium == ((translated_alphabet < padded_input_sequence) ^ eof_msg_mask)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.939925Z", "start_time": "2024-07-18T21:01:01.928416Z" } } }, { "cell_type": "code", "execution_count": 17, "outputs": [], "source": [ "orignal_message = resolve_message(translated_alphabet, unwrapped_medium)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.940010Z", "start_time": "2024-07-18T21:01:01.931157Z" } } }, { "cell_type": "code", "execution_count": 18, "outputs": [ { "data": { "text/plain": "True" }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "message == orignal_message" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-07-18T21:01:01.940128Z", "start_time": "2024-07-18T21:01:01.933634Z" } } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }