Files
pydarc/darc_tutorial.ipynb

390 lines
12 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"outputs": [],
"source": [
"from src.key_generation import generate_keys\n",
"from src.models import SubstitutionKey, OuterKey, Mask\n",
"from src.server_darc import resolve_message"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.863976Z",
"start_time": "2024-07-03T15:44:47.828314Z"
}
}
},
{
"cell_type": "code",
"execution_count": 4,
"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-03T15:44:47.864152Z",
"start_time": "2024-07-03T15:44:47.831989Z"
}
}
},
{
"cell_type": "code",
"execution_count": 5,
"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-03T15:44:47.864191Z",
"start_time": "2024-07-03T15:44:47.834823Z"
}
}
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [],
"source": [
"server_keys, mutual_keys, client_keys = generate_keys(height, width)\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-03T15:44:47.870926Z",
"start_time": "2024-07-03T15:44:47.838679Z"
}
}
},
{
"cell_type": "code",
"execution_count": 7,
"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-03T15:44:47.913517Z",
"start_time": "2024-07-03T15:44:47.870246Z"
}
}
},
{
"cell_type": "code",
"execution_count": 8,
"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-03T15:44:47.913738Z",
"start_time": "2024-07-03T15:44:47.873170Z"
}
}
},
{
"cell_type": "code",
"execution_count": 9,
"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-03T15:44:47.913781Z",
"start_time": "2024-07-03T15:44:47.875550Z"
}
}
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [],
"source": [
"medium_phase1 = medium ^ m_func_2(spma ^ sema)\n",
"\n",
"medium_phase2 = (\n",
" (\n",
" medium_phase1 ^\n",
" m_func_2(cema ^ cpma ^ mpma)\n",
" ) << (cmi1 < mmo3)\n",
" ) < cmo1_cmo2\n",
"\n",
"medium_phase3 = (\n",
" ((medium_phase2 < ~cmo1_cmo2) << (~cmi1 < mmo3)) ^\n",
" m_func_2(cpma ^ cema ^ mema)\n",
" ) << (mpmi2 < mmo3) < cmo1_cmo2\n",
"\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.913820Z",
"start_time": "2024-07-03T15:44:47.884399Z"
}
}
},
{
"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",
")\n",
"\n",
"alphabet_phase2 = ((((\n",
" alphabet_phase1 ^\n",
" m_func_1(ceda ^ cpda ^ mpda)\n",
" ) < ~mdo3)) << cdi1) < (mdo3 << cdo1 << cdo2)\n",
"\n",
"alphabet_phase3 = (\n",
" ((alphabet_phase2 < ~cdo1_cdo2) << (~cdi1 < mdo3)) ^\n",
" m_func_1(ceda ^ cpda ^ meda)\n",
" ) << (mpdi2 < mdo3) < cdo1_cdo2\n",
"\n",
"\n",
"alphabet_phase3 = (\n",
" alphabet_phase1 ^ m_func_1(mpda ^ meda)\n",
") << (mpdi2 < mdo3) < cdo1_cdo2"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.917104Z",
"start_time": "2024-07-03T15:44:47.899159Z"
}
}
},
{
"cell_type": "code",
"execution_count": 12,
"outputs": [],
"source": [
"eof_msg_mask = Mask.init_matrix(width, height, message_len)\n",
"\n",
"\n",
"padded_input_sequence = OuterKey.init_matrix(height)\n",
"padded_input_sequence.matrix = [message + padded_input_sequence.matrix[0][message_len:]]\n",
"\n",
"darc_message = (\n",
" (\n",
" (medium_phase3 < ~cmo1_cmo2) ^\n",
" ((((alphabet_phase3 < ~cdo1_cdo2) < padded_input_sequence) ^ eof_msg_mask) << (mmi3 < nmmo6_nmmo4))\n",
" ) < mmo4\n",
" ) << (mmi4 < (mmo5 << ~mmo6))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.917166Z",
"start_time": "2024-07-03T15:44:47.902305Z"
}
}
},
{
"cell_type": "code",
"execution_count": 13,
"outputs": [],
"source": [
"unwrapped_medium = (\n",
" ((darc_message < ~mmo4) << ~(mmi4 < (mmo5 << nmmo6_nmmo4))) ^\n",
" (((spma ^ sema ^ mpma ^ mema) < mmo1) << ((mmi1 < mmo2) << (mpmi2 < mmo3)))\n",
" ) << (~mmi3 < nmmo6_nmmo4)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.917330Z",
"start_time": "2024-07-03T15:44:47.911658Z"
}
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [],
"source": [
"darc_message = ((\n",
" (\n",
" (medium_phase3 < ~cmo1_cmo2) ^\n",
" ((((alphabet_phase3 < ~cdo1_cdo2) < padded_input_sequence) ^ eof_msg_mask) << (mmi3 < nmmo6_nmmo4))\n",
" ) < mmo4\n",
" ) << (mmi4 < (mmo5 << ~mmo6))) < ~mmo4\n",
"\n",
"darc_message = ((\n",
" (\n",
" (medium_phase3 < ~cmo1_cmo2) ^\n",
" ((((alphabet_phase3 < ~cdo1_cdo2) < padded_input_sequence) ^ eof_msg_mask) << (mmi3 < nmmo6_nmmo4))\n",
" )\n",
" ) << (mmi4 < (mmo5 << ~mmo6 << ~mmo4)))\n",
"\n",
"unwrapped_medium = (\n",
" (darc_message << ~(mmi4 < (mmo5 << nmmo6_nmmo4))) ^\n",
" (((spma ^ sema ^ mpma ^ mema) < mmo1) << ((mmi1 < mmo2) << (mpmi2 < mmo3)))\n",
" ) << (~mmi3 < nmmo6_nmmo4)\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.929257Z",
"start_time": "2024-07-03T15:44:47.914223Z"
}
}
},
{
"cell_type": "code",
"execution_count": 15,
"outputs": [],
"source": [
"orignal_message = resolve_message(translated_alphabet, unwrapped_medium)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.929422Z",
"start_time": "2024-07-03T15:44:47.927326Z"
}
}
},
{
"cell_type": "code",
"execution_count": 16,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"message == orignal_message"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.945605Z",
"start_time": "2024-07-03T15:44:47.932864Z"
}
}
},
{
"cell_type": "code",
"execution_count": 16,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.945779Z",
"start_time": "2024-07-03T15:44:47.935609Z"
}
}
},
{
"cell_type": "code",
"execution_count": 16,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.965022Z",
"start_time": "2024-07-03T15:44:47.937832Z"
}
}
}
],
"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
}