From a23592c47ada51c73576c1be568f9dea35641039 Mon Sep 17 00:00:00 2001
From: Ferreol Axel <axel.ferreol@student-cs.fr>
Date: Thu, 9 Jun 2022 09:37:08 +0200
Subject: [PATCH] Add preprocess file

---
 preprocess.py   | 43 +++++++++++++++++++++++++++++++++++++++++++
 preprocess_2.py | 43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+)
 create mode 100644 preprocess.py
 create mode 100644 preprocess_2.py

diff --git a/preprocess.py b/preprocess.py
new file mode 100644
index 0000000..8527a6d
--- /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 0000000..d02622b
--- /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) 
+
-- 
GitLab