Skip to content

Commit

Permalink
WIP: Fix linear_layer_component.py and modular_component.py tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariags803 committed May 21, 2024
1 parent e8257ae commit 8a0a9e9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
4 changes: 2 additions & 2 deletions claasp/components/linear_layer_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,12 +594,12 @@ def milp_wordwise_deterministic_truncated_xor_differential_constraints(self, mod
EXAMPLES::
sage: from claasp.ciphers.block_ciphers.midori_block_cipher import MidoriBlockCipher
sage: cipher = MidoriBlockCipher(number_of_rounds=2)
sage: from claasp.cipher_modules.models.milp.milp_models.milp_wordwise_deterministic_truncated_xor_differential_model import MilpWordwiseDeterministicTruncatedXorDifferentialModel
sage: cipher = MidoriBlockCipher(number_of_rounds=2)
sage: milp = MilpWordwiseDeterministicTruncatedXorDifferentialModel(cipher)
sage: milp.init_model_in_sage_milp_class()
sage: linear_layer_component = cipher.component_from(0, 21)
sage: variables, constraints = linear_layer_component.milp_wordwise_deterministic_truncated_xor_differential_constraints(milp)
sage: variables, constraints = linear_layer_component.milp_wordwise_deterministic_truncated_xor_differential_constraints(milp) # random
sage: variables
[('x[mix_column_0_20_word_0_class_bit_0]', x_0),
('x[mix_column_0_20_word_0_class_bit_1]', x_1),
Expand Down
21 changes: 11 additions & 10 deletions claasp/components/modular_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,10 @@ def milp_xor_differential_propagation_constraints(self, model):
EXAMPLES::
sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules.models.milp.milp_model import MilpModel
sage: from claasp.cipher_modules.models.milp.milp_models.milp_xor_differential_model import MilpXorDifferentialModel
sage: speck = SpeckBlockCipher(block_bit_size=32, key_bit_size=64, number_of_rounds=2)
sage: modadd_component = speck.component_from(0, 1)
sage: milp = MilpModel(speck)
sage: milp = MilpXorDifferentialModel(speck)
sage: milp.init_model_in_sage_milp_class()
sage: variables, constraints = modadd_component.milp_xor_differential_propagation_constraints(milp)
sage: variables
Expand Down Expand Up @@ -461,14 +461,14 @@ def milp_bitwise_deterministic_truncated_xor_differential_constraints(self, mode
EXAMPLES::
sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: cipher = SpeckBlockCipher(block_bit_size=32, key_bit_size=64, number_of_rounds=2)
sage: from claasp.cipher_modules.models.milp.milp_models.milp_bitwise_deterministic_truncated_xor_differential_model import MilpBitwiseDeterministicTruncatedXorDifferentialModel
sage: cipher = SpeckBlockCipher(block_bit_size=32, key_bit_size=64, number_of_rounds=2)
sage: milp = MilpBitwiseDeterministicTruncatedXorDifferentialModel(cipher)
sage: milp.init_model_in_sage_milp_class()
sage: modadd_component = cipher.get_component_from_id("modadd_0_1")
sage: variables, constraints = modadd_component.milp_bitwise_deterministic_truncated_xor_differential_constraints(milp)
sage: constraints
[x_48 <= 16,
[x_48 <= 15,
0 <= x_48,
0 <= 16 + x_48 - 17*x_49,
x_48 - 17*x_49 <= 0,
Expand Down Expand Up @@ -642,7 +642,7 @@ def minizinc_xor_differential_propagation_constraints(self, model):
sage: modadd_component = fancy.component_from(1, 9)
sage: _, constraints = modadd_component.minizinc_xor_differential_propagation_constraints(minizinc)
sage: constraints[6]
'constraint modular_addition_word(array1d(0..6-1, [modadd_1_9_x0,modadd_1_9_x1,modadd_1_9_x2,modadd_1_9_x3,modadd_1_9_x4,modadd_1_9_x5]),array1d(0..6-1, [modadd_1_9_x6,modadd_1_9_x7,modadd_1_9_x8,modadd_1_9_x9,modadd_1_9_x10,modadd_1_9_x11]),array1d(0..6-1, [modadd_1_9_y0_0,modadd_1_9_y1_0,modadd_1_9_y2_0,modadd_1_9_y3_0,modadd_1_9_y4_0,modadd_1_9_y5_0]), p_modadd_1_9_0, dummy_modadd_1_9_0, -1)=1;\n'
'constraint modular_addition_word(array1d(0..6-1, [modadd_1_9_x0,modadd_1_9_x1,modadd_1_9_x2,modadd_1_9_x3,modadd_1_9_x4,modadd_1_9_x5]),array1d(0..6-1, [modadd_1_9_x6,modadd_1_9_x7,modadd_1_9_x8,modadd_1_9_x9,modadd_1_9_x10,modadd_1_9_x11]),array1d(0..6-1, [modadd_1_9_y0_0,modadd_1_9_y1_0,modadd_1_9_y2_0,modadd_1_9_y3_0,modadd_1_9_y4_0,modadd_1_9_y5_0]), p_modadd_1_9_0, dummy_modadd_1_9_0, -1)=1;\nconstraint carry_modadd_1_9_0 = XOR3(array1d(0..6-1, [modadd_1_9_x0,modadd_1_9_x1,modadd_1_9_x2,modadd_1_9_x3,modadd_1_9_x4,modadd_1_9_x5]),array1d(0..6-1, [modadd_1_9_x6,modadd_1_9_x7,modadd_1_9_x8,modadd_1_9_x9,modadd_1_9_x10,modadd_1_9_x11]),array1d(0..6-1, [modadd_1_9_y0_0,modadd_1_9_y1_0,modadd_1_9_y2_0,modadd_1_9_y3_0,modadd_1_9_y4_0,modadd_1_9_y5_0]));\n'
"""
def create_block_of_modadd_constraints(input_vars_1_temp, input_vars_2_temp,
output_varstrs_temp, i, round_number):
Expand Down Expand Up @@ -744,9 +744,9 @@ def milp_xor_linear_mask_propagation_constraints(self, model):
EXAMPLES::
sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules.models.milp.milp_model import MilpModel
sage: from claasp.cipher_modules.models.milp.milp_models.milp_xor_linear_model import MilpXorLinearModel
sage: speck = SpeckBlockCipher(block_bit_size=32, key_bit_size=64, number_of_rounds=2)
sage: milp = MilpModel(speck)
sage: milp = MilpXorLinearModel(speck)
sage: milp.init_model_in_sage_milp_class()
sage: modadd_component = speck.component_from(0, 1)
sage: variables, constraints = modadd_component.milp_xor_linear_mask_propagation_constraints(milp)
Expand All @@ -761,7 +761,7 @@ def milp_xor_linear_mask_propagation_constraints(self, model):
0 <= -1*x_0 - x_16 + x_32 + x_48 + x_49,
0 <= x_0 + x_16 - x_32 + x_48 - x_49,
...
-4 <= x_15 + x_31 + x_47 + x_63 + x_64,
x_15 + x_31 + x_47 + x_63 + x_64 <= 4,
x_65 == x_48 + x_49 + x_50 + x_51 + x_52 + x_53 + x_54 + x_55 + x_56 + x_57 + x_58 + x_59 + x_60 + x_61 + x_62 + x_63,
x_66 == 100*x_65]
"""
Expand Down Expand Up @@ -946,9 +946,10 @@ def sat_bitwise_deterministic_truncated_xor_differential_constraints(self):
'modadd_0_1_1_0',
'modadd_0_1_2_0',
...
'rot_0_0_15_0 plaintext_31_0 -rot_0_0_15_1 -modadd_0_1_15_0',
'rot_0_0_15_0 plaintext_31_0 -plaintext_31_1 -modadd_0_1_15_0',
'rot_0_0_15_1 modadd_0_1_15_0 modadd_0_1_15_1 -plaintext_31_1',
'plaintext_31_1 modadd_0_1_15_0 modadd_0_1_15_1 -rot_0_0_15_1',
'modadd_0_1_15_0 -rot_0_0_15_1 -plaintext_31_1 -modadd_0_1_15_1'])
"""
in_ids_0, in_ids_1 = self._generate_input_double_ids()
out_len, out_ids_0, out_ids_1 = self._generate_output_double_ids()
Expand Down

0 comments on commit 8a0a9e9

Please sign in to comment.