Files
pydarc/sp_network_algebra_tutorial.ipynb

648 lines
14 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": 82,
"outputs": [],
"source": [
"from src.models import OuterKey, InnerKey, SubstitutionKey"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T17:15:30.365133Z",
"start_time": "2024-07-03T17:15:30.328857Z"
}
}
},
{
"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": 83,
"outputs": [],
"source": [
"height = 4\n",
"width = 3"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T17:15:30.365712Z",
"start_time": "2024-07-03T17:15:30.332512Z"
}
}
},
{
"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": 84,
"outputs": [
{
"data": {
"text/plain": "[[1, 2, 0], [0, 1, 2], [2, 1, 0], [2, 0, 1]]"
},
"execution_count": 84,
"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-03T17:15:30.366169Z",
"start_time": "2024-07-03T17:15:30.335990Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Outer Key\n",
"An outer key is ..."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 85,
"outputs": [
{
"data": {
"text/plain": "[[1, 2, 3, 0]]"
},
"execution_count": 85,
"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-03T17:15:30.405313Z",
"start_time": "2024-07-03T17:15:30.341422Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Substitution Key (formerly AlphabetKey or alpha_key)\n",
"Sub key is ..."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 106,
"outputs": [
{
"data": {
"text/plain": "[[237, 92, 1], [165, 114, 152], [38, 71, 114], [52, 116, 61]]"
},
"execution_count": 106,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a0 = SubstitutionKey.init_matrix(width, height)\n",
"a1 = SubstitutionKey.init_matrix(width, height)\n",
"\n",
"a0.matrix"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T17:26:59.359984Z",
"start_time": "2024-07-03T17:26:59.355497Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Operators Types\n",
"### ***<*** Outer Permutation\n",
"#### Substitution Key < Outer Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 107,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1, 2, 3, 0]]\n",
"[[237, 92, 1], [165, 114, 152], [38, 71, 114], [52, 116, 61]]\n",
"[[165, 114, 152], [38, 71, 114], [52, 116, 61], [237, 92, 1]]\n"
]
}
],
"source": [
"a0_o0 = a0 < o0\n",
"print(o0.matrix)\n",
"print(a0.matrix)\n",
"print(a0_o0.matrix)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T17:27:00.733689Z",
"start_time": "2024-07-03T17:27:00.729585Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Inner Key < Outer Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 108,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1, 2, 3, 0]]\n",
"[[1, 2, 0], [0, 1, 2], [2, 1, 0], [2, 0, 1]]\n",
"[[0, 1, 2], [2, 1, 0], [2, 0, 1], [1, 2, 0]]\n"
]
}
],
"source": [
"i0_o0 = i0 < o0\n",
"print(o0.matrix)\n",
"print(i0.matrix)\n",
"print(i0_o0.matrix)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T17:27:04.433733Z",
"start_time": "2024-07-03T17:27:04.429380Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### ***<<*** Inner Permutation\n",
"#### Outer Key << OuterKey"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 109,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2, 1, 3, 0]]\n",
"[[1, 2, 3, 0]]\n",
"[[3, 2, 0, 1]]\n"
]
}
],
"source": [
"o0_o1 = o0 << o1\n",
"print(o1.matrix)\n",
"print(o0.matrix)\n",
"print(o0_o1.matrix)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T17:27:08.293098Z",
"start_time": "2024-07-03T17:27:08.288126Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Inner Key << Inner Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 110,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0, 1, 2], [0, 2, 1], [0, 1, 2], [2, 1, 0]]\n",
"[[1, 2, 0], [0, 1, 2], [2, 1, 0], [2, 0, 1]]\n",
"[[1, 2, 0], [0, 2, 1], [2, 1, 0], [1, 0, 2]]\n"
]
}
],
"source": [
"i0_i1 = i0 << i1\n",
"print(i1.matrix)\n",
"print(i0.matrix)\n",
"print(i0_i1.matrix)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T17:27:11.574355Z",
"start_time": "2024-07-03T17:27:11.568652Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Substitution Key << Inner Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 111,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1, 2, 0], [0, 1, 2], [2, 1, 0], [2, 0, 1]]\n",
"[[237, 92, 1], [165, 114, 152], [38, 71, 114], [52, 116, 61]]\n",
"[[92, 1, 237], [165, 114, 152], [114, 71, 38], [61, 52, 116]]\n"
]
}
],
"source": [
"a0_i0 = a0 << i0\n",
"print(i0.matrix)\n",
"print(a0.matrix)\n",
"print(a0_i0.matrix)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T17:27:14.277217Z",
"start_time": "2024-07-03T17:27:14.272132Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### ***^*** Substitution\n",
"#### Substitution Key ^ Substitution Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 114,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[237, 92, 1], [165, 114, 152], [38, 71, 114], [52, 116, 61]]\n",
"[[184, 159, 115], [199, 221, 35], [158, 70, 86], [74, 43, 129]]\n",
"[[85, 195, 114], [98, 175, 187], [184, 1, 36], [126, 95, 188]]\n",
"85\n"
]
}
],
"source": [
"a0_a1 = a0 ^ a1\n",
"print(a0.matrix)\n",
"print(a1.matrix)\n",
"print(a0_a1.matrix)\n",
"print(a0.matrix[0][0] ^ a1.matrix[0][0])"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T17:29:15.922958Z",
"start_time": "2024-07-03T17:29:15.915126Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## dddd"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 88,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 88,
"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-03T17:15:30.408639Z",
"start_time": "2024-07-03T17:15:30.361902Z"
}
}
},
{
"cell_type": "code",
"execution_count": 89,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 89,
"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-03T17:15:30.408838Z",
"start_time": "2024-07-03T17:15:30.365269Z"
}
}
},
{
"cell_type": "code",
"execution_count": 90,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 90,
"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-03T17:15:30.408892Z",
"start_time": "2024-07-03T17:15:30.368768Z"
}
}
},
{
"cell_type": "code",
"execution_count": 91,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(~(i0 << i1)) == (~i1 << ~i0)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T17:15:30.408977Z",
"start_time": "2024-07-03T17:15:30.371377Z"
}
}
},
{
"cell_type": "code",
"execution_count": 92,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 92,
"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-03T17:15:30.409076Z",
"start_time": "2024-07-03T17:15:30.375604Z"
}
}
},
{
"cell_type": "code",
"execution_count": 93,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 93,
"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-03T17:15:30.409139Z",
"start_time": "2024-07-03T17:15:30.378496Z"
}
}
},
{
"cell_type": "code",
"execution_count": 94,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 94,
"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-03T17:15:30.409210Z",
"start_time": "2024-07-03T17:15:30.381727Z"
}
}
},
{
"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": 95,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"i0 < (o0 << o1 << o2) != (i0 < (o0 << o1 << o2)) # not equal !!!!!!"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-03T17:15:30.409260Z",
"start_time": "2024-07-03T17:15:30.384147Z"
}
}
}
],
"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
}