aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJ08nY2023-08-03 14:24:51 +0200
committerJ08nY2023-08-03 14:24:51 +0200
commit2f4ea0f9a2fb5c5c12d119b0517f659408c9bb03 (patch)
treea241b499c9dc7aa82e2a40f3d6f9c85c78fd6903
parent21519e597a18e5c964ea0b91f05fa8c2f8e696bb (diff)
downloadpyecsca-notebook-2f4ea0f9a2fb5c5c12d119b0517f659408c9bb03.tar.gz
pyecsca-notebook-2f4ea0f9a2fb5c5c12d119b0517f659408c9bb03.tar.zst
pyecsca-notebook-2f4ea0f9a2fb5c5c12d119b0517f659408c9bb03.zip
Add non-working EPA notebook.
-rw-r--r--re/epa.ipynb162
1 files changed, 162 insertions, 0 deletions
diff --git a/re/epa.ipynb b/re/epa.ipynb
new file mode 100644
index 0000000..2ed690b
--- /dev/null
+++ b/re/epa.ipynb
@@ -0,0 +1,162 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "18343664-ebec-4e95-88f2-0231082b6b6e",
+ "metadata": {},
+ "source": [
+ "# EPA-based reverse engineering"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "dac3b016-508b-4e63-897a-8ab46cb4f6ed",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from pyecsca.ec.model import ShortWeierstrassModel\n",
+ "from pyecsca.ec.curve import EllipticCurve\n",
+ "from pyecsca.ec.params import DomainParameters\n",
+ "from pyecsca.ec.mod import Mod, miller_rabin\n",
+ "from pyecsca.ec.point import Point\n",
+ "from pyecsca.ec.error import NonInvertibleError\n",
+ "from pyecsca.ec.mult import LTRMultiplier"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "6e92cdd8-9040-4c6d-9700-f992d09195ee",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "model = ShortWeierstrassModel()\n",
+ "coords = model.coordinates[\"projective\"]\n",
+ "add = coords.formulas[\"add-2007-bl\"]\n",
+ "dbl = coords.formulas[\"dbl-2007-bl\"]\n",
+ "neg = coords.formulas[\"neg\"]\n",
+ "\n",
+ "p = 0xdb49063db56b7783fa01dd62077c5a88dfa28009 # such that phi(p)/p ~ 0.5\n",
+ "a = Mod(0xaee572fdd4790bcd4729bb3b612b52a573df46e9, p)\n",
+ "b = Mod(0xdab9e68366a593ca1df9cb2f20890a578729d6ef, p)\n",
+ "\n",
+ "n = 0x34a9fbe62b272f930b2e5027780a32300feb0dd8f\n",
+ "h = 1\n",
+ "gx = Mod(0xd4a3aaf43bdb25be7c308b69ae54f639e6e32e8c, p)\n",
+ "gy = Mod(0x7b6c82140bb427ac6e2a64507f60775949b2c8ce, p)\n",
+ "\n",
+ "g = Point(coords, X=gx, Y=gy, Z=Mod(1, p))\n",
+ "infty = Point(coords, X=Mod(0, p), Y=Mod(1, p), Z=Mod(0, p))\n",
+ "\n",
+ "curve = EllipticCurve(model, coords, p, infty, dict(a=a, b=b))\n",
+ "params = DomainParameters(curve, g, n, h)\n",
+ "print(miller_rabin(p))\n",
+ "print(curve.is_on_curve(g))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ffce5b71-3029-4219-a45a-1c8d78748fee",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "mult = LTRMultiplier(add, dbl, None, False, True, True)\n",
+ "mult.init(params, g)\n",
+ "non = 0\n",
+ "for _ in range(1000):\n",
+ " scalar = int(Mod.random(n*5))\n",
+ " \n",
+ " res = mult.multiply(scalar)\n",
+ " #print(res)\n",
+ " try:\n",
+ " res.Z.inverse()\n",
+ " print(\"yay\", scalar, res)\n",
+ " break\n",
+ " except NonInvertibleError as e:\n",
+ " non += 1\n",
+ "print(non)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "7611348f-ac20-402f-8791-6f4eb6d681af",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "b.inverse()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "4108fce0-28a9-4c28-a8d4-6051b86ce534",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "b"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ba9e5177-ca8f-488d-af4c-f3261e6563ef",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "dbl(p, g, a=a, b=b)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "9b42e209-5185-4121-b756-7f8c3374da5c",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "Mod(753921130225168876277183530034689476795287082245, p).inverse()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "dff4a049-2cad-40d8-9c49-98dcd9f93231",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "curve.affine_random()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "4ebc3535-737f-4ed6-8df5-d0629795ce8f",
+ "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.11.3"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}