Skip to content

Commit

Permalink
Merge pull request #133 from Crypto-TII/develop
Browse files Browse the repository at this point in the history
New features in impossible trail search models and bug fixes
  • Loading branch information
peacker authored Jan 30, 2024
2 parents 1a06855 + 0af6ab7 commit e016b14
Show file tree
Hide file tree
Showing 49 changed files with 1,812 additions and 562 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/build-develop-image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Build and push image from develop
on:
pull_request:
types: [ closed ]
branches:
- main

jobs:
build-image:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
persist-credentials: false
fetch-depth: 0

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Login dockerhub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_REGISTRY_USER }}
password: ${{ secrets.DOCKER_REGISTRY_PASSWORD }}

- name: Build & Push
uses: docker/build-push-action@v4
id: built-image
with:
context: .
file: ./docker/Dockerfile
push: true
tags: tiicrc/claasp-lib:latest
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max

- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Get current commit information
run: |
git clone ${{ secrets.DEPLOYMENT_REPOSITORY }} deployment-repo
git config --global user.name 'Github'
git config --global user.email ${{ secrets.DEPLOYMENT_REPOSITORY_EMAIL }}
cd deployment-repo && echo "Date: $(date) Commit: $(git rev-parse HEAD)" >> claasp-dev.log
git add claasp-dev.log
git commit -m "Updating deployment-repo from github"
git push origin master
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ all: install
fi

builddocker:
docker build -f docker/Dockerfile -t $(DOCKER_IMG_NAME) .
docker build -f docker/Dockerfile --target claasp-base -t $(DOCKER_IMG_NAME) .

rundocker: builddocker
docker run -i -p 8887:8887 --mount type=bind,source=`pwd`,target=/home/sage/tii-claasp -t $(DOCKER_IMG_NAME) \
sh -c "cd /home/sage/tii-claasp && make install && cd /home/sage/tii-claasp && exec /bin/bash"

builddocker-m1:
docker build --build-arg="GUROBI_ARCH=armlinux64" -f docker/Dockerfile --platform linux/aarch64 -t $(DOCKER_IMG_NAME) .
docker build --build-arg="GUROBI_ARCH=armlinux64" -f docker/Dockerfile --platform linux/aarch64 --target claasp-base -t $(DOCKER_IMG_NAME) .

rundocker-m1: builddocker-m1
docker run -i -p 8888:8888 --mount type=bind,source=`pwd`,target=/home/sage/tii-claasp -t $(DOCKER_IMG_NAME) \
Expand Down
6 changes: 3 additions & 3 deletions claasp/cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ def get_partial_cipher(self, start_round=None, end_round=None, keep_key_schedule
end_round, keep_key_schedule)

