refactor set_key -> position_key

This commit is contained in:
2025-03-20 04:01:15 -05:00
parent 7b92a6b40b
commit 65f3559ef0
8 changed files with 241 additions and 247 deletions

View File

@@ -9,30 +9,31 @@ from src.customer_cipher import CustomerCipher
@dataclass
class UserCipher:
prop_key: np.ndarray
combined_set_key: np.ndarray
property_key: np.ndarray
combined_position_key: np.ndarray
pass_key: np.ndarray
mask_key: np.ndarray
max_nkode_len: int
@classmethod
def create(cls, keypad_size: KeypadSize, customer_set_key: np.ndarray, max_nkode_len: int) -> 'UserCipher':
if len(customer_set_key) != keypad_size.props_per_key:
raise ValueError("Invalid set values")
user_set_key = np.random.choice(2**16,size=keypad_size.props_per_key, replace=False)
def create(cls, keypad_size: KeypadSize, customer_pos_key: np.ndarray, max_nkode_len: int) -> 'UserCipher':
if len(customer_pos_key) != keypad_size.props_per_key:
raise ValueError("Invalid position values")
user_pos_key = np.random.choice(2**16,size=keypad_size.props_per_key, replace=False)
return UserCipher(
prop_key=np.random.choice(2 ** 16, size=keypad_size.total_props, replace=False),
property_key=np.random.choice(2 ** 16, size=keypad_size.total_props, replace=False),
pass_key=np.random.choice(2 ** 16, size=max_nkode_len, replace=False),
mask_key=np.random.choice(2**16, size=max_nkode_len, replace=False),
combined_set_key=user_set_key ^ customer_set_key,
combined_position_key=user_pos_key ^ customer_pos_key,
max_nkode_len=max_nkode_len
)
def pad_user_mask(self, user_mask: np.ndarray, set_vals: np.ndarray) -> np.ndarray:
def pad_user_mask(self, user_mask: np.ndarray, pos_vals: np.ndarray) -> np.ndarray:
# TODO: replace with new method
if len(user_mask) >= self.max_nkode_len:
raise ValueError("User encoded_mask is too long")
padding_size = self.max_nkode_len - len(user_mask)
padding = np.random.choice(set_vals, size=padding_size, replace=True).astype(np.uint16)
padding = np.random.choice(pos_vals, size=padding_size, replace=True).astype(np.uint16)
return np.concatenate([user_mask, padding])
@staticmethod
@@ -90,7 +91,7 @@ class UserCipher:
passcode_cipher = self.pass_key.copy()
passcode_cipher[:passcode_len] = (
passcode_cipher[:passcode_len] ^
self.prop_key[passcode_prop_idx] ^
self.property_key[passcode_prop_idx] ^
customer_prop.property_key[passcode_prop_idx]
)
return passcode_cipher.astype(np.uint16).tobytes()
@@ -100,21 +101,21 @@ class UserCipher:
passcode_prop_idx: list[int],
customer_cipher: CustomerCipher
) -> str:
set_idxs = customer_cipher.get_passcode_position_indices_padded(passcode_prop_idx, len(self.mask_key))
ordered_set_key = self.combined_set_key[set_idxs]
ordered_customer_key = customer_cipher.position_key[set_idxs]
mask = ordered_set_key ^ ordered_customer_key ^ self.mask_key
pos_idxs = customer_cipher.get_passcode_position_indices_padded(passcode_prop_idx, len(self.mask_key))
ordered_pos_key = self.combined_position_key[pos_idxs]
ordered_customer_pos_key = customer_cipher.position_key[pos_idxs]
mask = ordered_pos_key ^ ordered_customer_pos_key ^ self.mask_key
encoded_mask = self.encode_base64_str(mask)
return encoded_mask
def decipher_mask(self, encoded_mask: str, customer_set_key: np.ndarray, passcode_len: int) -> list[int]:
def decipher_mask(self, encoded_mask: str, customer_pos_key: np.ndarray, passcode_len: int) -> list[int]:
mask = self.decode_base64_str(encoded_mask)
# user_set_key ordered by the user's nkode and padded to be length max_nkode_len
ordered_set_key = mask ^ self.mask_key
user_set_key = customer_set_key ^ self.combined_set_key
passcode_sets = []
for partial_set in ordered_set_key[:passcode_len]:
set_idx = np.where(user_set_key == partial_set)[0][0]
passcode_sets.append(int(customer_set_key[set_idx]))
return passcode_sets
# user_pos_key ordered by the user's nkode and padded to be length max_nkode_len
ordered_user_pos_key = mask ^ self.mask_key
user_pos_key = customer_pos_key ^ self.combined_position_key
passcode_position = []
for position_val in ordered_user_pos_key[:passcode_len]:
position_idx = np.where(user_pos_key == position_val)[0][0]
passcode_position.append(int(customer_pos_key[position_idx]))
return passcode_position