Files
pydarc/darc_tutorial.ipynb
2024-07-04 14:25:31 -05:00

504 lines
15 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 36,
"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-04T18:05:07.552043Z",
"start_time": "2024-07-04T18:05:07.542908Z"
}
}
},
{
"cell_type": "code",
"execution_count": 37,
"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-04T18:05:07.552147Z",
"start_time": "2024-07-04T18:05:07.546164Z"
}
}
},
{
"cell_type": "code",
"execution_count": 38,
"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-04T18:05:07.552308Z",
"start_time": "2024-07-04T18:05:07.549140Z"
}
}
},
{
"cell_type": "code",
"execution_count": 39,
"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)\n",
"medium = SubstitutionKey(matrix=[[0 for _ in range(width)] for _ in range(height)])"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T18:05:07.580066Z",
"start_time": "2024-07-04T18:05:07.553448Z"
}
}
},
{
"cell_type": "code",
"execution_count": 40,
"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-04T18:05:07.593317Z",
"start_time": "2024-07-04T18:05:07.587709Z"
}
}
},
{
"cell_type": "code",
"execution_count": 41,
"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-04T18:05:07.593468Z",
"start_time": "2024-07-04T18:05:07.590552Z"
}
}
},
{
"cell_type": "code",
"execution_count": 42,
"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-04T18:05:07.595183Z",
"start_time": "2024-07-04T18:05:07.593423Z"
}
}
},
{
"cell_type": "code",
"execution_count": 43,
"outputs": [],
"source": [
"medium_phase1 = medium ^ m_func_2(spma ^ sema)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T18:05:07.598748Z",
"start_time": "2024-07-04T18:05:07.596986Z"
}
}
},
{
"cell_type": "code",
"execution_count": 52,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 52,
"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",
" medium ^\n",
" m_func_2(server_substitution_medium ^ sema ^ cema)\n",
" ) << (cmi1 < mmo3)\n",
" ) < cmo1_cmo2\n",
")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T18:05:42.393236Z",
"start_time": "2024-07-04T18:05:42.389787Z"
}
}
},
{
"cell_type": "code",
"execution_count": 59,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 59,
"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",
" medium ^\n",
" m_func_2(server_substitution_medium ^ sema ^ cpma ^ mema)\n",
" ) << (mpmi2 < mmo3) < cmo1_cmo2\n",
")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T18:16:20.923343Z",
"start_time": "2024-07-04T18:16:20.920280Z"
}
}
},
{
"cell_type": "code",
"execution_count": 46,
"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-04T18:05:07.665047Z",
"start_time": "2024-07-04T18:05:07.639926Z"
}
}
},
{
"cell_type": "code",
"execution_count": 62,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 62,
"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-04T18:29:06.076785Z",
"start_time": "2024-07-04T18:29:06.071174Z"
}
}
},
{
"cell_type": "code",
"execution_count": 72,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 72,
"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-04T18:40:21.743276Z",
"start_time": "2024-07-04T18:40:21.739864Z"
}
}
},
{
"cell_type": "code",
"execution_count": 47,
"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-04T18:05:07.678354Z",
"start_time": "2024-07-04T18:05:07.643198Z"
}
}
},
{
"cell_type": "code",
"execution_count": 75,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 75,
"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",
" medium ^\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-04T18:46:10.473478Z",
"start_time": "2024-07-04T18:46:10.457255Z"
}
}
},
{
"cell_type": "code",
"execution_count": 102,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 102,
"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:21:34.911369Z",
"start_time": "2024-07-04T19:21:34.908479Z"
}
}
},
{
"cell_type": "code",
"execution_count": 49,
"outputs": [],
"source": [
"orignal_message = resolve_message(translated_alphabet, unwrapped_medium)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T18:05:07.678472Z",
"start_time": "2024-07-04T18:05:07.656814Z"
}
}
},
{
"cell_type": "code",
"execution_count": 50,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"message == orignal_message"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T18:05:07.678731Z",
"start_time": "2024-07-04T18:05:07.659154Z"
}
}
}
],
"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
}