Minizinc xor differential model

class MinizincXorDifferentialModel(cipher, window_size_list=None, probability_weight_per_round=None, sat_or_milp='sat', include_word_operations_mzn_file=True)

Bases: MinizincModel

add_comment(comment)

Write a ‘comment’ at the beginning of the model.

INPUT:

  • commentstring; string with the comment to be added

add_constraint_from_str(str_constraint)
add_output_comment(comment)
build_all_xor_differential_trails_with_fixed_weight(fixed_weight, fixed_variables)

Build a MiniZinc MILP model setting as objective the lowest weight for the xor differential trail.

INPUT:

  • fixed_weightinteger; the probability weight for the entire model

  • fixed_variableslist; the variables to be fixed in the model

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules.models.minizinc.minizinc_models.minizinc_xor_differential_model import MinizincXorDifferentialModel
sage: speck = SpeckBlockCipher(number_of_rounds=5, block_bit_size=32, key_bit_size=64)
sage: minizinc = MinizincXorDifferentialModel(speck)
sage: bit_positions = [i for i in range(speck.output_bit_size)]
sage: bit_positions_key = list(range(64))
sage: fixed_variables = [{ 'component_id': 'plaintext',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions,
....:     'operator': '>',
....:     'value': '0' }]
sage: fixed_variables.append({ 'component_id': 'key',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions_key,
....:     'operator': '=',
....:     'value': '0' })
sage: minizinc.build_lowest_weight_xor_differential_trail_model(fixed_variables)
sage: result = minizinc.solve('Xor')
sage: result.statistics['nSolutions'] > 1
True
build_lowest_weight_xor_differential_trail_model(fixed_variables, max_weight=None, min_weight=None)

Build a MiniZinc MILP model setting as objective the lowest weight for the xor differential trail.

INPUT:

  • fixed_variableslist; the variables to be fixed in the model

  • max_weightinteger (default: None)

  • min_weightinteger (default: None)

EXAMPLES:

sage: from claasp.cipher_modules.models.minizinc.minizinc_models.minizinc_xor_differential_model import MinizincXorDifferentialModel
sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: speck = SpeckBlockCipher(number_of_rounds=5, block_bit_size=32, key_bit_size=64)
sage: minizinc = MinizincXorDifferentialModel(speck)
sage: bit_positions = [i for i in range(speck.output_bit_size)]
sage: bit_positions_key = list(range(64))
sage: fixed_variables = [{ 'component_id': 'plaintext',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions,
....:     'operator': '>',
....:     'value': '0' }]
sage: fixed_variables.append({ 'component_id': 'key',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions_key,
....:     'operator': '=',
....:     'value': '0' })
sage: minizinc.build_lowest_weight_xor_differential_trail_model(fixed_variables)
sage: result = minizinc.solve('Xor')
sage: result.statistics['nSolutions'] > 1
True
build_lowest_xor_differential_trails_with_at_most_weight(fixed_weight, fixed_variables)

Build a MiniZinc MILP model setting as objective the lowest weight fot he xor differential trail.

INPUT:

  • fixed_weightinteger; the upper bound for the weight

  • fixed_variableslist; the variables to be fixed in the model

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules.models.minizinc.minizinc_models.minizinc_xor_differential_model import MinizincXorDifferentialModel
sage: speck = SpeckBlockCipher(number_of_rounds=5, block_bit_size=32, key_bit_size=64)
sage: minizinc = MinizincXorDifferentialModel(speck)
sage: bit_positions = [i for i in range(speck.output_bit_size)]
sage: bit_positions_key = list(range(64))
sage: fixed_variables = [{ 'component_id': 'plaintext',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions,
....:     'operator': '>',
....:     'value': '0' }]
sage: fixed_variables.append({ 'component_id': 'key',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions_key,
....:     'operator': '=',
....:     'value': '0' })
sage: minizinc.build_lowest_xor_differential_trails_with_at_most_weight(
....:     100, fixed_variables
....: )
sage: result = minizinc.solve('Xor')
sage: result.statistics['nSolutions'] > 1
True
build_xor_differential_trail_model(weight=- 1, fixed_variables=[])

