create darc tutorial

This commit is contained in:
2024-07-03 09:51:15 -05:00
parent a90db4b768
commit 18991b3516

382
darc_tutorial.ipynb Normal file
View File

@@ -0,0 +1,382 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 718,
"outputs": [],
"source": [
"from src.key_generation import generate_keys\n",
"from src.models import AlphabetKey, OuterKey, Mask\n",
"from src.server_darc import resolve_message"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T14:42:19.329136Z",
"start_time": "2024-07-03T14:42:19.325881Z"
}
}
},
{
"cell_type": "code",
"execution_count": 705,
"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-03T00:47:13.503333Z",
"start_time": "2024-07-03T00:47:13.499467Z"
}
}
},
{
"cell_type": "code",
"execution_count": 706,
"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-03T00:47:13.505703Z",
"start_time": "2024-07-03T00:47:13.503057Z"
}
}
},
{
"cell_type": "code",
"execution_count": 707,
"outputs": [],
"source": [
"server_keys, mutual_keys, client_keys = generate_keys(height, width)\n",
"alphabet = AlphabetKey.init_matrix(width, height, 255)\n",
"medium = AlphabetKey(matrix=[[0 for _ in range(width)] for _ in range(height)])"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T00:47:13.536496Z",
"start_time": "2024-07-03T00:47:13.506896Z"
}
}
},
{
"cell_type": "code",
"execution_count": 708,
"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-03T00:47:13.541138Z",
"start_time": "2024-07-03T00:47:13.539201Z"
}
}
},
{
"cell_type": "code",
"execution_count": 709,
"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-03T00:47:13.553403Z",
"start_time": "2024-07-03T00:47:13.541940Z"
}
}
},
{
"cell_type": "code",
"execution_count": 710,
"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-03T00:47:13.553530Z",
"start_time": "2024-07-03T00:47:13.544750Z"
}
}
},
{
"cell_type": "code",
"execution_count": 711,
"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-03T00:47:13.569014Z",
"start_time": "2024-07-03T00:47:13.553586Z"
}
}
},
{
"cell_type": "code",
"execution_count": 712,
"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-03T00:47:13.574463Z",
"start_time": "2024-07-03T00:47:13.568318Z"
}
}
},
{
"cell_type": "code",
"execution_count": 713,
"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-03T00:47:13.604020Z",
"start_time": "2024-07-03T00:47:13.571898Z"
}
}
},
{
"cell_type": "code",
"execution_count": 714,
"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-03T00:47:13.604168Z",
"start_time": "2024-07-03T00:47:13.581481Z"
}
}
},
{
"cell_type": "code",
"execution_count": 715,
"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",
"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-03T00:47:13.604250Z",
"start_time": "2024-07-03T00:47:13.589989Z"
}
}
},
{
"cell_type": "code",
"execution_count": 716,
"outputs": [],
"source": [
"orignal_message = resolve_message(translated_alphabet, unwrapped_medium)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T00:47:13.608055Z",
"start_time": "2024-07-03T00:47:13.593458Z"
}
}
},
{
"cell_type": "code",
"execution_count": 717,
"outputs": [
{
"data": {
"text/plain": "False"
},
"execution_count": 717,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"message == orignal_message"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T00:47:13.608198Z",
"start_time": "2024-07-03T00:47:13.597454Z"
}
}
},
{
"cell_type": "code",
"execution_count": 717,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T00:47:13.608236Z",
"start_time": "2024-07-03T00:47:13.600039Z"
}
}
},
{
"cell_type": "code",
"execution_count": 717,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T00:47:13.608276Z",
"start_time": "2024-07-03T00:47:13.601932Z"
}
}
}
],
"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
}