diff --git a/claasp/cipher_modules/statistical_tests/nist_statistical_tests.py b/claasp/cipher_modules/statistical_tests/nist_statistical_tests.py index d3e0624a..36490673 100644 --- a/claasp/cipher_modules/statistical_tests/nist_statistical_tests.py +++ b/claasp/cipher_modules/statistical_tests/nist_statistical_tests.py @@ -47,6 +47,10 @@ } +nist_executable_root_directory = f"/usr/local/bin/sts-2.1.2/" +nist_temp_directory = f"nist_sts_temp_dir" +nist_local_experiment_folder = f"{nist_temp_directory}/experiments/" + class NISTStatisticalTests: @@ -86,10 +90,10 @@ def nist_statistical_tests(self, test_type, EXAMPLE: - from claasp.cipher_modules.statistical_tests.nist_statistical_tests import StatisticalTests + from claasp.cipher_modules.statistical_tests.nist_statistical_tests import NISTStatisticalTests from claasp.ciphers.block_ciphers.speck_block_cipher import SpeckBlockCipher speck = SpeckBlockCipher(number_of_rounds=5) - nist_tests = StatisticalTests(speck) + nist_tests = NISTStatisticalTests(speck) nist_avalanche_test_results = nist_tests.nist_statistical_tests('avalanche') """ @@ -285,7 +289,7 @@ def _run_nist_statistical_tests_tool(input_file, bit_stream_length=10000, number ....: os.makedirs(f'test_reports/statistical_tests/experiments') sage: result = NISTStatisticalTests._run_nist_statistical_tests_tool( ....: f'claasp/cipher_modules/statistical_tests/input_data_example', - ....: 10000, 10, 1) + ....: 10000, 10, 1, statistical_test_option_list='1' + 14 * '0') Statistical Testing In Progress......... Statistical Testing Complete!!!!!!!!!!!! @@ -316,24 +320,22 @@ def _mkdir_folder_experiment(path_prefix, folder_experiment): "RandomExcursionsVariant" ] - nist_local_experiment_folder = f"/usr/local/bin/sts-2.1.2/experiments/" + input_file = os.path.abspath(input_file) + os.system(f'cp -r {nist_executable_root_directory} {nist_temp_directory}') for directory in ["AlgorithmTesting", "BBS", "CCG", "G-SHA1", "LCG", "MODEXP", "MS", "QCG1", "QCG2", "XOR"]: path_prefix = os.path.join(nist_local_experiment_folder, directory) for experiment_name in folder_experiments: _mkdir_folder_experiment(path_prefix, experiment_name) - os.system(f'chmod -R 777 {nist_local_experiment_folder}') - input_file = os.path.abspath(input_file) - output_code = os.system(f'niststs {input_file} {bit_stream_length} {number_of_bit_streams} {input_file_format} ' + output_code = os.system(f'{nist_temp_directory}/assess {input_file} {bit_stream_length} {number_of_bit_streams} {input_file_format} ' f'{statistical_test_option_list}') if output_code != 256: return output_code - else: - os.system(f'chmod -R 777 {nist_local_experiment_folder}') - return True + + return True @staticmethod - def _parse_report(report_filename, statistical_test_option_list='1' + 14 * '0'): + def _parse_report(report_filename): """ Parse the nist statistical tests report. It will return the parsed result in a dictionary format. @@ -387,7 +389,7 @@ def _parse_report(report_filename, statistical_test_option_list='1' + 14 * '0'): try: total_passed_line_2 = \ [line for line in lines if 'is approximately =' in line and 'for a sample size' in line][0] - total_passed = [int(x) for x in total_passed_line_2 if x.isdigit()] + total_passed = [int(x) for x in total_passed_line_2.split(' ') if x.isnumeric()] if len(total_passed) != 1: total_2 = total_passed[1] passed_2 = total_passed[0] @@ -482,17 +484,17 @@ def _generate_chart_round(report_dict, output_dir='', show_graph=False): plt.scatter(x, y, color="cadetblue") plt.title( - f'{report_dict["cipher_name"]}:{report_dict["data_type"]}, Round " {report_dict["round"]}|{report_dict["rounds"]}') + f'{report_dict["cipher_name"]}:{report_dict["data_type"]}, Round " {report_dict["round"]+1}|{report_dict["rounds"]}') plt.xlabel('Test ID') plt.ylabel('Passing Rate') if show_graph == False: if output_dir == '': - output_dir = f'nist_{report_dict["data_type"]}_{report_dict["cipher_name"]}_round_{report_dict["round"]}.png' + output_dir = f'nist_{report_dict["data_type"]}_{report_dict["cipher_name"]}_round_{report_dict["round"]+1}.png' plt.savefig(output_dir) else: plt.savefig( - output_dir + '/' + f'nist_{report_dict["data_type"]}_{report_dict["cipher_name"]}_round_{report_dict["round"]}.png') + output_dir + '/' + f'nist_{report_dict["data_type"]}_{report_dict["cipher_name"]}_round_{report_dict["round"]+1}.png') else: plt.show() plt.clf() @@ -533,7 +535,7 @@ def _generate_chart_all(report_dict_list, report_folder="", show_graph=False): plt.plot(x, y, 'o--', color='olive', alpha=0.4) if random_round > -1: plt.title( - f'{report_dict_list[0]["cipher_name"]}: {report_dict_list[0]["data_type"]}, Random at {random_round}|{report_dict_list[0]["rounds"]}') + f'{report_dict_list[0]["cipher_name"]}: {report_dict_list[0]["data_type"]}, Random at {random_round+1}|{report_dict_list[0]["rounds"]}') else: plt.title(f'{report_dict_list[0]["cipher_name"]}: {report_dict_list[0]["data_type"]}') plt.xlabel('Round') @@ -567,9 +569,8 @@ def _write_execution_time(self, execution_description, execution_time): except Exception as e: print(f'Error: {e.strerror}') - def _generate_nist_dicts(self,time_date, dataset, round_start, round_end, statistical_test_option_list='1' + 14 * '0'): + def _generate_nist_dicts(self,time_date, dataset, round_start, round_end, statistical_test_option_list=15 * '1'): # seems that the statistical tools cannot change the default folder 'experiments' - nist_local_experiment_folder = f"/usr/local/bin/sts-2.1.2/experiments/" dataset_folder = 'dataset' dataset_filename = f'nist_input_{self._cipher_primitive}' dataset_filename = os.path.join(dataset_folder, dataset_filename) @@ -604,6 +605,8 @@ def _generate_nist_dicts(self,time_date, dataset, round_start, round_end, statis except OSError: shutil.rmtree(report_folder_round) shutil.move(nist_local_experiment_folder, report_folder_round) + shutil.rmtree(nist_temp_directory) + self._write_execution_time(f'Compute round {round_number}', sts_execution_time)