Skip to content

Commit

Permalink
Added fix for trail search and vectorized evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgerault committed Apr 28, 2024
1 parent 0fcf20c commit ee0b981
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
8 changes: 6 additions & 2 deletions claasp/cipher_modules/generic_functions_vectorized_bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def bit_vector_select_word(input, bits, verbosity=False):
return output


def bit_vector_SBOX(input, sbox, verbosity=False):
def bit_vector_SBOX(input, sbox, verbosity=False, output_bit_size = None):
"""
Computes the SBox operation on binary values.
Expand All @@ -95,6 +95,10 @@ def bit_vector_SBOX(input, sbox, verbosity=False):
int_val = np.packbits(tmp, axis=0)
int_output = sbox[int_val]
output = np.unpackbits(int_output, axis=0)
if output_bit_size is None:
output = output[-input.shape[0]:]
else:
output = output[-output_bit_size:]
if verbosity:
print("SBox")
print("Input : ", input.transpose())
Expand All @@ -103,7 +107,7 @@ def bit_vector_SBOX(input, sbox, verbosity=False):
print("Output : ", output.transpose())
print("---")

return output[-input.shape[0]:]
return output


def bit_vector_XOR(input, number_of_inputs, output_bit_size, verbosity=False):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,9 @@ def update_sbox_lat_valid_probabilities(self, component, valid_probabilities):
for i in range(sbox_lat.nrows()):
set_of_occurrences = set(sbox_lat.rows()[i])
set_of_occurrences -= {0}
valid_probabilities.update({round(100 * math.log2(2 ** input_size / abs(occurrence)))
for occurrence in set_of_occurrences})
valid_probabilities.update(
{round(100 * math.log2(abs(pow(2, input_size - 1) / occurence))) for occurence in
set_of_occurrences})
self.sbox_mant.append((description, output_id_link))

def weight_xor_linear_constraints(self, weight):
Expand Down
5 changes: 2 additions & 3 deletions claasp/components/sbox_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ def cp_update_lat_valid_probabilities(component, valid_probabilities, sbox_mant)
for i in range(sbox_lat.nrows()):
set_of_occurrences = set(sbox_lat.rows()[i])
set_of_occurrences -= {0}
valid_probabilities.update({round(100 * math.log2(2 ** input_size / abs(occurrence)))
for occurrence in set_of_occurrences})
valid_probabilities.update({round(100 * math.log2(abs(pow(2, input_size - 1) / occurence))) for occurence in set_of_occurrences})
sbox_mant.append((description, output_id_link))


Expand Down Expand Up @@ -698,7 +697,7 @@ def get_bit_based_vectorized_python_code(self, params, convert_output_to_bytes):
sbox_params = [f'bit_vector_select_word({self.input_id_links[i]}, {self.input_bit_positions[i]})'
for i in range(len(self.input_id_links))]
return [f' {self.id} = bit_vector_SBOX(bit_vector_CONCAT([{",".join(sbox_params)} ]), '
f'np.array({self.description}, dtype=np.uint8))']
f'np.array({self.description}, dtype=np.uint8), output_bit_size = {self.output_bit_size})']

def get_byte_based_vectorized_python_code(self, params):
return [f' {self.id} = byte_vector_SBOX({params}, np.array({self.description}, dtype=np.uint8))']
Expand Down

0 comments on commit ee0b981

Please sign in to comment.