diff options
Diffstat (limited to 'epare/simulate.ipynb')
| -rw-r--r-- | epare/simulate.ipynb | 315 |
1 files changed, 315 insertions, 0 deletions
diff --git a/epare/simulate.ipynb b/epare/simulate.ipynb new file mode 100644 index 0000000..0acc8f6 --- /dev/null +++ b/epare/simulate.ipynb @@ -0,0 +1,315 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "805d746e-610b-4d40-80d2-a8080a993f96", + "metadata": {}, + "source": [ + "# Simulating EPA-RE using points of low-order" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "b4386513-cc14-434b-a748-2863f8657452", + "metadata": {}, + "outputs": [], + "source": [ + "import pickle\n", + "import itertools\n", + "\n", + "import matplotlib\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "\n", + "from collections import Counter\n", + "\n", + "from pathlib import Path\n", + "from random import randint\n", + "from typing import Type, Any\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\n", + "\n", + "from common import MultIdent, MultResults, enable_spawn, spawn_context" + ] + }, + { + "cell_type": "markdown", + "id": "5b156d2a-7345-47f8-a76e-71a7d2be9d22", + "metadata": {}, + "source": [ + "## Initialize" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "5c0e42dc-8c61-4e2e-962c-6af48f6eb321", + "metadata": {}, + "outputs": [], + "source": [ + "# All dbl-and-add multipliers from https://github.com/J08nY/pyecsca/blob/master/pyecsca/ec/mult\n", + "\n", + "window_mults = [\n", + " MultIdent(SlidingWindowMultiplier, width=3),\n", + " MultIdent(SlidingWindowMultiplier, width=4),\n", + " MultIdent(SlidingWindowMultiplier, width=5),\n", + " MultIdent(SlidingWindowMultiplier, width=6),\n", + " MultIdent(FixedWindowLTRMultiplier, m=2**4),\n", + " MultIdent(FixedWindowLTRMultiplier, m=2**5),\n", + " MultIdent(FixedWindowLTRMultiplier, m=2**6),\n", + " MultIdent(WindowBoothMultiplier, width=3),\n", + " MultIdent(WindowBoothMultiplier, width=4),\n", + " MultIdent(WindowBoothMultiplier, width=5),\n", + " MultIdent(WindowBoothMultiplier, width=6)\n", + "]\n", + "naf_mults = [\n", + " MultIdent(WindowNAFMultiplier, width=3),\n", + " MultIdent(WindowNAFMultiplier, width=4),\n", + " MultIdent(WindowNAFMultiplier, width=5),\n", + " MultIdent(WindowNAFMultiplier, width=6),\n", + " MultIdent(BinaryNAFMultiplier, direction=ProcessingDirection.LTR),\n", + " MultIdent(BinaryNAFMultiplier, direction=ProcessingDirection.RTL)\n", + "]\n", + "comb_mults = [\n", + " MultIdent(CombMultiplier, width=2),\n", + " MultIdent(CombMultiplier, width=3),\n", + " MultIdent(CombMultiplier, width=4),\n", + " MultIdent(CombMultiplier, width=5),\n", + " MultIdent(CombMultiplier, width=6),\n", + " MultIdent(BGMWMultiplier, width=2, direction=ProcessingDirection.LTR),\n", + " MultIdent(BGMWMultiplier, width=3, direction=ProcessingDirection.LTR),\n", + " MultIdent(BGMWMultiplier, width=4, direction=ProcessingDirection.LTR),\n", + " MultIdent(BGMWMultiplier, width=5, direction=ProcessingDirection.LTR),\n", + " MultIdent(BGMWMultiplier, width=6, direction=ProcessingDirection.LTR),\n", + " MultIdent(BGMWMultiplier, width=2, direction=ProcessingDirection.RTL),\n", + " MultIdent(BGMWMultiplier, width=3, direction=ProcessingDirection.RTL),\n", + " MultIdent(BGMWMultiplier, width=4, direction=ProcessingDirection.RTL),\n", + " MultIdent(BGMWMultiplier, width=5, direction=ProcessingDirection.RTL),\n", + " MultIdent(BGMWMultiplier, width=6, direction=ProcessingDirection.RTL)\n", + "]\n", + "binary_mults = [\n", + " MultIdent(LTRMultiplier, always=False),\n", + " MultIdent(LTRMultiplier, always=True),\n", + " MultIdent(RTLMultiplier, always=False),\n", + " MultIdent(RTLMultiplier, always=True),\n", + " MultIdent(CoronMultiplier)\n", + "]\n", + "other_mults = [\n", + " MultIdent(FullPrecompMultiplier, always=False),\n", + " MultIdent(FullPrecompMultiplier, always=True),\n", + " MultIdent(SimpleLadderMultiplier, complete=True),\n", + " MultIdent(SimpleLadderMultiplier, complete=False)\n", + "]\n", + "\n", + "all_mults = window_mults + naf_mults + binary_mults + other_mults + comb_mults" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "a660e3ac-401b-47a0-92de-55afe63c420a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "41\n" + ] + } + ], + "source": [ + "print(len(all_mults))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "07bc266d-35eb-4f6d-bdba-e9f6f66827f1", + "metadata": {}, + "outputs": [], + "source": [ + "# Needs imports on the inside to be spawn enabled to save memory.\n", + "\n", + "def get_general_multiples(bits: int, samples: int = 1000) -> MultResults:\n", + " from random import randint\n", + " results = []\n", + " for _ in range(samples):\n", + " big_scalar = randint(1, 2**bits)\n", + " results.append({big_scalar})\n", + " return MultResults(results, samples)\n", + "\n", + "def get_general_n_multiples(bits: int, n: int, samples: int = 1000) -> MultResults:\n", + " from random import randint\n", + " results = []\n", + " for _ in range(samples):\n", + " smult = set()\n", + " for i in range(n):\n", + " b = randint(1,256)\n", + " smult.add(randint(2**b,2**(b+1)))\n", + " results.append(smult)\n", + " return MultResults(results, samples)\n", + "\n", + "def get_small_scalar_multiples(mult: MultIdent, params: DomainParameters, bits: int, samples: int = 1000, use_init: bool = True, use_multiply: bool = True) -> MultResults:\n", + " from pyecsca.sca.re.rpa import multiples_computed\n", + " from random import randint\n", + " results = []\n", + " for _ in range(samples):\n", + " big_scalar = randint(1, 2**bits)\n", + " results.append(multiples_computed(big_scalar, params, mult.klass, mult.partial, use_init, use_multiply))\n", + " return MultResults(results, samples)" + ] + }, + { + "cell_type": "markdown", + "id": "8c5e9543-8447-4362-b9e2-c896d71f69a9", + "metadata": {}, + "source": [ + "## Prepare" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "bb604b15-4ad6-43c0-9cfa-1b31611d73ce", + "metadata": {}, + "outputs": [], + "source": [ + "multiples_mults = {}" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "4d5c7f10-618f-4612-b594-81d1607b0d1d", + "metadata": {}, + "outputs": [], + "source": [ + "category = \"secg\"\n", + "curve = \"secp256r1\"\n", + "params = get_params(category, curve, \"projective\")\n", + "num_workers = 20\n", + "bits = params.order.bit_length()\n", + "samples = 1000\n", + "selected_mults = all_mults" + ] + }, + { + "cell_type": "markdown", + "id": "3aaf712e-5b97-4390-8dd4-e1db1dfe36a2", + "metadata": {}, + "source": [ + "## Run\n", + "Run this cell as many times as you want. It will accumulate into multiples_mults." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "84359084-4116-436c-92cd-d43fdfeca842", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "8050887d56444467ae4a9e8345acaab5", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Computing small scalar distributions.: 0%| | 0/41 [00:00<?, ?it/s]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "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", + " pool.submit_task(mult,\n", + " target,\n", + " mult, params, 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 error := future.exception():\n", + " print(error)\n", + " continue\n", + " if mult not in multiples_mults:\n", + " multiples_mults[mult] = res\n", + " else:\n", + " # Accumulate\n", + " multiples_mults[mult].merge(res)" + ] + }, + { + "cell_type": "markdown", + "id": "6845ba69-74b0-4709-a64d-dd4860255ee2", + "metadata": {}, + "source": [ + "### Save" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0ae7f726-2981-48af-8ae3-a9afcf2dc18f", + "metadata": {}, + "outputs": [], + "source": [ + "with open(f\"multiples_{category}_{curve}_{bits}\",\"wb\") as h:\n", + " pickle.dump(multiples_mults, h)" + ] + }, + { + "cell_type": "markdown", + "id": "b4471a1d-fdc3-4be7-bd61-5ddd22180b41", + "metadata": {}, + "source": [ + "### Load" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3d291832-b0c7-4c3a-9989-22079e4e0f53", + "metadata": {}, + "outputs": [], + "source": [ + "with open(f\"multiples_{category}_{curve}_{bits}\", \"rb\") as f:\n", + " multiples_mults = pickle.load(f)" + ] + } + ], + "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 +} |
