Skip to content

Commit

Permalink
Fix: Mzn hybrid model for permutations
Browse files Browse the repository at this point in the history
  • Loading branch information
p-huynh committed Feb 3, 2025
1 parent cb2c6ab commit ded3b1f
Showing 1 changed file with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@
component.
"""

import os
import ast
import math
import itertools
import math
import os
import subprocess

from sage.combinat.permutation import Permutation
from sage.crypto.sbox import SBox

from claasp.cipher_modules.models.cp.mzn_model import solve_satisfy, MznModel
from claasp.cipher_modules.models.cp.mzn_models.mzn_xor_differential_model import update_and_or_ddt_valid_probabilities
from claasp.cipher_modules.models.cp.mzn_models.mzn_impossible_xor_differential_model import \
MznImpossibleXorDifferentialModel
from claasp.cipher_modules.models.cp.mzn_models.mzn_xor_differential_model import update_and_or_ddt_valid_probabilities
from claasp.cipher_modules.models.utils import convert_solver_solution_to_dictionary, check_if_implemented_component, \
get_bit_bindings

from claasp.name_mappings import (CONSTANT, INTERMEDIATE_OUTPUT, CIPHER_OUTPUT, SBOX, WORD_OPERATION,
IMPOSSIBLE_XOR_DIFFERENTIAL, INPUT_PLAINTEXT)
IMPOSSIBLE_XOR_DIFFERENTIAL, INPUT_PLAINTEXT, INPUT_KEY)


class MznHybridImpossibleXorDifferentialModel(MznImpossibleXorDifferentialModel):

Expand Down Expand Up @@ -211,7 +211,7 @@ def _find_paths(self, graph, end_node, stop_at=INPUT_PLAINTEXT, path=None):
perm = Permutation([i + 1 for i in self._extract_ones(matrix)]).inverse()
P = [i - 1 for i in perm]
end_node = (end_node[0], str(P[int(end_node[-2])])) + ('i',)
except ValueError:
except Exception:
pass

if stop_at in end_node[0] or not any(end_node in neighbors for neighbors in graph.values()):
Expand Down Expand Up @@ -256,7 +256,6 @@ def _output_is_aligned_with_sboxes(self, path_indices):
return True

def _generate_wordwise_incompatibility_constraint(self, component):
self.sbox_size = 4

if self.sbox_size:
current_round = self._cipher.get_round_from_component_id(component.id)
Expand Down Expand Up @@ -371,7 +370,8 @@ def final_impossible_constraints(self, number_of_rounds, initial_round, middle_r
new_constraint = new_constraint + \
f'\"inverse_{component.id} = \"++ show(inverse_{component.id})++ \"\\n\" ++ \"0\" ++ \"\\n\" ++'
if self.get_component_round(
component.id) == middle_round - 1 and component.id not in key_schedule_components_ids:
component.id) == middle_round - 1 and component.id not in key_schedule_components_ids and component.description == [
'round_output']:
for i in range(component.output_bit_size):
bitwise_incompatibility_constraint += f'({component.id}[{i}]+inverse_{component.id}[{i}]=1) \\/ '
wordwise_incompatibility_constraint += self._generate_wordwise_incompatibility_constraint(
Expand Down Expand Up @@ -543,7 +543,8 @@ def input_constraints(self, number_of_rounds=None, middle_round=None, probabilis
elif CONSTANT not in component.type:
cp_declarations.append(f'array[0..{output_size - 1}] of var ext_domain: {prefix}{output_id_link};')

cp_constraints.append("constraint inverse_key = key;")
if INPUT_KEY in self._cipher.inputs:
cp_constraints.append("constraint inverse_key = key;")
for input_id, input_size in self._cipher.inputs_size_to_dict().items():
cp_constraints.append(f'constraint forall (i in 0..{input_size-1})({input_id}[i] <= 2);')

Expand Down

0 comments on commit ded3b1f

Please sign in to comment.