diff options
Diffstat (limited to 'analysis/countermeasures/curvegen.ipynb')
| -rw-r--r-- | analysis/countermeasures/curvegen.ipynb | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/analysis/countermeasures/curvegen.ipynb b/analysis/countermeasures/curvegen.ipynb new file mode 100644 index 0000000..949fe8e --- /dev/null +++ b/analysis/countermeasures/curvegen.ipynb @@ -0,0 +1,202 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "a98d0f65-3419-4c00-9921-66b5d3e3ee44", + "metadata": {}, + "source": [ + "# Curve generation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4a81787a-c139-479f-84a5-93d4fd07698a", + "metadata": {}, + "outputs": [], + "source": [ + "import itertools\n", + "import random\n", + "import time\n", + "import json\n", + "\n", + "import cypari2\n", + "\n", + "from subprocess import Popen, PIPE\n", + "\n", + "from tqdm.auto import trange, tqdm\n", + "\n", + "from common import divisor_map" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1d385964-888d-4704-8ff8-40ebf2fac3da", + "metadata": {}, + "outputs": [], + "source": [ + "bits = 256" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "227173fe-308f-4c2f-abf9-9a4480600484", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "pari = cypari2.Pari()\n", + "orders = []\n", + "for divisor in tqdm(divisor_map[\"all\"]):\n", + " dbits = divisor.bit_length()\n", + " pbits = bits - dbits\n", + " if dbits == bits:\n", + " print(divisor, 1)\n", + " orders.append((divisor, 1, divisor))\n", + " continue\n", + " if dbits > bits:\n", + " print(f\"Cannot fill in for divisor with {dbits}.\")\n", + " continue\n", + " while True:\n", + " prime = int(pari.randomprime([2**(pbits-1), 2**(pbits+1)]))\n", + " order = divisor * prime\n", + " if order.bit_length() == bits:\n", + " break\n", + " print(divisor, prime)\n", + " orders.append((divisor, prime, order))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "93fc0fc1-f1d5-48e3-90db-5907f3530813", + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"curves_1.json\", \"r\") as f:\n", + " parsed = json.load(f)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "af70d331-8ac7-4985-ab98-0201c6c399fb", + "metadata": {}, + "outputs": [], + "source": [ + "commands = []\n", + "processes = []\n", + "for divisor, prime, order in orders:\n", + " if str(divisor) in parsed:\n", + " continue\n", + " command = [\"./ecgen\", \"--fp\", \"-n\", f\"{divisor},{prime}\", \"-u\", \"--points\", \"none\", \"-m\", \"12g\", \"--threads\", \"8\", str(bits)]\n", + " commands.append(command)\n", + " print(\" \".join(command))\n", + "\n", + "random.shuffle(commands)\n", + "total = len(commands)\n", + "\n", + "results = {}\n", + "errored = {}\n", + "running = []\n", + "max_procs = 20\n", + "with tqdm(total=total, smoothing=0, desc=\"Computing curves\") as pbar:\n", + " while commands or running:\n", + " done = []\n", + " for cmd, start, proc in running:\n", + " now = time.time()\n", + " divisor = int(cmd[3].split(\",\")[0])\n", + " if (ret := proc.poll()) is not None:\n", + " print(\" \".join(cmd))\n", + " err = proc.stderr.read()\n", + " res = proc.stdout.read()\n", + " if err:\n", + " errored[divisor] = err\n", + " print(err)\n", + " else:\n", + " results[divisor] = res\n", + " pbar.update(1)\n", + " done.append((cmd, start, proc))\n", + " if len(results) % 10 == 0:\n", + " print(f\"Results {len(results)}, remaining {len(commands)}, errored {len(errored)}\")\n", + " elif now - start > 60 * 60:\n", + " print(\"Timed out: \" + \" \".join(cmd))\n", + " errored[divisor] = \"timeout\"\n", + " proc.kill()\n", + " pbar.update(1)\n", + " done.append((cmd, start, proc))\n", + " for d in done:\n", + " running.remove(d)\n", + " time.sleep(1)\n", + " while len(running) < max_procs and commands:\n", + " command = commands.pop()\n", + " start = time.time()\n", + " proc = Popen(command, stdout=PIPE, stderr=PIPE, text=True)\n", + " running.append((command, start, proc))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ddd92921-ccdd-4978-b36f-65872973d138", + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"curves_1.json\", \"r\") as f:\n", + " parsed = json.load(f)\n", + "if results:\n", + " for div, res in results.items():\n", + " try:\n", + " data = json.loads(res)\n", + " parsed[str(div)] = data[0]\n", + " print(\"ok\", div)\n", + " except:\n", + " print(\"failed\", div)\n", + "with open(\"curves_1.json\", \"w\") as f:\n", + " json.dump(parsed, f)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c26f55ff-4cf4-43d0-b1ed-5f3c0c6708df", + "metadata": {}, + "outputs": [], + "source": [ + "len(parsed)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5bf5e3e7-7472-458b-bdc8-e5b7a8d65ec1", + "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 +} |
