aboutsummaryrefslogtreecommitdiff
path: root/util/plot_gen.py
diff options
context:
space:
mode:
Diffstat (limited to 'util/plot_gen.py')
-rwxr-xr-xutil/plot_gen.py177
1 files changed, 113 insertions, 64 deletions
diff --git a/util/plot_gen.py b/util/plot_gen.py
index 98d8261..0d518e6 100755
--- a/util/plot_gen.py
+++ b/util/plot_gen.py
@@ -12,21 +12,24 @@
#
import numpy as np
+from scipy.stats import entropy
import matplotlib.pyplot as plt
-import matplotlib.ticker as ticker
-import matplotlib.colors as colors
-from operator import itemgetter
+from matplotlib import ticker, colors
from copy import deepcopy
import argparse
+from utils import hw, moving_average, plot_hist
+
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Plot results of ECTester key generation timing.")
parser.add_argument("-o", "--output", dest="output", type=argparse.FileType("wb"), help="Write image to [file], do not display.", metavar="file")
- parser.add_argument("--pub", dest="pub", action="store_true", help="Show public key scatter plot.")
- parser.add_argument("--priv", dest="priv", action="store_true", help="Show private key scatter plot.")
- parser.add_argument("--hist", dest="hist", action="store_true", help="Show histogram.")
- parser.add_argument("--hw-hist", dest="hw_hist", action="store_true", help="Show Hamming weight 2D histogram (private key Hamming weight and generation time).")
- parser.add_argument("--skip-first", dest="skip_first", action="store_true", help="Skip first entry, as it's usually a large outlier.")
+ parser.add_argument("--priv", dest="priv", action="store_true", help="Show private key MSB heatmap plot.")
+ parser.add_argument("--hist", dest="hist", action="store_true", help="Show keygen time histogram.")
+ parser.add_argument("--export-hist", dest="export_hist", action="store_true", help="Show export time histogram.")
+ parser.add_argument("--avg", dest="avg", action="store_true", help="Show moving average of keygen time.")
+ parser.add_argument("--hw-hist", dest="hw_hist", action="store_true", help="Show Hamming weight heatmap (private key Hamming weight and keygen time).")
+ parser.add_argument("--log", dest="log", action="store_true", help="Use logarithmic scale.")
+ parser.add_argument("--skip-first", dest="skip_first", nargs="?", const=1, type=int, help="Skip first entry, as it's usually a large outlier.")
parser.add_argument("-t", "--title", dest="title", type=str, nargs="?", default="", help="What title to give the figure.")
parser.add_argument("file", type=str, help="The file to plot(csv).")
@@ -35,27 +38,53 @@ if __name__ == "__main__":
with open(opts.file, "r") as f:
header = f.readline()
header_names = header.split(";")
+ if len(header_names) not in (4, 5):
+ print("Bad data?")
+ exit(1)
- plots = [opts.priv, opts.pub, opts.hist, opts.hw_hist]
+ plots = [opts.priv, opts.hist, opts.export_hist, opts.avg, opts.hw_hist]
n_plots = sum(plots)
if n_plots == 0:
- n_plots = 4
- plots = [True, True, True, True]
+ plots = [True for _ in range(5)]
+ if len(header_names) == 4:
+ n_plots = 4
+ plots[2] = False
+ else:
+ n_plots = 5
+
+
+ if plots[2] and len(header_names) != 5:
+ n_plots = n_plots - 1
+ if n_plots == 0:
+ print("Nothing to plot.")
+ exit(1)
+ plots[2] = False
hx = lambda x: int(x, 16)
- data = np.genfromtxt(opts.file, delimiter=";", skip_header=1, converters={2: hx, 3: hx}, dtype=np.dtype([("index","u4"), ("time","u4"), ("pub", "O"), ("priv", "O")]))
+ if len(header_names) == 4:
+ data = np.genfromtxt(opts.file, delimiter=";", skip_header=1, converters={2: hx, 3: hx}, dtype=np.dtype([("index", "u4"), ("gen_time", "u4"), ("pub", "O"), ("priv", "O")]))
+ else:
+ data = np.genfromtxt(opts.file, delimiter=";", skip_header=1, converters={3: hx, 4: hx}, dtype=np.dtype([("index", "u4"), ("gen_time", "u4"), ("export_time", "u4"), ("pub", "O"), ("priv", "O")]))
+
if opts.skip_first:
- data = data[1:]
+ data = data[opts.skip_first:]
- if "nano" in header_names[1]:
- unit = r"$\mu s$"
- time_data = map(lambda x: x[1]//1000, data)
- else:
- unit = r"ms"
- time_data = map(itemgetter(1), data)
- time_data = list(time_data)
- priv_data = list(map(itemgetter(2), data))
- pub_data = list(map(itemgetter(3), data))
+
+ gen_time_data = data["gen_time"]
+ export_time_data = None
+ if "export_time" in data.dtype.names:
+ export_time_data = data["export_time"]
+ pub_data = data["pub"]
+ priv_data = data["priv"]
+
+ gen_unit = "ms"
+ if header_names[1].endswith("[nano]"):
+ gen_unit = r"$\mu s$"
+ np.floor_divide(gen_time_data, 1000, out=gen_time_data)
+ export_unit = "ms"
+ if len(header_names) == 5 and header_names[2].endswith("[nano]"):
+ export_unit = r"$\mu s$"
+ np.floor_divide(export_time_data, 1000, out=export_time_data)
plt.style.use("ggplot")
fig = plt.figure()
@@ -66,66 +95,86 @@ if __name__ == "__main__":
elif opts.title:
fig.suptitle(opts.title)
layout_kwargs["rect"] = [0, 0.02, 1, 0.98]
- fig.tight_layout(**layout_kwargs)
+ fig.tight_layout(**layout_kwargs)
+
+ max_gen_time = max(gen_time_data)
+ min_gen_time = min(gen_time_data)
+ bit_size = len(bin(max(priv_data))) - 2
+
+ sorted_data = np.sort(data, order="gen_time")
+
+ i = 0
+ entropies = {}
+ while i < len(data):
+ time_val = sorted_data["gen_time"][i]
+ j = i
+ msbs = [0 for _ in range(256)]
+ while j < len(data) and sorted_data["gen_time"][j] == time_val:
+ msbs[(sorted_data["priv"][j] >> (bit_size - 8)) & 0xff] += 1
+ j += 1
+ if j - 100 > i:
+ entropies[time_val] = entropy(msbs, base=2)
+ i = j
+
+ entropy = sum(entropies.values())/len(entropies)
+
+ cmap = deepcopy(plt.cm.plasma)
+ cmap.set_bad("black")
+
+ norm = colors.Normalize()
+ if opts.log:
+ norm = colors.LogNorm()
plot_i = 1
if plots[0]:
axe_private = fig.add_subplot(n_plots, 1, plot_i)
- axe_private.scatter(time_data, priv_data, marker="x", s=10)
- axe_private.set_ylabel("private key value\n(big endian)")
- axe_private.set_xlabel("time ({})".format(unit))
+ priv_msb = np.array(list(map(lambda x: x >> (bit_size - 8), priv_data)), dtype=np.dtype("u1"))
+ max_msb = max(priv_msb)
+ min_msb = min(priv_msb)
+ heatmap, xedges, yedges = np.histogram2d(priv_msb, gen_time_data, bins=[max_msb - min_msb, max_gen_time - min_gen_time])
+ extent = [min_msb, max_msb, yedges[0], yedges[-1]]
+ axe_private.imshow(heatmap.T, extent=extent, aspect="auto", cmap=cmap, origin="low", interpolation="nearest", norm=norm)
+ axe_private.set_xlabel("private key MSB value")
+ axe_private.set_ylabel("keygen time ({})".format(gen_unit))
plot_i += 1
if plots[1]:
- axe_public = fig.add_subplot(n_plots, 1, plot_i)
- axe_public.scatter(time_data, pub_data, marker="x", s=10)
- axe_public.set_ylabel("public key value\n(big endian)")
- axe_public.set_xlabel("time ({})".format(unit))
+ axe_hist = fig.add_subplot(n_plots, 1, plot_i)
+ plot_hist(axe_hist, gen_time_data, "keygen time ({})".format(gen_unit), opts.log)
plot_i += 1
if plots[2]:
axe_hist = fig.add_subplot(n_plots, 1, plot_i)
- time_max = max(time_data)
- time_avg = np.average(time_data)
- time_median = np.median(time_data)
- axe_hist.hist(time_data, bins=time_max//3, log=True)
- axe_hist.axvline(x=time_avg, alpha=0.7, linestyle="dotted", color="red", label="avg = {}".format(time_avg))
- axe_hist.axvline(x=time_median, alpha=0.7, linestyle="dotted", color="green", label="median = {}".format(time_median))
- axe_hist.set_ylabel("count\n(log)")
- axe_hist.set_xlabel("time ({})".format(unit))
- axe_hist.xaxis.set_major_locator(ticker.MaxNLocator())
- axe_hist.legend(loc="best")
+ plot_hist(axe_hist, export_time_data, "export time ({})".format(export_unit), opts.log)
plot_i += 1
if plots[3]:
- priv_bit_bins = {}
- for i in range(len(data)):
- skey = priv_data[i]
- time = time_data[i]
- skey_hw = 0
- while skey:
- skey_hw += 1
- skey &= skey - 1
- if skey_hw in priv_bit_bins:
- priv_bit_bins[skey_hw].append(time)
- else:
- priv_bit_bins[skey_hw] = [time]
- priv_bit_x = []
- priv_bit_y = []
- for k,v in priv_bit_bins.items():
- priv_bit_x.extend([k] * len(v))
- priv_bit_y.extend(v)
+ axe_avg = fig.add_subplot(n_plots, 1, plot_i)
+ #if len(header_names) == 5:
+ # axe_other = axe_avg.twinx()
+ # axe_other.plot(moving_average(export_time_data, 100), color="green", alpha=0.6, label="export, window = 100")
+ # axe_other.plot(moving_average(export_time_data, 1000), color="yellow", alpha=0.6, label="export, window = 1000")
+ # axe_other.legend(loc="lower right")
+ axe_avg.plot(moving_average(gen_time_data, 100), label="window = 100")
+ axe_avg.plot(moving_average(gen_time_data, 1000), label="window = 1000")
+ axe_avg.set_ylabel("keygen time ({})".format(gen_unit))
+ axe_avg.set_xlabel("index")
+ axe_avg.legend(loc="best")
+ plot_i += 1
+
+ if plots[4]:
axe_priv_hist = fig.add_subplot(n_plots, 1, plot_i)
- h, xe, ye = np.histogram2d(priv_bit_x, priv_bit_y, bins=[max(priv_bit_bins) - min(priv_bit_bins), (max(time_data) - min(time_data))//5])
- cmap = deepcopy(plt.cm.plasma)
- cmap.set_bad("black")
- im = axe_priv_hist.imshow(h.T, origin="low", cmap=cmap, aspect="auto", extent=[xe[0], xe[-1], ye[0], ye[-1]], norm=colors.LogNorm())
+ priv_hw = np.array(list(map(hw, priv_data)), dtype=np.dtype("u2"))
+ h, xe, ye = np.histogram2d(priv_hw, gen_time_data, bins=[max(priv_hw) - min(priv_hw), max_gen_time - min_gen_time])
+ im = axe_priv_hist.imshow(h.T, origin="low", cmap=cmap, aspect="auto", extent=[xe[0], xe[-1], ye[0], ye[-1]], norm=norm)
+ axe_priv_hist.axvline(x=bit_size//2, alpha=0.7, linestyle="dotted", color="white", label=str(bit_size//2) + " bits")
axe_priv_hist.set_xlabel("private key Hamming weight")
- axe_priv_hist.set_ylabel("time ({})".format(unit))
+ axe_priv_hist.set_ylabel("keygen time ({})".format(gen_unit))
+ axe_priv_hist.legend(loc="best")
fig.colorbar(im, ax=axe_priv_hist)
- if plot_i > 2:
- fig.text(0.01, 0.02, "Data size: {}".format(len(time_data)), size="small")
+ fig.text(0.01, 0.02, "Data size: {}".format(len(gen_time_data)), size="small")
+ fig.text(0.01, 0.04, "Entropy of privkey MSB(estimated): {:.2f} b".format(entropy), size="small")
if opts.output is None:
plt.show()