aboutsummaryrefslogtreecommitdiff
path: root/util/plot_gen.ipynb
diff options
context:
space:
mode:
authorJ08nY2019-03-21 18:10:50 +0100
committerJ08nY2019-03-21 18:10:50 +0100
commit74f08103b0b17c9139c18168e27f79efe6324eb6 (patch)
tree353cc52c55b79a7e034115a2f7297faf9dccf24c /util/plot_gen.ipynb
parent8dda00c46e73f2a44e7c387a6b4e86055ffecea2 (diff)
downloadECTester-74f08103b0b17c9139c18168e27f79efe6324eb6.tar.gz
ECTester-74f08103b0b17c9139c18168e27f79efe6324eb6.tar.zst
ECTester-74f08103b0b17c9139c18168e27f79efe6324eb6.zip
Diffstat (limited to 'util/plot_gen.ipynb')
-rw-r--r--util/plot_gen.ipynb111
1 files changed, 89 insertions, 22 deletions
diff --git a/util/plot_gen.ipynb b/util/plot_gen.ipynb
index db618cc..1b8e94d 100644
--- a/util/plot_gen.ipynb
+++ b/util/plot_gen.ipynb
@@ -272,13 +272,15 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
- "end_time": "2019-03-17T19:16:44.688872Z",
- "start_time": "2019-03-17T19:16:44.684485Z"
+ "end_time": "2019-03-19T13:41:01.468943Z",
+ "start_time": "2019-03-19T13:41:01.417360Z"
}
},
"outputs": [],
"source": [
- "display(\"Bitsize:\", bit_size)"
+ "display(\"Bitsize:\", bit_size)\n",
+ "display(\"Histogram time bins: {}\".format(hist_size_gen_time))\n",
+ "display(\"Histogram time bins(trimmed): {}\".format(hist_size_gen_time_trim))"
]
},
{
@@ -308,7 +310,7 @@
"outputs": [],
"source": [
"fig_private = plt.figure(figsize=(10.5, 8), dpi=90)\n",
- "axe_private = fig_private.add_subplot(1, 1, 1)\n",
+ "axe_private = fig_private.add_subplot(1, 1, 1, title=\"Private key MSB vs keygen time\")\n",
"priv_msb = np.array(list(map(lambda x: x >> (bit_size - 8), data_trimmed[\"priv\"])), dtype=np.dtype(\"u1\"))\n",
"max_msb = max(priv_msb)\n",
"min_msb = min(priv_msb)\n",
@@ -321,6 +323,7 @@
"axe_private.set_ylabel(\"keygen time ({})\".format(gen_disp_unit))\n",
"fig_private.colorbar(im, ax=axe_private)\n",
"\n",
+ "fig_private.tight_layout()\n",
"del priv_msb"
]
},
@@ -347,8 +350,8 @@
"source": [
"fig_priv_hist = plt.figure(figsize=(10.5, 12), dpi=90)\n",
"gs = gridspec.GridSpec(2, 1, height_ratios=[2.5, 1])\n",
- "axe_priv_hist = fig_priv_hist.add_subplot(gs[0])\n",
- "axe_priv_hist_hw = fig_priv_hist.add_subplot(gs[1], sharex = axe_priv_hist)\n",
+ "axe_priv_hist = fig_priv_hist.add_subplot(gs[0], title=\"Private key Hamming weight vs keygen time\")\n",
+ "axe_priv_hist_hw = fig_priv_hist.add_subplot(gs[1], sharex=axe_priv_hist, title=\"Private key Hamming weight\")\n",
"priv_hw = np.array(list(map(hw, data_trimmed[\"priv\"])), dtype=np.dtype(\"u2\"))\n",
"h, xe, ye = np.histogram2d(priv_hw, data_trimmed[\"gen_time\"], bins=[max(priv_hw) - min(priv_hw), hist_size_gen_time_trim])\n",
"im = axe_priv_hist.imshow(h.T, origin=\"low\", cmap=cmap, aspect=\"auto\", extent=[xe[0], xe[-1], ye[0], ye[-1]], norm=norm)\n",
@@ -364,11 +367,13 @@
"norm_pdf = norm_dist.pdf(pdf_range, *param[:-2], loc=param[-2], scale=param[-1]) * description_gen_trim.nobs\n",
"axe_priv_hist_hw.plot(pdf_range, norm_pdf, label=\"fitted normal distribution\")\n",
"axe_priv_hist_hw.legend(loc=\"best\")\n",
- "fig_priv_hist.colorbar(im, ax=[axe_priv_hist, axe_priv_hist_hw])\n",
+ "\n",
"\n",
"display(HTML(\"<b>Private key Hamming weight fitted with normal distribution:</b>\"))\n",
"display(HTML(tabulate.tabulate([(\"Mean\", \"Variance\"), param], tablefmt=\"html\")))\n",
"\n",
+ "fig_priv_hist.tight_layout()\n",
+ "fig_priv_hist.colorbar(im, ax=[axe_priv_hist, axe_priv_hist_hw])\n",
"del priv_hw"
]
},
@@ -394,7 +399,8 @@
"axe_hist_full = fig_kg_hist.add_subplot(2, 1, 1)\n",
"axe_hist_trim = fig_kg_hist.add_subplot(2, 1, 2)\n",
"plot_hist(axe_hist_full, data[\"gen_time\"], \"keygen time ({})\".format(gen_disp_unit), log_scale, hist_size_gen_time);\n",
- "plot_hist(axe_hist_trim, data_trimmed[\"gen_time\"], \"keygen time ({})\".format(gen_disp_unit), log_scale, hist_size_gen_time_trim);"
+ "plot_hist(axe_hist_trim, data_trimmed[\"gen_time\"], \"keygen time ({})\".format(gen_disp_unit), log_scale, hist_size_gen_time_trim);\n",
+ "fig_kg_hist.tight_layout()"
]
},
{
@@ -421,7 +427,8 @@
" axe_hist_full = fig_exp_hist.add_subplot(2, 1, 1)\n",
" axe_hist_trim = fig_exp_hist.add_subplot(2, 1, 2)\n",
" plot_hist(axe_hist_full, data[\"export_time\"], \"export time ({})\".format(export_disp_unit), log_scale, hist_size_gen_time);\n",
- " plot_hist(axe_hist_trim, data_trimmed[\"export_time\"], \"export time ({})\".format(export_disp_unit), log_scale, hist_size_gen_time_trim);"
+ " plot_hist(axe_hist_trim, data_trimmed[\"export_time\"], \"export time ({})\".format(export_disp_unit), log_scale, hist_size_gen_time_trim);\n",
+ " fig_exp_hist.tight_layout()"
]
},
{
@@ -443,7 +450,7 @@
"outputs": [],
"source": [
"fig_avg = plt.figure(figsize=(10.5, 7), dpi=90)\n",
- "axe_avg = fig_avg.add_subplot(1, 1, 1)\n",
+ "axe_avg = fig_avg.add_subplot(1, 1, 1, title=\"Moving average of key generation time\")\n",
"avg_100 = moving_average(data[\"gen_time\"], 100)\n",
"avg_1000 = moving_average(data[\"gen_time\"], 1000)\n",
"axe_avg.plot(avg_100, label=\"window = 100\")\n",
@@ -455,6 +462,8 @@
"axe_avg.set_ylabel(\"keygen time ({})\".format(gen_disp_unit))\n",
"axe_avg.set_xlabel(\"index\")\n",
"axe_avg.legend(loc=\"best\")\n",
+ "\n",
+ "fig_avg.tight_layout()\n",
"del avg_100, avg_1000"
]
},
@@ -480,10 +489,12 @@
"fig_priv_hists = plt.figure(figsize=(10.5, 8), dpi=90)\n",
"priv_msb = np.array(list(map(lambda x: x >> (bit_size - 8), data[\"priv\"])), dtype=np.dtype(\"u1\"))\n",
"priv_lsb = np.array(list(map(lambda x: x & 0xff, data[\"priv\"])), dtype=np.dtype(\"u1\"))\n",
- "axe_msb_s_hist = fig_priv_hists.add_subplot(2, 1, 1)\n",
- "axe_lsb_s_hist = fig_priv_hists.add_subplot(2, 1, 2)\n",
+ "axe_msb_s_hist = fig_priv_hists.add_subplot(2, 1, 1, title=\"Private key MSB\")\n",
+ "axe_lsb_s_hist = fig_priv_hists.add_subplot(2, 1, 2, title=\"Private key LSB\")\n",
"plot_hist(axe_msb_s_hist, priv_msb, \"private key MSB\", log_scale)\n",
"plot_hist(axe_lsb_s_hist, priv_lsb, \"private key LSB\", log_scale)\n",
+ "\n",
+ "fig_priv_hists.tight_layout()\n",
"del priv_msb, priv_lsb"
]
},
@@ -517,8 +528,8 @@
"fig_pub_hists = plt.figure(figsize=(10.5, 14), dpi=90)\n",
"\n",
"def _plot_coord(data, name, offset):\n",
- " axe_msb_pub_hist = fig_pub_hists.add_subplot(4, 1, offset)\n",
- " axe_lsb_pub_hist = fig_pub_hists.add_subplot(4, 1, offset + 1)\n",
+ " axe_msb_pub_hist = fig_pub_hists.add_subplot(4, 1, offset, title=\"{} coordinate MSB\".format(name))\n",
+ " axe_lsb_pub_hist = fig_pub_hists.add_subplot(4, 1, offset + 1, title=\"{} coordinate LSB\".format(name))\n",
" pub_msb = np.array(list(map(lambda x: x >> (bit_size - 8), data)))\n",
" pub_lsb = np.array(list(map(lambda x: x & 0xff, data)))\n",
" plot_hist(axe_msb_pub_hist, pub_msb, \"{} coordinate MSB\".format(name), log_scale)\n",
@@ -528,6 +539,8 @@
"_plot_coord(xs, \"X\", 1)\n",
"_plot_coord(ys, \"Y\", 3)\n",
"\n",
+ "fig_pub_hists.tight_layout()\n",
+ "\n",
"del pub_coords, xs, ys"
]
},
@@ -552,8 +565,8 @@
"source": [
"fig_bl = plt.figure(figsize=(10.5, 12), dpi=90)\n",
"gs = gridspec.GridSpec(2, 1, height_ratios=[2.5, 1])\n",
- "axe_bl_heat = fig_bl.add_subplot(gs[0])\n",
- "axe_bl_hist = fig_bl.add_subplot(gs[1], sharex=axe_bl_heat)\n",
+ "axe_bl_heat = fig_bl.add_subplot(gs[0], title=\"Private key bit length vs keygen time\")\n",
+ "axe_bl_hist = fig_bl.add_subplot(gs[1], sharex=axe_bl_heat, title=\"Private key bit length\")\n",
"\n",
"bl_data = np.array(list(map(lambda x: x.bit_length(), data_trimmed[\"priv\"])), dtype=np.dtype(\"u2\"))\n",
"\n",
@@ -563,6 +576,8 @@
"axe_bl_heat.set_ylabel(\"keygen time ({})\".format(gen_disp_unit))\n",
"\n",
"plot_hist(axe_bl_hist, bl_data, \"Private key bit length\", log_scale, align=\"right\")\n",
+ "\n",
+ "fig_priv_hist.tight_layout()\n",
"fig_priv_hist.colorbar(im, ax=[axe_bl_heat, axe_bl_hist])\n",
"\n",
"del bl_data"
@@ -572,6 +587,48 @@
"cell_type": "markdown",
"metadata": {},
"source": [
+ "### Private key bit length histogram given time\n",
+ "Interactively shows the histogram of private key bit length given a selected time range centered around `center` of width `width`. Ideally, the means of these conditional distributions are equal, while the variances can vary."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "fig_bl_time = plt.figure(figsize=(10.5, 5), dpi=90)\n",
+ "axe_bl_time = fig_bl_time.add_subplot(111)\n",
+ "axe_bl_time.set_autoscalex_on(False)\n",
+ "def f(center, width):\n",
+ " lower_bnd = center - width/2\n",
+ " upper_bnd = center + width/2\n",
+ " values = data_trimmed[np.logical_and(data_trimmed[\"gen_time\"] <= upper_bnd,\n",
+ " data_trimmed[\"gen_time\"] >= lower_bnd)]\n",
+ " axe_bl_time.clear()\n",
+ " axe_bl_time.set_title(\"Private key bit length, given keygen time $\\in ({}, {})$ {}\".format(int(lower_bnd), int(upper_bnd), sign_disp_unit))\n",
+ " bl_data = np.array(list(map(lambda x: x.bit_length(), values[\"priv\"])), dtype=np.dtype(\"u2\"))\n",
+ " plot_hist(axe_bl_time, bl_data, \"private key bit length\", bins=11, range=(bit_size-10, bit_size+1), align=\"left\")\n",
+ " axe_bl_time.set_xlim((bit_size-10, bit_size))\n",
+ " fig_bl_time.tight_layout()\n",
+ "\n",
+ "center_w = widgets.IntSlider(min=min(data_trimmed[\"gen_time\"]),\n",
+ " max=max(data_trimmed[\"gen_time\"]),\n",
+ " step=1,\n",
+ " value=description_sign_trim.mean,\n",
+ " continuous_update=False,\n",
+ " description=\"center {}\".format(sign_disp_unit))\n",
+ "width_w = widgets.IntSlider(min=1, max=100, continuous_update=False,\n",
+ " description=\"width {}\".format(sign_disp_unit))\n",
+ "w = interactive(f, center=center_w,\n",
+ " width=width_w)\n",
+ "display(w)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
"## Validation\n",
"Perform some tests on the produced data and compare to expected results.\n",
"\n",
@@ -612,7 +669,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "All of the following tests should pass (e.g. be true):"
+ "All of the following tests should pass (e.g. be true), given a large enough sample:"
]
},
{
@@ -622,16 +679,26 @@
"outputs": [],
"source": [
"max_priv = max(data[\"priv\"])\n",
- "display(max_priv < r)\n",
- "display(r <= p or max_priv > p)\n",
- "display(max_priv.bit_length() == r.bit_length())"
+ "\n",
+ "print(\"Private keys are smaller than order:\\t\\t\\t\" + str(max_priv < r))\n",
+ "print(\"Private keys are larger than prime(if order > prime):\\t\" + str(r <= p or max_priv > p))\n",
+ "print(\"Private keys reach full bit length of order:\\t\\t\" + str(max_priv.bit_length() == r.bit_length()))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "print(\"Private key bit length (min, max):\" + str(min(data[\"priv\"]).bit_length()) + \", \" + str(max(data[\"priv\"]).bit_length()))"
]
}
],
"metadata": {
"@webio": {
- "lastCommId": "a0b5176ec9b441fc87297b9ed343c5b3",
- "lastKernelId": "a6864201-5c88-44d7-8438-ea96f7295d78"
+ "lastCommId": "bfabd932302644968194449673036179",
+ "lastKernelId": "b7098475-e3c7-43a2-9851-9b285c0afdc4"
},
"hide_input": false,
"kernelspec": {