diff options
| author | J08nY | 2023-08-03 14:24:04 +0200 |
|---|---|---|
| committer | J08nY | 2023-08-03 14:24:04 +0200 |
| commit | 981d34d23cfd41438405b3fce4988ae4870ac9ea (patch) | |
| tree | 4a4ecccea28f809ee1d14bdb3eb3124c1741b5fd | |
| parent | 803db884d51b7a88f83ff8149806745e5e209976 (diff) | |
| download | pyecsca-notebook-981d34d23cfd41438405b3fce4988ae4870ac9ea.tar.gz pyecsca-notebook-981d34d23cfd41438405b3fce4988ae4870ac9ea.tar.zst pyecsca-notebook-981d34d23cfd41438405b3fce4988ae4870ac9ea.zip | |
Add ZVP notebook.
| -rw-r--r-- | re/zvp.ipynb | 243 |
1 files changed, 243 insertions, 0 deletions
diff --git a/re/zvp.ipynb b/re/zvp.ipynb new file mode 100644 index 0000000..1df4dbb --- /dev/null +++ b/re/zvp.ipynb @@ -0,0 +1,243 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "3ff024c8-7e90-4094-ab07-a051a601d9cd", + "metadata": {}, + "source": [ + "# ZVP-based reverse-engineering" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "17a9e580-5f1e-45c9-8afd-fc35e833b0b0", + "metadata": {}, + "outputs": [], + "source": [ + "import sympy\n", + "from sympy import FF, sympify, symbols\n", + "\n", + "from pyecsca.ec.model import ShortWeierstrassModel\n", + "from pyecsca.ec.coordinates import AffineCoordinateModel\n", + "from pyecsca.ec.curve import EllipticCurve\n", + "from pyecsca.ec.params import DomainParameters\n", + "from pyecsca.ec.point import Point\n", + "from pyecsca.ec.mod import Mod, SymbolicMod\n", + "from pyecsca.ec.context import DefaultContext, local\n", + "from pyecsca.misc.cfg import getconfig" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d8ea0c4d-86e1-46af-ac20-569e6ef5439d", + "metadata": {}, + "outputs": [], + "source": [ + "cfg = getconfig()\n", + "cfg.ec.mod_implementation = \"python\"" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "8de124f1-9498-4e55-8b7f-1bd291ccf3fe", + "metadata": {}, + "outputs": [], + "source": [ + "model = ShortWeierstrassModel()\n", + "coordsaff = AffineCoordinateModel(model)\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", + "# A 64-bit prime order curve for testing things out\n", + "p = 0xc50de883f0e7b167\n", + "a = Mod(0x4833d7aa73fa6694, p)\n", + "b = Mod(0xa6c44a61c5323f6a, p)\n", + "gx = Mod(0x5fd1f7d38d4f2333, p)\n", + "gy = Mod(0x21f43957d7e20ceb, p)\n", + "n = 0xc50de885003b80eb\n", + "h = 1\n", + "\n", + "field = FF(p)\n", + "\n", + "infty = Point(coords, X=Mod(0, p), Y=Mod(1, p), Z=Mod(0, p))\n", + "g = Point(coords, X=gx, Y=gy, Z=Mod(1, p))\n", + "\n", + "curve = EllipticCurve(model, coords, p, infty, dict(a=a,b=b))\n", + "params = DomainParameters(curve, g, n, h)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "e91d81b6-a3ce-4b1f-8ace-3e55e0d6fadf", + "metadata": {}, + "outputs": [], + "source": [ + "# add\n", + "# TODO: this is wrooooong, needs to have X1 and X2...\n", + "P1 = Point(coords, **{str(var): SymbolicMod(symbols(var + \"1\"), p) for var in coords.variables})\n", + "P2 = Point(coords, **{str(var): SymbolicMod(symbols(var + \"2\"), p) for var in coords.variables})\n", + "parameters = {str(var): SymbolicMod(var, p) for var in symbols(model.parameter_names)}\n", + "with local(DefaultContext()) as ctx:\n", + " res = add(p, P1, P2, **parameters)[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "7f4c7416-49a9-4fe5-a1ff-38f2cb6c5815", + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\displaystyle 2 Z_{1} Z_{2} \\left(Y_{1} Z_{2} + Y_{2} Z_{1}\\right) \\left(Z_{1}^{2} Z_{2}^{2} \\left(Y_{1} Z_{2} + Y_{2} Z_{1}\\right)^{4} + \\left(X_{1} Z_{2} + X_{2} Z_{1}\\right)^{2} - \\left(X_{1} Z_{2} + X_{2} Z_{1} + Z_{1} Z_{2} \\left(Y_{1} Z_{2} + Y_{2} Z_{1}\\right)^{2}\\right)^{2} + 2 \\left(- X_{1} X_{2} Z_{1} Z_{2} + Z_{1}^{2} Z_{2}^{2} a + \\left(X_{1} Z_{2} + X_{2} Z_{1}\\right)^{2}\\right)^{2}\\right)$" + ], + "text/plain": [ + "2*Z1*Z2*(Y1*Z2 + Y2*Z1)*(Z1**2*Z2**2*(Y1*Z2 + Y2*Z1)**4 + (X1*Z2 + X2*Z1)**2 - (X1*Z2 + X2*Z1 + Z1*Z2*(Y1*Z2 + Y2*Z1)**2)**2 + 2*(-X1*X2*Z1*Z2 + Z1**2*Z2**2*a + (X1*Z2 + X2*Z1)**2)**2)" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "res.X.x" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "89a34db8-81d2-4741-ae0a-5268b46d0974", + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\displaystyle - 2 Z_{1}^{2} Z_{2}^{2} \\left(Y_{1} Z_{2} + Y_{2} Z_{1}\\right)^{4} + \\left(- X_{1} X_{2} Z_{1} Z_{2} + Z_{1}^{2} Z_{2}^{2} a + \\left(X_{1} Z_{2} + X_{2} Z_{1}\\right)^{2}\\right) \\left(- 3 Z_{1}^{2} Z_{2}^{2} \\left(Y_{1} Z_{2} + Y_{2} Z_{1}\\right)^{4} - 3 \\left(X_{1} Z_{2} + X_{2} Z_{1}\\right)^{2} + 3 \\left(X_{1} Z_{2} + X_{2} Z_{1} + Z_{1} Z_{2} \\left(Y_{1} Z_{2} + Y_{2} Z_{1}\\right)^{2}\\right)^{2} - 4 \\left(- X_{1} X_{2} Z_{1} Z_{2} + Z_{1}^{2} Z_{2}^{2} a + \\left(X_{1} Z_{2} + X_{2} Z_{1}\\right)^{2}\\right)^{2}\\right)$" + ], + "text/plain": [ + "-2*Z1**2*Z2**2*(Y1*Z2 + Y2*Z1)**4 + (-X1*X2*Z1*Z2 + Z1**2*Z2**2*a + (X1*Z2 + X2*Z1)**2)*(-3*Z1**2*Z2**2*(Y1*Z2 + Y2*Z1)**4 - 3*(X1*Z2 + X2*Z1)**2 + 3*(X1*Z2 + X2*Z1 + Z1*Z2*(Y1*Z2 + Y2*Z1)**2)**2 - 4*(-X1*X2*Z1*Z2 + Z1**2*Z2**2*a + (X1*Z2 + X2*Z1)**2)**2)" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "res.Y.x" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "8d3db4ac-95fb-4483-a572-265744461daf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\displaystyle 4 Z_{1}^{3} Z_{2}^{3} \\left(Y_{1} Z_{2} + Y_{2} Z_{1}\\right)^{3}$" + ], + "text/plain": [ + "4*Z1**3*Z2**3*(Y1*Z2 + Y2*Z1)**3" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "res.Z.x" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "645ca07a-8d47-446d-93ca-eab4de26dfff", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "U1 = X1*Z2 \t X1*Z2\n", + "U2 = X2*Z1 \t X2*Z1\n", + "S1 = Y1*Z2 \t Y1*Z2\n", + "S2 = Y2*Z1 \t Y2*Z1\n", + "ZZ = Z1*Z2 \t Z1*Z2\n", + "T = U1+U2 \t X1*Z2 + X2*Z1\n", + "TT = T \t (X1*Z2 + X2*Z1)**2\n", + "M = S1+S2 \t Y1*Z2 + Y2*Z1\n", + "t0 = ZZ \t Z1**2*Z2**2\n", + "t1 = a*t0 \t Z1**2*Z2**2*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a + (X1*Z2 + X2*Z1)**2)*(-3*Z1**2*Z2**2*(Y1*Z2 + Y2*Z1)**4 - 3*(X1*Z2 + X2*Z1)**2 + 3*(X1*Z2 + X2*Z1 + Z1*Z2*(Y1*Z2 + Y2*Z1)**2)**2 - 4*(-X1*X2*Z1*Z2 + Z1**2*Z2**2*a + (X1*Z2 + X2*Z1)**2)**2)\n", + "t14 = F \t Z1**2*Z2**2*(Y1*Z2 + Y2*Z1)**2\n", + "t15 = F*t14 \t Z1**3*Z2**3*(Y1*Z2 + Y2*Z1)**3\n", + "Z3 = 4*t15 \t 4*Z1**3*Z2**3*(Y1*Z2 + Y2*Z1)**3\n" + ] + } + ], + "source": [ + "for op_result in ctx.actions.get_by_index([0])[0].op_results:\n", + " print(repr(op_result), \"\\t\", op_result.value.x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "08e17876-331a-445a-8fbc-1990a0b5f69d", + "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 +} |
