From 1162bd54e17b139430e95e12f4526224a74c5198 Mon Sep 17 00:00:00 2001 From: Donovan Date: Fri, 14 Mar 2025 09:35:19 -0500 Subject: [PATCH] rename attribute to property --- notebooks/dispersion_tutorial.ipynb | 22 +-- notebooks/nkode_tutorial.ipynb | 252 ++++++++++++++-------------- src/customer.py | 8 +- src/models.py | 2 +- src/user_cipher.py | 16 +- src/user_keypad.py | 31 ++-- src/user_signup_session.py | 6 +- src/utils.py | 14 +- test/test_nkode_api.py | 4 +- test/test_nkode_interface.py | 8 +- test/test_user_cipher_keys.py | 2 +- test/test_user_keypad.py | 10 +- 12 files changed, 187 insertions(+), 188 deletions(-) diff --git a/notebooks/dispersion_tutorial.ipynb b/notebooks/dispersion_tutorial.ipynb index dca29ea..c4effe6 100644 --- a/notebooks/dispersion_tutorial.ipynb +++ b/notebooks/dispersion_tutorial.ipynb @@ -44,8 +44,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-14T11:14:49.006056Z", - "start_time": "2025-03-14T11:14:48.964902Z" + "end_time": "2025-03-14T14:34:55.702615Z", + "start_time": "2025-03-14T14:34:55.694963Z" } }, "outputs": [ @@ -60,7 +60,7 @@ "output_type": "display_data" } ], - "execution_count": 1 + "execution_count": 4 }, { "cell_type": "code", @@ -73,8 +73,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-14T11:15:32.023001Z", - "start_time": "2025-03-14T11:15:32.009838Z" + "end_time": "2025-03-14T14:34:55.718161Z", + "start_time": "2025-03-14T14:34:55.714512Z" } }, "outputs": [ @@ -83,13 +83,13 @@ "text/plain": [ "" ], - "text/markdown": "|key|set0|set1|set2|set3|\n|-|-|-|-|-|\n|key1|4|40|44|400|\n|key2|3|30|33|300|\n|key3|2|20|22|200|\n|key4|1|10|11|100|\n|key5|5|50|55|500|" + "text/markdown": "|key|set0|set1|set2|set3|\n|-|-|-|-|-|\n|key1|1|10|11|100|\n|key2|4|40|44|400|\n|key3|3|30|33|300|\n|key4|2|20|22|200|\n|key5|5|50|55|500|" }, "metadata": {}, "output_type": "display_data" } ], - "execution_count": 3 + "execution_count": 5 }, { "cell_type": "code", @@ -105,8 +105,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-14T11:17:30.317806Z", - "start_time": "2025-03-14T11:17:30.313082Z" + "end_time": "2025-03-14T14:34:55.731332Z", + "start_time": "2025-03-14T14:34:55.728135Z" } }, "outputs": [ @@ -115,13 +115,13 @@ "text/plain": [ "" ], - "text/markdown": "|key|set0|set1|set2|set3|\n|-|-|-|-|-|\n|key1|2|50|33|100|\n|key2|1|40|22|500|\n|key3|5|30|11|400|\n|key4|4|20|55|300|\n|key5|3|10|44|200|" + "text/markdown": "|key|set0|set1|set2|set3|\n|-|-|-|-|-|\n|key1|5|30|44|100|\n|key2|1|20|33|400|\n|key3|4|50|22|300|\n|key4|3|10|55|200|\n|key5|2|40|11|500|" }, "metadata": {}, "output_type": "display_data" } ], - "execution_count": 5 + "execution_count": 6 } ], "metadata": { diff --git a/notebooks/nkode_tutorial.ipynb b/notebooks/nkode_tutorial.ipynb index 30c99a1..204be73 100644 --- a/notebooks/nkode_tutorial.ipynb +++ b/notebooks/nkode_tutorial.ipynb @@ -12,12 +12,12 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-14T14:18:14.423890Z", - "start_time": "2025-03-14T14:18:14.420788Z" + "end_time": "2025-03-14T14:35:01.634660Z", + "start_time": "2025-03-14T14:35:01.631713Z" } }, "outputs": [], - "execution_count": 23 + "execution_count": 67 }, { "cell_type": "code", @@ -26,26 +26,26 @@ " return \"test_username\" + \"\".join([choice(ascii_lowercase) for _ in range(6)])\n", "\n", "\n", - "def select_keys_with_passcode_values(user_passcode: list[int], keypad: np.ndarray, attrs_per_key: int) -> list[int]:\n", - " indices = [np.where(keypad == attr)[0][0] for attr in user_passcode]\n", - " return [int(index // attrs_per_key) for index in indices]\n", + "def select_keys_with_passcode_values(user_passcode: list[int], keypad: np.ndarray, props_per_key: int) -> list[int]:\n", + " indices = [np.where(keypad == prop)[0][0] for prop in user_passcode]\n", + " return [int(index // props_per_key) for index in indices]\n", "\n", "\n", - "def keypad_view(keypad: np.ndarray, attrs_per_key: int):\n", + "def keypad_view(keypad: np.ndarray, props_per_key: int):\n", " print(\"Keypad View\")\n", - " interface_keypad = keypad.reshape(-1, attrs_per_key)\n", + " interface_keypad = keypad.reshape(-1, props_per_key)\n", " for idx, key_vals in enumerate(interface_keypad):\n", " print(f\"Key {idx}: {key_vals}\")\n" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-14T14:18:14.434459Z", - "start_time": "2025-03-14T14:18:14.430679Z" + "end_time": "2025-03-14T14:35:01.653961Z", + "start_time": "2025-03-14T14:35:01.650229Z" } }, "outputs": [], - "execution_count": 24 + "execution_count": 68 }, { "cell_type": "code", @@ -55,12 +55,12 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-14T14:18:14.453086Z", - "start_time": "2025-03-14T14:18:14.451030Z" + "end_time": "2025-03-14T14:35:01.663429Z", + "start_time": "2025-03-14T14:35:01.661723Z" } }, "outputs": [], - "execution_count": 25 + "execution_count": 69 }, { "cell_type": "markdown", @@ -100,12 +100,12 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-14T14:18:14.461793Z", - "start_time": "2025-03-14T14:18:14.457601Z" + "end_time": "2025-03-14T14:35:01.675378Z", + "start_time": "2025-03-14T14:35:01.671221Z" } }, "outputs": [], - "execution_count": 26 + "execution_count": 70 }, { "cell_type": "markdown", @@ -138,8 +138,8 @@ "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-14T14:18:14.472200Z", - "start_time": "2025-03-14T14:18:14.469516Z" + "end_time": "2025-03-14T14:35:01.692557Z", + "start_time": "2025-03-14T14:35:01.689797Z" } }, "outputs": [ @@ -147,17 +147,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "Customer Set Key: [57513 39162 37238 40595 30029 8573]\n", + "Customer Set Key: [21871 61274 2713 19029 26505 50649]\n", "Customer properties Key:\n", - "[14659 8841 6728 32702 39675 63074]\n", - "[19274 15250 33729 24532 34839 38348]\n", - "[ 2539 51139 60808 11045 55452 61221]\n", - "[55550 11904 10001 59259 26026 26740]\n", - "[59910 49698 45525 62003 49414 40644]\n" + "[42655 23174 1254 25551 11488 27210]\n", + "[60953 53938 20104 25057 1695 3339]\n", + "[32918 29164 48427 35503 41361 24040]\n", + "[44451 37655 36339 20189 49770 11581]\n", + "[31824 8094 2501 29432 12778 20956]\n" ] } ], - "execution_count": 27 + "execution_count": 71 }, { "cell_type": "markdown", @@ -171,14 +171,14 @@ "source": [ "set_properties_dict = dict(zip(customer.cipher.set_key, customer_prop_keypad.T))\n", "print(f\"Set to Properties Map:\")\n", - "for set_val, attrs in set_properties_dict.items():\n", - " print(f\"{set_val}: {attrs}\")" + "for set_val, props in set_properties_dict.items():\n", + " print(f\"{set_val}: {props}\")" ], "metadata": { "collapsed": false, "ExecuteTime": { - "end_time": "2025-03-14T14:18:14.486583Z", - "start_time": "2025-03-14T14:18:14.484301Z" + "end_time": "2025-03-14T14:35:01.702167Z", + "start_time": "2025-03-14T14:35:01.699777Z" } }, "outputs": [ @@ -187,16 +187,16 @@ "output_type": "stream", "text": [ "Set to Properties Map:\n", - "57513: [14659 19274 2539 55550 59910]\n", - "39162: [ 8841 15250 51139 11904 49698]\n", - "37238: [ 6728 33729 60808 10001 45525]\n", - "40595: [32702 24532 11045 59259 62003]\n", - "30029: [39675 34839 55452 26026 49414]\n", - "8573: [63074 38348 61221 26740 40644]\n" + "21871: [42655 60953 32918 44451 31824]\n", + "61274: [23174 53938 29164 37655 8094]\n", + "2713: [ 1254 20104 48427 36339 2501]\n", + "19029: [25551 25057 35503 20189 29432]\n", + "26505: [11488 1695 41361 49770 12778]\n", + "50649: [27210 3339 24040 11581 20956]\n" ] } ], - "execution_count": 28 + "execution_count": 72 }, { "metadata": {}, @@ -218,8 +218,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:14.510067Z", - "start_time": "2025-03-14T14:18:14.507204Z" + "end_time": "2025-03-14T14:35:01.722470Z", + "start_time": "2025-03-14T14:35:01.719357Z" } }, "cell_type": "code", @@ -232,15 +232,15 @@ "name": "stdout", "output_type": "stream", "text": [ - "[[ 1 7 13 19 25]\n", - " [ 0 6 12 18 24]\n", - " [ 5 11 17 23 29]\n", - " [ 2 8 14 20 26]\n", - " [ 4 10 16 22 28]]\n" + "[[24 0 12 6 18]\n", + " [28 4 16 10 22]\n", + " [27 3 15 9 21]\n", + " [26 2 14 8 20]\n", + " [25 1 13 7 19]]\n" ] } ], - "execution_count": 29 + "execution_count": 73 }, { "metadata": {}, @@ -253,8 +253,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:14.535581Z", - "start_time": "2025-03-14T14:18:14.531909Z" + "end_time": "2025-03-14T14:35:01.746158Z", + "start_time": "2025-03-14T14:35:01.742671Z" } }, "cell_type": "code", @@ -266,8 +266,8 @@ "selected_keys_set = select_keys_with_passcode_values(user_passcode, signup_keypad, keypad_size.numb_of_keys)\n", "print(f\"User Passcode: {user_passcode}\")\n", "print(f\"Selected Keys\\n{selected_keys_set}\")\n", - "server_side_attr = [int(customer.cipher.prop_key[idx]) for idx in user_passcode]\n", - "print(f\"User Passcode Server-side properties: {server_side_attr}\")" + "server_side_prop = [int(customer.cipher.prop_key[idx]) for idx in user_passcode]\n", + "print(f\"User Passcode Server-side properties: {server_side_prop}\")" ], "outputs": [ { @@ -275,25 +275,25 @@ "output_type": "stream", "text": [ "Keypad View\n", - "Key 0: [ 1 7 13 19 25]\n", - "Key 1: [ 0 6 12 18 24]\n", - "Key 2: [ 5 11 17 23 29]\n", - "Key 3: [ 2 8 14 20 26]\n", - "Key 4: [ 4 10 16 22 28]\n", - "User Passcode: [ 1 7 13 19]\n", + "Key 0: [24 0 12 6 18]\n", + "Key 1: [28 4 16 10 22]\n", + "Key 2: [27 3 15 9 21]\n", + "Key 3: [26 2 14 8 20]\n", + "Key 4: [25 1 13 7 19]\n", + "User Passcode: [24 0 12 6]\n", "Selected Keys\n", "[0, 0, 0, 0]\n", - "User Passcode Server-side properties: [8841, 15250, 51139, 11904]\n" + "User Passcode Server-side properties: [31824, 42655, 32918, 60953]\n" ] } ], - "execution_count": 30 + "execution_count": 74 }, { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:14.556541Z", - "start_time": "2025-03-14T14:18:14.553217Z" + "end_time": "2025-03-14T14:35:01.770157Z", + "start_time": "2025-03-14T14:35:01.766784Z" } }, "cell_type": "code", @@ -309,23 +309,23 @@ "output_type": "stream", "text": [ "Keypad View\n", - "Key 0: [ 0 11 16 20 25]\n", - "Key 1: [ 2 6 13 22 29]\n", - "Key 2: [ 4 8 17 19 24]\n", - "Key 3: [ 1 10 12 23 26]\n", - "Key 4: [ 5 7 14 18 28]\n", + "Key 0: [26 3 16 6 19]\n", + "Key 1: [27 0 13 10 20]\n", + "Key 2: [24 4 14 7 21]\n", + "Key 3: [28 1 15 8 18]\n", + "Key 4: [25 2 12 9 22]\n", "Selected Keys\n", - "[3, 4, 1, 2]\n" + "[2, 1, 4, 0]\n" ] } ], - "execution_count": 31 + "execution_count": 75 }, { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:14.884832Z", - "start_time": "2025-03-14T14:18:14.575902Z" + "end_time": "2025-03-14T14:35:02.101712Z", + "start_time": "2025-03-14T14:35:01.792851Z" } }, "cell_type": "code", @@ -343,7 +343,7 @@ ] } ], - "execution_count": 32 + "execution_count": 76 }, { "metadata": {}, @@ -362,8 +362,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:14.894865Z", - "start_time": "2025-03-14T14:18:14.889948Z" + "end_time": "2025-03-14T14:35:02.115075Z", + "start_time": "2025-03-14T14:35:02.110397Z" } }, "cell_type": "code", @@ -389,22 +389,22 @@ " max_nkode_len=customer.nkode_policy.max_nkode_len, \n", ")\n", "\n", - "passcode_server_attr = [int(customer.cipher.prop_key[idx]) for idx in user_passcode]\n", - "passcode_server_set = [int(customer.cipher.get_prop_set_val(attr)) for attr in passcode_server_attr]\n", + "passcode_server_prop = [int(customer.cipher.prop_key[idx]) for idx in user_passcode]\n", + "passcode_server_set = [int(customer.cipher.get_prop_set_val(prop)) for prop in passcode_server_prop]\n", "print(f\"Passcode Set Vals: {passcode_server_set}\")\n", - "print(f\"Passcode Attr Vals: {passcode_server_attr}\")" + "print(f\"Passcode prop Vals: {passcode_server_prop}\")" ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Passcode Set Vals: [39162, 39162, 39162, 39162]\n", - "Passcode Attr Vals: [8841, 15250, 51139, 11904]\n" + "Passcode Set Vals: [21871, 21871, 21871, 21871]\n", + "Passcode prop Vals: [31824, 42655, 32918, 60953]\n" ] } ], - "execution_count": 33 + "execution_count": 77 }, { "metadata": {}, @@ -426,8 +426,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:14.917821Z", - "start_time": "2025-03-14T14:18:14.914851Z" + "end_time": "2025-03-14T14:35:02.132228Z", + "start_time": "2025-03-14T14:35:02.129307Z" } }, "cell_type": "code", @@ -441,7 +441,7 @@ "mask = user_keys.encode_base64_str(ciphered_mask)" ], "outputs": [], - "execution_count": 34 + "execution_count": 78 }, { "metadata": {}, @@ -450,16 +450,16 @@ "#### Encipher Passcode\n", "UserCipherKeys.encipher_salt_hash_code:\n", "\n", - "- ciphered_customer_attr = alpha_key ^ customer_attr\n", - "- ciphered_passcode_i = pass_key_i ^ ciphered_customer_attr_i\n", + "- ciphered_customer_prop = alpha_key ^ customer_prop\n", + "- ciphered_passcode_i = pass_key_i ^ ciphered_customer_prop_i\n", "- code = hash(ciphered_passcode, salt)" ] }, { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:15.243102Z", - "start_time": "2025-03-14T14:18:14.937865Z" + "end_time": "2025-03-14T14:35:02.448903Z", + "start_time": "2025-03-14T14:35:02.143953Z" } }, "cell_type": "code", @@ -468,13 +468,13 @@ "import hashlib\n", "import base64\n", "\n", - "ciphered_customer_attrs = np.bitwise_xor(customer.cipher.prop_key, user_keys.prop_key)\n", - "passcode_ciphered_attrs = [ciphered_customer_attrs[idx] for idx in user_passcode]\n", + "ciphered_customer_props = np.bitwise_xor(customer.cipher.prop_key, user_keys.prop_key)\n", + "passcode_ciphered_props = [ciphered_customer_props[idx] for idx in user_passcode]\n", "pad_len = customer.nkode_policy.max_nkode_len - passcode_len\n", "\n", - "passcode_ciphered_attrs.extend([0 for _ in range(pad_len)])\n", + "passcode_ciphered_props.extend([0 for _ in range(pad_len)])\n", "\n", - "ciphered_code = np.bitwise_xor(passcode_ciphered_attrs, user_keys.pass_key)\n", + "ciphered_code = np.bitwise_xor(passcode_ciphered_props, user_keys.pass_key)\n", "\n", "#passcode_bytes = int_array_to_bytes(ciphered_code)\n", "passcode_bytes = ciphered_code.tobytes()\n", @@ -483,13 +483,13 @@ "code = hashed_data.decode(\"utf-8\")" ], "outputs": [], - "execution_count": 35 + "execution_count": 79 }, { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:15.249186Z", - "start_time": "2025-03-14T14:18:15.247147Z" + "end_time": "2025-03-14T14:35:02.459349Z", + "start_time": "2025-03-14T14:35:02.457325Z" } }, "cell_type": "code", @@ -502,7 +502,7 @@ ")" ], "outputs": [], - "execution_count": 36 + "execution_count": 80 }, { "metadata": {}, @@ -516,8 +516,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:15.563030Z", - "start_time": "2025-03-14T14:18:15.257823Z" + "end_time": "2025-03-14T14:35:02.775720Z", + "start_time": "2025-03-14T14:35:02.468856Z" } }, "cell_type": "code", @@ -535,17 +535,17 @@ "output_type": "stream", "text": [ "Keypad View\n", - "Key 0: [0 1 2 3 4 5]\n", - "Key 1: [ 6 7 8 9 10 11]\n", + "Key 0: [24 25 26 27 28 29]\n", + "Key 1: [0 1 2 3 4 5]\n", "Key 2: [12 13 14 15 16 17]\n", - "Key 3: [18 19 20 21 22 23]\n", - "Key 4: [24 25 26 27 28 29]\n", + "Key 3: [ 6 7 8 9 10 11]\n", + "Key 4: [18 19 20 21 22 23]\n", "Selected Keys: [0, 1, 2, 3]\n", "True\n" ] } ], - "execution_count": 37 + "execution_count": 81 }, { "metadata": {}, @@ -577,8 +577,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:15.574718Z", - "start_time": "2025-03-14T14:18:15.571571Z" + "end_time": "2025-03-14T14:35:02.787442Z", + "start_time": "2025-03-14T14:35:02.784207Z" } }, "cell_type": "code", @@ -598,11 +598,11 @@ "name": "stdout", "output_type": "stream", "text": [ - "[39162, 39162, 39162, 39162]\n" + "[21871, 21871, 21871, 21871]\n" ] } ], - "execution_count": 38 + "execution_count": 82 }, { "metadata": {}, @@ -612,8 +612,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:15.591180Z", - "start_time": "2025-03-14T14:18:15.587992Z" + "end_time": "2025-03-14T14:35:02.805826Z", + "start_time": "2025-03-14T14:35:02.802523Z" } }, "cell_type": "code", @@ -624,8 +624,8 @@ "for idx in range(passcode_len):\n", " key_numb = selected_keys_login[idx]\n", " set_idx = set_vals_idx[idx]\n", - " selected_attr_idx = customer.users[username].user_keypad.get_prop_idx_by_keynumb_setidx(key_numb, set_idx)\n", - " presumed_selected_properties_idx.append(selected_attr_idx)\n", + " selected_prop_idx = customer.users[username].user_keypad.get_prop_idx_by_keynumb_setidx(key_numb, set_idx)\n", + " presumed_selected_properties_idx.append(selected_prop_idx)\n", "\n", "print(user_passcode.tolist() == presumed_selected_properties_idx)" ], @@ -638,7 +638,7 @@ ] } ], - "execution_count": 39 + "execution_count": 83 }, { "metadata": {}, @@ -648,8 +648,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:15.916787Z", - "start_time": "2025-03-14T14:18:15.610843Z" + "end_time": "2025-03-14T14:35:03.129079Z", + "start_time": "2025-03-14T14:35:02.822467Z" } }, "cell_type": "code", @@ -666,7 +666,7 @@ ] } ], - "execution_count": 40 + "execution_count": 84 }, { "metadata": {}, @@ -682,8 +682,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:16.539065Z", - "start_time": "2025-03-14T14:18:15.926843Z" + "end_time": "2025-03-14T14:35:03.757448Z", + "start_time": "2025-03-14T14:35:03.142511Z" } }, "cell_type": "code", @@ -708,14 +708,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "mask: 4NCIa/fFtx0udGaF1ub+O/6oU04=, code: $2b$12$XuGO.allDcb91peVIjwsq./Ba/TZI3wxAc0acgWkApzkzGWfwPQCC\n", - "mask: 4NCIa/fFtx0udGaF1ub+O/6oU04=, code: $2b$12$XuGO.allDcb91peVIjwsq./Ba/TZI3wxAc0acgWkApzkzGWfwPQCC\n", + "mask: QUR1wAPG8jJh/nG1LbgMIupAY1I=, code: $2b$12$0Ia665myY64l8.UyKRc4tu/OR.0BG6KFLyXcuDdIXrDkpP6cjo7xO\n", + "mask: QUR1wAPG8jJh/nG1LbgMIupAY1I=, code: $2b$12$0Ia665myY64l8.UyKRc4tu/OR.0BG6KFLyXcuDdIXrDkpP6cjo7xO\n", "True\n", - "mask: ceaWZR+hNRpEkj3fq1cfPu1Zyok=, code: $2b$12$q7lqdTj6qBMDDGEog9Pq3.M2Wso0TI8cx4/PhOK/fE1mhsws2FGe.\n" + "mask: SB1G0mGYnu2Fy2usk08r9uTTugo=, code: $2b$12$pKJtlWu9gwXqA4b.Q/WEZ.yzz9ntnXnFSindkyVOWa3sNOoVd0LLK\n" ] } ], - "execution_count": 41 + "execution_count": 85 }, { "metadata": {}, @@ -729,8 +729,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:16.551437Z", - "start_time": "2025-03-14T14:18:16.547371Z" + "end_time": "2025-03-14T14:35:03.770972Z", + "start_time": "2025-03-14T14:35:03.766802Z" } }, "cell_type": "code", @@ -742,7 +742,7 @@ "new_sets = customer.cipher.set_key" ], "outputs": [], - "execution_count": 42 + "execution_count": 86 }, { "metadata": {}, @@ -755,8 +755,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:16.568881Z", - "start_time": "2025-03-14T14:18:16.566223Z" + "end_time": "2025-03-14T14:35:03.781705Z", + "start_time": "2025-03-14T14:35:03.779309Z" } }, "cell_type": "code", @@ -769,7 +769,7 @@ " user.cipher.prop_key = np.bitwise_xor(user.cipher.prop_key, props_xor)" ], "outputs": [], - "execution_count": 43 + "execution_count": 87 }, { "metadata": {}, @@ -779,8 +779,8 @@ { "metadata": { "ExecuteTime": { - "end_time": "2025-03-14T14:18:16.884432Z", - "start_time": "2025-03-14T14:18:16.572989Z" + "end_time": "2025-03-14T14:35:04.107750Z", + "start_time": "2025-03-14T14:35:03.797025Z" } }, "cell_type": "code", @@ -794,7 +794,7 @@ "user.renew = False" ], "outputs": [], - "execution_count": 44 + "execution_count": 88 } ], "metadata": { diff --git a/src/customer.py b/src/customer.py index 5f7c925..b173c36 100644 --- a/src/customer.py +++ b/src/customer.py @@ -38,11 +38,11 @@ class Customer: for idx in range(passcode_len): key_numb = selected_keys[idx] set_idx = set_vals_idx[idx] - selected_attr_idx = user.user_keypad.get_prop_idx_by_keynumb_setidx(key_numb, set_idx) - presumed_property_idxs.append(selected_attr_idx) + selected_prop_idx = user.user_keypad.get_prop_idx_by_keynumb_setidx(key_numb, set_idx) + presumed_property_idxs.append(selected_prop_idx) - enciphered_attr = user.cipher.encipher_salt_hash_code(presumed_property_idxs, self.cipher) - if enciphered_attr != user.enciphered_passcode.code: + enciphered_prop = user.cipher.encipher_salt_hash_code(presumed_property_idxs, self.cipher) + if enciphered_prop != user.enciphered_passcode.code: return False if user.renew: diff --git a/src/models.py b/src/models.py index 101aafa..04e1158 100644 --- a/src/models.py +++ b/src/models.py @@ -12,7 +12,7 @@ class NKodePolicy: min_nkode_len: int = 4 distinct_sets: int = 0 distinct_properties: int = 4 - byte_len: int = 2 # Todo: this should change the total number of bytes an attribute or set value can be + byte_len: int = 2 # Todo: this should change the total number of bytes an properities or set value can be lock_out: int = 5 expiration: int = -1 # in seconds -1 means nkode never expires diff --git a/src/user_cipher.py b/src/user_cipher.py index 0a964ca..ee9f15d 100644 --- a/src/user_cipher.py +++ b/src/user_cipher.py @@ -74,8 +74,8 @@ class UserCipher: customer_cipher: CustomerCipher ) -> EncipheredNKode: passcode_prop_idx_array = np.array(passcode_prop_idx, dtype=np.uint16) - passcode_attrs = np.array([customer_cipher.prop_key[idx] for idx in passcode_prop_idx_array], dtype=np.uint16) - passcode_sets = np.array([customer_cipher.get_prop_set_val(attr) for attr in passcode_attrs], dtype=np.uint16) + passcode_props = np.array([customer_cipher.prop_key[idx] for idx in passcode_prop_idx_array], dtype=np.uint16) + passcode_sets = np.array([customer_cipher.get_prop_set_val(prop) for prop in passcode_props], dtype=np.uint16) mask = self.encipher_mask(passcode_sets.tolist(), customer_cipher) code = self.encipher_salt_hash_code(passcode_prop_idx, customer_cipher) return EncipheredNKode( @@ -90,24 +90,24 @@ class UserCipher: ) -> str: passcode_prop_idx_array = np.array(passcode_prop_idx, dtype=np.uint16) passcode_len = len(passcode_prop_idx_array) - passcode_attrs = np.array([customer_prop.prop_key[idx] for idx in passcode_prop_idx_array], dtype=np.uint16) + passcode_props = np.array([customer_prop.prop_key[idx] for idx in passcode_prop_idx_array], dtype=np.uint16) passcode_cipher = self.pass_key.copy() for idx in range(passcode_len): - attr_idx = passcode_prop_idx_array[idx] - passcode_cipher[idx] = passcode_cipher[idx] ^ self.prop_key[attr_idx] ^ passcode_attrs[idx] + prop_idx = passcode_prop_idx_array[idx] + passcode_cipher[idx] = passcode_cipher[idx] ^ self.prop_key[prop_idx] ^ passcode_props[idx] return self._hash_passcode(passcode_cipher) def encipher_mask( self, passcode_sets: list[int], - customer_attributes: CustomerCipher + customer_properites: CustomerCipher ) -> str: - padded_passcode_sets = self.pad_user_mask(passcode_sets, customer_attributes.set_key) + padded_passcode_sets = self.pad_user_mask(passcode_sets, customer_properites.set_key) # Get indices of set values - set_idx = np.array([customer_attributes.get_set_index(set_val) for set_val in padded_passcode_sets], + set_idx = np.array([customer_properites.get_set_index(set_val) for set_val in padded_passcode_sets], dtype=np.uint16) mask_set_keys = np.array([self.set_key[idx] for idx in set_idx], dtype=np.uint16) diff --git a/src/user_keypad.py b/src/user_keypad.py index f59bb72..a082ebb 100644 --- a/src/user_keypad.py +++ b/src/user_keypad.py @@ -22,13 +22,12 @@ class UserKeypad: raise ValueError("Keypad size is dispersable") self.random_keypad_shuffle() keypad_matrix = self.keypad_matrix() - attr_set_view = keypad_matrix.T - #attr_set_view = secure_fisher_yates_shuffle(attr_set_view) - attr_set_view = np.random.permutation(attr_set_view) - attr_set_view = attr_set_view[:self.keypad_size.numb_of_keys] - keypad_matrix = attr_set_view.reshape(-1)#matrix_transpose(attr_set_view) + prop_set_view = keypad_matrix.T + prop_set_view = np.random.permutation(prop_set_view) + prop_set_view = prop_set_view[:self.keypad_size.numb_of_keys] + keypad_matrix = prop_set_view.reshape(-1) return UserKeypad( - keypad=keypad_matrix.reshape(-1),#matrix_to_list(keypad_matrix), + keypad=keypad_matrix.reshape(-1), keypad_size=KeypadSize( numb_of_keys=self.keypad_size.numb_of_keys, props_per_key=self.keypad_size.numb_of_keys @@ -56,10 +55,10 @@ class UserKeypad: #shuffled_keys = secure_fisher_yates_shuffle(user_keypad_matrix) shuffled_keys = rng.permutation(user_keypad_matrix, axis=0) #prop_rotation = secure_fisher_yates_shuffle(list(range(self.keypad_size.numb_of_keys)))[:self.keypad_size.props_per_key] - attr_rotation = rng.permutation(list(range(self.keypad_size.numb_of_keys)))[:self.keypad_size.props_per_key] + prop_rotation = rng.permutation(list(range(self.keypad_size.numb_of_keys)))[:self.keypad_size.props_per_key] dispersed_keypad = random_property_rotation( shuffled_keys, - attr_rotation.tolist(), + prop_rotation.tolist(), ) self.keypad = dispersed_keypad.reshape(-1) @@ -72,11 +71,11 @@ class UserKeypad: #user_keypad_matrix = self.keypad_matrix() #shuffled_keys = secure_fisher_yates_shuffle(user_keypad_matrix) #keypad_by_sets = [] - #for idx, attrs in enumerate(matrix_transpose(shuffled_keys)): + #for idx, props in enumerate(matrix_transpose(shuffled_keys)): # if idx in selected_sets: - # keypad_by_sets.append(secure_fisher_yates_shuffle(attrs)) + # keypad_by_sets.append(secure_fisher_yates_shuffle(props)) # else: - # keypad_by_sets.append(attrs) + # keypad_by_sets.append(props) #self.keypad = matrix_to_list(matrix_transpose(keypad_by_sets)) pass @@ -85,9 +84,9 @@ class UserKeypad: user_keypad_keypad = self.keypad_matrix() graph = {} for key in user_keypad_keypad: - for attr in key: - graph[attr] = set(key) - graph[attr].remove(attr) + for prop in key: + graph[prop] = set(key) + graph[prop].remove(prop) return graph def get_prop_idx_by_keynumb_setidx(self, key_numb: int, set_idx: int) -> int: @@ -95,5 +94,5 @@ class UserKeypad: raise ValueError(f"key_numb must be between 0 and {self.keypad_size.numb_of_keys - 1}") if not (0 <= set_idx < self.keypad_size.props_per_key): raise ValueError(f"set_idx must be between 0 and {self.keypad_size.props_per_key - 1}") - keypad_attr_idx = self.keypad_matrix() - return int(keypad_attr_idx[key_numb][set_idx]) + keypad_prop_idx = self.keypad_matrix() + return int(keypad_prop_idx[key_numb][set_idx]) diff --git a/src/user_signup_session.py b/src/user_signup_session.py index 26ba1f5..25cb835 100644 --- a/src/user_signup_session.py +++ b/src/user_signup_session.py @@ -19,14 +19,14 @@ class UserSignupSession: def deduce_passcode(self, confirm_key_entry: list[int]) -> list[int]: if not all(0 <= key <= self.keypad_size.numb_of_keys for key in confirm_key_entry): raise ValueError("Key values must be within valid range") - attrs_per_key = self.keypad_size.props_per_key + props_per_key = self.keypad_size.props_per_key set_key_entry = self.set_key_entry if len(set_key_entry) != len(confirm_key_entry): raise ValueError("Key entry lengths must match") set_keypad = self.set_keypad confirm_keypad = self.confirm_keypad - set_key_vals = [set_keypad[key * attrs_per_key:(key + 1) * attrs_per_key] for key in set_key_entry] - confirm_key_vals = [confirm_keypad[key * attrs_per_key:(key + 1) * attrs_per_key] for key in + set_key_vals = [set_keypad[key * props_per_key:(key + 1) * props_per_key] for key in set_key_entry] + confirm_key_vals = [confirm_keypad[key * props_per_key:(key + 1) * props_per_key] for key in confirm_key_entry] passcode = [] for idx in range(len(set_key_entry)): diff --git a/src/utils.py b/src/utils.py index f9e7125..bfb68da 100644 --- a/src/utils.py +++ b/src/utils.py @@ -2,13 +2,13 @@ import numpy as np def random_property_rotation( user_keypad: np.ndarray, - attr_rotation: list[int] + prop_rotation: list[int] ) -> np.ndarray: transposed = user_keypad.T - if len(attr_rotation) != len(transposed): - raise ValueError("prop_rotation must be the same length as the number of attributes") - for idx, attr_set in enumerate(transposed): - rotation = attr_rotation[idx] - rotation = rotation % len(attr_set) if len(attr_set) > 0 else 0 - transposed[idx] = np.roll(attr_set, rotation) + if len(prop_rotation) != len(transposed): + raise ValueError("prop_rotation must be the same length as the number of properties") + for idx, prop_set in enumerate(transposed): + rotation = prop_rotation[idx] + rotation = rotation % len(prop_set) if len(prop_set) > 0 else 0 + transposed[idx] = np.roll(prop_set, rotation) return transposed.T diff --git a/test/test_nkode_api.py b/test/test_nkode_api.py index 341bd88..58f6f07 100644 --- a/test/test_nkode_api.py +++ b/test/test_nkode_api.py @@ -20,7 +20,7 @@ def test_create_new_user_and_renew_keys(nkode_api, keypad_size, passocode_len): session_id, set_keypad = nkode_api.generate_signup_keypad(customer_id) user_passcode = set_keypad[:passocode_len] - signup_key_selection = lambda keypad: [int(np.where(keypad == attr)[0][0]) // keypad_size.numb_of_keys for attr in user_passcode] + signup_key_selection = lambda keypad: [int(np.where(keypad == prop)[0][0]) // keypad_size.numb_of_keys for prop in user_passcode] set_key_selection = signup_key_selection(set_keypad) confirm_keypad = nkode_api.set_nkode(username, customer_id, set_key_selection, session_id) @@ -33,7 +33,7 @@ def test_create_new_user_and_renew_keys(nkode_api, keypad_size, passocode_len): ) assert successful_confirm - sign_in_key_selection = lambda keypad: [int(np.where(keypad ==attr)[0][0]) // keypad_size.props_per_key for attr in user_passcode] + sign_in_key_selection = lambda keypad: [int(np.where(keypad ==prop)[0][0]) // keypad_size.props_per_key for prop in user_passcode] login_keypad = nkode_api.get_login_keypad(username, customer_id) login_key_selection = sign_in_key_selection(login_keypad) successful_login = nkode_api.login(customer_id, username, login_key_selection) diff --git a/test/test_nkode_interface.py b/test/test_nkode_interface.py index b56908c..8dce0fb 100644 --- a/test/test_nkode_interface.py +++ b/test/test_nkode_interface.py @@ -7,9 +7,9 @@ from src.models import KeypadSize "keypad_size", [KeypadSize(numb_of_keys=10, props_per_key=11)] ) -def test_attr_set_idx(keypad_size): +def test_prop_set_idx(keypad_size): user_keypad = UserKeypad.create(keypad_size) - for attr_idx in range(keypad_size.numb_of_props): - user_keypad_idx = user_keypad.keypad[attr_idx] + for prop_idx in range(keypad_size.numb_of_props): + user_keypad_idx = user_keypad.keypad[prop_idx] - assert (attr_idx % keypad_size.props_per_key == user_keypad_idx % keypad_size.props_per_key) + assert (prop_idx % keypad_size.props_per_key == user_keypad_idx % keypad_size.props_per_key) diff --git a/test/test_user_cipher_keys.py b/test/test_user_cipher_keys.py index 9f82e66..3671420 100644 --- a/test/test_user_cipher_keys.py +++ b/test/test_user_cipher_keys.py @@ -35,7 +35,7 @@ def test_decode_mask(keypad_size, max_nkode_len): user_keys = UserCipher.create(keypad_size, set_vals, max_nkode_len) passcode = user_keys.encipher_nkode(passcode_entry, customer) - orig_passcode_set_vals = [customer.get_prop_set_val(attr) for attr in passcode_values] + orig_passcode_set_vals = [customer.get_prop_set_val(prop) for prop in passcode_values] passcode_set_vals = user_keys.decipher_mask(passcode.mask, set_vals, len(passcode_entry)) assert (len(passcode_set_vals) == len(orig_passcode_set_vals)) assert (all(orig_passcode_set_vals[idx] == passcode_set_vals[idx] for idx in range(len(passcode_set_vals)))) diff --git a/test/test_user_keypad.py b/test/test_user_keypad.py index 811c1f4..b7deadf 100644 --- a/test/test_user_keypad.py +++ b/test/test_user_keypad.py @@ -13,15 +13,15 @@ def test_dispersion(user_keypad): pre_dispersion_graph = user_keypad.property_adjacency_graph() user_keypad.disperse_keypad() post_dispersion_graph = user_keypad.property_adjacency_graph() - for attr, adj_graph in pre_dispersion_graph.items(): - assert (adj_graph.isdisjoint(post_dispersion_graph[attr])) + for prop, adj_graph in pre_dispersion_graph.items(): + assert (adj_graph.isdisjoint(post_dispersion_graph[prop])) -#def test_shuffle_attrs(user_keypad): +#def test_shuffle_props(user_keypad): # """there's no easy way to test this. At some point we'll have to run this code thousands of time to see if we get # expected statistical outcomes like: -# - every attribute gets to every key with a uniform distribution -# - every attribute is adjacent to every other attribute with uniform distribution +# - every property gets to every key with a uniform distribution +# - every property is adjacent to every other property with uniform distribution # - the order in which the cipher move from key to key is random (i.e. the distance traveled is uniform) # """ # pre_shuffle_keypad = user_keypad.keypad