Commit 7758a155c0dec947997a2208e356d124f366cf9b

Authored by Brice Colombier
1 parent fc8e9fac14
Exists in master

Add F-test

Showing 6 changed files with 1571 additions and 1506 deletions

... ... @@ -9,4 +9,5 @@
9 9 test*.*
10 10 *.png
11 11 traces/
  12 +data/
... ... @@ -7,7 +7,7 @@
7 7 def realign(traces_file):
8 8 traces = np.load(traces_file)
9 9 averaged_traces = np.mean(traces, axis=0)
10   - print np.shape(averaged_traces)
  10 + print(np.shape(averaged_traces))
11 11 dirname, filename = os.path.split(args.traces_file)
12 12 np.save(os.path.join(dirname, "averaged_"+filename), averaged_traces[None,:])
13 13  
bin_to_npy.py View file @ 7758a15
... ... @@ -8,11 +8,11 @@
8 8 if not nb_columns:
9 9 nb_columns = int(np.shape(traces)[0]/nb_rows)
10 10 traces = np.reshape(traces, (nb_rows, nb_columns))
11   - print np.shape(traces)
  11 + print(np.shape(traces))
12 12 dirname, filename = os.path.split(traces_name)
13 13 filename, extension = os.path.splitext(filename)
14 14  
15   - print os.path.join(dirname, filename)+".npy"
  15 + print(os.path.join(dirname, filename)+".npy")
16 16 np.save(os.path.join(dirname, filename)+".npy", traces)
17 17  
18 18 if __name__ == "__main__":
  1 +import numpy as np
  2 +import matplotlib.pyplot as plt
  3 +import scipy.stats as stats
  4 +
  5 +import argparse
  6 +
  7 +import warnings
  8 +warnings.filterwarnings('error')
  9 +
  10 +def generate_dummy_data(n_traces, n_samples):
  11 +
  12 + vals = np.random.randint(0, 256, n_traces)
  13 + traces = np.random.normal(0, 1, (n_traces, n_samples))
  14 + traces[:,5]+=0.1*vals
  15 + return (traces, vals)
  16 +
  17 +
  18 +def find_extreme_f_values(f_statistics):
  19 + d = np.abs(f_statistics - np.median(f_statistics))
  20 + mdev = np.median(d)
  21 + s = d/mdev if mdev else 0.
  22 + extreme_vals = f_statistics[s>7]
  23 + return np.where(np.isin(f_statistics, extreme_vals) == True)[0]
  24 +
  25 +
  26 +def f_test(traces, intermediate_values, highlight):
  27 +
  28 + nb_traces, nb_samples = np.shape(traces)
  29 + f_statistics = np.zeros(nb_samples)
  30 + int_val_clusters = [[index for (index, val) in enumerate(intermediate_values) if (val == target)] for target in range(256)]
  31 + for sample_index, samples in enumerate(traces.T):
  32 + if sample_index%(nb_samples/10)==0:
  33 + print("{:>2}%".format(int(100*sample_index/nb_samples)))
  34 + samples_clusters = [samples[int_val_cluster] for int_val_cluster in int_val_clusters]
  35 + try:
  36 + f_statistic, p_value = stats.f_oneway(*samples_clusters)
  37 + except RuntimeWarning:
  38 + raise ValueError("Cluster(s) of intermediate values #{} are empty:".format(int_val_clusters.index([])))
  39 + f_statistics[sample_index] = f_statistic
  40 + if highlight:
  41 + plt.plot(np.average(traces, axis=0))
  42 + for extreme_val in find_extreme_f_values(f_statistics):
  43 + plt.axvspan(extreme_val-1, extreme_val+1, color='red', alpha=0.5)
  44 + else:
  45 + plt.plot(f_statistics, "o", markersize=2, color='red')
  46 + plt.show()
  47 +
  48 +
  49 +if __name__ == "__main__":
  50 +
  51 + parser = argparse.ArgumentParser(description='Compute F-test on a set of traces')
  52 + parser.add_argument("traces_names", type=str, help="name of the traces file")
  53 + parser.add_argument("intermediate_values", type=str, help="name of the intermediate values file")
  54 + parser.add_argument("subset", type=int, nargs='?', default=0, help="size of the subset of traces to consider")
  55 + parser.add_argument("-hi", "--highlight", action="store_true", help="highlight leakage on traces")
  56 + args = parser.parse_args()
  57 +
  58 + # traces, int_val = generate_dummy_data(20000, 30)
  59 + traces = np.load(args.traces_names)
  60 + int_val = np.load(args.intermediate_values)
  61 + if args.subset:
  62 + traces = traces[:args.subset,:]
  63 + int_val = int_val[:args.subset]
  64 + sca_f_test(traces, int_val, args.highlight)
filter_highest_variance.py View file @ 7758a15
... ... @@ -45,4 +45,5 @@
45 45 elif args.nsamples:
46 46 filtered_variance_traces = filter_highest_variance(traces, nsamples=args.nsamples)
47 47 np.save(os.path.join(dirname, filename+"_filtered_highest_variance_nsamples_"+str(args.nsamples)+extension), filtered_variance_traces)
  48 +
get_mat_dim.py View file @ 7758a15
... ... @@ -11,5 +11,5 @@
11 11 args = parser.parse_args()
12 12  
13 13 traces = np.load(args.traces_name)
14   - print "Matrix of {0} of size {1}".format(traces.dtype, np.shape(traces))
  14 + print("Matrix of {0} of size {1}".format(traces.dtype, np.shape(traces)))