if start_round > 0:
for input_type in set(self.inputs) - {INPUT_KEY}:
for input_type in set([input for input in self.inputs if INPUT_KEY not in input]):
removed_components_ids.append(input_type)
input_index = partial_cipher.inputs.index(input_type)
partial_cipher.inputs.pop(input_index)
Expand Down Expand Up @@ -1145,8 +1145,8 @@ def cipher_partial_inverse(self, start_round=None, end_round=None, keep_key_sche
partial_cipher_inverse = partial_cipher.cipher_inverse()

key_schedule_component_ids = get_key_schedule_component_ids(partial_cipher_inverse)
key_schedule_components = [partial_cipher_inverse.get_component_from_id(id) for id in
key_schedule_component_ids[1:]]
key_schedule_components = [partial_cipher_inverse.get_component_from_id(id) for id in key_schedule_component_ids if
INPUT_KEY not in id]

if not keep_key_schedule:
for current_round in partial_cipher_inverse.rounds_as_list:
Expand Down
8 changes: 1 addition & 7 deletions claasp/cipher_modules/code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ def generate_bit_based_vectorized_python_code_string(cipher, store_intermediate_

code.extend([f' {cipher.inputs[i]}=input[{i}]' for i in range(len(cipher.inputs))])
for component in cipher.get_all_components():
start = time.time()
params = prepare_input_bit_based_vectorized_python_code_string(component)
component_types_allowed = ['constant', 'linear_layer', 'concatenate', 'mix_column',
'sbox', 'cipher_output', 'intermediate_output', 'fsr']
Expand All @@ -260,8 +259,6 @@ def generate_bit_based_vectorized_python_code_string(cipher, store_intermediate_
name = component.id
if verbosity and component.type != 'constant':
code.append(f' bit_vector_print_as_hex_values("{name}_output", {name})')
end=time.time()
print(f'{component.id} time = {end-start}')
if store_intermediate_outputs:
code.append(' return intermediateOutputs')
elif CIPHER_INVERSE_SUFFIX in cipher.id:
Expand Down Expand Up @@ -319,7 +316,6 @@ def generate_byte_based_vectorized_python_code_string(cipher, store_intermediate
code.append(f' {cipher.inputs[i]}=input[{i}]')
bit_sizes[cipher.inputs[i]] = cipher.inputs_bit_size[i]
for component in cipher.get_all_components():
start = time.time()
params = prepare_input_byte_based_vectorized_python_code_string(bit_sizes, component)
bit_sizes[component.id] = component.output_bit_size
component_types_allowed = ['constant', 'linear_layer', 'concatenate', 'mix_column',
Expand All @@ -335,8 +331,6 @@ def generate_byte_based_vectorized_python_code_string(cipher, store_intermediate
if verbosity and component.type != 'constant':
code.append(f' byte_vector_print_as_hex_values("{name}_input", {params})')
code.append(f' byte_vector_print_as_hex_values("{name}_output", {name})')
end=time.time()
print(f'{component.id} time = {end-start}')
if store_intermediate_outputs:
code.append(' return intermediateOutputs')
elif CIPHER_INVERSE_SUFFIX in cipher.id:
Expand Down Expand Up @@ -648,7 +642,7 @@ def build_function_call(component):
elif component.type == FSR:
registers_info = component.description[0]
bits_inside_word = component.description[1]
if len(component.description) is 2:
if len(component.description) == 2:
number_of_clocks = 1
else:
number_of_clocks = component.description[2]
Expand Down
2 changes: 1 addition & 1 deletion claasp/cipher_modules/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

def evaluate(cipher, cipher_input, intermediate_output=False, verbosity=False):
python_code_string = code_generator.generate_python_code_string(cipher, verbosity)

f_module = ModuleType("evaluate")
exec(python_code_string, f_module.__dict__)

Expand Down
16 changes: 10 additions & 6 deletions claasp/cipher_modules/inverse_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ def get_cipher_components(self):
setattr(c, 'round', int(c.id.split("_")[-2]))
# build input components
for index, input_id in enumerate(self.inputs):
input_component = Component(input_id, "cipher_input", Input(0, [[]], [[]]), self.inputs_bit_size[index], [input_id])
if INPUT_KEY in input_id:
input_component = Component(input_id, "cipher_input", Input(0, [[]], [[]]), self.inputs_bit_size[index],
[INPUT_KEY])
else:
input_component = Component(input_id, "cipher_input", Input(0, [[]], [[]]), self.inputs_bit_size[index], [input_id])
setattr(input_component, 'round', -1)
component_list.append(input_component)
return component_list
Expand Down Expand Up @@ -673,7 +677,7 @@ def _add_output_bit_equivalences(id, bit_positions, component, all_equivalent_bi
flag_is_intersection_of_input_id_links_null, input_bit_positions = is_intersection_of_input_id_links_null(
inverse_component, component)

if (component.id == INPUT_KEY) or (component.type == CONSTANT):
if (component.description == [INPUT_KEY]) or (component.type == CONSTANT):
for i in range(component.output_bit_size):
output_bit_name_updated = id + "_" + str(i) + "_output_updated"
bit = {
Expand Down Expand Up @@ -821,7 +825,7 @@ def component_inverse(component, available_bits, all_equivalent_bits, key_schedu
component.output_bit_size, [component.id])
inverse_component.__class__ = cipher_output_component.CipherOutput
setattr(inverse_component, "round", component.round)
elif component.type == CIPHER_INPUT and (component.id == INPUT_KEY or component.id == INPUT_TWEAK):
elif component.type == CIPHER_INPUT and (component.description == [INPUT_KEY] or component.id == INPUT_TWEAK):
inverse_component = Component(component.id, CIPHER_INPUT,
Input(0, [[]], [[]]),
component.output_bit_size, [component.id])
Expand All @@ -833,7 +837,7 @@ def component_inverse(component, available_bits, all_equivalent_bits, key_schedu
component, available_output_components, all_equivalent_bits, self)
inverse_component = Component(component.id, INTERMEDIATE_OUTPUT,
Input(component.output_bit_size, input_id_links, input_bit_positions),
component.output_bit_size, [component.id])
component.output_bit_size, component.description)
inverse_component.__class__ = intermediate_output_component.IntermediateOutput
setattr(inverse_component, "round", component.round)
update_output_bits(inverse_component, self, all_equivalent_bits, available_bits)
Expand Down Expand Up @@ -941,7 +945,7 @@ def get_component_from_id(component_id, self):


def get_key_schedule_component_ids(self):
key_schedule_component_ids = [INPUT_KEY]
key_schedule_component_ids = [input for input in self.inputs if INPUT_KEY in input]
component_list = self.get_all_components()
for c in component_list:
flag_belong_to_key_schedule = True
Expand Down Expand Up @@ -1248,7 +1252,7 @@ def sort_cipher_graph(cipher):
def remove_components_from_rounds(cipher, start_round, end_round, keep_key_schedule):
list_of_rounds = cipher.rounds_as_list[:start_round] + cipher.rounds_as_list[end_round + 1:]
key_schedule_component_ids = get_key_schedule_component_ids(cipher)
key_schedule_components = [cipher.get_component_from_id(id) for id in key_schedule_component_ids[1:]]
key_schedule_components = [cipher.get_component_from_id(id) for id in key_schedule_component_ids if INPUT_KEY not in id]

if not keep_key_schedule:
for current_round in cipher.rounds_as_list:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from claasp.cipher_modules.models.milp.utils.config import SOLVER_DEFAULT
from claasp.cipher_modules.models.milp.utils.milp_name_mappings import MILP_BITWISE_DETERMINISTIC_TRUNCATED, \
MILP_BACKWARD_SUFFIX, MILP_BUILDING_MESSAGE, MILP_TRUNCATED_XOR_DIFFERENTIAL_OBJECTIVE
from claasp.cipher_modules.models.milp.utils.utils import fix_variables_value_deterministic_truncated_xor_differential_constraints
from claasp.cipher_modules.models.milp.utils.milp_truncated_utils import \
fix_variables_value_deterministic_truncated_xor_differential_constraints
from claasp.cipher_modules.models.milp.milp_model import MilpModel, verbose_print
from claasp.cipher_modules.models.utils import set_component_solution
from claasp.name_mappings import (CONSTANT, INTERMEDIATE_OUTPUT, CIPHER_OUTPUT,
Expand Down
Loading

0 comments on commit e016b14

Please sign in to comment.