initial commit
This commit is contained in:
90
src/utils.py
Normal file
90
src/utils.py
Normal file
@@ -0,0 +1,90 @@
|
||||
import secrets
|
||||
|
||||
|
||||
def random_number(min_val: int, max_val: int) -> int:
|
||||
""" originally secure_rand """
|
||||
return min_val + secrets.randbelow(max_val - min_val + 1)
|
||||
|
||||
|
||||
def random_shuffle(array: list[int]) -> list[int]:
|
||||
for i in range(len(array) - 1, 0, -1):
|
||||
j = random_number(0, i)
|
||||
array[i], array[j] = array[j], array[i]
|
||||
return array
|
||||
|
||||
|
||||
def mask_array(array_len: int) -> list[int]:
|
||||
new_array = []
|
||||
for i in range(array_len):
|
||||
random_val = random_number(0, array_len)
|
||||
new_array.append(random_val)
|
||||
return new_array
|
||||
|
||||
|
||||
def build_shuffle_matrix(row_length: int, column_length: int) -> list[list[int]]:
|
||||
""" originally buildRandom """
|
||||
output = []
|
||||
for i in range(column_length):
|
||||
output.append(random_shuffle(list(range(row_length))))
|
||||
|
||||
return output
|
||||
|
||||
|
||||
def build_mask_matrix(row_length: int, column_length: int, max_value: int) -> list[list[int]]:
|
||||
""" originally buildScramble """
|
||||
output = []
|
||||
for i in range(column_length):
|
||||
row = [random_number(0, max_value - 1) for _ in range(row_length)]
|
||||
output.append(row)
|
||||
return output
|
||||
|
||||
|
||||
def mapped_transform(operand: list[list[int]], function_map: list[list[int]]):
|
||||
""" originally transpose """
|
||||
assert len(operand) == len(function_map)
|
||||
assert len(operand[0]) == len(function_map[0])
|
||||
|
||||
output = []
|
||||
for i, el in enumerate(function_map):
|
||||
row = []
|
||||
for j, el2 in enumerate(el):
|
||||
row.append(operand[el2][j])
|
||||
output.append(row)
|
||||
return output
|
||||
|
||||
|
||||
def chain_map_transform(functions: list[list[list[int]]]) -> list[list[int]]:
|
||||
""" originally chainMap """
|
||||
output = functions[0]
|
||||
for i in range(1, len(functions)):
|
||||
output = mapped_transform(output, functions[i])
|
||||
return output
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
alphabet = "abcd"
|
||||
bytes_per_char = 4
|
||||
alphabet_len = len(alphabet)
|
||||
|
||||
functionKey = build_shuffle_matrix(bytes_per_char, alphabet_len)
|
||||
functionEphemeralKey = build_shuffle_matrix(bytes_per_char, alphabet_len)
|
||||
|
||||
outerFunctionFunctionKey = build_shuffle_matrix(alphabet_len, 1)
|
||||
outerFunctionFunctionEphemeralKey = build_shuffle_matrix(alphabet_len, 1)
|
||||
|
||||
outerServerShuffleKey = build_shuffle_matrix(alphabet_len, 1)
|
||||
outerServerShuffleEphemeralKey = build_shuffle_matrix(alphabet_len, 1)
|
||||
|
||||
serverKey = build_mask_matrix(bytes_per_char, alphabet_len, 255)
|
||||
serverEphemeralKey = build_mask_matrix(bytes_per_char, alphabet_len, 255)
|
||||
|
||||
interfaceConfig = build_mask_matrix(bytes_per_char, alphabet_len, alphabet_len)
|
||||
|
||||
outerFunctionFunctionEphemeralKeyApplied = mapped_transform(outerFunctionFunctionKey,
|
||||
outerFunctionFunctionEphemeralKey)
|
||||
|
||||
outerServerShuffleEphemeralKeyApplied = mapped_transform(outerServerShuffleKey, outerServerShuffleEphemeralKey)
|
||||
|
||||
inputData = mapped_transform(interfaceConfig, outerServerShuffleEphemeralKeyApplied)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user