From f18c581abc512fc880f8cc17458ef8f37cf5555a Mon Sep 17 00:00:00 2001 From: SamKarkache <93103617+SamKarkache@users.noreply.github.com> Date: Tue, 16 Apr 2024 15:11:40 -0400 Subject: [PATCH 1/3] fixed documentation typo --- WPI_SCA_LIBRARY/FileFormat.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/WPI_SCA_LIBRARY/FileFormat.py b/WPI_SCA_LIBRARY/FileFormat.py index 30df603..e8f3056 100644 --- a/WPI_SCA_LIBRARY/FileFormat.py +++ b/WPI_SCA_LIBRARY/FileFormat.py @@ -345,7 +345,9 @@ def add_dataset(self, name: str, data_to_add: np.ndarray, datatype: any) -> 'Dat :type name: str :param data_to_add: The NumPy array of data to be added to the new dataset :type data_to_add: np.ndarray - :returns: The newly created dataset + :param datatype: The datatype of the dataset + :type datatype: any + :returns: The newly created Dataset object :rtype: Dataset """ dataset = self.add_dataset_internal(name, existing=False, dataset=None) From ef4029f2c3ddd7153aa3687737c16abe9eee1d41 Mon Sep 17 00:00:00 2001 From: SamKarkache <93103617+SamKarkache@users.noreply.github.com> Date: Tue, 16 Apr 2024 15:27:27 -0400 Subject: [PATCH 2/3] update index.rst --- doc/index.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/doc/index.rst b/doc/index.rst index f0c1ba7..7cf0555 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -24,7 +24,5 @@ Library Features fileformat.rst cwscope.rst - lecroy.rst metrics.rst - leakagemodels.rst - dpa.rst \ No newline at end of file + leakagemodels.rst \ No newline at end of file From 7398c1b562c6eff23d4671a3d6048eae10686ace Mon Sep 17 00:00:00 2001 From: SamKarkache Date: Tue, 16 Apr 2024 18:10:55 -0400 Subject: [PATCH 3/3] reformat DPA.py --- Testing/DPATesting.py | 2 +- WPI_SCA_LIBRARY/DPA.py | 33 +++++++++++++++++---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Testing/DPATesting.py b/Testing/DPATesting.py index 39e97b8..ca29f33 100644 --- a/Testing/DPATesting.py +++ b/Testing/DPATesting.py @@ -5,7 +5,7 @@ ciphertext = np.load("TestFile//Experiments//CWCapture1//CWCapture1Ciphertexts.npy") plaintext = np.load("TestFile//Experiments//CWCapture1//CWCapture1Plaintexts.npy") -cpaoutput, guess_corr, guess = calculate_dpa(traces, plaintext, order=2, window_size_dpa=20, num_of_traces=100) +cpaoutput, guess_corr, guess = calculate_dpa(traces, plaintext, order=2, num_of_traces=100) print(traces.shape) print(cpaoutput.shape) diff --git a/WPI_SCA_LIBRARY/DPA.py b/WPI_SCA_LIBRARY/DPA.py index 42e50ca..400544f 100644 --- a/WPI_SCA_LIBRARY/DPA.py +++ b/WPI_SCA_LIBRARY/DPA.py @@ -29,7 +29,7 @@ def calculate_window_averages(traces, window_size=5, traces_max=0): return fma -def calculate_dpa(traces, iv, order=1, key_guess=0, window_size_fma=5, window_size_dpa=20, num_of_traces=0): +def calculate_dpa(traces, iv, order=1, key_guess=0, window_size_fma=5, num_of_traces=0): if order == 1: max_cpa = [0] * 1 @@ -90,6 +90,7 @@ def calculate_dpa(traces, iv, order=1, key_guess=0, window_size_fma=5, window_si return cpa_output, guess_corr, guess + def calculate_second_order_dpa_mem_efficient(traces, IV, window_width): num_of_samples = traces.shape[1] num_of_traces = traces.shape[0] @@ -108,7 +109,8 @@ def calculate_second_order_dpa_mem_efficient(traces, IV, window_width): while current_num_in_P < window_width: if current_partition_index == 0: - sub_array = np.abs(np.subtract(traces[:, current_P_index + 1:], traces[:, current_P_index].reshape(-1, 1))) + sub_array = np.abs( + np.subtract(traces[:, current_P_index + 1:], traces[:, current_P_index].reshape(-1, 1))) length_sub_array = sub_array.shape[1] if length_sub_array + current_num_in_P <= window_width: @@ -121,26 +123,25 @@ def calculate_second_order_dpa_mem_efficient(traces, IV, window_width): current_num_in_P = window_width current_partition_index = number_to_add else: - sub_array = np.abs(np.subtract(traces[:, current_P_index + 1:], traces[:, current_P_index].reshape(-1, 1))) + sub_array = np.abs( + np.subtract(traces[:, current_P_index + 1:], traces[:, current_P_index].reshape(-1, 1))) length_sub_array = sub_array.shape[1] num_left_to_add = length_sub_array - current_partition_index if num_left_to_add <= window_width: - P[:, current_num_in_P: num_left_to_add] = sub_array[:,current_partition_index:] + P[:, current_num_in_P: num_left_to_add] = sub_array[:, current_partition_index:] current_num_in_P = current_num_in_P + num_left_to_add current_P_index = current_P_index + 1 current_partition_index = 0 else: - P[:] = sub_array[:,current_partition_index:current_partition_index+window_width] + P[:] = sub_array[:, current_partition_index:current_partition_index + window_width] current_num_in_P = window_width current_partition_index = current_partition_index + window_width - #solve for cpa output dpa_trace = P - t_bar = np.mean(dpa_trace) o_t = std_dev(dpa_trace, t_bar) hws = np.array([[intermediate_value(textout) for textout in IV[0:num_of_traces]]]).transpose() @@ -156,11 +157,12 @@ def calculate_second_order_dpa_mem_efficient(traces, IV, window_width): current_num_in_P = 0 P = np.zeros((num_of_traces, num_to_add)) - if(num_to_add > 0): + if num_to_add > 0: while current_num_in_P < num_to_add: if current_partition_index == 0: - sub_array = np.abs(np.subtract(traces[:, current_P_index + 1:], traces[:, current_P_index].reshape(-1, 1))) + sub_array = np.abs( + np.subtract(traces[:, current_P_index + 1:], traces[:, current_P_index].reshape(-1, 1))) length_sub_array = sub_array.shape[1] if length_sub_array + current_num_in_P <= num_to_add: @@ -173,18 +175,19 @@ def calculate_second_order_dpa_mem_efficient(traces, IV, window_width): current_num_in_P = num_to_add current_partition_index = number_to_add else: - sub_array = np.abs(np.subtract(traces[:, current_P_index + 1:], traces[:, current_P_index].reshape(-1, 1))) + sub_array = np.abs( + np.subtract(traces[:, current_P_index + 1:], traces[:, current_P_index].reshape(-1, 1))) length_sub_array = sub_array.shape[1] num_left_to_add = length_sub_array - current_partition_index if num_left_to_add <= num_to_add: - P[:, current_num_in_P: num_left_to_add] = sub_array[:,current_partition_index:] + P[:, current_num_in_P: num_left_to_add] = sub_array[:, current_partition_index:] current_num_in_P = current_num_in_P + num_left_to_add current_P_index = current_P_index + 1 current_partition_index = 0 else: - P[:] = sub_array[:,current_partition_index:current_partition_index+num_to_add] + P[:] = sub_array[:, current_partition_index:current_partition_index + num_to_add] current_num_in_P = num_to_add current_partition_index = current_partition_index + num_to_add @@ -197,8 +200,6 @@ def calculate_second_order_dpa_mem_efficient(traces, IV, window_width): # hws_bar = mean(out[:,5]) o_hws = std_dev(hws, hws_bar) correlation = cov(dpa_trace, t_bar, hws, hws_bar) - cpaoutput[start_index :] = correlation / (o_t * o_hws) - - + cpaoutput[start_index:] = correlation / (o_t * o_hws) - return cpaoutput \ No newline at end of file + return cpaoutput