From 6dde089b1f2f85583aa546fabe0645fe0fd0cb3b Mon Sep 17 00:00:00 2001 From: J08nY Date: Mon, 10 Mar 2025 19:41:59 +0100 Subject: Remove RE notebook. Fix simulate one. --- epare/re.ipynb | 708 --------------------------------------------------- epare/simulate.ipynb | 59 +---- 2 files changed, 9 insertions(+), 758 deletions(-) delete mode 100644 epare/re.ipynb 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 \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", -- cgit v1.2.3-70-g09d2