diff options
Diffstat (limited to 'epare/simulate.ipynb')
| -rw-r--r-- | epare/simulate.ipynb | 165 |
1 files changed, 57 insertions, 108 deletions
diff --git a/epare/simulate.ipynb b/epare/simulate.ipynb index 74cfe2b..4a0a07a 100644 --- a/epare/simulate.ipynb +++ b/epare/simulate.ipynb @@ -17,6 +17,7 @@ "source": [ "import pickle\n", "import itertools\n", + "import glob\n", "\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", @@ -25,7 +26,7 @@ "from collections import Counter\n", "\n", "from pathlib import Path\n", - "from random import randint\n", + "from random import randint, randbytes\n", "from typing import Type, Any\n", "\n", "from bs4 import BeautifulSoup\n", @@ -33,13 +34,10 @@ "\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.sca.re.rpa import multiples_computed\n", "from pyecsca.misc.utils import TaskExecutor\n", "\n", - "from common import MultIdent, MultResults, enable_spawn, spawn_context" + "from common import *" ] }, { @@ -53,70 +51,6 @@ { "cell_type": "code", "execution_count": null, - "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": null, "id": "a660e3ac-401b-47a0-92de-55afe63c420a", "metadata": {}, "outputs": [], @@ -152,13 +86,31 @@ " 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", + "def get_small_scalar_multiples(mult: MultIdent,\n", + " params: DomainParameters,\n", + " bits: int,\n", + " samples: int = 1000,\n", + " use_init: bool = True,\n", + " use_multiply: bool = True,\n", + " seed: bytes | None = None) -> MultResults:\n", " from pyecsca.sca.re.rpa import multiples_computed\n", - " from random import randint\n", + " import random\n", + " \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", + " if seed is not None:\n", + " random.seed(seed)\n", + "\n", + " # If no countermeasure is used, we have fully random scalars.\n", + " # Otherwise, fix one per chunk.\n", + " if mult.countermeasure is None:\n", + " scalars = [random.randint(1, 2**bits) for _ in range(samples)]\n", + " else:\n", + " one = random.randint(1, 2**bits)\n", + " scalars = [one for _ in range(samples)]\n", + "\n", + " for scalar in scalars:\n", + " # Use a list for less memory usage.\n", + " results.append(list(multiples_computed(scalar, params, mult.klass, mult.partial, use_init, use_multiply)))\n", " return MultResults(results, samples)" ] }, @@ -173,16 +125,6 @@ { "cell_type": "code", "execution_count": null, - "id": "bb604b15-4ad6-43c0-9cfa-1b31611d73ce", - "metadata": {}, - "outputs": [], - "source": [ - "multiples_mults = {}" - ] - }, - { - "cell_type": "code", - "execution_count": null, "id": "4d5c7f10-618f-4612-b594-81d1607b0d1d", "metadata": {}, "outputs": [], @@ -202,7 +144,7 @@ "metadata": {}, "source": [ "## Run\n", - "Run this cell as many times as you want. It will accumulate into multiples_mults." + "Run this cell as many times as you want. It will write chunks into files." ] }, { @@ -212,11 +154,15 @@ "metadata": {}, "outputs": [], "source": [ + "multiples_mults = {}\n", + "chunk_id = randbytes(4).hex()\n", "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 countermeasure in (None, \"gsr\", \"additive\", \"multiplicative\", \"euclidean\"):\n", + " mwc = mult.with_countermeasure(countermeasure)\n", + " pool.submit_task(mwc,\n", + " target,\n", + " mwc, params, bits, samples, seed=chunk_id)\n", " for mult, future in tqdm(pool.as_completed(), desc=\"Computing small scalar distributions.\", total=len(pool.tasks)):\n", " print(f\"Got {mult}.\")\n", " if error := future.exception():\n", @@ -227,46 +173,49 @@ " multiples_mults[mult] = res\n", " else:\n", " # Accumulate\n", - " multiples_mults[mult].merge(res)" + " multiples_mults[mult].merge(res)\n", + " # Handle the enable_spawn trick that messes up class modules.\n", + " for k, v in multiples_mults.items():\n", + " v.__class__ = MultResults\n", + " v.__module__ = \"common\"\n", + "with open(f\"multiples_{category}_{curve}_{bits}_ctr_chunk{chunk_id}.pickle\",\"wb\") as h:\n", + " pickle.dump(multiples_mults, h)" ] }, { "cell_type": "markdown", - "id": "6845ba69-74b0-4709-a64d-dd4860255ee2", + "id": "b4471a1d-fdc3-4be7-bd61-5ddd22180b41", "metadata": {}, "source": [ - "### Save" + "### Load\n", + "Beware, the following load with try to load all chunks into memory, that will be very large." ] }, { "cell_type": "code", "execution_count": null, - "id": "0ae7f726-2981-48af-8ae3-a9afcf2dc18f", + "id": "3d291832-b0c7-4c3a-9989-22079e4e0f53", "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" + "multiples_mults = {}\n", + "for fname in glob.glob(f\"multiples_{category}_{curve}_{bits}_chunk*.pickle\"):\n", + " with open(fname, \"rb\") as f:\n", + " multiples_loaded = pickle.load(f)\n", + " for mult, vals in multiples_loaded.items():\n", + " if mult not in multiples_mults:\n", + " multiples_mults[mult] = vals\n", + " else:\n", + " multiples_mults[mult].merge(vals)" ] }, { "cell_type": "code", "execution_count": null, - "id": "3d291832-b0c7-4c3a-9989-22079e4e0f53", + "id": "264385f2-0f58-4f18-bfc2-61f2c5b6e5c8", "metadata": {}, "outputs": [], - "source": [ - "with open(f\"multiples_{category}_{curve}_{bits}\", \"rb\") as f:\n", - " multiples_mults = pickle.load(f)" - ] + "source": [] } ], "metadata": { |
