Files
pydarc/darc_tutorial.ipynb
2024-07-04 14:33:40 -05:00

500 lines
15 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 121,
"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-04T19:31:39.644601Z",
"start_time": "2024-07-04T19:31:39.628805Z"
}
}
},
{
"cell_type": "code",
"execution_count": 122,
"outputs": [],
"source": [
"def create_random_message(message_len, height):\n",
" message = OuterKey.init_matrix(height).matrix[0]\n",
" return message[:message_len]"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T19:31:39.644729Z",
"start_time": "2024-07-04T19:31:39.630714Z"
}
}
},
{
"cell_type": "code",
"execution_count": 123,
"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-04T19:31:39.645110Z",
"start_time": "2024-07-04T19:31:39.634024Z"
}
}
},
{
"cell_type": "code",
"execution_count": 124,
"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-04T19:31:39.666246Z",
"start_time": "2024-07-04T19:31:39.638300Z"
}
}
},
{
"cell_type": "code",
"execution_count": 125,
"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-04T19:31:39.676976Z",
"start_time": "2024-07-04T19:31:39.672058Z"
}
}
},
{
"cell_type": "code",
"execution_count": 126,
"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-04T19:31:39.677097Z",
"start_time": "2024-07-04T19:31:39.674721Z"
}
}
},
{
"cell_type": "code",
"execution_count": 127,
"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-04T19:31:39.679186Z",
"start_time": "2024-07-04T19:31:39.677255Z"
}
}
},
{
"cell_type": "code",
"execution_count": 128,
"outputs": [],
"source": [
"medium_phase1 = m_func_2(spma ^ sema)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T19:31:39.682789Z",
"start_time": "2024-07-04T19:31:39.681048Z"
}
}
},
{
"cell_type": "code",
"execution_count": 129,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 129,
"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-04T19:31:39.697827Z",
"start_time": "2024-07-04T19:31:39.687941Z"
}
}
},
{
"cell_type": "code",
"execution_count": 130,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 130,
"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-04T19:31:39.712580Z",
"start_time": "2024-07-04T19:31:39.696392Z"
}
}
},
{
"cell_type": "code",
"execution_count": 131,
"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-04T19:31:39.712704Z",
"start_time": "2024-07-04T19:31:39.704054Z"
}
}
},
{
"cell_type": "code",
"execution_count": 132,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 132,
"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-04T19:31:39.727975Z",
"start_time": "2024-07-04T19:31:39.712372Z"
}
}
},
{
"cell_type": "code",
"execution_count": 133,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 133,
"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-04T19:31:39.742787Z",
"start_time": "2024-07-04T19:31:39.721754Z"
}
}
},
{
"cell_type": "code",
"execution_count": 134,
"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-04T19:31:39.742960Z",
"start_time": "2024-07-04T19:31:39.726783Z"
}
}
},
{
"cell_type": "code",
"execution_count": 135,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 135,
"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-04T19:31:39.743236Z",
"start_time": "2024-07-04T19:31:39.739866Z"
}
}
},
{
"cell_type": "code",
"execution_count": 136,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 136,
"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-04T19:31:39.761839Z",
"start_time": "2024-07-04T19:31:39.748041Z"
}
}
},
{
"cell_type": "code",
"execution_count": 137,
"outputs": [],
"source": [
"orignal_message = resolve_message(translated_alphabet, unwrapped_medium)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T19:31:39.761929Z",
"start_time": "2024-07-04T19:31:39.751011Z"
}
}
},
{
"cell_type": "code",
"execution_count": 138,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 138,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"message == orignal_message"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T19:31:39.761994Z",
"start_time": "2024-07-04T19:31:39.753450Z"
}
}
}
],
"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
}