Build the model for the search of xor differential trails.

INPUT:

  • weightinteger (default: -1); If set to non-negative integer, fixes the xor trail search to a specific weight

  • fixed_variableslist (default: []); variables that need to be fixed to a certain value dictionaries contain name, bit_size and value (as integer) | [ | { | ‘component_id’: ‘plaintext’, | ‘constraint_type’: ‘equal’/’not_equal’ | ‘bit_positions’: [0, 1, 2, 3], | ‘binary_value’: [0, 0, 0, 0] | } | ]

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules.models.minizinc.minizinc_models.minizinc_xor_differential_model import MinizincXorDifferentialModel
sage: speck = SpeckBlockCipher(number_of_rounds=22)
sage: minizinc = MinizincXorDifferentialModel(speck)
sage: minizinc.build_xor_differential_trail_model()
property cipher
property cipher_id
connect_rounds()

Return a list of constraints that link the bits from each component.

INPUT:

  • None

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules.models.minizinc.minizinc_models.minizinc_xor_differential_model import MinizincXorDifferentialModel
sage: speck = SpeckBlockCipher(number_of_rounds=22)
sage: minizinc = MinizincXorDifferentialModel(speck)
sage: minizinc.connect_rounds()[:24][0]
'constraint rot_0_0_x0 = plaintext_y0;'
constraint_permutation_and_key_schedule_separately_by_input_sizes()
extend_model_constraints(constraints)
extend_variables(variables)
find_all_xor_differential_trails_with_fixed_weight(fixed_weight, fixed_values=[], solver_name=None)

Return all the XOR differential trails with weight equal to fixed_weight.

The value returned is a list of solutions in standard format.

INPUT:

  • fixed_weightinteger; upper limit probability weight

  • fixed_valueslist (default: []); dictioanries contain variables values whose output need to be fixed

  • solver_namestring (default: None); the name of the solver (if needed)

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules.models.minizinc.minizinc_models.minizinc_xor_differential_model import MinizincXorDifferentialModel
sage: speck = SpeckBlockCipher(number_of_rounds=5, block_bit_size=32, key_bit_size=64)
sage: minizinc = MinizincXorDifferentialModel(speck)
sage: bit_positions = [i for i in range(speck.output_bit_size)]
sage: bit_positions_key = list(range(64))
sage: fixed_variables = [{ 'component_id': 'plaintext',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions,
....:     'operator': '>',
....:     'value': '0' }]
sage: fixed_variables.append({ 'component_id': 'key',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions_key,
....:     'operator': '=',
....:     'value': '0' })
sage: result = minizinc.find_all_xor_differential_trails_with_fixed_weight(
....: 5, solver_name='Xor', fixed_values=fixed_variables
....: )
sage: print(result['total_weight'])
None
find_all_xor_differential_trails_with_weight_at_most(min_weight, max_weight=64, fixed_values=[], solver_name=None)

Return all XOR differential trails with weight greater than min_weight and lower/equal to max_weight.

The value returned is a list of solutions in standard format.

INPUT:

  • min_weightinteger; the lower bound for the weight

  • max_weightinteger (default: 64); the upper bound for the weight

  • fixed_valueslist (default: []); dictionaries contain variables values whose output need to be fixed

  • solver_namestring (default: None); the name of the solver (if needed)

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules.models.minizinc.minizinc_models.minizinc_xor_differential_model import MinizincXorDifferentialModel
sage: speck = SpeckBlockCipher(number_of_rounds=4, block_bit_size=32, key_bit_size=64)
sage: minizinc = MinizincXorDifferentialModel(speck)
sage: bit_positions = list(range(32))
sage: bit_positions_key = list(range(64))
sage: fixed_variables = [{ 'component_id': 'plaintext',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions,
....:     'operator': '>',
....:     'value': '0' }]
sage: fixed_variables.append({ 'component_id': 'key',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions_key,
....:     'operator': '=',
....:     'value': '0' })
sage: result = minizinc.find_all_xor_differential_trails_with_weight_at_most(
....:     1, solver_name='Xor', fixed_values=fixed_variables
....: )
sage: result[0]['total_weight'] > 1
True
find_lowest_weight_xor_differential_trail(fixed_values=[], solver_name=None)

