Skip to content

Commit fde9769

Browse files
author
lolololol
committed
added resample behaviour for tracediff
1 parent 82a9610 commit fde9769

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

lib/sparkgap/filemanager.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#!/usr/bin/env python3
22

3-
# v2: hdf5
4-
53
import scipy.io
64
import h5py
75
import numpy
86
import numpy as np
97
import os
108
import sys
11-
# import matplotlib.pyplot as plt
9+
import random
1210

1311
class CaptureSet:
1412
def __init__(self,tracecount=15000,samplecount=100000,in_len=16,out_len=16,migrateData=False):
@@ -96,8 +94,6 @@ def slice(self,offset,numsamples):
9694
def getMeant(self):
9795
print("TraceManager2: getMeant called")
9896
m = numpy.mean(self.traces,axis=0)
99-
# plt.plot(m)
100-
# plt.show()
10197
return m
10298

10399
def loadCiphertexts(self):
@@ -136,6 +132,12 @@ def cutTraces(self,start,end):
136132
del self.traces
137133
self.traces = self.tracesNew
138134

135+
def randomSelect(self,samplesz):
136+
print("TraceManager2: Redrawing magic circle")
137+
print(np.random.choice(self.traces.shape[0], size = samplesz, replace=False))
138+
self.traces = self.traces[sorted(np.random.choice(self.traces.shape[0], size = samplesz, replace=False))]
139+
self.traceCount = len(self.traces)
140+
139141
def __init__(self,filename):
140142
print("TraceManager2: Initializing with filename %s" % filename)
141143
self.fn = filename
@@ -165,15 +167,4 @@ def save(fn,traces=None,data=None,data_out=None,freq=0,additionalParams={}):
165167
# tn.f.create_dataset("data_out",data=data_out)
166168

167169
if __name__ == "__main__":
168-
if len(sys.argv) != 2:
169-
print("This is not meant to be called directly :)")
170-
tm = TraceManager(sys.argv[1])
171-
f1 = open("inputs.txt","w")
172-
f2 = open("outputs.txt","w")
173-
for i in range(0,len(tm.data_in)):
174-
d1 = tm.data_in[i]
175-
f1.write("".join(["%02x" % x for x in d1]) + "\n")
176-
d2 = tm.data_out[i]
177-
f2.write("".join(["%02x" % x for x in d2]) + "\n")
178-
f1.close()
179-
f2.close()
170+
print("This file should not be called directly :)")

tracediff.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def doCompareTraces_SINGLE(tn1):
5656
TRACE_FILES = []
5757
CONFIG_OFFSET = None
5858
CONFIG_SAMPLES = None
59+
CONFIG_SAMPSZ = None
5960

6061
import random
6162
# group 1 is random, group2 is fixed.
@@ -108,10 +109,13 @@ def onclick(event):
108109
lastX = localX
109110

110111
def doCompareTraces(tn1,tn2):
111-
global CONFIG_OFFSET,CONFIG_SAMPLES
112+
global CONFIG_OFFSET,CONFIG_SAMPLES,CONFIG_SAMPSZ
112113
tm_in1 = sparkgap.filemanager.TraceManager(tn1)
113-
trace_avg1 = tm_in1.getMeant()
114114
tm_in2 = sparkgap.filemanager.TraceManager(tn2)
115+
if CONFIG_SAMPSZ is not None:
116+
tm_in1.randomSelect(CONFIG_SAMPSZ)
117+
tm_in2.randomSelect(CONFIG_SAMPSZ)
118+
trace_avg1 = tm_in1.getMeant()
115119
trace_avg2 = tm_in2.getMeant()
116120
if len(trace_avg1) != len(trace_avg2):
117121
print("The traces do not have equal lengths, I can't use this")
@@ -132,16 +136,23 @@ def doCompareTraces(tn1,tn2):
132136
a3.set_title("Avg Standard Deviation (Noise)")
133137
# (nn0,nn1,points) = doTLVA(tm_in1,tm_in2)
134138
group1_traces = []
139+
group2_traces = []
135140
for f in range(0,tm_in1.getTraceCount()):
136141
group1_traces.append(tm_in1.getSingleTrace(f)[CONFIG_OFFSET:CONFIG_OFFSET+CONFIG_SAMPLES])
137-
a3.plot(np.std(group1_traces,axis=0,keepdims=True)[0])
142+
for f in range(0,tm_in2.getTraceCount()):
143+
group2_traces.append(tm_in2.getSingleTrace(f)[CONFIG_OFFSET:CONFIG_OFFSET+CONFIG_SAMPLES])
144+
a3.plot(np.std(group1_traces,axis=0,keepdims=True)[0],label=os.path.basename(tn1))
145+
a3.plot(np.std(group2_traces,axis=0,keepdims=True)[0], label = os.path.basename(tn2))
146+
a3.legend()
138147
plt.show()
139148

140149
if __name__ == "__main__":
141-
opts,remainder = getopt.getopt(sys.argv[1:],"f:o:n:",["file=","offset=","samples="])
150+
opts,remainder = getopt.getopt(sys.argv[1:],"f:o:n:",["file=","offset=","samples=","samplesize="])
142151
for opt, arg in opts:
143152
if opt in ("-f","--file"):
144153
TRACE_FILES.append(arg)
154+
elif opt == "--samplesize":
155+
CONFIG_SAMPSZ = int(arg)
145156
elif opt in ("-o","--offset"):
146157
CONFIG_OFFSET = int(arg)
147158
elif opt in ("-n","--samples"):

0 commit comments

Comments
 (0)