diff --git a/preprocess.py b/preprocess.py new file mode 100644 index 0000000000000000000000000000000000000000..8527a6d1641b3c658f3fafd8c6c4639aae329bc9 --- /dev/null +++ b/preprocess.py @@ -0,0 +1,43 @@ +from re import TEMPLATE +from os import path + +import numpy as np +from matplotlib import pyplot as plt +from scipy import signal + +def shift_trace(trace, shift): + length = len(trace) + trace_aligned = np.zeros((length), dtype = np.float32) + if shift < 0: + shift = length + shift + trace_aligned[:length - shift] = trace[shift:] + trace_aligned[length - shift:] = trace[:shift] + return trace_aligned + +num_points = 25000 #number of traces +precision = 100 #interval of prints + +open_path = "" #path to the traces +save_path = "" #path to save preprocessed traces +template_path = "" #path to save the template + + +for index in range(num_points): + trace = np.load(path.join(open_path,"avg__{}.npy".format(index))) + + if index < 1: + template=trace + np.save(path.join(save_path, "avg__{}.npy".format(index)), trace) + continue + + else: + correlation = signal.correlate(trace**2, template**2) + shift = np.argmax(correlation) - (len(trace)-1) + trace = shift_trace(trace, shift) + np.save(path.join(save_path, "avg__{}.npy".format(index)), trace) + template = (trace + (float(index))*template)/(float(index+1)) + + if index % precision == 0: + print("Nb processed={}".format(index)) + +np.save(template_path,template) \ No newline at end of file diff --git a/preprocess_2.py b/preprocess_2.py new file mode 100644 index 0000000000000000000000000000000000000000..d02622b7759d63ce8e7915a8165343901bbcd154 --- /dev/null +++ b/preprocess_2.py @@ -0,0 +1,43 @@ +import sys +import os +from os import path, system + +import numpy as np +from matplotlib import pyplot as plt +from scipy import signal + +def shift_trace(trace, shift): + length = len(trace) + trace_aligned = np.zeros((length), dtype = np.float32) + if shift < 0: + shift = length + shift + trace_aligned[:length - shift] = trace[shift:] + trace_aligned[length - shift:] = trace[:shift] + return trace_aligned + + +profile_traces_path = "" # repertoire des 25 000 traces pour le profile +open_path = "" #repertoire des 2 500 traces collectees pour l attaque +save_path = "" #repertoire de sauvegarde des 2 500 traces pour l attaque + + +num_points_profile = 50000 +traces = [] +for index in range(num_points_profile): + traces.append( np.load(path.join(profile_traces_path, 'avg__%d.npy' %(index) ) ) ) +template = np.average(traces, axis=0) + + +num_points_attack = 25000 + +for index in range(num_points_attack): + if index % 500 == 0: + print "Nb processed= %d" % index + trace = np.load(path.join(open_path, 'avg__%d.npy' %(index) ) ) + + correlation = signal.correlate(trace**2, template**2) + shift = np.argmax(correlation) - (len(template)-1) + trace = shift_trace(trace, shift) + + np.save(path.join(save_path, 'avg__%d.npy' % (index) ), trace) +