Find the lowest weight solution in a MiniZinc MILP model.

INPUT:

  • fixed_valueslist (default: []); disctionaries contains variables values whose output need to be fixed

  • solver_namestring (default: None); the name of the solver (if needed)

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules.models.minizinc.minizinc_models.minizinc_xor_differential_model import MinizincXorDifferentialModel
sage: speck = SpeckBlockCipher(number_of_rounds=5, block_bit_size=32, key_bit_size=64)
sage: minizinc = MinizincXorDifferentialModel(speck)
sage: bit_positions = list(range(32))
sage: bit_positions_key = list(range(64))
sage: fixed_variables = [{ 'component_id': 'plaintext',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions,
....:     'operator': '>',
....:     'value': '0' }]
sage: fixed_variables.append({ 'component_id': 'key',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions_key,
....:     'operator': '=',
....:     'value': '0' })
sage: result = minizinc.find_lowest_weight_xor_differential_trail(
....:     solver_name='Xor', fixed_values=fixed_variables
....: )
sage: result["total_weight"]
9

sage: minizinc = MinizincXorDifferentialModel(speck, [0, 0, 0, 0, 0])
sage: result = minizinc.find_lowest_weight_xor_differential_trail(solver_name='Xor', fixed_values=fixed_variables)
sage: result["total_weight"]
9
find_min_of_max_xor_differential_between_permutation_and_key_schedule(fixed_values=[], solver_name=None)
fix_variables_value_constraints(fixed_variables=[])

Return a list of constraints that fix the input variables to a specific value.

INPUT:

  • fixed_variableslist (default: []); the variables to be fixed in standard format

EXAMPLES:

sage: from claasp.cipher_modules.models.minizinc.minizinc_models.minizinc_xor_differential_model import MinizincXorDifferentialModel
sage: from claasp.ciphers.block_ciphers.raiden_block_cipher import RaidenBlockCipher
sage: raiden = RaidenBlockCipher(number_of_rounds=1)
sage: minizinc = MinizincXorDifferentialModel(raiden)
sage: minizinc.build_xor_differential_trail_model()
sage: fixed_variables = [{
....:     'component_id': 'key',
....:     'constraint_type': 'equal',
....:     'bit_positions': [0, 1, 2, 3],
....:     'bit_values': [0, 1, 0, 1]
....: }]
sage: minizinc.fix_variables_value_constraints(fixed_variables)[0]
'constraint key_y0 = 0;'

sage: fixed_variables = [{ 'component_id': 'plaintext',
....:     'constraint_type': 'sum',
....:     'bit_positions': [0, 1, 2, 3],
....:     'operator': '>',
....:     'value': '0' }]
sage: minizinc.fix_variables_value_constraints(fixed_variables)[0]
'constraint plaintext_y0+plaintext_y1+plaintext_y2+plaintext_y3>0;'
get_model_constraints()
get_probability_vars_from_key_schedule()
get_probability_vars_from_permutation()
get_variables()
init_constraints()
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_typestring; the model to retrieve

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules.models.minizinc.minizinc_model import MinizincModel
sage: speck = SpeckBlockCipher(number_of_rounds=4)
sage: minizinc = MinizincModel(speck)
sage: minizinc.model_constraints('xor_differential')
Traceback (most recent call last):
...
ValueError: No model generated
objective_generator(strategy='min_all_probabilities')
output_probability_per_round()
static parse_probability_vars(result, solution, probability_vars)
satisfy_generator()
set_max_number_of_carries_on_arx_cipher(max_number_of_carries)
set_max_number_of_nonlinear_carries(max_number_of_nonlinear_carries)
solve(solver_name=None, timeout_in_seconds_=30, processes_=4, nr_solutions_=None, random_seed_=None, all_solutions_=False, intermediate_solutions_=False, free_search_=False, optimisation_level_=None)

