complete darc tutorial

This commit is contained in:
2024-07-04 14:25:31 -05:00
parent 62fbf38f55
commit 8c0f4362ec
2 changed files with 252 additions and 133 deletions

View File

@@ -2,24 +2,23 @@
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 36,
"outputs": [],
"source": [
"from src.key_generation import generate_keys\n",
"from src.models import SubstitutionKey, OuterKey, Mask\n",
"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-03T15:44:47.863976Z",
"start_time": "2024-07-03T15:44:47.828314Z"
"end_time": "2024-07-04T18:05:07.552043Z",
"start_time": "2024-07-04T18:05:07.542908Z"
}
}
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 37,
"outputs": [],
"source": [
"def create_random_message(message_len, height):\n",
@@ -29,14 +28,14 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.864152Z",
"start_time": "2024-07-03T15:44:47.831989Z"
"end_time": "2024-07-04T18:05:07.552147Z",
"start_time": "2024-07-04T18:05:07.546164Z"
}
}
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 38,
"outputs": [],
"source": [
"height = 70\n",
@@ -47,31 +46,51 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.864191Z",
"start_time": "2024-07-03T15:44:47.834823Z"
"end_time": "2024-07-04T18:05:07.552308Z",
"start_time": "2024-07-04T18:05:07.549140Z"
}
}
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 39,
"outputs": [],
"source": [
"server_keys, mutual_keys, client_keys = generate_keys(height, width)\n",
"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-03T15:44:47.870926Z",
"start_time": "2024-07-03T15:44:47.838679Z"
"end_time": "2024-07-04T18:05:07.580066Z",
"start_time": "2024-07-04T18:05:07.553448Z"
}
}
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 40,
"outputs": [],
"source": [
"sdi1 = server_keys.persistent.data.inner_key_1 << server_keys.ephemeral.data.inner_key_1\n",
@@ -115,14 +134,14 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.913517Z",
"start_time": "2024-07-03T15:44:47.870246Z"
"end_time": "2024-07-04T18:05:07.593317Z",
"start_time": "2024-07-04T18:05:07.587709Z"
}
}
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 41,
"outputs": [],
"source": [
"cdo1_cdo2 = cdo1 << cdo2\n",
@@ -133,14 +152,14 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.913738Z",
"start_time": "2024-07-03T15:44:47.873170Z"
"end_time": "2024-07-04T18:05:07.593468Z",
"start_time": "2024-07-04T18:05:07.590552Z"
}
}
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 42,
"outputs": [],
"source": [
"m_func_1 = lambda x: (x < mdo1) << (mdi1 < mdo2)\n",
@@ -152,42 +171,99 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.913781Z",
"start_time": "2024-07-03T15:44:47.875550Z"
"end_time": "2024-07-04T18:05:07.595183Z",
"start_time": "2024-07-04T18:05:07.593423Z"
}
}
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 43,
"outputs": [],
"source": [
"medium_phase1 = medium ^ m_func_2(spma ^ sema)\n",
"\n",
"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",
"\n",
"medium_phase3 = (\n",
" ((medium_phase2 < ~cmo1_cmo2) << (~cmi1 < mmo3)) ^\n",
" m_func_2(cpma ^ cema ^ mema)\n",
" ) << (mpmi2 < mmo3) < cmo1_cmo2\n",
"\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-03T15:44:47.913820Z",
"start_time": "2024-07-03T15:44:47.884399Z"
"end_time": "2024-07-04T18:05:42.393236Z",
"start_time": "2024-07-04T18:05:42.389787Z"
}
}
},
{
"cell_type": "code",
"execution_count": 11,
"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",
@@ -198,110 +274,174 @@
"alphabet_phase1 = (\n",
" s_func_1(alphabet) ^\n",
" m_func_1(spda ^ seda)\n",
")\n",
"\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",
"\n",
"alphabet_phase3 = (\n",
" alphabet_phase1 ^ m_func_1(mpda ^ meda)\n",
") << (mpdi2 < mdo3) < cdo1_cdo2"
"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-03T15:44:47.917104Z",
"start_time": "2024-07-03T15:44:47.899159Z"
"end_time": "2024-07-04T18:40:21.743276Z",
"start_time": "2024-07-04T18:40:21.739864Z"
}
}
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 47,
"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))"
"padded_input_sequence.matrix = [message + padded_input_sequence.matrix[0][message_len:]]"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.917166Z",
"start_time": "2024-07-03T15:44:47.902305Z"
"end_time": "2024-07-04T18:05:07.678354Z",
"start_time": "2024-07-04T18:05:07.643198Z"
}
}
},
{
"cell_type": "code",
"execution_count": 13,
"outputs": [],
"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",
" (((spma ^ sema ^ mpma ^ mema) < mmo1) << ((mmi1 < mmo2) << (mpmi2 < mmo3)))\n",
" ) << (~mmi3 < nmmo6_nmmo4)"
" (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-03T15:44:47.917330Z",
"start_time": "2024-07-03T15:44:47.911658Z"
"end_time": "2024-07-04T19:21:34.911369Z",
"start_time": "2024-07-04T19:21:34.908479Z"
}
}
},
{
"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,
"execution_count": 49,
"outputs": [],
"source": [
"orignal_message = resolve_message(translated_alphabet, unwrapped_medium)"
@@ -309,20 +449,20 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T15:44:47.929422Z",
"start_time": "2024-07-03T15:44:47.927326Z"
"end_time": "2024-07-04T18:05:07.678472Z",
"start_time": "2024-07-04T18:05:07.656814Z"
}
}
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 50,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 16,
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
@@ -333,34 +473,8 @@
"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"
"end_time": "2024-07-04T18:05:07.678731Z",
"start_time": "2024-07-04T18:05:07.659154Z"
}
}
}

View File

@@ -202,10 +202,15 @@ def merge_message(
cmo1 = client_keys.persistent.medium.outer_key_1 << client_keys.ephemeral.medium.outer_key_1
cmo2 = client_keys.persistent.medium.outer_key_2 << client_keys.ephemeral.medium.outer_key_2
darc_message = (
cmo1_cmo2 = cmo1 << cmo2
cdo1_cdo2 = cdo1 << cdo2
nmmo6_nmmo4 = ~(mmo4 << mmo6)
darc_message = ((
(
(medium_phase3 < ~(cmo1 << cmo2)) ^
(((alphabet_phase3 < (~(cdo1 << cdo2) << padded_input_sequence)) ^ eof_msg_mask) << (mmi3 < ~(mmo4 << mmo6)))
) < mmo4
) << (mmi4 < (mmo5 << ~mmo6))
(medium_phase3 < ~cmo1_cmo2) ^
((((alphabet_phase3 < ~cdo1_cdo2) < padded_input_sequence) ^ eof_msg_mask) << (
mmi3 < nmmo6_nmmo4))
)
) << (mmi4 < (mmo5 << nmmo6_nmmo4))) < mmo4
return darc_message