Utils

aggregate_list_of_dictionary(dataset, group_by_key, sum_value_keys)

Aggregate by group_by_key the list of objects in dataset by summing_up the values in sum_value_keys.

INPUT:

  • datasetlist of dictionaries

  • group_by_keystring

  • sum_value_keyslist

EXAMPLES:

sage: from claasp.utils.utils import aggregate_list_of_dictionary
sage: from collections import Counter
sage: import datetime
sage: my_dataset = [
....:     {
....:         'date': datetime.date(2013, 1, 1),
....:         'id': 99,
....:         'value1': 10,
....:         'value2': 10
....:     },
....:     {
....:         'date': datetime.date(2013, 1, 1),
....:         'id': 98,
....:         'value1': 10,
....:         'value2': 10
....:     },
....:     {
....:         'date': datetime.date(2013, 1, 2),
....:         'id': 99,
....:         'value1': 10,
....:         'value2': 10
....:     }
....: ]
sage: expected_output = {
....:      datetime.date(2013, 1, 2): Counter({'value2': 10, 'value1': 10}),
....:      datetime.date(2013, 1, 1): Counter({'value2': 20, 'value1': 20})
....: }
sage: aggregate_list_of_dictionary(my_dataset, 'date', ['value1', 'value2']) == expected_output
True
bytes_positions_to_little_endian_for_32_bits(lst)

Read the bytes positions in little-endian order.

INPUT:

  • lstlist

EXAMPLES:

sage: from claasp.utils.utils import bytes_positions_to_little_endian_for_32_bits
sage: lst = list(range(32))
sage: output_lst = [24, 25, 26, 27, 28, 29, 30, 31, 16, 17, 18, 19, 20, 21, 22, 23, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7]
sage: bytes_positions_to_little_endian_for_32_bits(lst) == output_lst
True
bytes_positions_to_little_endian_for_multiple_of_32(lst, number_of_blocks)
calculate_inputs(planes, plane_num=3, lane_num=4)
convert_2d_index_to_1d_index(i, array_dim)
create_new_state_for_calculation(plane_num=3)
extract_inputs(input_ids_list, input_bit_positions_list, bit_positions_to_be_extracted)
generate_sample_from_gf_2_n(n, number_of_samples=100)
get_2d_array_element_from_1d_array_index(i, lst, array_dim)
get_ci(i, qi, si, t)
get_inputs_parameter(inputs_list)
get_ith_word(word_size, i, id_str=None, lst_by_id='')
get_k_th_bit(n, k)

Return the k-th bit of the number n.

INPUT:

  • ninteger; integer number

  • kinteger; integer number representing the index of the bit we need

EXAMPLES:

sage: from claasp.utils.utils import get_k_th_bit
sage: get_k_th_bit(3, 0)
1
get_number_of_rounds_from(block_bit_size, key_bit_size, number_of_rounds, parameters_configurations)
group_list_by_key(lst)

Group list of dictionaries by key.

INPUT:

  • lstlist; list of dictionaries

EXAMPLES:

sage: from claasp.utils.utils import group_list_by_key
sage: lst_example = [{'cipher_output': [{'1': 0}]}, {'round_key_output': [{'1': 0}]}, {'round_key_output': [{'3': 0}]}, {'cipher_output': [{'2': 0}]}, {'round_key_output': [{'2': 0}]}, {'cipher_output': [{'4': 0}]}]
sage: group_list_by_key(lst_example)
defaultdict(<class 'list'>, {'cipher_output': [[{'1': 0}], [{'2': 0}], [{'4': 0}]], 'round_key_output': [[{'1': 0}], [{'3': 0}], [{'2': 0}]]})
int_to_poly(integer_value, word_size, variable)
layer_and_lane_initialization(plane_num=3, lane_num=4, lane_size=32)
merging_list_of_lists(lst)

Merge list of lists.

INPUT:

  • lstlist; list of lists

EXAMPLES:

sage: from claasp.utils.utils import merging_list_of_lists
sage: merging_list_of_lists([[1,2],[3,4]])
[1, 2, 3, 4]
point_pair(dist=0.001, dim=1)

Return a pair of lists x, y of length dim where all elements are equal to 1 except one of them.

The non-one element is chosen randomly.

Also, the Euclidean distance between x and y is less than dim. And the non-one element of x is taking from U(low,high).

INPUT:

  • distfloat (default: 0.001); real number use to bound the Euclidean distance between x and y

  • diminteger (default: 1); length of the list x and y

EXAMPLES:

sage: from claasp.utils.utils import point_pair
sage: point_pair(0.001, 1) # random
poly_to_int(polynom, word_size, a)
pprint_dictionary(dictionary)

Pretty-print of a dictionary.

INPUT:

  • dictionarydictionary

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.utils.utils import pprint_dictionary
sage: speck = SpeckBlockCipher(block_bit_size=16, key_bit_size=32, number_of_rounds=5)
sage: from claasp.cipher_modules.avalanche_tests import AvalancheTests
sage: test = AvalancheTests(speck)
sage: d = test.avalanche_tests(number_of_samples=100)
sage: pprint_dictionary(d["test_results"]["plaintext"]["round_output"]["avalanche_dependence_vectors"][0]) # random
pprint_dictionary_to_file(dictionary, name_file)

Pretty-print of a dictionary.

INPUT:

  • dictionarydictionary

  • name_filestring

EXAMPLES:

sage: from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher
sage: from claasp.utils.utils import pprint_dictionary_to_file
sage: from claasp.cipher_modules.avalanche_tests import AvalancheTests
sage: import inspect
sage: import claasp
sage: import os.path
sage: speck = SpeckBlockCipher(block_bit_size=16, key_bit_size=32, number_of_rounds=5)
sage: test = AvalancheTests(speck)
sage: d = test.avalanche_tests(number_of_samples=100)
sage: tii_path = inspect.getfile(claasp)
sage: tii_dir_path = os.path.dirname(tii_path)
sage: pprint_dictionary_to_file(d["input_parameters"], f"{tii_dir_path}/test_json")
sage: os.path.isfile(f"{tii_dir_path}/test_json")
True

sage: import os
sage: os.remove(f"{tii_dir_path}/test_json")
set_2d_array_element_from_1d_array_index(i, lst, element, array_dim)
sgn_function(x)

Implement the sign function.

INPUT:

  • xfloat; real number

EXAMPLES:

sage: from claasp.utils.utils import sgn_function
sage: sgn_function(-1)
-1
signed_distance(lst_x, lst_y)

Implement Definition 13 (signed distance function) that is in [MUR2020].

INPUT:

  • lst_xlist; list of real numbers

  • lst_ylist; list of real numbers

EXAMPLES:

sage: from claasp.utils.utils import signed_distance
sage: lst_x = [0.001, -0.99]
sage: lst_y = [0.002, -0.90]
sage: signed_distance(lst_x, lst_y)
0
simplify_inputs(inputs_id, inputs_pos)