Solve the model passed in str_model_path by using MiniZinc and str_solver.

INPUT:

  • model_typestring; the type of the model that has been solved

  • solver_namestring (default: None); name of the solver to be used together with MiniZinc

  • timeout_in_seconds_integer (default: 30); time in seconds to interrupt the solving process

  • processes_integer (default: 4); set the number of processes the solver can use. (Only available when the -p flag is supported by the solver)

  • nr_solutions_integer (default: None); the requested number of solution. (Only available on satisfaction problems and when the -n flag is supported by the solver)

  • random_seed_integer (default: None); set the random seed for solver. (Only available when the -r flag is supported by the solver)

  • intermediate_solutions_boolean (default: False); request the solver to output any intermediate solutions that are found during the solving process. (Only available on optimisation problems and when the -a flag is supported by the solver)

  • all_solutions_boolean (default: False); request to solver to find all solutions. (Only available on satisfaction problems and when the -a flag is supported by the solver)

  • free_searchboolean (default: False); allow the solver to ignore the search definition within the instance (Only available when the -f flag is supported by the solver)

  • optimisation_level_integer (default: None); set the MiniZinc compiler optimisation level

    • 0: Disable optimisation

    • 1: Single pass optimisation (default)

    • 2: Flatten twice to improve flattening decisions

    • 3: Perform root-node-propagation

    • 4: Probe bounds of all variables at the root node

    • 5: Probe values of all variables at the root node

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules.models.minizinc.minizinc_models.minizinc_xor_differential_model import MinizincXorDifferentialModel
sage: speck = SpeckBlockCipher(number_of_rounds=5, block_bit_size=32, key_bit_size=64)
sage: minizinc = MinizincXorDifferentialModel(speck)
sage: bit_positions = [i for i in range(speck.output_bit_size)]
sage: bit_positions_key = list(range(64))
sage: fixed_variables = [{ 'component_id': 'plaintext',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions,
....:     'operator': '>',
....:     'value': '0' }]
sage: fixed_variables.append({ 'component_id': 'key',
....:     'constraint_type': 'sum',
....:     'bit_positions': bit_positions_key,
....:     'operator': '=',
....:     'value': '0' })
sage: minizinc.build_xor_differential_trail_model(-1, fixed_variables)
sage: result = minizinc.solve('Xor')
sage: result.statistics['nSolutions']
1
weight_constraints(weight=None, operator='=', max_weight=None)

Return listS of variables and constraints that fix the total weight of the trail to a specific value.

INPUT:

  • weightinteger (default: None); the total weight of the trail

  • operatorstr (default: =)

  • max_weightinteger (default: None)

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules.models.minizinc.minizinc_models.minizinc_xor_differential_model import MinizincXorDifferentialModel
sage: speck = SpeckBlockCipher(number_of_rounds=3)
sage: minizinc = MinizincXorDifferentialModel(speck)
sage: minizinc.build_xor_differential_trail_model()
sage: minizinc.weight_constraints(7)
['constraint sum(p_modadd_0_1_0++p_modadd_1_2_0++p_modadd_1_7_0++p_modadd_2_2_0++p_modadd_2_7_0) = 7;']
write_minizinc_model_to_file(file_path, prefix='')

Write the MiniZinc model into a file inside file_path.

INPUT:

  • file_pathstring; the path of the file that will contain the model

  • prefixstr (default: ``)