Files
pydarc/sp_network_algebra_tutorial.ipynb

1134 lines
22 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": 69,
"outputs": [],
"source": [
"from src.models import OuterKey, InnerKey, SubstitutionKey"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.685220Z",
"start_time": "2024-07-04T14:12:16.446987Z"
}
}
},
{
"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": 70,
"outputs": [],
"source": [
"height = 4\n",
"width = 3"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.686123Z",
"start_time": "2024-07-04T14:12:16.453332Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Operand Types\n",
"### Inner Permutation Key\n",
"An inner permutation key (inner key for short) is a list of `height` rows, each a random permutation of an identity array of length `width`"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 71,
"outputs": [
{
"data": {
"text/plain": "[[0, 1, 2], [1, 0, 2], [1, 0, 2], [2, 1, 0]]"
},
"execution_count": 71,
"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",
"i_identity = InnerKey.init_identity_matrix(width, height)\n",
"\n",
"i0.matrix"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.830392Z",
"start_time": "2024-07-04T14:12:16.460887Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Outer Permutation Key\n",
"An outer key is ..."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 72,
"outputs": [
{
"data": {
"text/plain": "[[2, 0, 1, 3]]"
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"o0 = OuterKey.init_matrix(height)\n",
"o1 = OuterKey.init_matrix(height)\n",
"o2 = OuterKey.init_matrix(height)\n",
"o_identity = OuterKey.init_identity_matrix(height)\n",
"\n",
"o0.matrix"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.831572Z",
"start_time": "2024-07-04T14:12:16.469310Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Substitution Key (formerly Alphabet Key or alpha_key)\n",
"Sub key is ..."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 73,
"outputs": [
{
"data": {
"text/plain": "[[51, 14, 207], [3, 171, 4], [73, 204, 152], [142, 175, 64]]"
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a0 = SubstitutionKey.init_matrix(width, height)\n",
"a1 = SubstitutionKey.init_matrix(width, height)\n",
"a2 = SubstitutionKey.init_matrix(width, height)\n",
"a_identity = SubstitutionKey.init_identity_matrix(width, height)\n",
"\n",
"a0.matrix"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.832461Z",
"start_time": "2024-07-04T14:12:16.476713Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Operators Types\n",
"### < Outer Permutation\n",
"#### Substitution Key < Outer Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 74,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2, 0, 1, 3]]\n",
"[[51, 14, 207], [3, 171, 4], [73, 204, 152], [142, 175, 64]]\n",
"[[73, 204, 152], [51, 14, 207], [3, 171, 4], [142, 175, 64]]\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-04T14:12:16.833320Z",
"start_time": "2024-07-04T14:12:16.484335Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Inner Key < Outer Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 75,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2, 0, 1, 3]]\n",
"[[0, 1, 2], [1, 0, 2], [1, 0, 2], [2, 1, 0]]\n",
"[[1, 0, 2], [0, 1, 2], [1, 0, 2], [2, 1, 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-04T14:12:16.834157Z",
"start_time": "2024-07-04T14:12:16.492114Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### << Inner Permutation\n",
"#### Outer Key << Outer Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 76,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0, 1, 3, 2]]\n",
"[[2, 0, 1, 3]]\n",
"[[2, 0, 3, 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-04T14:12:16.834973Z",
"start_time": "2024-07-04T14:12:16.499677Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Inner Key << Inner Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 77,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1, 0, 2], [0, 1, 2], [0, 2, 1], [2, 1, 0]]\n",
"[[0, 1, 2], [1, 0, 2], [1, 0, 2], [2, 1, 0]]\n",
"[[1, 0, 2], [1, 0, 2], [1, 2, 0], [0, 1, 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-04T14:12:16.835789Z",
"start_time": "2024-07-04T14:12:16.507413Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Substitution Key << Inner Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 78,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0, 1, 2], [1, 0, 2], [1, 0, 2], [2, 1, 0]]\n",
"[[51, 14, 207], [3, 171, 4], [73, 204, 152], [142, 175, 64]]\n",
"[[51, 14, 207], [171, 3, 4], [204, 73, 152], [64, 175, 142]]\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-04T14:12:16.854289Z",
"start_time": "2024-07-04T14:12:16.515159Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### ~Permutation Inversion\n",
"#### ~Outer Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 79,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2, 0, 1, 3]]\n",
"[[1, 2, 0, 3]]\n"
]
}
],
"source": [
"print(o0.matrix)\n",
"print((~o0).matrix)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.855384Z",
"start_time": "2024-07-04T14:12:16.522867Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### ~Inner Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 80,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0, 1, 2], [1, 0, 2], [1, 0, 2], [2, 1, 0]]\n",
"[[0, 1, 2], [1, 0, 2], [1, 0, 2], [2, 1, 0]]\n"
]
}
],
"source": [
"print(i0.matrix)\n",
"print((~i0).matrix)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.856225Z",
"start_time": "2024-07-04T14:12:16.530564Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### ^ Substitution\n",
"#### Substitution Key ^ Substitution Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 81,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[51, 14, 207], [3, 171, 4], [73, 204, 152], [142, 175, 64]]\n",
"[[1, 41, 28], [228, 126, 163], [255, 48, 188], [53, 120, 13]]\n",
"[[50, 39, 211], [231, 213, 167], [182, 252, 36], [187, 215, 77]]\n",
"50\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-04T14:12:16.857040Z",
"start_time": "2024-07-04T14:12:16.538722Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Substitution Permutation Algebra\n",
"#### properites:\n",
"- associative: (a + b) + c = a + (b + c)\n",
"- commutative: a + b = b + a\n",
"- identity: a + 0 = a or a * 1 = a\n",
"- inverse: a + (-a) = 0 or a * 1/a = 1 (for all a ~= 0)\n",
"- distributive: a(b + c) = ab + ac\n",
"\n",
"### Associative\n",
"#### Outer Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 82,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(o0 << o1 << o2) == ((o0 << o1) << o2) == (o0 << (o1 << o2))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.858322Z",
"start_time": "2024-07-04T14:12:16.546322Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Inner Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 83,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(i0 << i1 << i2) == ((i0 << i1) << i2) == (i0 << (i1 << i2))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.863346Z",
"start_time": "2024-07-04T14:12:16.554187Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Substitution Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 84,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a0 ^ a1 ^ a2) == ((a0 ^ a1) ^ a2) == (a0 ^ (a1 ^ a2))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.864336Z",
"start_time": "2024-07-04T14:12:16.562025Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Commutative Property\n",
"Substitution is the only key type that is commutative\n",
"#### Substitution Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 85,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a0 ^ a1) == (a1 ^ a0)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.865178Z",
"start_time": "2024-07-04T14:12:16.569781Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Identity Property\n",
"#### Outer Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 86,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(o0 << o_identity) == o0"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.866342Z",
"start_time": "2024-07-04T14:12:16.578046Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Inner Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 87,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(i0 << i_identity) == i0"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.867309Z",
"start_time": "2024-07-04T14:12:16.589133Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Substitution Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 88,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a0 ^ a_identity) == a0"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.868143Z",
"start_time": "2024-07-04T14:12:16.599974Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Inverse Property\n",
"#### Outer Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 89,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(o0 << ~o0) == o_identity"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.868953Z",
"start_time": "2024-07-04T14:12:16.608436Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Inner Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 90,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(i0 << ~i0) == i_identity"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.869884Z",
"start_time": "2024-07-04T14:12:16.619030Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Substitution Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 91,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a0 ^ a0) == a_identity"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.870708Z",
"start_time": "2024-07-04T14:12:16.629750Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Distributive\n",
"#### Inner Key/Outer Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 92,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"((i0 << i1) < o0) == ((i0 < o0) << (i1 < o0))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.871520Z",
"start_time": "2024-07-04T14:12:16.638198Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Substitution Key/Outer Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 93,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"((a0 ^ a1) < o0) == ((a0 < o0) ^ (a1 < o0))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.872334Z",
"start_time": "2024-07-04T14:12:16.646323Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Substitution Key/Inner Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 94,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"((a0 ^ a1) << i0) == ((a0 << i0) ^ (a1 << i0))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.873134Z",
"start_time": "2024-07-04T14:12:16.654286Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"#### Substitution Key/Inner Key/Outer Key"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 95,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"((a0 << i0) < o0) == ((a0 < o0) << (i0 < o0))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.874410Z",
"start_time": "2024-07-04T14:12:16.662231Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"### Other Examples"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 96,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a0 << (i0 < o0)) == (((a0 < ~o0) << i0) < o0)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.882191Z",
"start_time": "2024-07-04T14:12:16.670307Z"
}
}
},
{
"cell_type": "code",
"execution_count": 97,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"((a0 < o0) << i0) == ((a0 << (i0 < ~o0)) < o0)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.882308Z",
"start_time": "2024-07-04T14:12:16.674096Z"
}
}
},
{
"cell_type": "code",
"execution_count": 98,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(~(i0 << i1)) == (~i1 << ~i0)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.882394Z",
"start_time": "2024-07-04T14:12:16.677061Z"
}
}
},
{
"cell_type": "code",
"execution_count": 99,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(~(o0 << o1)) == (~o1 << ~o0)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.883495Z",
"start_time": "2024-07-04T14:12:16.683278Z"
}
}
},
{
"cell_type": "code",
"execution_count": 100,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 100,
"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-04T14:12:16.883574Z",
"start_time": "2024-07-04T14:12:16.687331Z"
}
}
},
{
"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": 101,
"outputs": [
{
"data": {
"text/plain": "True"
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"i0 < (o0 << o1 << o2) != (i0 < (o0 << o1 << o2)) # not equal !!!!!!"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-07-04T14:12:16.883645Z",
"start_time": "2024-07-04T14:12:16.690509Z"
}
}
}
],
"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
}