aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2025-03-10 19:41:59 +0100
committerJ08nY2025-04-16 12:25:06 +0200
commit6dde089b1f2f85583aa546fabe0645fe0fd0cb3b (patch)
treee135243f40a938b80cc70a378b7b9b34f0a4080c
parentca59db2917f9de89602c90673d08895a4d3f484d (diff)
downloadECTester-6dde089b1f2f85583aa546fabe0645fe0fd0cb3b.tar.gz
ECTester-6dde089b1f2f85583aa546fabe0645fe0fd0cb3b.tar.zst
ECTester-6dde089b1f2f85583aa546fabe0645fe0fd0cb3b.zip
-rw-r--r--epare/re.ipynb708
-rw-r--r--epare/simulate.ipynb59
2 files changed, 9 insertions, 758 deletions
diff --git a/epare/re.ipynb b/epare/re.ipynb
deleted file mode 100644
index e02b221..0000000
--- a/epare/re.ipynb
+++ /dev/null
@@ -1,708 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "3232df80-2a65-47ce-bc77-6a64f44d2404",
- "metadata": {},
- "outputs": [],
- "source": [
- "import multiprocessing\n",
- "import inspect\n",
- "import tempfile\n",
- "import sys\n",
- "import pickle\n",
- "\n",
- "import matplotlib\n",
- "import matplotlib.pyplot as plt\n",
- "\n",
- "from importlib import import_module, invalidate_caches\n",
- "from functools import partial\n",
- "from random import randint\n",
- "from collections import Counter\n",
- "\n",
- "from bs4 import BeautifulSoup\n",
- "from tqdm.auto import tqdm, trange\n",
- "\n",
- "from pyecsca.ec.params import DomainParameters, get_params\n",
- "from pyecsca.ec.mult import *\n",
- "from pyecsca.sca.re.rpa import MultipleContext, rpa_distinguish, RPA, multiples_computed\n",
- "from pyecsca.ec.context import DefaultContext, local\n",
- "from pyecsca.ec.model import ShortWeierstrassModel\n",
- "from pyecsca.ec.coordinates import AffineCoordinateModel\n",
- "from pyecsca.misc.utils import TaskExecutor"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "24de57fb-db3c-4c48-8d30-4d0814cce684",
- "metadata": {},
- "outputs": [],
- "source": [
- "model = ShortWeierstrassModel()\n",
- "\n",
- "# All dbl-and-add multipliers from https://github.com/J08nY/pyecsca/blob/master/pyecsca/ec/mult\n",
- "\n",
- "# Use partial funcs such that the \"multiples_computed\" method from RPA module can use fake formulas.\n",
- "window_mults = [\n",
- " partial(SlidingWindowMultiplier, width=4),\n",
- " partial(SlidingWindowMultiplier, width=5),\n",
- " partial(SlidingWindowMultiplier, width=6),\n",
- " partial(FixedWindowLTRMultiplier, m=2**4),\n",
- " partial(FixedWindowLTRMultiplier, m=2**5),\n",
- " partial(FixedWindowLTRMultiplier, m=2**6),\n",
- " partial(WindowBoothMultiplier, width=4),\n",
- " partial(WindowBoothMultiplier, width=5),\n",
- " partial(WindowBoothMultiplier, width=6)\n",
- "]\n",
- "naf_mults = [\n",
- " partial(WindowNAFMultiplier, width=4),\n",
- " partial(WindowNAFMultiplier, width=5),\n",
- " partial(WindowNAFMultiplier, width=6),\n",
- " partial(BinaryNAFMultiplier)\n",
- "]\n",
- "comb_mults = [\n",
- " partial(CombMultiplier, width=4),\n",
- " partial(CombMultiplier, width=5),\n",
- " partial(CombMultiplier, width=6),\n",
- " partial(BGMWMultiplier, width=4),\n",
- " partial(BGMWMultiplier, width=5),\n",
- " partial(BGMWMultiplier, width=6)\n",
- "]\n",
- "binary_mults = [\n",
- " partial(LTRMultiplier),\n",
- " partial(RTLMultiplier),\n",
- " partial(CoronMultiplier)\n",
- "]\n",
- "other_mults = [\n",
- " partial(SimpleLadderMultiplier),\n",
- " partial(FullPrecompMultiplier)\n",
- "]\n",
- "\n",
- "with_precomputation = window_mults + naf_mults[:-1] + other_mults[:-1] + comb_mults\n",
- "\n",
- "all_mults = window_mults + naf_mults + binary_mults + other_mults + comb_mults"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "id": "2bedc98a-f777-4dad-8e31-eb5d2ddeb8f4",
- "metadata": {},
- "outputs": [],
- "source": [
- "def get_small_scalars(params, mult, scalar, precomp_only = False):\n",
- " mult_class = mult.func\n",
- " if precomp_only:\n",
- " use_init = True\n",
- " use_multiply = False\n",
- " else:\n",
- " use_init = True\n",
- " use_multiply = True\n",
- " return multiples_computed(scalar, params, mult_class, mult, use_init, use_multiply)\n",
- "\n",
- "def divides_any(l,small_scalars):\n",
- " for s in small_scalars:\n",
- " if s%l==0:\n",
- " return True\n",
- " return False\n",
- "\n",
- "def mult_label(mult):\n",
- " if isinstance(mult, ScalarMultiplier):\n",
- " for attr in (\"width\", \"m\"):\n",
- " if not hasattr(mult, attr):\n",
- " continue\n",
- " return f\"{mult.__class__.__name__}_{getattr(mult, attr)}\"\n",
- " return mult.__class__.__name__\n",
- " else:\n",
- " # mult is a callable created from partial()\n",
- " return f\"{mult.func.__name__}_{mult.args}_{mult.keywords}\"\n",
- "\n",
- "def get_general_distributions(divisors, bits, samples = 1000):\n",
- " distributions = {l:0 for l in divisors}\n",
- " for _ in range(samples):\n",
- " big_scalar = randint(1,2**bits)\n",
- " for l in divisors:\n",
- " if big_scalar%l==0:\n",
- " distributions[l]+=1\n",
- " for l,v in distributions.items():\n",
- " distributions[l] = v/samples\n",
- " return distributions\n",
- "\n",
- "def get_general_n_distributions(divisors, bits, n, samples = 1000):\n",
- " distributions = {l:0 for l in divisors}\n",
- " for _ in range(samples):\n",
- " big_scalars = []\n",
- " for i in range(n):\n",
- " b = randint(1,256)\n",
- " \n",
- " big_scalars.append(randint(2**b,2**(b+1)))\n",
- " for l in divisors:\n",
- " if divides_any(l, big_scalars):\n",
- " distributions[l]+=1\n",
- " for l,v in distributions.items():\n",
- " distributions[l] = v/samples\n",
- " return distributions\n",
- "\n",
- "def get_small_scalar_distributions(mult, category, curve, divisors, bits, samples = 1000, precomp_only = False):\n",
- " small_scalars_distributions = {l:0 for l in divisors}\n",
- " params = get_params(category, curve, \"projective\")\n",
- " for _ in range(samples):\n",
- " big_scalar = randint(1,2**bits)\n",
- " small_scalars = get_small_scalars(params, mult, big_scalar, precomp_only)\n",
- " for l in divisors:\n",
- " if divides_any(l, small_scalars):\n",
- " small_scalars_distributions[l]+=1\n",
- " for l,v in small_scalars_distributions.items():\n",
- " small_scalars_distributions[l] = v/samples\n",
- " return small_scalars_distributions\n",
- "\n",
- "def merge_probs(*prob_maps):\n",
- " # Merge two or more maps of \"small-scalar\" -> \"probability\" together by averaging them.\n",
- " # This is correct if they were collected with the same amount of samples. If the\n",
- " # amount of samples differs a lot this will not update as much as it should, but will\n",
- " # update in the correct direction nonetheless.\n",
- " counter = Counter()\n",
- " nprobs = len(prob_maps)\n",
- " for prob_map in prob_maps:\n",
- " for k, v in prob_map.items():\n",
- " counter[k] += v\n",
- " return {k: v / nprobs for k, v in counter.items()}"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "bab2a086-8b3d-4e76-bf5c-46ea2b617708",
- "metadata": {},
- "outputs": [],
- "source": [
- "#2<p<200\n",
- "small_primes = [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]\n",
- "#200<p<400\n",
- "\n",
- "medium_primes = [211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397]\n",
- "\n",
- "powers_of_two = [2, 4, 8, 16, 32, 64, 128, 256, 512]\n",
- "\n",
- "all_divisors = small_primes+medium_primes+powers_of_two\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "4d2a0f19-8275-4db8-b3fc-c930d8ba2177",
- "metadata": {},
- "outputs": [],
- "source": [
- "category = \"secg\"\n",
- "curve = \"secp256r1\"\n",
- "num_workers = 16\n",
- "bits = 256\n",
- "samples = 10000\n",
- "selected_mults = all_mults\n",
- "selected_divisors = all_divisors\n",
- "\n",
- "distributions_mults = {}\n",
- "distributions_mults_precomp = {}"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "4bbf52ae-834e-4168-ad65-9eaa9b113e14",
- "metadata": {},
- "outputs": [],
- "source": [
- "with TaskExecutor(max_workers=num_workers) as pool:\n",
- " for mult in selected_mults:\n",
- " pool.submit_task(mult,\n",
- " get_small_scalar_distributions,\n",
- " mult, category, curve, selected_divisors, bits, samples)\n",
- " for mult, future in tqdm(pool.as_completed(), desc=\"Computing small scalar distributions.\", total=len(pool.tasks)):\n",
- " print(f\"Got {mult_label(mult)}.\")\n",
- " if mult not in distributions_mults:\n",
- " distributions_mults[mult] = future.result()\n",
- " else:\n",
- " # Accumulate\n",
- " distributions_mults[mult] = merge_probs(distributions_mults[mult], future.result())\n",
- "\n",
- "with TaskExecutor(max_workers=num_workers) as pool:\n",
- " for mult in with_precomputation:\n",
- " pool.submit_task(mult,\n",
- " get_small_scalar_distributions,\n",
- " mult, category, curve, selected_divisors, bits, samples, precomp_only=True)\n",
- " for mult, future in tqdm(pool.as_completed(), desc=\"Computing small scalar distributions (precomp_only).\", total=len(pool.tasks)):\n",
- " print(f\"Got {mult_label(mult)}.\")\n",
- " if mult not in distributions_mults_precomp:\n",
- " distributions_mults_precomp[mult] = future.result()\n",
- " else:\n",
- " # Accumulate\n",
- " distributions_mults_precomp[mult] = merge_probs(distributions_mults_precomp[mult], future.result())\n",
- "\n",
- "# Single-core variant:\n",
- "# distributions_mults = {mult:get_small_scalar_distributions(mult,category,curve,selected_divisors,bits, samples) for mult in tqdm(selected_mults)}\n",
- "# distributions_mults_precomp = {mult:get_small_scalar_distributions(mult,category,curve,selected_divisors,bits,samples,precomp_only=True) for mult in with_precomputation}\n",
- "\n",
- "# Dump\n",
- "with open(f\"distributions_{category}_{curve}_{bits}\",\"wb\") as h:\n",
- " pickle.dump(distributions_mults, h)\n",
- "with open(f\"distributions_{category}_{curve}_{bits}_precomp\",\"wb\") as h:\n",
- " pickle.dump(distributions_mults_precomp, h)\n",
- "\n",
- "# Load\n",
- "#with open(f\"distributions_{category}_{curve}_{bits}\",\"rb\") as h:\n",
- "# distributions_mults = pickle.load(h)\n",
- "#with open(f\"distributions_{category}_{curve}_{bits}_precomp\",\"rb\") as h:\n",
- "# distributions_mults_precomp = pickle.load(h)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "8454cb7a-5308-43c6-9cd0-5de7946ec72a",
- "metadata": {},
- "outputs": [],
- "source": [
- "# general_distributions = get_general_distributions(selected_divisors, bits, samples)\n",
- "# general_n_distributions = get_general_n_distributions(selected_divisors, bits, 256, samples)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "906b5d78-b3a4-4cbb-8051-092d411ba735",
- "metadata": {},
- "outputs": [],
- "source": [
- "\n",
- "selected_mults = all_mults#window_mults[0:1]+window_mults[5:6]+naf_mults[1:2]#[mult for mult in all_mults if not mult in comb_mults]\n",
- "selected_divisors = all_divisors\n",
- "colors = {mult:matplotlib.cm.tab20(range(len(selected_mults)))[i] for i,mult in enumerate(selected_mults)}\n",
- "\n",
- "\n",
- "fig = plt.subplots(figsize =(36, 12)) \n",
- "\n",
- "L = len(selected_divisors)\n",
- "selected_divisors = sorted(selected_divisors)\n",
- "for mult in selected_mults:\n",
- " y_values = [distributions_mults[mult][l] for l in selected_divisors]\n",
- " plt.plot([l for l in range(L)],y_values,color = colors[mult], label = mult_label(mult))\n",
- "\n",
- "# mult = list(fixedwindow_dist.keys())[0]\n",
- "# plt.plot([l for l in range(L)],[fixedwindow_dist[l] for l in selected_divisors],color = \"pink\", label = mult_label(mult))\n",
- "\n",
- "# measured_dist = measured_distribution(library,selected_divisors)\n",
- "# mes_x, mes_y = [],[]\n",
- "# for i,l in enumerate(selected_divisors):\n",
- "# if l in measured_dist:\n",
- "# mes_y.append(measured_dist[l])\n",
- "# mes_x.append(i)\n",
- "# plt.scatter(mes_x,mes_y,color = \"black\", label = library)\n",
- "\n",
- "attempts = 0\n",
- "fails =0\n",
- "for i in range(51):\n",
- " with open(f\"cards/jcop/199_{i}.txt\") as f:\n",
- " attempts += f.read().count(\"ALG_EC_SVDP_DH of remote pubkey and local privkey\")+1\n",
- " fails += 1\n",
- "plt.scatter([selected_divisors.index(199)],[fails/attempts],s=[40],color = \"black\", label = \"jcop\")\n",
- "\n",
- "plt.plot([l for l in range(L)],[general_distributions[l] for l in selected_divisors],color = \"black\", label = \"prime-distribution\")\n",
- "\n",
- "\n",
- "plt.xlabel('divisors') \n",
- "plt.ylabel(\"prob\") \n",
- "plt.xticks([r for r in range(L)], selected_divisors)\n",
- "\n",
- "plt.legend()\n",
- "plt.show() \n",
- "fig[0].savefig(f\"graphs/re.png\",dpi=300)\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "d240059f-9ed6-4864-b4bf-525de576272f",
- "metadata": {},
- "outputs": [],
- "source": [
- "selected_mults = with_precomputation\n",
- "selected_divisors = small_primes#all_divisors\n",
- "colors = {mult:matplotlib.cm.tab20(range(len(selected_mults)))[i] for i,mult in enumerate(selected_mults)}\n",
- "\n",
- "\n",
- "fig = plt.subplots(figsize =(24, 12)) \n",
- "\n",
- "L = len(selected_divisors)\n",
- "selected_divisors = sorted(selected_divisors)\n",
- "for mult in selected_mults:\n",
- " plt.plot([l for l in range(L)],[distributions_mults_precomp[mult][l] for l in selected_divisors],color = colors[mult], label = mult_label(mult))\n",
- "\n",
- "\n",
- "measured_dist = measured_distribution(library,selected_divisors)\n",
- "mes_x, mes_y = [],[]\n",
- "for i,l in enumerate(selected_divisors):\n",
- " if l in measured_dist:\n",
- " mes_y.append(measured_dist[l])\n",
- " mes_x.append(i)\n",
- "plt.scatter(mes_x,mes_y,color = \"black\", label = library)\n",
- "\n",
- "\n",
- "plt.xlabel('divisors') \n",
- "plt.ylabel(\"prob\") \n",
- "plt.xticks([r for r in range(L)], selected_divisors)\n",
- "\n",
- "plt.legend()\n",
- "plt.show() "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "0da0eed4-a5dc-4dde-9bd1-8b519d02375e",
- "metadata": {},
- "outputs": [],
- "source": [
- "def nok_ecdh(line):\n",
- " return int(line.split(\";\")[-1].strip(),16)==0\n",
- "\n",
- "def measured_distribution(library, selected_divisors):\n",
- " measured_distribution = {}\n",
- " counts = {order:0 for order in selected_divisors}\n",
- " for div in selected_divisors:\n",
- " errors = 0\n",
- " with open(f\"./ecdh/{library}/ecdh_{div}.txt\") as f:\n",
- " for line in f.readlines()[1:]:\n",
- " if nok_ecdh(line):\n",
- " errors+=1\n",
- " counts[div]+=1\n",
- " measured_distribution[div] = errors\n",
- " \n",
- " for o,v in measured_distribution.items():\n",
- " if counts[o]!=0:\n",
- " measured_distribution[o] = v/counts[o]\n",
- " return measured_distribution"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "6f8b83e3-7e2e-409a-82bd-7d44316236c6",
- "metadata": {},
- "outputs": [],
- "source": [
- "selected_mults = other_mults[:1]+binary_mults[:1]+comb_mults[:1]+window_mults[:1]\n",
- "selected_divisors = small_primes#all_divisors\n",
- "library = \"tomcrypt\"\n",
- "colors = {mult:matplotlib.cm.tab20(range(len(selected_mults)))[i] for i,mult in enumerate(selected_mults)}\n",
- "\n",
- "fig = plt.subplots(figsize =(24, 12)) \n",
- "\n",
- "L = len(selected_divisors)\n",
- "selected_divisors = sorted(selected_divisors)\n",
- "for mult in selected_mults:\n",
- " plt.plot([l for l in range(L)],[distributions_mults[mult][l] for l in selected_divisors],color = colors[mult], label = mult_label(mult))\n",
- "\n",
- "\n",
- "measured_dist = measured_distribution(library,selected_divisors)\n",
- "mes_x, mes_y = [],[]\n",
- "for i,l in enumerate(selected_divisors):\n",
- " if l in measured_dist:\n",
- " mes_y.append(measured_dist[l])\n",
- " mes_x.append(i)\n",
- "plt.scatter(mes_x,mes_y,color = \"black\", label = library)\n",
- "\n",
- "\n",
- "plt.xlabel('divisors') \n",
- "plt.ylabel(\"prob\") \n",
- "plt.xticks([r for r in range(L)], selected_divisors)\n",
- "plt.legend(loc=\"upper right\")\n",
- "plt.show() \n",
- "fig[0].savefig(f\"graphs/{library}/re.png\",dpi=300)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "b7936e5b-76d2-410f-82b1-36333071bd12",
- "metadata": {},
- "outputs": [],
- "source": [
- "selected_mults = [mult for mult in with_precomputation if mult in comb_mults]\n",
- "selected_divisors = small_primes#all_divisors\n",
- "library = \"mbedtls\"\n",
- "colors = {mult:matplotlib.cm.tab20(range(len(selected_mults)))[i] for i,mult in enumerate(selected_mults)}\n",
- "\n",
- "fig = plt.subplots(figsize =(24, 12)) \n",
- "\n",
- "L = len(selected_divisors)\n",
- "selected_divisors = sorted(selected_divisors)\n",
- "for mult in selected_mults:\n",
- " plt.plot([l for l in range(L)],[distributions_mults_precomp[mult][l] for l in selected_divisors],color = colors[mult], label = mult_label(mult))\n",
- "\n",
- "\n",
- "measured_dist = measured_distribution(library,selected_divisors)\n",
- "mes_x, mes_y = [],[]\n",
- "for i,l in enumerate(selected_divisors):\n",
- " if l in measured_dist:\n",
- " mes_y.append(measured_dist[l])\n",
- " mes_x.append(i)\n",
- "plt.scatter(mes_x,mes_y,color = \"black\", label = library)\n",
- "\n",
- "plt.xlabel('divisors') \n",
- "plt.ylabel(\"prob\") \n",
- "plt.xticks([r for r in range(L)], selected_divisors)\n",
- "plt.legend()\n",
- "plt.show() \n",
- "fig[0].savefig(f\"graphs/{library}/re.png\",dpi=300)"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "dbdd0219-3937-4ef3-8cb3-b90f03af9977",
- "metadata": {},
- "source": [
- "BouncyCastle\n",
- " - WindowBooth-5?\n",
- "\n",
- "Mbedtls\n",
- " - CombMultiplier-4\n",
- " - confirmed in library\n",
- " \n",
- "tomcrypt\n",
- " - ladder or coron\n",
- " - ladder confirmed in library\n",
- "\n",
- "OpenSSL, LibreSSL, Botan, Crypto++ and IPPCP followed the general distribution of divisibility by the primes. So they have some countermeasure.\n",
- "\n",
- "Note that some libraries for some orders output \"Invalid algorithm parameter: Not supported.\". For libcrypt, SunEC and Nettle it happened for all orders.\n",
- "BoringSSL\n",
- " - \"Invalid algorithm parameter: Error creating EC_GROUP, EC_GROUP_set_generator.\"\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "34609ae2-fa8c-4437-a601-cd25a0708a19",
- "metadata": {},
- "outputs": [],
- "source": [
- "def scatter(library, color):\n",
- " measured_dist = measured_distribution(library,selected_divisors)\n",
- " mes_x, mes_y = [],[]\n",
- " for i,l in enumerate(selected_divisors):\n",
- " if l in measured_dist:\n",
- " mes_y.append(measured_dist[l])\n",
- " mes_x.append(i)\n",
- " plt.scatter(mes_x,mes_y,color = color, label = library)\n",
- "\n",
- "selected_divisors = small_primes#all_divisors\n",
- "\n",
- "fig = plt.subplots(figsize =(24, 12)) \n",
- "\n",
- "L = len(selected_divisors)\n",
- "\n",
- "colors = matplotlib.cm.tab20(range(6))\n",
- "scatter(\"openssl\",colors[0])\n",
- "scatter(\"libressl\",colors[1])\n",
- "scatter(\"botan\",colors[2])\n",
- "scatter(\"Crypto++\",colors[3])\n",
- "scatter(\"ippcp\",colors[4])\n",
- "\n",
- "plt.plot([l for l in range(L)],[general_distributions[l] for l in selected_divisors],color = colors[5], label = \"prime-distribution\")\n",
- "\n",
- "plt.xlabel('divisors') \n",
- "plt.ylabel(\"prob\") \n",
- "plt.xticks([r for r in range(L)], selected_divisors)\n",
- "plt.legend()\n",
- "plt.show() \n",
- "fig[0].savefig(f\"graphs/resistant.png\",dpi=300)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "1f403fa3-880d-45a8-aaf3-964b0fbc38d7",
- "metadata": {},
- "outputs": [],
- "source": [
- "def scatter(library, color):\n",
- " measured_dist = measured_distribution(library,selected_divisors)\n",
- " mes_x, mes_y = [],[]\n",
- " for i,l in enumerate(selected_divisors):\n",
- " if l in measured_dist:\n",
- " mes_y.append(measured_dist[l])\n",
- " mes_x.append(i)\n",
- " # plt.scatter(mes_x,mes_y,color = color, label = library)\n",
- " plt.plot(mes_x,mes_y,color = color, linewidth = 1, label = library)\n",
- "\n",
- "selected_divisors = small_primes[:12]#all_divisors\n",
- "\n",
- "fig = plt.subplots(figsize =(10, 4)) \n",
- "\n",
- "L = len(selected_divisors)\n",
- "\n",
- "colors = matplotlib.cm.tab20(range(9))\n",
- "scatter(\"openssl\",colors[0])\n",
- "scatter(\"libressl\",colors[1])\n",
- "scatter(\"botan\",colors[2])\n",
- "scatter(\"Crypto++\",colors[3])\n",
- "scatter(\"mbedtls\",colors[4])\n",
- "scatter(\"libressl\",colors[5])\n",
- "scatter(\"BouncyCastle\",colors[6])\n",
- "scatter(\"tomcrypt\",colors[7])\n",
- "scatter(\"ippcp\",colors[8])\n",
- "\n",
- "\n",
- "# plt.plot([l for l in range(L)],[general_distributions[l] for l in selected_divisors],color = colors[9], label = \"divison-distribution\")\n",
- "\n",
- "plt.xlabel('Input point order',fontsize=15) \n",
- "plt.ylabel(\"Error rate\",fontsize=15) \n",
- "plt.xticks([r for r in range(L)], [v if i%1==0 else \"\" for i,v in enumerate(selected_divisors)])\n",
- "plt.legend(loc=\"center right\",prop={'size': 11})\n",
- "plt.tight_layout()\n",
- "plt.show() \n",
- "fig[0].savefig(f\"graphs/lib_dists.png\",dpi=300)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "a0415861-c9ee-4420-bb82-6fda216d401c",
- "metadata": {},
- "outputs": [],
- "source": [
- "def bars(library, color,shift,width):\n",
- " measured_dist = measured_distribution(library,selected_divisors)\n",
- " mes_x, mes_y = [],[]\n",
- " for i,l in enumerate(selected_divisors):\n",
- " offset = width*shift\n",
- " if l in measured_dist:\n",
- " mes_y.append(measured_dist[l])\n",
- " mes_x.append(2*i+offset)\n",
- " plt.bar(mes_x,mes_y,width=0.1,color = color,align ='center', label = labels.get(library,library))\n",
- "\n",
- "selected_divisors = small_primes[:12]#all_divisors\n",
- "\n",
- "fig = plt.subplots(figsize =(10, 4)) \n",
- "\n",
- "L = len(selected_divisors)\n",
- "\n",
- "colors = matplotlib.cm.tab20(range(9))\n",
- "width = 0.2\n",
- "for i,lib in enumerate((\"openssl\",\"libressl\",\"botan\",\"Crypto++\",\"mbedtls\",\"BouncyCastle\",\"tomcrypt\",\"ippcp\")):\n",
- " bars(lib,colors[i],i,width)\n",
- "\n",
- "\n",
- "plt.plot([2*l+4*width for l in range(L)],[general_distributions[l] for l in selected_divisors],color = colors[8], label = \"expected distribution\")\n",
- "\n",
- "plt.xlabel('Input point order',fontsize=15) \n",
- "plt.ylabel(\"Error rate\",fontsize=15) \n",
- "plt.xticks([2*r+4*width for r in range(L)], [v if i%1==0 else \"\" for i,v in enumerate(selected_divisors)])\n",
- "plt.legend(loc=\"upper right\",prop={'size': 11})\n",
- "plt.tight_layout()\n",
- "plt.show() \n",
- "fig[0].savefig(f\"graphs/lib_dists.png\",dpi=300)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "e5f036c8-3d47-4105-9a67-79b2b8e1ecdb",
- "metadata": {},
- "outputs": [],
- "source": [
- "from math import sqrt\n",
- "\n",
- "selected_mults = all_mults#window_mults[0:1]+window_mults[5:6]+naf_mults[1:2]#[mult for mult in all_mults if not mult in comb_mults]\n",
- "selected_divisors = small_primes#ll_divisors\n",
- "colors = {mult:matplotlib.cm.tab20(range(len(selected_mults)))[i] for i,mult in enumerate(selected_mults)}\n",
- "\n",
- "\n",
- "fig = plt.subplots(figsize =(30, 20)) \n",
- "\n",
- "L = len(selected_divisors)\n",
- "selected_divisors = sorted(selected_divisors)\n",
- "for mult in selected_mults:\n",
- " y_values,y_values_mstd, y_values_pstd = [],[],[]\n",
- " for l in selected_divisors:\n",
- " p = distributions_mults[mult][l]\n",
- " y_values.append(1/p)\n",
- " y_values_mstd.append(1/p-sqrt((1-p)/p**2))\n",
- " y_values_pstd.append(1/p+sqrt((1-p)/p**2))\n",
- " plt.plot([l for l in range(L)],y_values,color = colors[mult], label = mult_label(mult))\n",
- " plt.fill_between([l for l in range(L)], y_values_mstd , y_values_pstd, alpha = 0.1, color = colors[mult])\n",
- " \n",
- "# mult = list(fixedwindow_dist.keys())[0]\n",
- "# plt.plot([l for l in range(L)],[fixedwindow_dist[l] for l in selected_divisors],color = \"pink\", label = mult_label(mult))\n",
- "\n",
- "# measured_dist = measured_distribution(library,selected_divisors)\n",
- "# mes_x, mes_y = [],[]\n",
- "# for i,l in enumerate(selected_divisors):\n",
- "# if l in measured_dist:\n",
- "# mes_y.append(measured_dist[l])\n",
- "# mes_x.append(i)\n",
- "# plt.scatter(mes_x,mes_y,color = \"black\", label = library)\n",
- "\n",
- "plt.plot([l for l in range(L)],[1/general_distributions[l] for l in selected_divisors],color = \"black\", label = \"prime-distribution\")\n",
- "\n",
- "steps_dist = {}\n",
- "for i in range(51):\n",
- " with open(f\"cards/jcop/199_{i}.txt\") as f:\n",
- " steps = f.read().count(\"ALG_EC_SVDP_DH of remote pubkey and local privkey\")\n",
- " if not steps in steps_dist:\n",
- " steps_dist[steps] = 0\n",
- " steps_dist[steps]+=1\n",
- "ys = sorted(list(steps_dist.keys()))\n",
- "plt.scatter([selected_divisors.index(199)]*len(ys),ys, s=[steps_dist[y]*20 for y in ys],color=\"black\")\n",
- "for i, y in enumerate(ys):\n",
- " plt.annotate(str(steps_dist[y]), (selected_divisors.index(199)-1, y))\n",
- "avg = 0\n",
- "for s,c in steps_dist.items():\n",
- " avg+=s*c\n",
- "avg = avg/sum(steps_dist.values())\n",
- "plt.scatter([selected_divisors.index(199)],[avg], s=[30],color=\"yellow\")\n",
- " \n",
- "plt.xlabel('divisors') \n",
- "plt.ylabel(\"prob\") \n",
- "plt.yticks(range(12))\n",
- "plt.xticks([r for r in range(L)], selected_divisors)\n",
- "\n",
- "plt.legend()\n",
- "plt.show() \n",
- "fig[0].savefig(f\"graphs/re.png\",dpi=300)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "37a64963-e63a-4c74-8414-6d29482e7151",
- "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.13.1"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 5
-}
diff --git a/epare/simulate.ipynb b/epare/simulate.ipynb
index 186950b..74cfe2b 100644
--- a/epare/simulate.ipynb
+++ b/epare/simulate.ipynb
@@ -10,7 +10,7 @@
},
{
"cell_type": "code",
- "execution_count": 1,
+ "execution_count": null,
"id": "b4386513-cc14-434b-a748-2863f8657452",
"metadata": {},
"outputs": [],
@@ -52,7 +52,7 @@
},
{
"cell_type": "code",
- "execution_count": 2,
+ "execution_count": null,
"id": "5c0e42dc-8c61-4e2e-962c-6af48f6eb321",
"metadata": {},
"outputs": [],
@@ -116,25 +116,17 @@
},
{
"cell_type": "code",
- "execution_count": 3,
+ "execution_count": null,
"id": "a660e3ac-401b-47a0-92de-55afe63c420a",
"metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "41\n"
- ]
- }
- ],
+ "outputs": [],
"source": [
"print(len(all_mults))"
]
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": null,
"id": "07bc266d-35eb-4f6d-bdba-e9f6f66827f1",
"metadata": {},
"outputs": [],
@@ -180,7 +172,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": null,
"id": "bb604b15-4ad6-43c0-9cfa-1b31611d73ce",
"metadata": {},
"outputs": [],
@@ -190,7 +182,7 @@
},
{
"cell_type": "code",
- "execution_count": 6,
+ "execution_count": null,
"id": "4d5c7f10-618f-4612-b594-81d1607b0d1d",
"metadata": {},
"outputs": [],
@@ -215,43 +207,10 @@
},
{
"cell_type": "code",
- "execution_count": 7,
+ "execution_count": null,
"id": "84359084-4116-436c-92cd-d43fdfeca842",
"metadata": {},
- "outputs": [
- {
- "data": {
- "application/vnd.jupyter.widget-view+json": {
- "model_id": "0f2977641ab546afafc38be4f638f92c",
- "version_major": 2,
- "version_minor": 0
- },
- "text/plain": [
- "Computing small scalar distributions.: 0%| | 0/41 [00:00<?, ?it/s]"
- ]
- },
- "metadata": {},
- "output_type": "display_data"
- },
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Got SlidingWindowMultiplier_()_{'width': 4}.\n"
- ]
- },
- {
- "ename": "NameError",
- "evalue": "name 'res' is not defined",
- "output_type": "error",
- "traceback": [
- "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
- "\u001b[31mNameError\u001b[39m Traceback (most recent call last)",
- "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[7]\u001b[39m\u001b[32m, line 12\u001b[39m\n\u001b[32m 10\u001b[39m \u001b[38;5;28;01mcontinue\u001b[39;00m\n\u001b[32m 11\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m mult \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m multiples_mults:\n\u001b[32m---> \u001b[39m\u001b[32m12\u001b[39m multiples_mults[mult] = \u001b[43mres\u001b[49m\n\u001b[32m 13\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 14\u001b[39m \u001b[38;5;66;03m# Accumulate\u001b[39;00m\n\u001b[32m 15\u001b[39m multiples_mults[mult].merge(res)\n",
- "\u001b[31mNameError\u001b[39m: name 'res' is not defined"
- ]
- }
- ],
+ "outputs": [],
"source": [
"with TaskExecutor(max_workers=num_workers, mp_context=spawn_context) as pool, enable_spawn(get_small_scalar_multiples) as target:\n",
" for mult in selected_mults:\n",