From f9f7081fc62b19dc2f3b9615828ea47338b31453 Mon Sep 17 00:00:00 2001 From: Donovan Date: Mon, 17 Mar 2025 04:31:01 -0500 Subject: [PATCH] fix split shuffle --- notebooks/nkode_tutorial.ipynb | 17 ++++++++++++----- src/user_keypad.py | 6 +++++- test/test_user_keypad.py | 5 ++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/notebooks/nkode_tutorial.ipynb b/notebooks/nkode_tutorial.ipynb index 513645a..b67acaa 100644 --- a/notebooks/nkode_tutorial.ipynb +++ b/notebooks/nkode_tutorial.ipynb @@ -205,12 +205,14 @@ "source": [ "### User Signup\n", "To create a new must call this endpoints in order:\n", - "1. Generate Keypad\n", - "2. Set User NKode\n", - "3. Confirm User NKode\n", + "1. Generate a randomly shuffled keypad\n", + "2. Set user nKode\n", + "3. Confirm user nKode\n", "\n", "#### Generate Keypad\n", - " For the server to determine the users nkode, the user's keypad must be dispersable. To make the keypad dispersable, the server will randomly drop properties sets to the number of properties is equal to the number of keys. In our case, the server drops 1 properties set to give us a 5 X 5 keypad with possible index values ranging from 0-29.\n", + " For the server to determine the users nkode, the user's keypad must be dispersable.\n", + " To make the keypad dispersable, the server will randomly drop properties sets to the number of properties is equal to the number of keys.\n", + " In our case, the server drops 1 properties set to give us a 5 X 5 keypad with possible index values ranging from 0-29.\n", " - Run the cell below over and over to see it change. Notice that values never move out of their columns just their rows.\n", " - each value in the keypad is the index value of a customer properties\n", " - the user never learns what their \"real\" properties is. All they do is specify an index in the customer keypad\n" @@ -248,7 +250,12 @@ "cell_type": "markdown", "source": [ "#### Set nKode\n", - "The user identifies properties in the keypad they want in their nkode. Each properties in the gui has an index value. Below the user has selected 16, 9, 6, 19. Graphically represent with anything. The only requirement is that the graphical properties must be associated with the same index value everytime the user goes to login. If the user wants to change anything about their keypad(the number of keys, properties or graphical properties), they must also change their nkode." + "The user identifies properties in the keypad they want in their nkode.\n", + "Each properties in the gui has an index value.\n", + "Below the user has selected 16, 9, 6, 19.\n", + "Graphically represent with anything.\n", + "The only requirement is that the graphical properties must be associated with the same index value everytime the user goes to login.\n", + "If the user wants to change anything about their keypad(the number of keys, properties or graphical properties), they must also change their nkode." ] }, { diff --git a/src/user_keypad.py b/src/user_keypad.py index ffcb0b6..da0d750 100644 --- a/src/user_keypad.py +++ b/src/user_keypad.py @@ -63,9 +63,13 @@ class UserKeypad: self.keypad = dispersed_keypad.reshape(-1) def split_shuffle(self): + # shuffle all keys + keypad_view = self.keypad_matrix() + np.random.shuffle(keypad_view) + # select half the property sets prop_permutation = np.random.permutation(self.keypad_size.props_per_key)[: self.keypad_size.props_per_key // 2] key_permutation = np.random.permutation(self.keypad_size.numb_of_keys) - keypad_view = self.keypad_matrix().copy() + # shuffle the selected property sets to new keys as a group keypad_view[:, prop_permutation] = keypad_view[key_permutation, :][:, prop_permutation] self.keypad = keypad_view.reshape(-1) diff --git a/test/test_user_keypad.py b/test/test_user_keypad.py index f7bb7a6..5f8fd6e 100644 --- a/test/test_user_keypad.py +++ b/test/test_user_keypad.py @@ -5,7 +5,7 @@ from src.models import KeypadSize @pytest.fixture() def user_keypad(): - return UserKeypad.create(keypad_size=KeypadSize(props_per_key=8, numb_of_keys=8)) + return UserKeypad.create(keypad_size=KeypadSize(props_per_key=5, numb_of_keys=5)) def test_dispersion(user_keypad): @@ -25,8 +25,11 @@ def test_shuffle_props(user_keypad): - 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.copy() + print("") + print(user_keypad.keypad_matrix()) user_keypad.split_shuffle() post_shuffle_keypad = user_keypad.keypad.copy() + print(user_keypad.keypad_matrix()) assert (not all( post_shuffle_keypad[idx] == pre_shuffle_keypad[idx] for idx in range(len(post_shuffle_keypad)) ))