refactor use numpy in user_cipher.py

This commit is contained in:
2025-03-11 10:33:08 -05:00
parent dd0b496a21
commit 526e537586
7 changed files with 230 additions and 177 deletions

View File

@@ -9,7 +9,7 @@ from src.utils import xor_lists
class Customer:
customer_id: UUID
nkode_policy: NKodePolicy
customer_cipher: CustomerCipher
cipher: CustomerCipher
users: dict[str, User]
# TODO: validate policy and keypad size don't conflict
@@ -23,16 +23,16 @@ class Customer:
if username not in self.users:
raise ValueError(f"User '{username}' does not exist")
numb_of_keys = self.customer_cipher.keypad_size.numb_of_keys
numb_of_keys = self.cipher.keypad_size.numb_of_keys
if not all(0 <= key_idx < numb_of_keys for key_idx in selected_keys):
raise ValueError(f"Invalid key indices. Must be between 0 and {numb_of_keys - 1}")
passcode_len = len(selected_keys)
user = self.users[username]
passcode_set_vals = user.user_keys.decipher_mask(
user.enciphered_passcode.mask, self.customer_cipher.set_key, passcode_len)
set_vals_idx = [self.customer_cipher.get_set_index(set_val) for set_val in passcode_set_vals]
passcode_set_vals = user.cipher.decipher_mask(
user.enciphered_passcode.mask, self.cipher.set_key, passcode_len)
set_vals_idx = [self.cipher.get_set_index(set_val) for set_val in passcode_set_vals]
presumed_selected_attributes_idx = []
for idx in range(passcode_len):
@@ -41,20 +41,20 @@ class Customer:
selected_attr_idx = user.user_keypad.get_attr_idx_by_keynumb_setidx(key_numb, set_idx)
presumed_selected_attributes_idx.append(selected_attr_idx)
enciphered_attr = user.user_keys.encipher_salt_hash_code(presumed_selected_attributes_idx, self.customer_cipher)
enciphered_attr = user.cipher.encipher_salt_hash_code(presumed_selected_attributes_idx, self.cipher)
if enciphered_attr != user.enciphered_passcode.code:
return False
if user.renew:
user.refresh_passcode(presumed_selected_attributes_idx, self.customer_cipher)
user.refresh_passcode(presumed_selected_attributes_idx, self.cipher)
return True
def renew_keys(self) -> bool:
old_attrs = self.customer_cipher.prop_key.copy()
old_sets = self.customer_cipher.set_key.copy()
self.customer_cipher.renew()
new_attrs = self.customer_cipher.prop_key
new_sets = self.customer_cipher.set_key
old_attrs = self.cipher.prop_key.copy()
old_sets = self.cipher.set_key.copy()
self.cipher.renew()
new_attrs = self.cipher.prop_key
new_sets = self.cipher.set_key
attrs_xor = xor_lists(new_attrs, old_attrs)
set_xor = xor_lists(new_sets, old_sets)
@@ -66,7 +66,7 @@ class Customer:
def valid_new_nkode(self, passcode_attr_idx: list[int]) -> bool:
nkode_len = len(passcode_attr_idx)
passcode_set_values = [
self.customer_cipher.get_prop_set_val(self.customer_cipher.prop_key[attr_idx]) for attr_idx in passcode_attr_idx
self.cipher.get_prop_set_val(self.cipher.prop_key[attr_idx]) for attr_idx in passcode_attr_idx
]
distinct_sets = len(set(passcode_set_values))
distinct_attributes = len(set(passcode_attr_idx))