Skip to content

Commit

Permalink
WIP: Fix cp_xor_differential_trail_search_fixing_number_of_active_sbo…
Browse files Browse the repository at this point in the history
…xes_model.py tests
  • Loading branch information
AnaCaceres committed May 21, 2024
1 parent 8e50274 commit de2bf78
Showing 1 changed file with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ def build_xor_differential_trail_second_step_model(self, weight=-1, fixed_variab
EXAMPLES::
sage: from claasp.cipher_modules.models.cp.cp_models.cp_xor_differential_trail_search_fixing_number_of_active_sboxes_model import (
....: CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel)
....: CpXorDifferentialFixingNumberOfActiveSboxesModel)
sage: from claasp.ciphers.block_ciphers.aes_block_cipher import AESBlockCipher
sage: from claasp.cipher_modules.models.utils import set_fixed_variables, integer_to_bit_list
sage: aes = AESBlockCipher(number_of_rounds=2)
sage: cp = CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel(aes)
sage: cp = CpXorDifferentialFixingNumberOfActiveSboxesModel(aes)
sage: fixed_variables = [set_fixed_variables('key', 'not_equal', range(128),
....: integer_to_bit_list(0, 128, 'little'))]
sage: cp.build_xor_differential_trail_second_step_model(-1, fixed_variables)
Expand Down Expand Up @@ -98,10 +98,10 @@ def find_all_xor_differential_trails_with_fixed_weight(self, fixed_weight, fixed
sage: from claasp.ciphers.block_ciphers.aes_block_cipher import AESBlockCipher
sage: from claasp.cipher_modules.models.cp.cp_models.cp_xor_differential_trail_search_fixing_number_of_active_sboxes_model import (
....: CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel)
....: CpXorDifferentialFixingNumberOfActiveSboxesModel)
sage: from claasp.cipher_modules.models.utils import set_fixed_variables, integer_to_bit_list
sage: aes = AESBlockCipher(number_of_rounds=2)
sage: cp = CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel(aes)
sage: cp = CpXorDifferentialFixingNumberOfActiveSboxesModel(aes)
sage: fixed_variables = [set_fixed_variables('key', 'equal', range(128),
....: integer_to_bit_list(0, 128, 'little'))]
sage: fixed_variables.append(set_fixed_variables('plaintext', 'not_equal', range(128),
Expand Down Expand Up @@ -135,19 +135,20 @@ def find_lowest_weight_xor_differential_trail(self, fixed_values=[], first_step_
sage: from claasp.ciphers.block_ciphers.aes_block_cipher import AESBlockCipher
sage: from claasp.cipher_modules.models.cp.cp_models.cp_xor_differential_trail_search_fixing_number_of_active_sboxes_model import (
....: CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel)
....: CpXorDifferentialFixingNumberOfActiveSboxesModel)
sage: from claasp.cipher_modules.models.utils import set_fixed_variables, integer_to_bit_list
sage: aes = AESBlockCipher(number_of_rounds=2)
sage: cp = CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel(aes)
sage: cp = CpXorDifferentialFixingNumberOfActiveSboxesModel(aes)
sage: fixed_variables = [set_fixed_variables('key', 'equal', range(128),
....: integer_to_bit_list(0, 128, 'little'))]
sage: fixed_variables.append(set_fixed_variables('plaintext', 'not_equal', range(128),
....: integer_to_bit_list(0, 128, 'little')))
sage: cp.find_lowest_weight_xor_differential_trail(fixed_variables, 'Chuffed', 'Chuffed') # random
{'cipher_id': 'aes_block_cipher_k128_p128_o128_r2',
5
{'cipher': 'aes_block_cipher_k128_p128_o128_r2',
'model_type': 'xor_differential',
'solver_name': 'Chuffed',
'components_values': {'key': {'value': '0', 'weight': 0},
'components_values': {'key': {'value': '00000000000000000000000000000000', 'weight': 0},
...
'total_weight': '30.0'}
"""
Expand All @@ -170,19 +171,19 @@ def find_one_xor_differential_trail(self, fixed_values=[], first_step_solver_nam
sage: from claasp.ciphers.block_ciphers.aes_block_cipher import AESBlockCipher
sage: from claasp.cipher_modules.models.cp.cp_models.cp_xor_differential_trail_search_fixing_number_of_active_sboxes_model import (
....: CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel)
....: CpXorDifferentialFixingNumberOfActiveSboxesModel)
sage: from claasp.cipher_modules.models.utils import set_fixed_variables, integer_to_bit_list
sage: aes = AESBlockCipher(number_of_rounds=2)
sage: cp = CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel(aes)
sage: cp = CpXorDifferentialFixingNumberOfActiveSboxesModel(aes)
sage: fixed_variables = [set_fixed_variables('key', 'equal', range(128),
....: integer_to_bit_list(0, 128, 'little'))]
sage: fixed_variables.append(set_fixed_variables('plaintext', 'not_equal', range(128),
....: integer_to_bit_list(0, 128, 'little')))
sage: cp.find_one_xor_differential_trail(fixed_variables, 'Chuffed', 'Chuffed') # random
{'cipher_id': 'aes_block_cipher_k128_p128_o128_r2',
{'cipher': 'aes_block_cipher_k128_p128_o128_r2',
'model_type': 'xor_differential',
...
'cipher_output_1_32':{'value': 'ffffffffffffffffffffffffffffffff', 'weight': 0}},
'cipher_output_1_32':{'value': 'ffffffffffffffffffffffffffffffff', 'weight': 0.0}},
'total_weight': '224.0'}
"""
return self.solve_full_two_steps_xor_differential_model('xor_differential_one_solution', 0, fixed_values, first_step_solver_name, second_step_solver_name)
Expand All @@ -205,16 +206,16 @@ def find_one_xor_differential_trail_with_fixed_weight(self, fixed_weight=-1, fix
sage: from claasp.ciphers.block_ciphers.aes_block_cipher import AESBlockCipher
sage: from claasp.cipher_modules.models.cp.cp_models.cp_xor_differential_trail_search_fixing_number_of_active_sboxes_model import (
....: CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel)
....: CpXorDifferentialFixingNumberOfActiveSboxesModel)
sage: from claasp.cipher_modules.models.utils import set_fixed_variables, integer_to_bit_list
sage: aes = AESBlockCipher(number_of_rounds=2)
sage: cp = CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel(aes)
sage: cp = CpXorDifferentialFixingNumberOfActiveSboxesModel(aes)
sage: fixed_variables = [set_fixed_variables('key', 'equal', range(128),
....: integer_to_bit_list(0, 128, 'little'))]
sage: fixed_variables.append(set_fixed_variables('plaintext', 'not_equal', range(128),
....: integer_to_bit_list(0, 128, 'little')))
sage: cp.find_one_xor_differential_trail_with_fixed_weight(224, fixed_variables, 'Chuffed', 'Chuffed') # random
{'cipher_id': 'aes_block_cipher_k128_p128_o128_r2',
{'cipher': 'aes_block_cipher_k128_p128_o128_r2',
'model_type': 'xor_differential',
'solver_name': 'Chuffed',
...
Expand All @@ -234,16 +235,16 @@ def generate_table_of_solutions(self, solution, solver_name):
EXAMPLES::
sage: from claasp.cipher_modules.models.cp.cp_models.cp_xor_differential_trail_search_fixing_number_of_active_sboxes_model import (
....: CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel)
....: CpXorDifferentialFixingNumberOfActiveSboxesModel)
sage: from claasp.ciphers.block_ciphers.aes_block_cipher import AESBlockCipher
sage: from claasp.cipher_modules.models.utils import set_fixed_variables, integer_to_bit_list
sage: aes = AESBlockCipher(number_of_rounds=2)
sage: cp = CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel(aes)
sage: cp = CpXorDifferentialFixingNumberOfActiveSboxesModel(aes)
sage: fixed_variables = [set_fixed_variables('key', 'not_equal', list(range(128)),
....: integer_to_bit_list(0, 128, 'little'))]
sage: cp.build_xor_differential_trail_first_step_model(-1,fixed_variables)
sage: first_step_solution, solve_time = cp.solve_model('xor_differential_first_step', 'Chuffed')
sage: cp.generate_table_of_solutions(first_step_solution)
sage: cp.generate_table_of_solutions(first_step_solution, 'Chuffed')
"""
cipher_name = self.cipher_id
separator = '----------'
Expand Down Expand Up @@ -290,9 +291,9 @@ def input_xor_differential_constraints(self):
sage: from claasp.ciphers.block_ciphers.aes_block_cipher import AESBlockCipher
sage: from claasp.cipher_modules.models.cp.cp_models.cp_xor_differential_trail_search_fixing_number_of_active_sboxes_model import (
....: CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel)
....: CpXorDifferentialFixingNumberOfActiveSboxesModel)
sage: aes = AESBlockCipher(number_of_rounds=2)
sage: cp = CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel(aes)
sage: cp = CpXorDifferentialFixingNumberOfActiveSboxesModel(aes)
sage: cp.input_xor_differential_constraints()
(['array[0..127] of var 0..1: key;',
'array[0..127] of var 0..1: plaintext;',
Expand Down Expand Up @@ -326,18 +327,19 @@ def solve_full_two_steps_xor_differential_model(self, model_type='xor_differenti
EXAMPLES::
sage: from claasp.cipher_modules.models.cp.cp_models.cp_xor_differential_trail_search_fixing_number_of_active_sboxes_model import (
....: CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel)
....: CpXorDifferentialFixingNumberOfActiveSboxesModel)
sage: from claasp.ciphers.block_ciphers.aes_block_cipher import AESBlockCipher
sage: from claasp.cipher_modules.models.utils import set_fixed_variables, integer_to_bit_list
sage: aes = AESBlockCipher(number_of_rounds=2)
sage: cp = CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel(aes)
sage: cp = CpXorDifferentialFixingNumberOfActiveSboxesModel(aes)
sage: fixed_variables = [set_fixed_variables('key', 'not_equal', list(range(128)),
....: integer_to_bit_list(0, 128, 'little'))]
sage: cp.solve_full_two_steps_xor_differential_model('xor_differential_one_solution', -1, fixed_variables, 'Chuffed', 'Chuffed') # random
{'building_time': 3.7489726543426514,
'cipher_id': 'aes_block_cipher_k128_p128_o128_r2',
1
{'cipher': 'aes_block_cipher_k128_p128_o128_r2',
...
'total_weight': '6'}
'total_weight': '6.0',
'building_time': 3.7489726543426514}
"""
possible_sboxes = 0
if weight > 0:
Expand Down Expand Up @@ -435,11 +437,11 @@ def solve_model(self, model_type, solver_name=None):
EXAMPLES::
sage: from claasp.cipher_modules.models.cp.cp_models.cp_xor_differential_trail_search_fixing_number_of_active_sboxes_model import (
....: CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel)
....: CpXorDifferentialFixingNumberOfActiveSboxesModel)
sage: from claasp.ciphers.block_ciphers.aes_block_cipher import AESBlockCipher
sage: from claasp.cipher_modules.models.utils import set_fixed_variables, integer_to_bit_list
sage: aes = AESBlockCipher(number_of_rounds=2)
sage: cp = CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel(aes)
sage: cp = CpXorDifferentialFixingNumberOfActiveSboxesModel(aes)
sage: fixed_variables = [set_fixed_variables('key', 'not_equal', list(range(128)),
....: integer_to_bit_list(0, 128, 'little'))]
sage: cp.build_xor_differential_trail_first_step_model(-1, fixed_variables)
Expand Down Expand Up @@ -508,16 +510,17 @@ def transform_first_step_model(self, attempt, active_sboxes, weight=-1):
EXAMPLES::
sage: from claasp.cipher_modules.models.cp.cp_models.cp_xor_differential_trail_search_fixing_number_of_active_sboxes_model import (
....: CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel)
....: CpXorDifferentialFixingNumberOfActiveSboxesModel)
sage: from claasp.ciphers.block_ciphers.aes_block_cipher import AESBlockCipher
sage: from claasp.cipher_modules.models.utils import set_fixed_variables, integer_to_bit_list
sage: aes = AESBlockCipher(number_of_rounds=2)
sage: cp = CpXorDifferentialTrailSearchFixingNumberOfActiveSboxesModel(aes)
sage: cp = CpXorDifferentialFixingNumberOfActiveSboxesModel(aes)
sage: fixed_variables = [set_fixed_variables('key', 'not_equal', range(128),
....: integer_to_bit_list(0, 128, 'little'))]
sage: cp.build_xor_differential_trail_first_step_model(-1, fixed_variables)
sage: first_step_solution, solve_time = cp.solve_model('xor_differential_first_step','Chuffed')
sage: cp.transform_first_step_model(0, first_step_solution[0])
1
"""
print(active_sboxes)
self._first_step_find_all_solutions = []
Expand Down

0 comments on commit de2bf78

Please sign in to comment.