aboutsummaryrefslogtreecommitdiff
path: root/util/plot_dh.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'util/plot_dh.ipynb')
-rw-r--r--util/plot_dh.ipynb176
1 files changed, 119 insertions, 57 deletions
diff --git a/util/plot_dh.ipynb b/util/plot_dh.ipynb
index 2e82292..e42b25c 100644
--- a/util/plot_dh.ipynb
+++ b/util/plot_dh.ipynb
@@ -9,11 +9,11 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 1,
"metadata": {
"ExecuteTime": {
- "end_time": "2019-03-18T18:35:11.337869Z",
- "start_time": "2019-03-18T18:35:11.331608Z"
+ "end_time": "2019-03-19T13:35:38.954375Z",
+ "start_time": "2019-03-19T13:35:38.578219Z"
}
},
"outputs": [],
@@ -27,7 +27,7 @@
"import matplotlib.pyplot as plt\n",
"from matplotlib import ticker, colors, gridspec\n",
"from copy import deepcopy\n",
- "from utils import plot_hist, moving_average, hw, time_scale\n",
+ "from utils import plot_hist, moving_average, hw, time_scale, hist_size_func\n",
"from binascii import unhexlify\n",
"from IPython.display import display, HTML\n",
"from ipywidgets import interact, interactive, fixed, interact_manual\n",
@@ -48,8 +48,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
- "end_time": "2019-03-18T18:35:28.957529Z",
- "start_time": "2019-03-18T18:35:28.952399Z"
+ "end_time": "2019-03-19T12:43:15.121139Z",
+ "start_time": "2019-03-19T12:43:15.117537Z"
}
},
"outputs": [],
@@ -87,7 +87,7 @@
"\n",
"# What function to use to calculate number of histogram bins of time\n",
"# one of \"sqrt\", \"sturges\", \"rice\", \"scott\" and \"fd\" or a number specifying the number of bins\n",
- "hist_size = \"rice\""
+ "hist_size = \"sturges\""
]
},
{
@@ -102,8 +102,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
- "end_time": "2019-03-18T18:35:30.394517Z",
- "start_time": "2019-03-18T18:35:29.499890Z"
+ "end_time": "2019-03-19T12:43:16.687260Z",
+ "start_time": "2019-03-19T12:43:16.031604Z"
}
},
"outputs": [],
@@ -170,21 +170,8 @@
"byte_size = (bit_size + 7) // 8\n",
"bit_size = byte_size * 8\n",
"\n",
- "if hist_size == \"sqrt\":\n",
- " hist_size_func = lambda n, xmin, xmax, var, xlower, xupper: int(sqrt(n)) + 1\n",
- "elif hist_size == \"sturges\":\n",
- " hist_size_func = lambda n, xmin, xmax, var, xlower, xupper: int(log(n, 2)) + 1\n",
- "elif hist_size == \"rice\":\n",
- " hist_size_func = lambda n, xmin, xmax, var, xlower, xupper: int(2 * n**(1/3))\n",
- "elif hist_size == \"scott\":\n",
- " hist_size_func = lambda n, xmin, xmax, var, xlower, xupper: (xmax - xmin) // int((3.5 * sqrt(var)) / (n**(1/3)))\n",
- "elif hist_size == \"fd\":\n",
- " hist_size_func = lambda n, xmin, xmax, var, xlower, xupper: (xmax - xmin) // int(2 * (xupper - xlower) / (n**(1/3)))\n",
- "else:\n",
- " hist_size_func = lambda n, xmin, xmax, var, xlower, xupper: hist_size\n",
- "\n",
- "hist_size_time = hist_size_func(description.nobs, min_time, max_time, description.variance, quantiles[1], quantiles[3])\n",
- "hist_size_time_trim = hist_size_func(description_trim.nobs, description_trim.minmax[0], description_trim.minmax[1], description_trim.variance, quantiles_trim[1], quantiles_trim[3])\n",
+ "hist_size_time = hist_size_func(hist_size)(description.nobs, min_time, max_time, description.variance, quantiles[1], quantiles[3])\n",
+ "hist_size_time_trim = hist_size_func(hist_size)(description_trim.nobs, description_trim.minmax[0], description_trim.minmax[1], description_trim.variance, quantiles_trim[1], quantiles_trim[3])\n",
"\n",
"if hist_size_time < 30:\n",
" hist_size_time = max_time - min_time\n",
@@ -211,8 +198,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
- "end_time": "2019-03-18T18:35:31.158217Z",
- "start_time": "2019-03-18T18:35:31.144280Z"
+ "end_time": "2019-03-19T12:43:17.706648Z",
+ "start_time": "2019-03-19T12:43:17.695215Z"
}
},
"outputs": [],
@@ -239,8 +226,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
- "end_time": "2019-03-18T18:35:32.593550Z",
- "start_time": "2019-03-18T18:35:32.588147Z"
+ "end_time": "2019-03-19T12:43:19.507884Z",
+ "start_time": "2019-03-19T12:43:19.502941Z"
}
},
"outputs": [],
@@ -262,8 +249,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
- "end_time": "2019-03-18T18:35:33.252850Z",
- "start_time": "2019-03-18T18:35:33.245928Z"
+ "end_time": "2019-03-19T12:43:20.963153Z",
+ "start_time": "2019-03-19T12:43:20.956502Z"
}
},
"outputs": [],
@@ -293,14 +280,14 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
- "end_time": "2019-03-18T18:35:34.581846Z",
- "start_time": "2019-03-18T18:35:34.472065Z"
+ "end_time": "2019-03-19T12:43:22.063050Z",
+ "start_time": "2019-03-19T12:43:21.967845Z"
}
},
"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 key agreement 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",
@@ -313,6 +300,7 @@
"axe_private.set_ylabel(\"key agreement time ({})\".format(time_disp_unit))\n",
"fig_private.colorbar(im, ax=axe_private)\n",
"\n",
+ "fig_private.tight_layout()\n",
"del priv_msb"
]
},
@@ -331,16 +319,16 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
- "end_time": "2019-03-18T18:35:39.673526Z",
- "start_time": "2019-03-18T18:35:38.253945Z"
+ "end_time": "2019-03-19T12:47:12.436692Z",
+ "start_time": "2019-03-19T12:47:11.310271Z"
}
},
"outputs": [],
"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 key agreement 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[\"time\"], bins=[max(priv_hw) - min(priv_hw), hist_size_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",
@@ -356,6 +344,8 @@
"norm_pdf = norm_dist.pdf(pdf_range, *param[:-2], loc=param[-2], scale=param[-1]) * description_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",
+ "\n",
+ "fig_priv_hist.tight_layout()\n",
"fig_priv_hist.colorbar(im, ax=[axe_priv_hist, axe_priv_hist_hw])\n",
"\n",
"display(HTML(\"<b>Private key Hamming weight fitted with normal distribution:</b>\"))\n",
@@ -376,8 +366,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
- "end_time": "2019-03-18T18:35:51.604298Z",
- "start_time": "2019-03-18T18:35:40.980632Z"
+ "end_time": "2019-03-19T12:47:20.496134Z",
+ "start_time": "2019-03-19T12:47:20.360405Z"
}
},
"outputs": [],
@@ -386,7 +376,9 @@
"axe_hist_full = fig_ka_hist.add_subplot(2, 1, 1)\n",
"axe_hist_trim = fig_ka_hist.add_subplot(2, 1, 2)\n",
"plot_hist(axe_hist_full, data[\"time\"], \"key agreement time ({})\".format(time_disp_unit), log_scale, hist_size_time);\n",
- "plot_hist(axe_hist_trim, data_trimmed[\"time\"], \"key agreement time ({})\".format(time_disp_unit), log_scale, hist_size_time_trim);"
+ "plot_hist(axe_hist_trim, data_trimmed[\"time\"], \"key agreement time ({})\".format(time_disp_unit), log_scale, hist_size_time_trim);\n",
+ "\n",
+ "fig_ka_hist.tight_layout()"
]
},
{
@@ -401,14 +393,14 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
- "end_time": "2019-03-18T18:36:00.467782Z",
- "start_time": "2019-03-18T18:36:00.418942Z"
+ "end_time": "2019-03-19T12:47:21.706585Z",
+ "start_time": "2019-03-19T12:47:21.658363Z"
}
},
"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 agreement time\")\n",
"avg_100 = moving_average(data[\"time\"], 100)\n",
"avg_1000 = moving_average(data[\"time\"], 1000)\n",
"axe_avg.plot(avg_100, label=\"window = 100\")\n",
@@ -421,6 +413,7 @@
"axe_avg.set_xlabel(\"index\")\n",
"axe_avg.legend(loc=\"best\")\n",
"\n",
+ "fig_avg.tight_layout()\n",
"del avg_100, avg_1000"
]
},
@@ -437,8 +430,8 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
- "end_time": "2019-03-18T18:36:02.558769Z",
- "start_time": "2019-03-18T18:36:02.216115Z"
+ "end_time": "2019-03-19T12:47:32.875112Z",
+ "start_time": "2019-03-19T12:47:32.542216Z"
},
"hide_input": false
},
@@ -447,11 +440,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",
"msb_h = plot_hist(axe_msb_s_hist, priv_msb, \"private key MSB\", log_scale, False, False)\n",
"lsb_h = plot_hist(axe_lsb_s_hist, priv_lsb, \"private key LSB\", log_scale, False, False)\n",
"\n",
+ "fig_priv_hists.tight_layout()\n",
"del priv_msb, priv_lsb"
]
},
@@ -468,16 +462,16 @@
"execution_count": null,
"metadata": {
"ExecuteTime": {
- "end_time": "2019-03-18T18:36:04.445752Z",
- "start_time": "2019-03-18T18:36:04.317542Z"
+ "end_time": "2019-03-19T12:47:34.638019Z",
+ "start_time": "2019-03-19T12:47:34.479903Z"
}
},
"outputs": [],
"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",
"bl_data = np.array(list(map(lambda x: x.bit_length(), data_trimmed[\"priv\"])), dtype=np.dtype(\"u2\"))\n",
"\n",
"h, xe, ye = np.histogram2d(bl_data, data_trimmed[\"time\"], bins=[max(bl_data) - min(bl_data), hist_size_time_trim])\n",
@@ -486,6 +480,8 @@
"axe_bl_heat.set_ylabel(\"key agreement time ({})\".format(time_disp_unit))\n",
"\n",
"plot_hist(axe_bl_hist, bl_data, \"Private key bit length\", log_scale, align=\"right\")\n",
+ "\n",
+ "fig_bl.tight_layout()\n",
"fig_bl.colorbar(im, ax=[axe_bl_heat, axe_bl_hist])\n",
"\n",
"del bl_data"
@@ -495,6 +491,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[\"time\"] <= upper_bnd,\n",
+ " data_trimmed[\"time\"] >= lower_bnd)]\n",
+ " axe_bl_time.clear()\n",
+ " axe_bl_time.set_title(\"Private key bit length, given key agreement 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[\"time\"]),\n",
+ " max=max(data_trimmed[\"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",
@@ -530,7 +568,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 and run without the `--fixed` or `-priv/-npriv` flags:"
]
},
{
@@ -540,16 +578,40 @@
"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())"
+ "un = len(np.unique(data[\"priv\"])) != 1\n",
+ "if un:\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": {
+ "ExecuteTime": {
+ "end_time": "2019-03-19T12:53:48.777395Z",
+ "start_time": "2019-03-19T12:53:48.766190Z"
+ }
+ },
+ "outputs": [],
+ "source": [
+ "if un:\n",
+ " print(\"Private key bit length (min, max):\" + str(min(data[\"priv\"]).bit_length()) + \", \" + str(max(data[\"priv\"]).bit_length()))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
}
],
"metadata": {
"@webio": {
- "lastCommId": "73e8d2ab400746298b234c8983722e8e",
- "lastKernelId": "cedfe41c-66b9-4611-ad6f-ab448422bbd2"
+ "lastCommId": "18de549d83f84e128074fc96e268d86f",
+ "lastKernelId": "875ada23-b504-4c96-8f24-68b5d72faab5"
},
"hide_input": false,
"kernelspec": {