Files
pydarc/sp_network_algebra_tutorial.ipynb

486 lines
10 KiB
Plaintext

{
"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
}