diff options
| author | vojtechsu | 2023-11-23 12:06:01 +0100 |
|---|---|---|
| committer | J08nY | 2023-12-05 14:06:07 +0100 |
| commit | e4d1106a9e633467ce39ce0c7d0591ad431beeb8 (patch) | |
| tree | 6aa8388686a19207185a51faadd30ce1653f436c | |
| parent | 146c17f7f68ece914b5654b6d1cfb445854a1df3 (diff) | |
| download | pyecsca-e4d1106a9e633467ce39ce0c7d0591ad431beeb8.tar.gz pyecsca-e4d1106a9e633467ce39ce0c7d0591ad431beeb8.tar.zst pyecsca-e4d1106a9e633467ce39ce0c7d0591ad431beeb8.zip | |
Add playground notebook
| -rw-r--r-- | pyecsca/ec/formula_gen/formula_playground.ipynb | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/pyecsca/ec/formula_gen/formula_playground.ipynb b/pyecsca/ec/formula_gen/formula_playground.ipynb new file mode 100644 index 0000000..e84f139 --- /dev/null +++ b/pyecsca/ec/formula_gen/formula_playground.ipynb @@ -0,0 +1,177 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from pyecsca.ec.model import ShortWeierstrassModel, MontgomeryModel, TwistedEdwardsModel\n", + "from pyecsca.ec.formula_gen.test import load_efd_formulas, load_library_formulas\n", + "from pyecsca.ec.formula_gen.formula_graph import EFDFormulaGraph\n", + "from pyecsca.ec.formula_gen.fliparoo import generate_fliparood_formulas, greedy_fliparoo, recursive_fliparoo\n", + "import pyecsca.ec.formula_gen.metrics as metrics\n", + "from pyecsca.ec.formula_gen.switch_sign import generate_switched_formulas\n", + "from tqdm.notebook import tqdm" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Draw formula\n", + "coordinate_name = \"jacobian\"\n", + "model = ShortWeierstrassModel()\n", + "name = \"add-1998-cmo-2\"\n", + "formula = load_efd_formulas(coordinate_name,model)[name]\n", + "graph = EFDFormulaGraph()\n", + "graph.construct_graph(formula)\n", + "graph.draw()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Draw all formulas\n", + "coordinate_name = \"jacobian\"\n", + "model = ShortWeierstrassModel()\n", + "for name, formula in load_efd_formulas(coordinate_name,model).items():\n", + " graph = EFDFormulaGraph()\n", + " graph.construct_graph(formula)\n", + " graph.draw(f\"{coordinate_name}:{name}.png\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Measure similarity of fliparood formulas\n", + "coordinate_name = \"jacobian\"\n", + "model = ShortWeierstrassModel()\n", + "name = \"add-1998-cmo-2\"\n", + "formula = load_efd_formulas(coordinate_name,model)[name]\n", + "for fliparood in generate_fliparood_formulas(formula):\n", + " print(metrics.formula_similarity(formula,fliparood))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Greedy fliparoo to connect two formulas\n", + "coordinate_name = \"jacobian\"\n", + "model = ShortWeierstrassModel()\n", + "name = \"add-1998-cmo-2\"\n", + "formula = load_efd_formulas(coordinate_name,model)[name]\n", + "lib_formula = load_library_formulas()[\"add-openssl-z256\"]\n", + "metric = lambda x: metrics.formula_similarity(x,formula)[\"ivs\"]\n", + "flips, closest, sim = greedy_fliparoo(lib_formula,metric)\n", + "print(f\"Number of flips: {flips}, similarity: {sim}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Generate all fliparoos for all library formulas\n", + "depth = 2\n", + "fliparood = {}\n", + "libs = load_library_formulas()\n", + "for name, formula in tqdm(libs.items()):\n", + " fliparood[name] = recursive_fliparoo(formula,depth)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\" Connect the generated formulas to efd \"\"\"\n", + "neighborhoods = fliparood \n", + "for name,formulas in fliparood.items():\n", + " coordinates, model = libs[name].coordinate_model.name, libs[name].coordinate_model.curve_model\n", + " similarities = {}\n", + " for efd_name, efd_formula in load_efd_formulas(coordinates, model.__class__).items():\n", + " metric = lambda x: metrics.formula_similarity(x,efd_formula)[\"ivs\"]\n", + " flips, closest_fliparoo = max(formulas, key = lambda x: metric(x[1]))\n", + " similarities[efd_name] = flips, metric(closest_fliparoo)\n", + " closest_efd, (flips, sim) = max(similarities.items(), key = lambda x: x[1][1])\n", + " print(f\"{name}. Closest match: {closest_efd}, flips={flips}, similarity={sim}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "switch_signed = {}\n", + "for name, fliparoo_neighborhood in tqdm(fliparood.items()):\n", + " neighb = list()\n", + " for flips,flip_f in fliparoo_neighborhood:\n", + " neighb.extend(generate_switched_formulas(flip_f))\n", + " switch_signed[name] = neighb" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\" Connect the generated formulas to efd \"\"\"\n", + "neighborhoods = switch_signed \n", + "for name,formulas in tqdm(switch_signed.items()):\n", + " coordinates, model = libs[name].coordinate_model.name, libs[name].coordinate_model.curve_model\n", + " similarities = {}\n", + " for efd_name, efd_formula in load_efd_formulas(coordinates, model.__class__).items():\n", + " metric = lambda x: metrics.formula_similarity(x,efd_formula)[\"ivs\"]\n", + " closest_switch_signed = max(formulas, key = metric)\n", + " similarities[efd_name] = metric(closest_switch_signed)\n", + " closest_efd, sim = max(similarities.items(), key = lambda x: x[1])\n", + " print(f\"{name}. Closest match: {closest_efd}, similarity={sim}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "env", + "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.10.12" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} |
