Code generator

build_code_for_components(cipher, cipher_code_string, i, verbosity)
build_code_for_continuous_diffusion_analysis_components(add_verbosity, cipher, cipher_code_string, round_number)
build_continuous_diffusion_analysis_function_call(component)
build_function_call(component)
constant_to_bitstring(val, output_size)
delete_generated_evaluate_c_shared_library(cipher)
generate_bit_based_c_code(cipher, intermediate_output, verbosity)
generate_bit_based_vectorized_python_code_string(cipher, store_intermediate_outputs=False, verbosity=False, convert_output_to_bytes=False)

Return string python code needed to evaluate a cipher using a vectorized implementation bit based oriented.

INPUT:

  • cipherCipher object; a cipher instance

  • store_intermediate_outputsboolean (default: False); set this flag to True in order to return a list with each round output

  • verbosityboolean (default: False); set to True to make the Python code print the input/output of each component

  • convert_output_to_bytesboolean (default: False)

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules import code_generator
sage: speck = SpeckBlockCipher()
sage: string_python_code = code_generator.generate_bit_based_vectorized_python_code_string(speck)
sage: string_python_code.split("
“)[0]

‘from claasp.cipher_modules.generic_functions_vectorized_bit import *

generate_byte_based_vectorized_python_code_string(cipher, store_intermediate_outputs=False, verbosity=False, integers_inputs_and_outputs=False)

Return string python code needed to evaluate a cipher using a vectorized implementation byte based oriented.

INPUT:

  • cipherCipher object; a cipher instance

  • store_intermediate_outputsboolean (default: False); set this flag to True in order to return a list with each round output

  • verbosityboolean (default: False); set to True to make the Python code print the input/output of each component

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules import code_generator
sage: speck = SpeckBlockCipher()
sage: string_python_code = code_generator.generate_byte_based_vectorized_python_code_string(speck)
sage: string_python_code.split("\n")[0]
'from claasp.cipher_modules.generic_functions_vectorized_byte import *'
generate_evaluate_c_code_shared_library(cipher, intermediate_output, verbosity)
generate_python_code_string(cipher, verbosity=False)

Return a string containing the python code that defines the self.evaluate() method.

INPUT:

  • cipherCipher object; a cipher instance

  • verbosityboolean (default: False); set to True to make the Python code print the input/output of each component

EXAMPLES:

sage: from claasp.ciphers.toys.fancy_block_cipher import FancyBlockCipher
sage: from claasp.cipher_modules import code_generator
sage: fancy = FancyBlockCipher()
sage: string_python_code = code_generator.generate_python_code_string(fancy)
sage: "def evaluate(input):" in string_python_code
True

# This test is skipped due to it changes the order of the intermediate outputs sometimes as:
# intermediate_output['cipher_output'] = []
# intermediate_output['round_key_output'] = []
sage: from claasp.ciphers.toys.identity_block_cipher import IdentityBlockCipher
sage: from claasp.cipher_modules import code_generator
sage: identity = IdentityBlockCipher()
sage: print(code_generator.generate_python_code_string(identity, verbosity=True)) # doctest: +SKIP
from copy import copy
from bitstring import BitArray
from claasp.cipher_modules.generic_functions import *

def evaluate(input):
    plaintext_output = copy(BitArray(uint=input[0], length=32))
    key_output = copy(BitArray(uint=input[1], length=32))
    intermediate_output = {}
    intermediate_output['round_key_output'] = []
    intermediate_output['cipher_output'] = []
    components_io = {}
    component_input = BitArray(1)
    print('\nRound_0\n')

    # round: 0, component: 0, component_id: concatenate_0_0
    component_input = select_bits(key_output, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
    output_bit_size = 32
    concatenate_0_0_output = component_input
    components_io['concatenate_0_0'] = [component_input.uint, concatenate_0_0_output.uint]
    print('concatenate_0_0_input = {}'.format(component_input))
    print('concatenate_0_0_output = {}'.format(concatenate_0_0_output))
...

    # round: 0, component: 3, component_id: cipher_output_0_3
    component_input = select_bits(concatenate_0_2_output, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
    output_bit_size = 32
    cipher_output_0_3_output = component_input
    intermediate_output['cipher_output'].append(cipher_output_0_3_output.uint)
    cipher_output = cipher_output_0_3_output.uint
    components_io['cipher_output_0_3'] = [component_input.uint, cipher_output_0_3_output.uint]
    print('cipher_output_0_3_input = {}'.format(component_input))
    print('cipher_output_0_3_output = {}'.format(cipher_output_0_3_output))

    return cipher_output, intermediate_output, components_io
generate_python_code_string_for_continuous_diffusion_analysis(cipher, verbosity=False)

Return a string containing the python code that defines a self.evaluate_continuous_diffusion_analysis() method.

INPUT:

  • cipherCipher object; a cipher instance

  • verbosityboolean (default: False); set to True to make the Python code print the input/output of each component

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.cipher_modules import code_generator
sage: speck = SpeckBlockCipher(number_of_rounds=2)
sage: string_python_code = code_generator.generate_python_code_string_for_continuous_diffusion_analysis(speck, verbosity=False)
sage: "def evaluate(input):" in string_python_code
True
generate_word_based_c_code(cipher, word_size, intermediate_output, verbosity)
get_cipher_output_component_bit_based_c_code(component, index, intermediate_output, list_sizes, string_dictionary, c_variables, cipher)
get_cipher_output_word_based_c_code(component, index, intermediate_output, intermediate_output_code, list_sizes, string_dictionary, verbosity, word_size, wordstring_variables)
get_intermediate_output_component_bit_based_c_code(component, index, intermediate_output, list_sizes, string_dictionary, verbosity)
get_intermediate_output_word_based_c_code(component, index, intermediate_output, intermediate_output_code, list_sizes, string_dictionary, verbosity, word_size, wordstring_variables)
get_number_of_inputs(component)
get_padding_component_bit_based_c_code(component, verbosity)
get_rounds_bit_based_c_code(cipher, intermediate_output, verbosity)
get_rounds_word_based_c_code(cipher, intermediate_output, verbosity, word_size)
get_word_operation_component_bit_based_c_code(component, verbosity)
get_word_operation_word_based_c_code(component, verbosity, word_size, wordstring_variables)
prepare_input_bit_based_vectorized_python_code_string(component)
prepare_input_byte_based_vectorized_python_code_string(bit_sizes, component)
update_intermediate_structure(string_dictionary, list_sizes, intermediate_output_code, component, index)