Sat model¶
SAT standard of Cipher¶
The target of this class is to build, solve and retrieve the solution of a SAT CNF representing some attacks on ciphers, e.g. the generic cipher inversion or the search for XOR differential trails (for SMT CNFs see the correspondent module). The internal format for SAT CNF clauses follows 3 rules:
every variable is a string with no spaces nor dashes;
if a literal is a negation of a variable, a dash is prepended to the variable;
the separator for literals is a space.
This module only handles the internal format. The translation in DIMACS
standard is performed whenever a solution method is called (e.g. solve,
find_lowest_weight_xor_differential_trail, …).
SAT Solvers¶
This module is able to use many different SAT solvers.
For any further information, refer to the file
claasp.cipher_modules.models.sat.solvers.py and to the section
Available SAT solvers.
REMARK: in order to be compliant with the library, the Most Significant Bit (MSB) is indexed by 0. Be careful whenever inspecting the code or, as well, a CNF.
- class SatModel(cipher, counter='sequential', compact=False)¶
Bases:
object- build_generic_sat_model_from_dictionary(component_and_model_types)¶
- calculate_component_weight(component, out_suffix, output_values_dict)¶
- property cipher_id¶
- static fix_variables_value_constraints(fixed_variables=[])¶
Return lists of variables and clauses for fixing variables in CIPHER model.
See also
SAT standard of Cipher for the format.
INPUT:
fixed_variables– list (default: []); variables in default format
EXAMPLES:
sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher sage: from claasp.cipher_modules.models.sat.sat_model import SatModel sage: speck = SpeckBlockCipher(number_of_rounds=3) sage: sat = SatModel(speck) sage: fixed_variables = [{ ....: 'component_id': 'plaintext', ....: 'constraint_type': 'equal', ....: 'bit_positions': [0, 1, 2, 3], ....: 'bit_values': [1, 0, 1, 1] ....: }, { ....: 'component_id': 'ciphertext', ....: 'constraint_type': 'not_equal', ....: 'bit_positions': [0, 1, 2, 3], ....: 'bit_values': [1, 1, 1, 0] ....: }] sage: SatModel.fix_variables_value_constraints(fixed_variables) ['plaintext_0', '-plaintext_1', 'plaintext_2', 'plaintext_3', '-ciphertext_0 -ciphertext_1 -ciphertext_2 ciphertext_3']
- property model_constraints¶
Return the model specified by
model_type.If the key refers to one of the available solver, Otherwise will raise a KeyError exception.
INPUT:
model_type– string; the model to retrieve
EXAMPLES:
sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher sage: from claasp.cipher_modules.models.sat.sat_model import SatModel sage: speck = SpeckBlockCipher(number_of_rounds=4) sage: sat = SatModel(speck) sage: sat.model_constraints('xor_differential') Traceback (most recent call last): ... ValueError: No model generated
- property sboxes_ddt_templates¶
- property sboxes_lat_templates¶
- solve(model_type, solver_name='CRYPTOMINISAT_EXT', options=None)¶
Return the solution of the model using the
solver_nameSAT solver.Note
Two types of solvers can be chosen: external or internal. In the following list of inputs, allowed SAT solvers are listed. Those ending with
_sagewill not create a subprocess nor additional files and will work completely embedded in Sage. Remaining solvers are allowed, but they need to be installed in the system.INPUT:
model_type– string; the model for which we want a solution. Available values are:'cipher''xor_differential''xor_linear'
solver_name– string (default: cryptominisat); the name of the solver
See also
EXAMPLES:
sage: from claasp.cipher_modules.models.sat.sat_models.sat_cipher_model import SatCipherModel sage: from claasp.ciphers.block_ciphers.tea_block_cipher import TeaBlockCipher sage: tea = TeaBlockCipher(number_of_rounds=32) sage: sat = SatCipherModel(tea) sage: sat.build_cipher_model() sage: sat.solve('cipher') # random {'cipher_id': 'tea_p64_k128_o64_r32', 'model_type': 'tea_p64_k128_o64_r32', 'solver_name': 'CRYPTOMINISAT_EXT', ... 'intermediate_output_31_15': {'value': '8ca8d5de0906f08e', 'weight': 0, 'sign': 1}, 'cipher_output_31_16': {'value': '8ca8d5de0906f08e', 'weight': 0, 'sign': 1}}, 'total_weight': 0, 'status': 'SATISFIABLE'}}
- weight_constraints(weight)¶
Return lists of variables and constraints that fix the total weight of the trail to a specific value.
INPUT:
weight– integer; the total weight of the trail
EXAMPLES:
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=3) sage: sat = SatXorDifferentialModel(speck) sage: sat.build_xor_differential_trail_model() sage: sat.weight_constraints(7) (['dummy_hw_0_0_0', 'dummy_hw_0_0_1', 'dummy_hw_0_0_2', ... '-dummy_hw_0_77_6 dummy_hw_0_78_6', '-hw_modadd_2_7_14 -dummy_hw_0_77_6', '-hw_modadd_2_7_15 -dummy_hw_0_78_6'])