aboutsummaryrefslogtreecommitdiff
path: root/epare/visualize.ipynb
blob: 709e5660c4907bd4de3e80b52b2409b0f44ce036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "52a95c74-8fc0-4021-a8e9-8587ff6f1d9e",
   "metadata": {},
   "source": [
    "# Visualizing prob-maps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3232df80-2a65-47ce-bc77-6a64f44d2404",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "import itertools\n",
    "import glob\n",
    "import gc\n",
    "\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "from tqdm.auto import tqdm, trange\n",
    "from statsmodels.stats.proportion import proportion_confint\n",
    "\n",
    "from pyecsca.ec.mult import *\n",
    "from pyecsca.misc.utils import TaskExecutor\n",
    "\n",
    "from common import *\n",
    "\n",
    "%matplotlib ipympl"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4273bd5e-0ec6-4e5c-b63e-74cc325a8ece",
   "metadata": {},
   "source": [
    "## Setup\n",
    "Setup some plotting and the computations of prob-maps out of the small scalar data and divisors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e89e66dc-4a9b-4320-8612-a8fa9af04b69",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup the ticks and colors deterministically.\n",
    "mult_klasses = sorted(list(set(map(lambda mult: mult.klass, all_mults))), key=lambda klass: klass.__name__)\n",
    "mult_kwarg_map = {klass: 0 for klass in mult_klasses}\n",
    "mult_cm_map = {mult: 0 for mult in all_mults}\n",
    "mult_colors = matplotlib.cm.tab20(range(len(mult_klasses)))\n",
    "mult_styles = ['-', '--', '-.', ':', (5, (10, 3)), (0, (5, 1)), (0, (3, 1, 1, 1, 1, 1)), (0, (3, 1, 1, 1)), (0, (1, 1)), (0, (3, 10, 1, 10))]\n",
    "mult_markers = [None, \"o\", \"+\", \"*\", \"^\", \"s\"]\n",
    "colors = {}\n",
    "styles = {}\n",
    "markers = {}\n",
    "for mult in all_mults:\n",
    "    color = mult_colors[mult_klasses.index(mult.klass) % len(mult_colors)]\n",
    "    style = mult_styles[mult_kwarg_map[mult.klass] % len(mult_styles)]\n",
    "    mult_kwarg_map[mult.klass] += 1\n",
    "    for cm in (None, \"gsr\", \"additive\", \"multiplicative\", \"euclidean\", \"bt\"):\n",
    "        mwc = mult.with_countermeasure(cm)\n",
    "        colors[mwc] = color\n",
    "        styles[mwc] = style\n",
    "        markers[mwc] = mult_markers[mult_cm_map[mult] % len(mult_markers)]\n",
    "        mult_cm_map[mult] += 1\n",
    "\n",
    "majticks = np.arange(0, 1, 0.1)\n",
    "minticks = np.arange(0, 1, 0.05)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2596562f-8a6a-4a25-ae82-a6b9562d8a40",
   "metadata": {},
   "source": [
    "## Divisors\n",
    "The cell below contains some interesting divisors for distinguishing scalarmults."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bab2a086-8b3d-4e76-bf5c-46ea2b617708",
   "metadata": {},
   "outputs": [],
   "source": [
    "from common import divisor_map\n",
    "for d, ds in divisor_map.items():\n",
    "    print(f\"{d:<27}\", ds[:3], \"...\", ds[-1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "638f8634-1f6e-4844-a796-096611dfbac2",
   "metadata": {},
   "outputs": [],
   "source": [
    "bits = 256\n",
    "num_workers = 28"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b008248-a0aa-41fa-933c-f325f8eec31b",
   "metadata": {},
   "source": [
    "## Configuration\n",
    "Select the mults you want to compute the prob-maps for here as well as a set of divisors. It is good to set `all` here, compute the prob-maps for all the divisors, save them and they continue with visualizing them on subsets of divisors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d2a0f19-8275-4db8-b3fc-c930d8ba2177",
   "metadata": {},
   "outputs": [],
   "source": [
    "selected_mults = all_mults\n",
    "divisor_name = \"all\"\n",
    "kind = \"precomp+necessary\"\n",
    "showci = False\n",
    "selected_divisors = divisor_map[divisor_name]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19d986ab-5fe7-4dd6-b5b5-4e75307217d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Optionally, load\n",
    "with open(f\"{divisor_name}_{kind}_distrs.pickle\", \"rb\") as f:\n",
    "    distributions_mults = pickle.load(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef5b7a43-74b4-4e72-a3a1-955e175f5297",
   "metadata": {},
   "source": [
    "Now, go over all the divisor sets and visualize them (without the combs) into PNGs in the graphs/ directory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ccc28f6-3994-4a0d-8639-2f6df4dddd26",
   "metadata": {},
   "outputs": [],
   "source": [
    "for mult, probmap in distributions_mults.items():\n",
    "    for divisor in sorted(divisor_map[divisor_name]):\n",
    "        if divisor not in probmap.probs:\n",
    "            print(f\"Missing {mult}, {divisor}\")\n",
    "    if probmap.kind is not None and probmap.kind != kind:\n",
    "        print(\"Bad kind! Did you forget to load?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b6f169b-07b3-4b27-ba36-8b90418cd072",
   "metadata": {},
   "source": [
    "## Plots (nocomb)\n",
    "Let's visualize all the divisor groups while looking at the multipliers and countermeasures except the comb-like ones."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "906b5d78-b3a4-4cbb-8051-092d411ba735",
   "metadata": {},
   "outputs": [],
   "source": [
    "for divisor_name in divisor_map:\n",
    "    plot_mults = list(filter(lambda mult: mult in distributions_mults and mult.klass not in (CombMultiplier, BGMWMultiplier), all_mults_with_ctr))\n",
    "    print(divisor_name, \"nocomb\")\n",
    "    plot_divisors = sorted(divisor_map[divisor_name])\n",
    "    L = len(plot_divisors)\n",
    "    N = len(plot_mults)\n",
    "    x = list(range(L))\n",
    "    \n",
    "    fig = plt.figure(figsize=(L/4+10, 24))\n",
    "    ax = plt.subplot(111)\n",
    "    \n",
    "    vals = np.zeros((N, L))\n",
    "    n_samples = 0\n",
    "    for i, mult in enumerate(plot_mults):\n",
    "        probmap = distributions_mults[mult]\n",
    "        y_values = [probmap[l] for l in plot_divisors]\n",
    "        vals[i,] = y_values\n",
    "        ax.plot(x, y_values,\n",
    "                color=colors[mult],\n",
    "                linestyle=styles[mult],\n",
    "                marker=markers[mult],\n",
    "                label=str(mult) if mult.countermeasure is None else \"_nolegend_\")\n",
    "        if showci:\n",
    "            cis = [conf_interval(p, probmap.samples) for p in y_values]\n",
    "            ci_low = [ci[0] for ci in cis]\n",
    "            ci_high = [ci[1] for ci in cis]\n",
    "            ax.fill_between(x, ci_low, ci_high, color=\"black\", alpha=0.1)\n",
    "        n_samples += probmap.samples\n",
    "    \n",
    "    ax.set_title(f\"{divisor_name} ({kind})\\nSamples: \" + str(n_samples//N))\n",
    "    \n",
    "    #var = np.var(vals, axis=0)\n",
    "    #ax.plot(x, var / np.max(var), label=\"cross-mult variance (normalized)\", ls=\"--\", lw=2, color=\"black\")\n",
    "    \n",
    "    ax.set_xlabel(\"divisors\")\n",
    "    ax.set_ylabel(\"error probability\")\n",
    "    ax.set_yticks(majticks)\n",
    "    ax.set_yticks(minticks, minor=True)\n",
    "    ax.set_xticks(x, plot_divisors, rotation=90)\n",
    "    \n",
    "    ax.grid(axis=\"y\", which=\"major\", alpha=0.7)\n",
    "    ax.grid(axis=\"y\", which=\"minor\", alpha=0.3)\n",
    "    ax.grid(axis=\"x\", alpha=0.7)\n",
    "    plt.tight_layout()\n",
    "    box = ax.get_position()\n",
    "    ax.set_position([box.x0, box.y0, box.width * 0.9, box.height])\n",
    "    \n",
    "    ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))\n",
    "\n",
    "    fig.savefig(f\"graphs/{kind}-kind/{divisor_name}-nocomb{'+ci' if showci else ''}.pdf\");\n",
    "    plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4068e7d0-addb-45d0-ba87-e572d4c82fbd",
   "metadata": {},
   "source": [
    "## Plots (allmults)\n",
    "Now, lets also do plots with allmults for all divisor groups."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b9aa7a8-0d9d-4ce3-a936-8ced2948f562",
   "metadata": {},
   "outputs": [],
   "source": [
    "for divisor_name in divisor_map:\n",
    "    plot_mults = list(filter(lambda mult: mult in distributions_mults, all_mults_with_ctr))\n",
    "    print(divisor_name, \"allmults\")\n",
    "    plot_divisors = sorted(divisor_map[divisor_name])\n",
    "    L = len(plot_divisors)\n",
    "    N = len(plot_mults)\n",
    "    x = list(range(L))\n",
    "    \n",
    "    fig = plt.figure(figsize=(L/4+10, 26))\n",
    "    ax = plt.subplot(111)\n",
    "    \n",
    "    vals = np.zeros((N, L))\n",
    "    n_samples = 0\n",
    "    for i, mult in enumerate(plot_mults):\n",
    "        probmap = distributions_mults[mult]\n",
    "        y_values = [probmap[l] for l in plot_divisors]\n",
    "        vals[i,] = y_values\n",
    "        ax.plot(x, y_values,\n",
    "                color=colors[mult],\n",
    "                linestyle=styles[mult],\n",
    "                marker=markers[mult],\n",
    "                label=str(mult) if mult.countermeasure is None else \"_nolegend_\")\n",
    "        if showci:\n",
    "            cis = [conf_interval(p, probmap.samples) for p in y_values]\n",
    "            ci_low = [ci[0] for ci in cis]\n",
    "            ci_high = [ci[1] for ci in cis]\n",
    "            ax.fill_between(x, ci_low, ci_high, color=\"black\", alpha=0.1)\n",
    "        n_samples += probmap.samples\n",
    "    \n",
    "    ax.set_title(f\"{divisor_name} ({kind})\\nSamples(avg): \" + str(n_samples//N))\n",
    "    \n",
    "    #var = np.var(vals, axis=0)\n",
    "    #ax.plot(x, var / np.max(var), label=\"cross-mult variance (normalized)\", ls=\"--\", lw=2, color=\"black\")\n",
    "    \n",
    "    ax.set_xlabel(\"divisors\")\n",
    "    ax.set_ylabel(\"error probability\")\n",
    "    ax.set_yticks(majticks)\n",
    "    ax.set_yticks(minticks, minor=True)\n",
    "    ax.set_xticks(x, plot_divisors, rotation=90)\n",
    "    \n",
    "    ax.grid(axis=\"y\", which=\"major\", alpha=0.7)\n",
    "    ax.grid(axis=\"y\", which=\"minor\", alpha=0.3)\n",
    "    ax.grid(axis=\"x\", alpha=0.7)\n",
    "    plt.tight_layout()\n",
    "    box = ax.get_position()\n",
    "    ax.set_position([box.x0, box.y0, box.width * 0.9, box.height])\n",
    "    \n",
    "    ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))\n",
    "\n",
    "    fig.savefig(f\"graphs/{kind}-kind/{divisor_name}-allmults{'+ci' if showci else ''}.pdf\")\n",
    "    plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df2e236a-4540-4677-a7f1-563c4cc37a3e",
   "metadata": {},
   "source": [
    "## Interactive plot\n",
    "Below you can choose a concrete divisor set and visualize it with all the mults, or just some to your liking."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b464865d-b169-446e-a9e7-0cead699aee1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#divisor_name = \"powers_of_2_large\"\n",
    "divisor_name = \"feature\"\n",
    "plot_mults = list(filter(lambda mult: mult in distributions_mults, all_mults_with_ctr))\n",
    "#plot_divisors = (61, 65, 111, 165, 1536, 12288) \n",
    "plot_divisors = (55, 65, 165, 248, 3072)\n",
    "L = len(plot_divisors)\n",
    "N = len(plot_mults)\n",
    "x = list(range(L))\n",
    "\n",
    "fig = plt.figure(figsize=(L/4+15, 24))\n",
    "ax = plt.subplot(111)\n",
    "\n",
    "vals = np.zeros((N, L))\n",
    "n_samples = 0\n",
    "for i, mult in enumerate(plot_mults):\n",
    "    probmap = distributions_mults[mult]\n",
    "    y_values = [probmap[l] for l in plot_divisors]\n",
    "    vals[i,] = y_values\n",
    "    ax.plot(x, y_values,\n",
    "            color=colors[mult],\n",
    "            linestyle=styles[mult],\n",
    "            marker=markers[mult],\n",
    "            label=str(mult) if mult.countermeasure is None else \"_nolegend_\")\n",
    "    if showci:\n",
    "        cis = [conf_interval(p, probmap.samples) for p in y_values]\n",
    "        ci_low = [ci[0] for ci in cis]\n",
    "        ci_high = [ci[1] for ci in cis]\n",
    "        ax.fill_between(x, ci_low, ci_high, color=\"black\", alpha=0.1)\n",
    "    n_samples += probmap.samples\n",
    "\n",
    "ax.set_title(f\"{divisor_name} ({kind})\\nSamples(avg): \" + str(n_samples//N))\n",
    "\n",
    "#var = np.var(vals, axis=0)\n",
    "#ax.plot(x, var / np.max(var), label=\"cross-mult variance (normalized)\", ls=\"--\", lw=2, color=\"black\")\n",
    "\n",
    "ax.set_xlabel(\"divisors\")\n",
    "ax.set_ylabel(\"error probability\")\n",
    "ax.set_yticks(majticks)\n",
    "ax.set_yticks(minticks, minor=True)\n",
    "ax.set_xticks(x, plot_divisors, rotation=90)\n",
    "\n",
    "ax.grid(axis=\"y\", which=\"major\", alpha=0.7)\n",
    "ax.grid(axis=\"y\", which=\"minor\", alpha=0.3)\n",
    "ax.grid(axis=\"x\", alpha=0.7)\n",
    "plt.tight_layout()\n",
    "box = ax.get_position()\n",
    "ax.set_position([box.x0, box.y0, box.width * 0.7, box.height])\n",
    "\n",
    "# Put a legend to the right of the current axis\n",
    "ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d68f0bfc-cdf1-4891-b0e5-0b6d1b02ded7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}