From 8527caced0a2e44f1d94086672af6794f26f7634 Mon Sep 17 00:00:00 2001 From: Donovan Date: Wed, 3 Jul 2024 11:44:26 -0500 Subject: [PATCH] Refactor AlphabetKet to SubstitutionKey --- permutation_algebra_examples.ipynb | 284 ----------------- sp_network_algebra_tutorial.ipynb | 485 +++++++++++++++++++++++++++++ src/client_darc.py | 18 +- src/client_darc_noclient.py | 6 +- src/models.py | 64 ++-- src/server_darc.py | 16 +- test/get_hard_coded_json.py | 4 +- test/test_darc.py | 10 +- 8 files changed, 544 insertions(+), 343 deletions(-) delete mode 100644 permutation_algebra_examples.ipynb create mode 100644 sp_network_algebra_tutorial.ipynb diff --git a/permutation_algebra_examples.ipynb b/permutation_algebra_examples.ipynb deleted file mode 100644 index 8376ee4..0000000 --- a/permutation_algebra_examples.ipynb +++ /dev/null @@ -1,284 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true, - "ExecuteTime": { - "end_time": "2024-07-03T14:43:00.143871Z", - "start_time": "2024-07-03T14:43:00.105294Z" - } - }, - "outputs": [], - "source": [ - "from src.models import OuterKey, InnerKey, AlphabetKey" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "outputs": [], - "source": [ - "height = 10\n", - "width = 7\n", - "\n", - "a0 = AlphabetKey.init_matrix(width, height, 255)\n", - "\n", - "o0 = OuterKey.init_matrix(height)\n", - "o1 = OuterKey.init_matrix(height)\n", - "o2 = OuterKey.init_matrix(height)\n", - "o3 = OuterKey.init_matrix(height)\n", - "\n", - "i0 = InnerKey.init_matrix(width, height)\n", - "i1 = InnerKey.init_matrix(width, height)\n", - "i2 = InnerKey.init_matrix(width, height)\n" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-07-03T14:43:11.536945Z", - "start_time": "2024-07-03T14:43:11.532046Z" - } - } - }, - { - "cell_type": "code", - "execution_count": 3, - "outputs": [ - { - "data": { - "text/plain": "True" - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a0_i0_o0_v0 = a0 << (i0 < o0)\n", - "a0_i0_o0_v1 = ((a0 < ~o0) << i0) < o0\n", - "\n", - "a0_i0_o0_v0 == a0_i0_o0_v1" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-07-03T14:43:36.810538Z", - "start_time": "2024-07-03T14:43:36.805348Z" - } - } - }, - { - "cell_type": "code", - "execution_count": 4, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] - } - ], - "source": [ - "a0_o0_i0_v0 = (a0 < o0) << i0\n", - "a0_o0_i0_v1 = (a0 << (i0 < ~o0)) < o0\n", - "a0_i0_o0_v0 == a0_i0_o0_v1" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-07-03T14:44:07.321778Z", - "start_time": "2024-07-03T14:44:07.318402Z" - } - } - }, - { - "cell_type": "code", - "execution_count": 5, - "outputs": [ - { - "data": { - "text/plain": "True" - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a0_o0_i0_v0 = (a0 < o0) << i0\n", - "a0_o0_i0_v1 = (a0 << (i0 < ~o0)) < o0\n", - "a0_o0_i0_v0 == a0_o0_i0_v1" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-07-03T14:44:48.380940Z", - "start_time": "2024-07-03T14:44:48.377032Z" - } - } - }, - { - "cell_type": "code", - "execution_count": 6, - "outputs": [ - { - "data": { - "text/plain": "True" - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "n_i0_i1 = ~(i0 << i1)\n", - "ni1_ni0 = ~i1 << ~i0\n", - "ni1_ni0 == n_i0_i1" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-07-03T14:45:01.588349Z", - "start_time": "2024-07-03T14:45:01.584641Z" - } - } - }, - { - "cell_type": "code", - "execution_count": 7, - "outputs": [ - { - "data": { - "text/plain": "True" - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "i0_i1_i2_v0 = i0 << i1 << i2\n", - "i0_i1_i2_v1 = i0 << (i1 << i2)\n", - "i0_i1_i2_v2 = (i0 << i1) << i2\n", - "i0_i1_i2_v0 == i0_i1_i2_v1 == i0_i1_i2_v2" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-07-03T14:45:41.860153Z", - "start_time": "2024-07-03T14:45:41.855897Z" - } - } - }, - { - "cell_type": "code", - "execution_count": 8, - "outputs": [ - { - "data": { - "text/plain": "True" - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "i0_i2_i1 = i0 << i2 << i1\n", - "i0 == i0_i2_i1 << ~(i2 << i1) == i0_i2_i1 << ~i1 << ~i2" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-07-03T14:47:02.948800Z", - "start_time": "2024-07-03T14:47:02.944314Z" - } - } - }, - { - "cell_type": "code", - "execution_count": 11, - "outputs": [ - { - "data": { - "text/plain": "True" - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "i0_o0_o1_o2_v1 = ((i0 < o0) < o1) < o2\n", - "i0_o0_o1_o2_v2 = (i0 < (o0 << o1)) < o2\n", - "i0_o0_o1_o2_v3 = ((i0 < o0) < (o1 << o2))\n", - "\n", - "i0_o0_o1_o2_v1 == i0_o0_o1_o2_v2 == i0_o0_o1_o2_v3" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-07-03T14:48:17.489611Z", - "start_time": "2024-07-03T14:48:17.485650Z" - } - } - }, - { - "cell_type": "code", - "execution_count": 12, - "outputs": [ - { - "data": { - "text/plain": "True" - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "i0 < (o0 << o1 << o2) != i0_o0_o1_o2_v1 # not equal" - ], - "metadata": { - "collapsed": false, - "ExecuteTime": { - "end_time": "2024-07-03T14:48:18.120561Z", - "start_time": "2024-07-03T14:48:18.116436Z" - } - } - }, - { - "cell_type": "code", - "execution_count": null, - "outputs": [], - "source": [], - "metadata": { - "collapsed": false - } - } - ], - "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 -} diff --git a/sp_network_algebra_tutorial.ipynb b/sp_network_algebra_tutorial.ipynb new file mode 100644 index 0000000..1e4887d --- /dev/null +++ b/sp_network_algebra_tutorial.ipynb @@ -0,0 +1,485 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "source": [ + "# Permutation Algebra\n", + "This notebook is a prerequisite to following the DARC tutorial" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 35, + "outputs": [], + "source": [ + "from src.models import OuterKey, InnerKey, SubstitutionKey" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.906547Z", + "start_time": "2024-07-03T16:38:57.867931Z" + } + } + }, + { + "cell_type": "markdown", + "source": [ + "## Key Generation Parameters\n", + "- block size: The number of characters we encode in a block. Block size isn't using in this notebook\n", + "- key height: The alphabet length. If we want to encode bytes, our alphabet length is 256.\n", + " If we want to encode lowercase letters a-z our alphabet length is 26. NKodes 10 key 7 attribute alphabet is 70.\n", + "- key width: The number of bytes an encrypted charter is in our alphabet.\n" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 36, + "outputs": [], + "source": [ + "height = 4\n", + "width = 3" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.907029Z", + "start_time": "2024-07-03T16:38:57.872544Z" + } + } + }, + { + "cell_type": "markdown", + "source": [ + "## Operand Types\n", + "### Inner Key\n", + "An inner key is a list of `height` rows, each a random permutation of an identity arrary of length `width`" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 37, + "outputs": [ + { + "data": { + "text/plain": "[[2, 0, 1], [0, 2, 1], [0, 2, 1], [1, 2, 0]]" + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "i0 = InnerKey.init_matrix(width, height)\n", + "i1 = InnerKey.init_matrix(width, height)\n", + "i2 = InnerKey.init_matrix(width, height)\n", + "\n", + "i0.matrix" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.907507Z", + "start_time": "2024-07-03T16:38:57.876023Z" + } + } + }, + { + "cell_type": "markdown", + "source": [ + "### Outer Key\n", + "An outer key is ..." + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 38, + "outputs": [ + { + "data": { + "text/plain": "[[3, 2, 1, 0]]" + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "o0 = OuterKey.init_matrix(height)\n", + "o1 = OuterKey.init_matrix(height)\n", + "o2 = OuterKey.init_matrix(height)\n", + "\n", + "o0.matrix" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.954081Z", + "start_time": "2024-07-03T16:38:57.880359Z" + } + } + }, + { + "cell_type": "markdown", + "source": [ + "### Substitution Key (formerly AlphabetKey or alpha_key)\n", + "Sub key is ..." + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 39, + "outputs": [ + { + "data": { + "text/plain": "[[21, 62, 42], [151, 171, 33], [31, 131, 141], [100, 202, 161]]" + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a0 = SubstitutionKey.init_matrix(width, height)\n", + "a1 = SubstitutionKey.init_matrix(width, height)\n", + "a0.matrix" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.954770Z", + "start_time": "2024-07-03T16:38:57.884556Z" + } + } + }, + { + "cell_type": "markdown", + "source": [ + "## Operators Types\n", + "### ***<<*** Inner Permutation" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 39, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.955162Z", + "start_time": "2024-07-03T16:38:57.887640Z" + } + } + }, + { + "cell_type": "markdown", + "source": [ + "### ***<*** Outer Permutation" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 39, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.955526Z", + "start_time": "2024-07-03T16:38:57.890158Z" + } + } + }, + { + "cell_type": "markdown", + "source": [ + "### ***^*** Substitution" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 39, + "outputs": [], + "source": [], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.957721Z", + "start_time": "2024-07-03T16:38:57.892476Z" + } + } + }, + { + "cell_type": "markdown", + "source": [ + "## dddd" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 40, + "outputs": [ + { + "data": { + "text/plain": "True" + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a0_i0_o0_v0 = a0 << (i0 < o0)\n", + "a0_i0_o0_v1 = ((a0 < ~o0) << i0) < o0\n", + "\n", + "a0_i0_o0_v0 == a0_i0_o0_v1" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.957791Z", + "start_time": "2024-07-03T16:38:57.896696Z" + } + } + }, + { + "cell_type": "code", + "execution_count": 41, + "outputs": [ + { + "data": { + "text/plain": "True" + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a0_o0_i0_v0 = (a0 < o0) << i0\n", + "a0_o0_i0_v1 = (a0 << (i0 < ~o0)) < o0\n", + "a0_i0_o0_v0 == a0_i0_o0_v1" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.958249Z", + "start_time": "2024-07-03T16:38:57.900751Z" + } + } + }, + { + "cell_type": "code", + "execution_count": 42, + "outputs": [ + { + "data": { + "text/plain": "True" + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a0_o0_i0_v0 = (a0 < o0) << i0\n", + "a0_o0_i0_v1 = (a0 << (i0 < ~o0)) < o0\n", + "a0_o0_i0_v0 == a0_o0_i0_v1" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.958404Z", + "start_time": "2024-07-03T16:38:57.903708Z" + } + } + }, + { + "cell_type": "code", + "execution_count": 50, + "outputs": [ + { + "data": { + "text/plain": "True" + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(~(i0 << i1)) == (~i1 << ~i0)" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:42:45.756613Z", + "start_time": "2024-07-03T16:42:45.746766Z" + } + } + }, + { + "cell_type": "code", + "execution_count": 44, + "outputs": [ + { + "data": { + "text/plain": "True" + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "i0_i1_i2_v0 = i0 << i1 << i2\n", + "i0_i1_i2_v1 = i0 << (i1 << i2)\n", + "i0_i1_i2_v2 = (i0 << i1) << i2\n", + "i0_i1_i2_v0 == i0_i1_i2_v1 == i0_i1_i2_v2" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.958613Z", + "start_time": "2024-07-03T16:38:57.910939Z" + } + } + }, + { + "cell_type": "code", + "execution_count": 49, + "outputs": [ + { + "data": { + "text/plain": "True" + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "i0 == ((i0 << i2 << i1) << ~(i2 << i1)) == ((i0 << i2 << i1) << ~i1 << ~i2)" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:41:01.725294Z", + "start_time": "2024-07-03T16:41:01.718345Z" + } + } + }, + { + "cell_type": "code", + "execution_count": 46, + "outputs": [ + { + "data": { + "text/plain": "True" + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "i0_o0_o1_o2_v1 = ((i0 < o0) < o1) < o2\n", + "i0_o0_o1_o2_v2 = (i0 < (o0 << o1)) < o2\n", + "i0_o0_o1_o2_v3 = (i0 < o0) < (o1 << o2)\n", + "i0_o0_o1_o2_v4 = (i0 < (o0 << o1 << o2))\n", + "\n", + "i0_o0_o1_o2_v1 == i0_o0_o1_o2_v2 == i0_o0_o1_o2_v3 == i0_o0_o1_o2_v4" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.958859Z", + "start_time": "2024-07-03T16:38:57.915888Z" + } + } + }, + { + "cell_type": "markdown", + "source": [ + "### Becareful about your order of operation\n", + "***Always use parenthesis to control the order of operations***" + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 47, + "outputs": [ + { + "data": { + "text/plain": "True" + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "i0 < (o0 << o1 << o2) != (i0 < (o0 << o1 << o2)) # not equal !!!!!!" + ], + "metadata": { + "collapsed": false, + "ExecuteTime": { + "end_time": "2024-07-03T16:38:57.958925Z", + "start_time": "2024-07-03T16:38:57.920831Z" + } + } + } + ], + "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 +} diff --git a/src/client_darc.py b/src/client_darc.py index 7959ff6..d8a9b6d 100644 --- a/src/client_darc.py +++ b/src/client_darc.py @@ -1,13 +1,13 @@ from src.models import ( - AlphabetKey, ClientKeys, MutualKeys, ClientPersistentDataKeys, ClientEphemeralDataKeys, + SubstitutionKey, ClientKeys, MutualKeys, ClientPersistentDataKeys, ClientEphemeralDataKeys, MutualPersistentDataKeys, MutualEphemeralDataKeys, ClientPersistentMediumKeys, ClientEphemeralMediumKeys, MutualPersistentMediumKeys, MutualEphemeralMediumKeys, DarcKey, OuterKey, Mask ) def darc_phase2( - alphabet_phase1: AlphabetKey, - medium_phase1: AlphabetKey, + alphabet_phase1: SubstitutionKey, + medium_phase1: SubstitutionKey, client_keys: ClientKeys, mutual_keys: MutualKeys ): @@ -29,7 +29,7 @@ def darc_phase2( def receive_alphabet( - alphabet_phase1: AlphabetKey, + alphabet_phase1: SubstitutionKey, client_persistent_data_keys: ClientPersistentDataKeys, client_ephemeral_data_keys: ClientEphemeralDataKeys, mutual_persistent_data_keys: MutualPersistentDataKeys, @@ -57,7 +57,7 @@ def receive_alphabet( def receive_medium( - medium_phase1: AlphabetKey, + medium_phase1: SubstitutionKey, client_persistent_medium_keys: ClientPersistentMediumKeys, client_ephemeral_medium_keys: ClientEphemeralMediumKeys, mutual_persistent_medium_keys: MutualPersistentMediumKeys, @@ -152,8 +152,8 @@ def transmit_medium( def darc_phase3( - alphabet_phase2: AlphabetKey, - medium_phase2: AlphabetKey, + alphabet_phase2: SubstitutionKey, + medium_phase2: SubstitutionKey, client_keys: ClientKeys, mutual_keys: MutualKeys ): @@ -175,8 +175,8 @@ def darc_phase3( def merge_message( - alphabet_phase3: AlphabetKey, - medium_phase3: AlphabetKey, + alphabet_phase3: SubstitutionKey, + medium_phase3: SubstitutionKey, client_keys: ClientKeys, mutual_keys: MutualKeys, message: list[int] diff --git a/src/client_darc_noclient.py b/src/client_darc_noclient.py index 2873cf6..ed38e1a 100644 --- a/src/client_darc_noclient.py +++ b/src/client_darc_noclient.py @@ -1,4 +1,4 @@ -from src.models import DarcKey, MutualKeys, OuterKey, AlphabetKey, \ +from src.models import DarcKey, MutualKeys, OuterKey, SubstitutionKey, \ MutualPersistentDataKeys, MutualEphemeralDataKeys @@ -16,7 +16,7 @@ def merge_message_no_client(alphabet: DarcKey, medium: DarcKey, mutual_keys: Mut input_seq = padded_input_sequence a_i = alphabet < input_seq - eof_arr = AlphabetKey.init_matrix(width, height, 255).matrix[0] + eof_arr = SubstitutionKey.init_matrix(width, height, 255).matrix[0] if msg_len < height: a_i.matrix[msg_len] = eof_arr @@ -36,7 +36,7 @@ def merge_message_no_client(alphabet: DarcKey, medium: DarcKey, mutual_keys: Mut return message -def darc_phase_2_3(alphabet_phase1: AlphabetKey, medium_phase1: AlphabetKey, mutual_keys: MutualKeys) -> AlphabetKey: +def darc_phase_2_3(alphabet_phase1: SubstitutionKey, medium_phase1: SubstitutionKey, mutual_keys: MutualKeys) -> SubstitutionKey: phase3_alphabet = transceive_alphabet(alphabet_phase1, mutual_keys.persistent.data, mutual_keys.ephemeral.data) phase3_medium = transceive_medium(medium_phase1, mutual_keys.persistent.medium, mutual_keys.ephemeral.medium) return (phase3_alphabet, phase3_medium) diff --git a/src/models.py b/src/models.py index 371c6b4..7192274 100644 --- a/src/models.py +++ b/src/models.py @@ -6,7 +6,7 @@ from pydantic import BaseModel class DarcKeyType(str, Enum): outer_shuffle = "outer_shuffle" inner_shuffle = "inner_shuffle" - alphabet = "alphabet" + substitution = "substitution" other = "other" @@ -61,8 +61,8 @@ class DarcKey(BaseModel): def __xor__(self, other): assert len(self.matrix) == len(other.matrix) assert len(self.matrix[0]) == len(other.matrix[0]) - assert self.key_type == DarcKeyType.alphabet - assert other.key_type == DarcKeyType.alphabet + assert self.key_type == DarcKeyType.substitution + assert other.key_type == DarcKeyType.substitution output = [] for i, el in enumerate(self.matrix): row = [] @@ -129,21 +129,21 @@ class InnerKey(DarcKey): ) -class AlphabetKey(DarcKey): - key_type: DarcKeyType = DarcKeyType.alphabet +class SubstitutionKey(DarcKey): + key_type: DarcKeyType = DarcKeyType.substitution @classmethod - def init_matrix(cls, width: int, height: int, max_value: int): + def init_matrix(cls, width: int, height: int, max_value: int = 255): """ originally buildScramble """ matrix = [cls._random_mask(width, max_value) for _ in range(height)] - return AlphabetKey( + return SubstitutionKey( matrix=matrix, max_value=max_value ) class Mask(DarcKey): - key_type: DarcKeyType = DarcKeyType.alphabet + key_type: DarcKeyType = DarcKeyType.substitution @classmethod def init_matrix(cls, width: int, height: int, col_mask: int): @@ -162,7 +162,7 @@ class Mask(DarcKey): class ClientEphemeralDataKeys(BaseModel): outer_key_1: OuterKey outer_key_2: OuterKey - alpha_key: AlphabetKey + alpha_key: SubstitutionKey inner_key_1: InnerKey @classmethod @@ -170,7 +170,7 @@ class ClientEphemeralDataKeys(BaseModel): return ClientEphemeralDataKeys( outer_key_1=OuterKey.init_matrix(height), outer_key_2=OuterKey.init_matrix(height), - alpha_key=AlphabetKey.init_matrix(width, height, 255), + alpha_key=SubstitutionKey.init_matrix(width, height, 255), inner_key_1=InnerKey.init_matrix(width, height), ) @@ -179,7 +179,7 @@ class ClientEphemeralMediumKeys(BaseModel): outer_key_1: OuterKey outer_key_2: OuterKey inner_key_1: InnerKey - alpha_key: AlphabetKey + alpha_key: SubstitutionKey @classmethod def random_init(cls, height: int, width: int): @@ -187,12 +187,12 @@ class ClientEphemeralMediumKeys(BaseModel): outer_key_1=OuterKey.init_matrix(height), outer_key_2=OuterKey.init_matrix(height), inner_key_1=InnerKey.init_matrix(width, height), - alpha_key=AlphabetKey.init_matrix(width, height, 255) + alpha_key=SubstitutionKey.init_matrix(width, height, 255) ) class ClientPersistentDataKeys(BaseModel): - alpha_key: AlphabetKey + alpha_key: SubstitutionKey inner_key_1: InnerKey outer_key_1: OuterKey outer_key_2: OuterKey @@ -200,7 +200,7 @@ class ClientPersistentDataKeys(BaseModel): @classmethod def random_init(cls, height: int, width: int): return ClientPersistentDataKeys( - alpha_key=AlphabetKey.init_matrix(width, height, 255), + alpha_key=SubstitutionKey.init_matrix(width, height, 255), inner_key_1=InnerKey.init_matrix(width, height), outer_key_1=OuterKey.init_matrix(height), outer_key_2=OuterKey.init_matrix(height), @@ -211,7 +211,7 @@ class ClientPersistentMediumKeys(BaseModel): outer_key_1: OuterKey outer_key_2: OuterKey inner_key_1: InnerKey - alpha_key: AlphabetKey + alpha_key: SubstitutionKey @classmethod def random_init(cls, height: int, width: int): @@ -219,60 +219,60 @@ class ClientPersistentMediumKeys(BaseModel): outer_key_1=OuterKey.init_matrix(height), outer_key_2=OuterKey.init_matrix(height), inner_key_1=InnerKey.init_matrix(width, height), - alpha_key=AlphabetKey.init_matrix(width, height, 255) + alpha_key=SubstitutionKey.init_matrix(width, height, 255) ) class ServerEphemeralDataKeys(BaseModel): outer_key_1: OuterKey - alpha_key: AlphabetKey + alpha_key: SubstitutionKey inner_key_1: InnerKey @classmethod def random_init(cls, height: int, width: int): return ServerEphemeralDataKeys( outer_key_1=OuterKey.init_matrix(height), - alpha_key=AlphabetKey.init_matrix(width, height, 255), + alpha_key=SubstitutionKey.init_matrix(width, height, 255), inner_key_1=InnerKey.init_matrix(width, height), ) class ServerEphemeralMediumKeys(BaseModel): - alpha_key: AlphabetKey + alpha_key: SubstitutionKey @classmethod def random_init(cls, height: int, width: int): return ServerEphemeralMediumKeys( - alpha_key=AlphabetKey.init_matrix(width, height, 255) + alpha_key=SubstitutionKey.init_matrix(width, height, 255) ) class ServerPersistentDataKeys(BaseModel): outer_key_1: OuterKey - alpha_key: AlphabetKey + alpha_key: SubstitutionKey inner_key_1: InnerKey @classmethod def random_init(cls, height: int, width: int): return ServerPersistentDataKeys( outer_key_1=OuterKey.init_matrix(height), - alpha_key=AlphabetKey.init_matrix(width, height, 255), + alpha_key=SubstitutionKey.init_matrix(width, height, 255), inner_key_1=InnerKey.init_matrix(width, height), ) class ServerPersistentMediumKeys(BaseModel): - alpha_key: AlphabetKey + alpha_key: SubstitutionKey @classmethod def random_init(cls, height: int, width: int): return ServerPersistentMediumKeys( - alpha_key=AlphabetKey.init_matrix(width, height, 255) + alpha_key=SubstitutionKey.init_matrix(width, height, 255) ) class MutualEphemeralDataKeys(BaseModel): - alpha_key: AlphabetKey + alpha_key: SubstitutionKey inner_key_1: InnerKey outer_key_1: OuterKey outer_key_2: OuterKey @@ -281,7 +281,7 @@ class MutualEphemeralDataKeys(BaseModel): @classmethod def random_init(cls, height: int, width: int): return MutualEphemeralDataKeys( - alpha_key=AlphabetKey.init_matrix(width, height, 255), + alpha_key=SubstitutionKey.init_matrix(width, height, 255), inner_key_1=InnerKey.init_matrix(width, height), outer_key_1=OuterKey.init_matrix(height), outer_key_2=OuterKey.init_matrix(height), @@ -290,7 +290,7 @@ class MutualEphemeralDataKeys(BaseModel): class MutualEphemeralMediumKeys(BaseModel): - alpha_key: AlphabetKey + alpha_key: SubstitutionKey outer_key_1: OuterKey outer_key_2: OuterKey @@ -306,7 +306,7 @@ class MutualEphemeralMediumKeys(BaseModel): @classmethod def random_init(cls, height: int, width: int): return MutualEphemeralMediumKeys( - alpha_key=AlphabetKey.init_matrix(width, height, 255), + alpha_key=SubstitutionKey.init_matrix(width, height, 255), outer_key_1=OuterKey.init_matrix(height), outer_key_2=OuterKey.init_matrix(height), outer_key_3=OuterKey.init_matrix(height), @@ -323,7 +323,7 @@ class MutualPersistentDataKeys(BaseModel): outer_key_1: OuterKey outer_key_2: OuterKey outer_key_3: OuterKey - alpha_key: AlphabetKey + alpha_key: SubstitutionKey inner_key_1: InnerKey inner_key_2: InnerKey @@ -333,14 +333,14 @@ class MutualPersistentDataKeys(BaseModel): outer_key_1=OuterKey.init_matrix(height), outer_key_2=OuterKey.init_matrix(height), outer_key_3=OuterKey.init_matrix(height), - alpha_key=AlphabetKey.init_matrix(width, height, 255), + alpha_key=SubstitutionKey.init_matrix(width, height, 255), inner_key_1=InnerKey.init_matrix(width, height), inner_key_2=InnerKey.init_matrix(width, height), ) class MutualPersistentMediumKeys(BaseModel): - alpha_key: AlphabetKey + alpha_key: SubstitutionKey outer_key_1: OuterKey outer_key_2: OuterKey @@ -357,7 +357,7 @@ class MutualPersistentMediumKeys(BaseModel): @classmethod def random_init(cls, height: int, width: int): return MutualPersistentMediumKeys( - alpha_key=AlphabetKey.init_matrix(width, height, 255), + alpha_key=SubstitutionKey.init_matrix(width, height, 255), outer_key_1=OuterKey.init_matrix(height), outer_key_2=OuterKey.init_matrix(height), outer_key_3=OuterKey.init_matrix(height), diff --git a/src/server_darc.py b/src/server_darc.py index 482c4a5..ef5ffbe 100644 --- a/src/server_darc.py +++ b/src/server_darc.py @@ -1,11 +1,11 @@ from src.models import ( - AlphabetKey, ServerPersistentDataKeys, ServerEphemeralDataKeys, MutualPersistentDataKeys, + SubstitutionKey, ServerPersistentDataKeys, ServerEphemeralDataKeys, MutualPersistentDataKeys, MutualEphemeralDataKeys, ServerKeys, MutualKeys, ServerPersistentMediumKeys, ServerEphemeralMediumKeys, MutualPersistentMediumKeys, MutualEphemeralMediumKeys ) -def darc_phase1(alphabet: AlphabetKey, medium: AlphabetKey, server_keys: ServerKeys, mutual_keys: MutualKeys): +def darc_phase1(alphabet: SubstitutionKey, medium: SubstitutionKey, server_keys: ServerKeys, mutual_keys: MutualKeys): alphabet_phase1 = translate_data( alphabet, server_keys.persistent.data, @@ -24,7 +24,7 @@ def darc_phase1(alphabet: AlphabetKey, medium: AlphabetKey, server_keys: ServerK def translate_data( - alphabet: AlphabetKey, + alphabet: SubstitutionKey, server_persistent_data_keys: ServerPersistentDataKeys, server_ephemeral_data_keys: ServerEphemeralDataKeys, mutual_persistent_data_keys: MutualPersistentDataKeys, @@ -49,7 +49,7 @@ def translate_data( def translate_medium( - medium: AlphabetKey, + medium: SubstitutionKey, server_persistent_medium_keys: ServerPersistentMediumKeys, server_ephemeral_medium_keys: ServerEphemeralMediumKeys, mutual_persistent_medium_keys: MutualPersistentMediumKeys, @@ -65,7 +65,7 @@ def translate_medium( return medium_phase1 -def darc_phase_4(alphabet: AlphabetKey, darc_message: AlphabetKey, server_keys: ServerKeys, mutual_keys: MutualKeys): +def darc_phase_4(alphabet: SubstitutionKey, darc_message: SubstitutionKey, server_keys: ServerKeys, mutual_keys: MutualKeys): medium_unwrapped = phase4_unwrap_medium( darc_message, server_keys.persistent.medium, @@ -84,7 +84,7 @@ def darc_phase_4(alphabet: AlphabetKey, darc_message: AlphabetKey, server_keys: def phase4_translate_alphabet( - alphabet: AlphabetKey, + alphabet: SubstitutionKey, server_persistent_data_keys: ServerPersistentDataKeys, server_ephemeral_data_keys: ServerEphemeralDataKeys, mutual_persistent_data_keys: MutualPersistentDataKeys, @@ -113,7 +113,7 @@ def phase4_translate_alphabet( def phase4_unwrap_medium( - darc_message: AlphabetKey, + darc_message: SubstitutionKey, server_persistent_medium_keys: ServerPersistentMediumKeys, server_ephemeral_medium_keys: ServerEphemeralMediumKeys, mutual_persistent_medium_keys: MutualPersistentMediumKeys, @@ -144,7 +144,7 @@ def phase4_unwrap_medium( return unwrapped_medium -def resolve_message(translated_alphabet: AlphabetKey, unwrapped_medium: AlphabetKey) -> list[int]: +def resolve_message(translated_alphabet: SubstitutionKey, unwrapped_medium: SubstitutionKey) -> list[int]: alphabet_map = {str(key): idx for idx, key in enumerate(translated_alphabet.matrix)} encoded_message = [str(alpha) for alpha in unwrapped_medium.matrix] diff --git a/test/get_hard_coded_json.py b/test/get_hard_coded_json.py index ed1d3f8..9449c42 100644 --- a/test/get_hard_coded_json.py +++ b/test/get_hard_coded_json.py @@ -4,7 +4,7 @@ from src.models import MutualEphemeralDataKeys, MutualEphemeralMediumKeys, Mutua MutualPersistentMediumKeys, MutualPersistentDataKeys, MutualPersistentKeys, MutualKeys, ServerEphemeralMediumKeys, \ ServerEphemeralDataKeys, ServerEphemeralKeys, ServerPersistentMediumKeys, ServerPersistentDataKeys, ServerKeys, \ ServerPersistentKeys, ClientEphemeralMediumKeys, ClientEphemeralDataKeys, ClientEphemeralKeys, \ - ClientPersistentMediumKeys, ClientKeys, ClientPersistentKeys, ClientPersistentDataKeys, AlphabetKey + ClientPersistentMediumKeys, ClientKeys, ClientPersistentKeys, ClientPersistentDataKeys, SubstitutionKey def keys(): @@ -129,6 +129,6 @@ def keys(): ) ) ) - alphabet = AlphabetKey(**server_ephemeral_keys["Alphabet"]) + alphabet = SubstitutionKey(**server_ephemeral_keys["Alphabet"]) return alphabet, server_keys, mutual_keys, client_keys diff --git a/test/test_darc.py b/test/test_darc.py index dbd2ee1..a283fc7 100644 --- a/test/test_darc.py +++ b/test/test_darc.py @@ -1,7 +1,7 @@ from src.client_darc_noclient import merge_message_no_client, darc_phase_2_3 from src.client_darc import merge_message, darc_phase2, darc_phase3 from src.key_generation import generate_keys -from src.models import AlphabetKey, OuterKey +from src.models import SubstitutionKey, OuterKey from src.server_darc import darc_phase1, darc_phase_4, resolve_message import pytest @@ -11,8 +11,8 @@ import pytest [(256, 10, 256), (100, 70, 70), (40, 50, 40), (256, 10, 128), (100, 70, 35), (40, 50, 20)] ) def test_darc(height, width, message_len): - alphabet = AlphabetKey.init_matrix(width, height, 255) - medium = AlphabetKey(matrix=[[0 for _ in range(width)] for _ in range(height)]) + alphabet = SubstitutionKey.init_matrix(width, height, 255) + medium = SubstitutionKey(matrix=[[0 for _ in range(width)] for _ in range(height)]) server_keys, mutual_keys, client_keys = generate_keys(height, width) original_message = OuterKey.init_matrix(height).matrix[0] @@ -35,8 +35,8 @@ def test_darc(height, width, message_len): [(256, 10, 256), (100, 70, 70), (40, 50, 40), (256, 10, 128), (100, 70, 35), (40, 50, 20)] ) def test_darc_no_client(height, width, message_len): - alphabet = AlphabetKey.init_matrix(width, height, 255) - medium = AlphabetKey(matrix=[[0 for _ in range(width)] for _ in range(height)]) + alphabet = SubstitutionKey.init_matrix(width, height, 255) + medium = SubstitutionKey(matrix=[[0 for _ in range(width)] for _ in range(height)]) server_keys, mutual_keys, client_keys = generate_keys(height, width)