aboutsummaryrefslogtreecommitdiff
path: root/epare/simulate.ipynb
blob: 89a55331ca1531ae855f150965443587dd4eb90c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
{
 "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": null,
   "id": "b4386513-cc14-434b-a748-2863f8657452",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "import itertools\n",
    "import glob\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, randbytes\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 multiples_computed\n",
    "from pyecsca.misc.utils import TaskExecutor\n",
    "\n",
    "from common import *"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b156d2a-7345-47f8-a76e-71a7d2be9d22",
   "metadata": {},
   "source": [
    "## Initialize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a660e3ac-401b-47a0-92de-55afe63c420a",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(all_mults))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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,\n",
    "                               params: DomainParameters,\n",
    "                               bits: int,\n",
    "                               samples: int = 100,\n",
    "                               use_init: bool = True,\n",
    "                               use_multiply: bool = True,\n",
    "                               seed: bytes | None = None,\n",
    "                               kind: str = \"precomp+necessary\") -> MultResults:\n",
    "    from pyecsca.sca.re.rpa import multiples_computed\n",
    "    import random\n",
    "    \n",
    "    results = []\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, kind=kind)))\n",
    "    return MultResults(results, samples)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c5e9543-8447-4362-b9e2-c896d71f69a9",
   "metadata": {},
   "source": [
    "## Prepare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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 = 100\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 write chunks into files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84359084-4116-436c-92cd-d43fdfeca842",
   "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",
    "        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",
    "            print(\"Error!\", error)\n",
    "            continue\n",
    "        res = future.result()\n",
    "        if mult not in multiples_mults:\n",
    "            multiples_mults[mult] = res\n",
    "        else:\n",
    "            # Accumulate\n",
    "            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": "b4471a1d-fdc3-4be7-bd61-5ddd22180b41",
   "metadata": {},
   "source": [
    "### Load\n",
    "**Beware**, the following load with try to load all chunks into memory, that will be very large.\n",
    "\n",
    "You probably dont want to run this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d291832-b0c7-4c3a-9989-22079e4e0f53",
   "metadata": {},
   "outputs": [],
   "source": [
    "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": "11b447f2-71ab-417e-a856-1724788cfc91",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}