refactor customer interface to attributes
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from uuid import UUID
|
||||
from pydantic import BaseModel
|
||||
from src.customer_interface import CustomerInterface
|
||||
from src.customer_attributes import CustomerAttributes
|
||||
from src.models import NKodePolicy
|
||||
from src.user import User
|
||||
from src.utils import xor_lists
|
||||
@@ -9,7 +9,7 @@ from src.utils import xor_lists
|
||||
class Customer(BaseModel):
|
||||
customer_id: UUID
|
||||
nkode_policy: NKodePolicy
|
||||
interface: CustomerInterface
|
||||
attributes: CustomerAttributes
|
||||
users: dict[str, User]
|
||||
|
||||
def add_new_user(self, user: User):
|
||||
@@ -17,13 +17,13 @@ class Customer(BaseModel):
|
||||
|
||||
def valid_key_entry(self, username, selected_keys) -> bool:
|
||||
assert (username in self.users.keys())
|
||||
assert (all(key_idx < self.interface.keypad_size.numb_of_keys for key_idx in selected_keys))
|
||||
assert (all(key_idx < self.attributes.keypad_size.numb_of_keys for key_idx in selected_keys))
|
||||
passcode_len = len(selected_keys)
|
||||
user = self.users[username]
|
||||
|
||||
passcode_set_vals = user.user_keys.decipher_mask(
|
||||
user.enciphered_passcode.mask, self.interface.set_vals, len(selected_keys))
|
||||
set_vals_idx = [self.interface.get_set_index(set_val) for set_val in passcode_set_vals]
|
||||
user.enciphered_passcode.mask, self.attributes.set_vals, len(selected_keys))
|
||||
set_vals_idx = [self.attributes.get_set_index(set_val) for set_val in passcode_set_vals]
|
||||
|
||||
presumed_selected_attributes = []
|
||||
presumed_selected_attributes_idx = []
|
||||
@@ -36,20 +36,20 @@ class Customer(BaseModel):
|
||||
presumed_selected_attributes_idx.append(selected_attr_idx)
|
||||
presumed_selected_attributes.append(selected_attr_idx)
|
||||
|
||||
enciphered_attr = user.user_keys.encipher_salt_hash_code(presumed_selected_attributes, self.interface)
|
||||
enciphered_attr = user.user_keys.encipher_salt_hash_code(presumed_selected_attributes, self.attributes)
|
||||
if enciphered_attr != user.enciphered_passcode.code:
|
||||
return False
|
||||
|
||||
if user.renew:
|
||||
user.refresh_passcode(presumed_selected_attributes_idx, self.interface)
|
||||
user.refresh_passcode(presumed_selected_attributes_idx, self.attributes)
|
||||
return True
|
||||
|
||||
def renew_keys(self) -> bool:
|
||||
attrs_before = self.interface.attr_vals.copy()
|
||||
sets_before = self.interface.set_vals.copy()
|
||||
self.interface.renew_interface()
|
||||
attrs_after = self.interface.attr_vals
|
||||
sets_after = self.interface.set_vals
|
||||
attrs_before = self.attributes.attr_vals.copy()
|
||||
sets_before = self.attributes.set_vals.copy()
|
||||
self.attributes.renew()
|
||||
attrs_after = self.attributes.attr_vals
|
||||
sets_after = self.attributes.set_vals
|
||||
|
||||
attrs_xor = xor_lists(attrs_after, attrs_before)
|
||||
set_xor = xor_lists(sets_after, sets_before)
|
||||
@@ -60,7 +60,7 @@ class Customer(BaseModel):
|
||||
def valid_new_nkode(self, passcode_attr_idx: list[int]) -> bool:
|
||||
nkode_len = len(passcode_attr_idx)
|
||||
passcode_set_values = [
|
||||
self.interface.get_attr_set_val(self.interface.attr_vals[attr_idx]) for attr_idx in passcode_attr_idx
|
||||
self.attributes.get_attr_set_val(self.attributes.attr_vals[attr_idx]) for attr_idx in passcode_attr_idx
|
||||
]
|
||||
distinct_sets = len(set(passcode_set_values))
|
||||
distinct_attributes = len(set(passcode_attr_idx))
|
||||
|
||||
@@ -4,7 +4,7 @@ from src.models import KeypadSize
|
||||
from src.utils import generate_random_nonrepeating_list
|
||||
|
||||
|
||||
class CustomerInterface(BaseModel):
|
||||
class CustomerAttributes(BaseModel):
|
||||
attr_vals: list[int]
|
||||
set_vals: list[int]
|
||||
keypad_size: KeypadSize
|
||||
@@ -14,13 +14,13 @@ class CustomerInterface(BaseModel):
|
||||
assert (keypad_size.numb_of_keys <= 256)
|
||||
assert (keypad_size.attrs_per_key <= 256)
|
||||
|
||||
return CustomerInterface(
|
||||
return CustomerAttributes(
|
||||
attr_vals=generate_random_nonrepeating_list(keypad_size.numb_of_attrs),
|
||||
set_vals=generate_random_nonrepeating_list(keypad_size.attrs_per_key),
|
||||
keypad_size=keypad_size,
|
||||
)
|
||||
|
||||
def renew_interface(self):
|
||||
def renew(self):
|
||||
self.attr_vals = generate_random_nonrepeating_list(self.keypad_size.numb_of_attrs)
|
||||
self.set_vals = generate_random_nonrepeating_list(self.keypad_size.attrs_per_key)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
from src.models import EncipheredNKode
|
||||
from src.customer_interface import CustomerInterface
|
||||
from src.customer_attributes import CustomerAttributes
|
||||
from src.user_cipher_keys import UserCipherKeys
|
||||
from src.user_interface import UserInterface
|
||||
from src.utils import xor_lists
|
||||
@@ -19,7 +19,7 @@ class User(BaseModel):
|
||||
self.user_keys.set_key = xor_lists(self.user_keys.set_key, sets_xor)
|
||||
self.user_keys.alpha_key = xor_lists(self.user_keys.alpha_key, attrs_xor)
|
||||
|
||||
def refresh_passcode(self, passcode_attr_idx: list[int], customer_interface: CustomerInterface):
|
||||
def refresh_passcode(self, passcode_attr_idx: list[int], customer_interface: CustomerAttributes):
|
||||
self.user_keys = UserCipherKeys.new(
|
||||
customer_interface.keypad_size,
|
||||
customer_interface.set_vals,
|
||||
|
||||
@@ -5,7 +5,7 @@ from secrets import choice
|
||||
from pydantic import BaseModel
|
||||
|
||||
from src.models import EncipheredNKode, KeypadSize
|
||||
from src.customer_interface import CustomerInterface
|
||||
from src.customer_attributes import CustomerAttributes
|
||||
from src.utils import generate_random_nonrepeating_list, xor_lists, int_array_to_bytes
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ class UserCipherKeys(BaseModel):
|
||||
def encipher_nkode(
|
||||
self,
|
||||
passcode_attr_idx: list[int],
|
||||
customer_interface: CustomerInterface
|
||||
customer_interface: CustomerAttributes
|
||||
) -> EncipheredNKode:
|
||||
|
||||
passcode_attrs = [customer_interface.attr_vals[idx] for idx in passcode_attr_idx]
|
||||
@@ -78,7 +78,7 @@ class UserCipherKeys(BaseModel):
|
||||
def encipher_salt_hash_code(
|
||||
self,
|
||||
passcode_attr_idx: list[int],
|
||||
customer_interface: CustomerInterface,
|
||||
customer_interface: CustomerAttributes,
|
||||
) -> str:
|
||||
passcode_len = len(passcode_attr_idx)
|
||||
passcode_attrs = [customer_interface.attr_vals[idx] for idx in passcode_attr_idx]
|
||||
@@ -95,7 +95,7 @@ class UserCipherKeys(BaseModel):
|
||||
def encipher_mask(
|
||||
self,
|
||||
passcode_sets: list[int],
|
||||
customer_interface: CustomerInterface
|
||||
customer_interface: CustomerAttributes
|
||||
) -> str:
|
||||
padded_passcode_sets = self.pad_user_mask(passcode_sets, customer_interface.set_vals)
|
||||
set_idx = [customer_interface.get_set_index(set_val) for set_val in padded_passcode_sets]
|
||||
|
||||
@@ -6,14 +6,14 @@ from src.utils import list_to_matrix, secure_fisher_yates_shuffle, matrix_to_lis
|
||||
|
||||
|
||||
class UserInterface(BaseModel):
|
||||
attr_indices: list[int]
|
||||
interface: list[int]
|
||||
keypad_size: KeypadSize
|
||||
|
||||
@staticmethod
|
||||
def new(keypad_size: KeypadSize):
|
||||
# Todo: this a hack do a proper random interface
|
||||
interface = UserInterface(
|
||||
attr_indices=list(range(keypad_size.numb_of_attrs)),
|
||||
interface=list(range(keypad_size.numb_of_attrs)),
|
||||
keypad_size=keypad_size
|
||||
)
|
||||
interface.disperse_interface()
|
||||
@@ -22,18 +22,22 @@ class UserInterface(BaseModel):
|
||||
return interface
|
||||
|
||||
def disperse_interface(self):
|
||||
user_interface_matrix = list_to_matrix(self.attr_indices, self.keypad_size.attrs_per_key)
|
||||
user_interface_matrix = list_to_matrix(self.interface, self.keypad_size.attrs_per_key)
|
||||
shuffled_keys = secure_fisher_yates_shuffle(user_interface_matrix)
|
||||
dispersed_interface = self._random_attribute_rotation(shuffled_keys, list(range(self.keypad_size.attrs_per_key)))
|
||||
self.attr_indices = matrix_to_list(dispersed_interface)
|
||||
|
||||
attr_rotation = secure_fisher_yates_shuffle(list(range(self.keypad_size.numb_of_keys)))[:self.keypad_size.attrs_per_key]
|
||||
dispersed_interface = self.random_attribute_rotation(
|
||||
shuffled_keys,
|
||||
attr_rotation,
|
||||
)
|
||||
self.interface = matrix_to_list(dispersed_interface)
|
||||
|
||||
def shuffle_interface(self):
|
||||
"""just like dispersion but only half the sets are rotated"""
|
||||
numb_of_selected_sets = self.keypad_size.attrs_per_key // 2
|
||||
# randomly shuffle half the sets. if attrs_per_key is odd, randomly add one 50% of the time
|
||||
numb_of_selected_sets += choice([0, 1]) if (self.keypad_size.attrs_per_key & 1) == 1 else 0
|
||||
selected_sets = secure_fisher_yates_shuffle(list(range(self.keypad_size.attrs_per_key)))[:numb_of_selected_sets]
|
||||
user_interface_matrix = list_to_matrix(self.attr_indices, self.keypad_size.attrs_per_key)
|
||||
user_interface_matrix = list_to_matrix(self.interface, self.keypad_size.attrs_per_key)
|
||||
shuffled_keys = secure_fisher_yates_shuffle(user_interface_matrix)
|
||||
interface_by_sets = []
|
||||
for idx, attrs in enumerate(matrix_transpose(shuffled_keys)):
|
||||
@@ -41,21 +45,22 @@ class UserInterface(BaseModel):
|
||||
interface_by_sets.append(secure_fisher_yates_shuffle(attrs))
|
||||
else:
|
||||
interface_by_sets.append(attrs)
|
||||
self.attr_indices = matrix_to_list(matrix_transpose(interface_by_sets))
|
||||
self.interface = matrix_to_list(matrix_transpose(interface_by_sets))
|
||||
|
||||
def _random_attribute_rotation(self, user_interface: list[list[int]], selected_sets: list[int]) -> list[list[int]]:
|
||||
attr_rotation = secure_fisher_yates_shuffle(list(range(self.keypad_size.numb_of_keys)))[:self.keypad_size.attrs_per_key]
|
||||
@staticmethod
|
||||
def random_attribute_rotation(
|
||||
user_interface: list[list[int]],
|
||||
attr_rotation: list[int]
|
||||
) -> list[list[int]]:
|
||||
transposed_user_interface = matrix_transpose(user_interface)
|
||||
assert (len(attr_rotation) == len(transposed_user_interface))
|
||||
for idx, attr_set in enumerate(transposed_user_interface):
|
||||
if idx not in selected_sets:
|
||||
continue
|
||||
rotation = attr_rotation[idx]
|
||||
transposed_user_interface[idx] = attr_set[rotation:] + attr_set[:rotation]
|
||||
return matrix_transpose(transposed_user_interface)
|
||||
|
||||
def attribute_adjacency_graph(self) -> dict[int, set[int]]:
|
||||
user_interface_keypad = list_to_matrix(self.attr_indices, self.keypad_size.attrs_per_key)
|
||||
user_interface_keypad = list_to_matrix(self.interface, self.keypad_size.attrs_per_key)
|
||||
graph = {}
|
||||
for key in user_interface_keypad:
|
||||
for attr in key:
|
||||
@@ -66,5 +71,5 @@ class UserInterface(BaseModel):
|
||||
|
||||
def get_key_attr_idxs(self, key_numb: int) -> list[int]:
|
||||
assert (0 <= key_numb < self.keypad_size.numb_of_keys)
|
||||
keypad_attr_idx = list_to_matrix(self.attr_indices, self.keypad_size.attrs_per_key)
|
||||
keypad_attr_idx = list_to_matrix(self.interface, self.keypad_size.attrs_per_key)
|
||||
return keypad_attr_idx[key_numb]
|
||||
|
||||
@@ -39,11 +39,11 @@ class UserSignupSession(BaseModel):
|
||||
def set_user_nkode(self, username: str, key_selection: list[int]):
|
||||
assert (all(0 <= key <= self.keypad_size.numb_of_keys for key in key_selection))
|
||||
set_interface = UserInterface(
|
||||
attr_indices=self.set_interface,
|
||||
interface=self.set_interface,
|
||||
keypad_size=self.keypad_size
|
||||
)
|
||||
set_interface.disperse_interface()
|
||||
self.username = username
|
||||
self.set_key_entry = key_selection
|
||||
self.confirm_interface = set_interface.attr_indices
|
||||
self.confirm_interface = set_interface.interface
|
||||
|
||||
|
||||
Reference in New Issue
Block a user