Skip to content

Commit

Permalink
Feature/ADD: add options and weight parameter for sat differential model
Browse files Browse the repository at this point in the history
  • Loading branch information
cs committed Nov 7, 2024
1 parent 6ae63d5 commit ed41e5d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions claasp/cipher_modules/models/sat/sat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ def _solve_with_external_sat_solver(self, model_type, solver_name, options, host
input_file, output_file)
elif solver_specs['solver_name'] == 'PARKISSAT_EXT':
input_file = f'{self.cipher_id}_{file_id}_sat_input.cnf'
options = ["-c=80"]
status, sat_time, sat_memory, values = utils.run_parkissat(solver_specs, options, dimacs, input_file)
elif solver_specs['solver_name'] == 'YICES_SAT_EXT':
input_file = f'{self.cipher_id}_{file_id}_sat_input.cnf'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,74 @@ def find_lowest_weight_xor_differential_trail(self, fixed_values=[], solver_name

return solution

def find_lowest_weight_xor_differential_trail(self, fixed_values=[], solver_name=solvers.SOLVER_DEFAULT, start_weight=0, options=None):
"""
Return the solution representing a trail with the lowest weight.
By default, the search is set in the single-key setting.
.. NOTE::
There could be more than one trail with the lowest weight. In order to find all the lowest weight trail,
run :py:meth:`~SatXorDifferentialModel.find_all_xor_differential_trails_with_fixed_weight`.
INPUT:
- ``fixed_values`` -- **list** (default: `[]`); can be created using ``set_fixed_variables`` method
- ``solver_name`` -- **string** (default: `CRYPTOMINISAT_EXT`); the name of the solver
.. SEEALSO::
:ref:`sat-solvers`
EXAMPLES::
# single-key setting
sage: from claasp.cipher_modules.models.sat.sat_models.sat_xor_differential_model import SatXorDifferentialModel
sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: speck = SpeckBlockCipher(number_of_rounds=5)
sage: sat = SatXorDifferentialModel(speck)
sage: trail = sat.find_lowest_weight_xor_differential_trail()
sage: trail['total_weight']
9.0
# related-key setting
sage: from claasp.cipher_modules.models.sat.sat_models.sat_xor_differential_model import SatXorDifferentialModel
sage: from claasp.cipher_modules.models.utils import set_fixed_variables
sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: speck = SpeckBlockCipher(number_of_rounds=5)
sage: sat = SatXorDifferentialModel(speck)
sage: key = set_fixed_variables(
....: component_id='key',
....: constraint_type='not_equal',
....: bit_positions=range(64),
....: bit_values=(0,)*64)
sage: trail = sat.find_lowest_weight_xor_differential_trail(fixed_values=[key])
sage: trail['total_weight']
1.0
"""
current_weight = start_weight
start_building_time = time.time()
self.build_xor_differential_trail_model(weight=current_weight, fixed_variables=fixed_values)
end_building_time = time.time()
solution = self.solve(XOR_DIFFERENTIAL, solver_name=solver_name)
solution['building_time_seconds'] = end_building_time - start_building_time
total_time = solution['solving_time_seconds']
max_memory = solution['memory_megabytes']
while solution['total_weight'] is None:
current_weight += 1
start_building_time = time.time()
self.build_xor_differential_trail_model(weight=current_weight, fixed_variables=fixed_values)
end_building_time = time.time()
solution = self.solve(XOR_DIFFERENTIAL, solver_name=solver_name, options=options)
solution['building_time_seconds'] = end_building_time - start_building_time
total_time += solution['solving_time_seconds']
max_memory = max((max_memory, solution['memory_megabytes']))
solution['solving_time_seconds'] = total_time
solution['memory_megabytes'] = max_memory
solution['test_name'] = "find_lowest_weight_xor_differential_trail"

return solution

def find_one_xor_differential_trail(self, fixed_values=[], solver_name=solvers.SOLVER_DEFAULT):
"""
Return the solution representing a XOR differential trail.
Expand Down

0 comments on commit ed41e5d

Please sign in to comment.