aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJ08nY2024-04-08 14:27:46 +0200
committerJ08nY2024-04-08 14:37:55 +0200
commit94a1b1d2c1323860cd89f6432c44745026668448 (patch)
tree9910c475075036a9e76474c1697a2c308281b334
parent57cadf3ea4ae80e999a2f18ab605b633cd78cbee (diff)
downloadpyecsca-notebook-94a1b1d2c1323860cd89f6432c44745026668448.tar.gz
pyecsca-notebook-94a1b1d2c1323860cd89f6432c44745026668448.tar.zst
pyecsca-notebook-94a1b1d2c1323860cd89f6432c44745026668448.zip
Update RPA with new API.
-rw-r--r--re/rpa.ipynb122
1 files changed, 84 insertions, 38 deletions
diff --git a/re/rpa.ipynb b/re/rpa.ipynb
index d435b15..9f9ba69 100644
--- a/re/rpa.ipynb
+++ b/re/rpa.ipynb
@@ -5,7 +5,12 @@
"id": "66c30004-cd2c-4d34-9999-f33f9e6fd5e9",
"metadata": {},
"source": [
- "# RPA-based reverse-engineering"
+ "# RPA-based reverse-engineering\n",
+ "This notebook showcases the RPA-based reverse-engineering technique for scalar multipliers.\n",
+ " - [Exploration](#Exploration)\n",
+ " - [Reverse-engineering](#Reverse-engineering)\n",
+ " - [Oracle simulation](#Oracle-simulation)\n",
+ " - [Method simulation](#Method-simulation)"
]
},
{
@@ -16,6 +21,7 @@
"outputs": [],
"source": [
"from collections import Counter\n",
+ "from math import sqrt\n",
"import numpy as np\n",
"import holoviews as hv\n",
"import matplotlib.pyplot as plt\n",
@@ -27,6 +33,7 @@
"from IPython.display import HTML, display\n",
"from tqdm.auto import tqdm, trange\n",
"import tabulate\n",
+ "from anytree import LevelOrderGroupIter, RenderTree\n",
"\n",
"from pyecsca.ec.model import ShortWeierstrassModel\n",
"from pyecsca.ec.coordinates import AffineCoordinateModel\n",
@@ -36,14 +43,14 @@
"from pyecsca.ec.point import Point\n",
"from pyecsca.ec.mod import Mod\n",
"from pyecsca.ec.mult import *\n",
- "from pyecsca.misc.cfg import TemporaryConfig\n",
+ "from pyecsca.misc.utils import silent\n",
"from pyecsca.sca.trace.sampling import downsample_average, downsample_max\n",
"from pyecsca.sca.trace.process import normalize, rolling_mean\n",
"from pyecsca.sca.trace.combine import average, subtract\n",
"from pyecsca.sca.trace.test import welch_ttest\n",
"from pyecsca.sca.attack.leakage_model import HammingWeight, NormalNoice\n",
"from pyecsca.ec.context import DefaultContext, local\n",
- "from pyecsca.sca.re.rpa import MultipleContext, rpa_distinguish\n",
+ "from pyecsca.sca.re.rpa import MultipleContext, rpa_distinguish, RPA\n",
"from pyecsca.sca.trace import Trace\n",
"from pyecsca.sca.trace.plot import plot_trace, plot_traces"
]
@@ -372,6 +379,45 @@
},
{
"cell_type": "markdown",
+ "id": "4cecc7dc-f609-4073-b0db-ac9631ac3edf",
+ "metadata": {},
+ "source": [
+ "We can also have a look at the distinguishing tree that the method builds for this set of multipliers."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "838ae83b-771d-4e4c-8977-ba997aa4fbb6",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "re = RPA(set(multipliers))\n",
+ "with silent():\n",
+ " re.build_tree(p256, tries=10)\n",
+ "print(re.tree.describe())"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "9a99df60-f6ec-40e1-a43a-d95eb64beb8c",
+ "metadata": {},
+ "source": [
+ "We can also look at the rough tree structure."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "136000fe-4a4a-4261-bacc-a49102080749",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "print(re.tree.render_basic())"
+ ]
+ },
+ {
+ "cell_type": "markdown",
"id": "068e1ba5-9884-4d2b-97f6-d54313daddad",
"metadata": {},
"source": [
@@ -423,30 +469,30 @@
" nonlocal calls\n",
" calls += 1\n",
" return noisy(scalar, affine_point)\n",
+ " re = RPA(set(multipliers))\n",
+ " re.build_tree(params, tries=10)\n",
" for j in range(num_tries):\n",
- " res = rpa_distinguish(params, multipliers, oracle, majority=majority)\n",
+ " res = re.run(oracle, majority=majority)\n",
" if mult in res:\n",
" correct += 1\n",
" if len(res) == 1:\n",
" precise += 1\n",
" return correct, precise, calls\n",
"\n",
- "with TemporaryConfig() as cfg:\n",
- " cfg.log.enabled = False\n",
- " with ProcessPoolExecutor(max_workers=num_cores) as pool:\n",
- " futures = []\n",
- " args = []\n",
- " for i, mult in enumerate(multipliers):\n",
- " for err in errs:\n",
- " for majority in majs:\n",
- " a = (params, multipliers, simulated_oracle, i, mult, err, majority)\n",
- " futures.append(pool.submit(measure_mult, *a))\n",
- " args.append(a)\n",
- " results = [None for _ in futures]\n",
- " for future in tqdm(as_completed(futures), total=len(futures), smoothing=0):\n",
- " j = futures.index(future)\n",
- " a = args[j]\n",
- " results[j] = future.result()"
+ "with silent(), ProcessPoolExecutor(max_workers=num_cores) as pool:\n",
+ " futures = []\n",
+ " args = []\n",
+ " for i, mult in enumerate(multipliers):\n",
+ " for err in errs:\n",
+ " for majority in majs:\n",
+ " a = (params, multipliers, simulated_oracle, i, mult, err, majority)\n",
+ " futures.append(pool.submit(measure_mult, *a))\n",
+ " args.append(a)\n",
+ " results = [None for _ in futures]\n",
+ " for future in tqdm(as_completed(futures), total=len(futures), smoothing=0):\n",
+ " j = futures.index(future)\n",
+ " a = args[j]\n",
+ " results[j] = future.result()"
]
},
{
@@ -675,31 +721,31 @@
" nonlocal calls\n",
" calls += 1\n",
" return biased(scalar, affine_point)\n",
+ " re = RPA(set(multipliers))\n",
+ " re.build_tree(params, tries=10)\n",
" for j in range(num_tries_b):\n",
- " res = rpa_distinguish(params, multipliers, oracle, majority=majority)\n",
+ " res = re.run(oracle, majority=majority)\n",
" if mult in res:\n",
" correct += 1\n",
" if len(res) == 1:\n",
" precise += 1\n",
" return correct, precise, calls\n",
"\n",
- "with TemporaryConfig() as cfg:\n",
- " cfg.log.enabled = False\n",
- " with ProcessPoolExecutor(max_workers=num_cores) as pool:\n",
- " futures = []\n",
- " args = []\n",
- " for i, mult in enumerate(multipliers):\n",
- " for err_0 in errs:\n",
- " for err_1 in errs:\n",
- " for majority in majs:\n",
- " a = (params, multipliers, simulated_oracle, i, mult, err_0, err_1, majority)\n",
- " futures.append(pool.submit(measure_mult, *a))\n",
- " args.append(a)\n",
- " results = [None for _ in futures]\n",
- " for future in tqdm(as_completed(futures), total=len(futures), smoothing=0):\n",
- " j = futures.index(future)\n",
- " a = args[j]\n",
- " results[j] = future.result()"
+ "with silent(), ProcessPoolExecutor(max_workers=num_cores) as pool:\n",
+ " futures = []\n",
+ " args = []\n",
+ " for i, mult in enumerate(multipliers):\n",
+ " for err_0 in errs:\n",
+ " for err_1 in errs:\n",
+ " for majority in majs:\n",
+ " a = (params, multipliers, simulated_oracle, i, mult, err_0, err_1, majority)\n",
+ " futures.append(pool.submit(measure_mult, *a))\n",
+ " args.append(a)\n",
+ " results = [None for _ in futures]\n",
+ " for future in tqdm(as_completed(futures), total=len(futures), smoothing=0):\n",
+ " j = futures.index(future)\n",
+ " a = args[j]\n",
+ " results[j] = future.result()"
]
},
{