From b638de4c24bd2b57e2357ac19c1cf5808f40f357 Mon Sep 17 00:00:00 2001 From: VASH Date: Sat, 12 Aug 2023 12:44:40 +0200 Subject: [PATCH 1/4] hasta chap 1 exercise 3 --- .vscode/settings.json | 5 ++ code-ch01/Chapter1.ipynb | 128 ++++++++++++++++++++++++++++++++------- code-ch01/ecc.py | 5 +- 3 files changed, 115 insertions(+), 23 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..1d7e370c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "[python]": { + "editor.defaultFormatter": "ms-python.autopep8" + }, +} \ No newline at end of file diff --git a/code-ch01/Chapter1.ipynb b/code-ch01/Chapter1.ipynb index f8055f46..e08968e2 100644 --- a/code-ch01/Chapter1.ipynb +++ b/code-ch01/Chapter1.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -19,9 +19,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], "source": [ "from ecc import FieldElement\n", "a = FieldElement(7, 13)\n", @@ -43,9 +52,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + ".\n", + "----------------------------------------------------------------------\n", + "Ran 1 test in 0.001s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# Exercise 1\n", "\n", @@ -55,18 +76,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "print(7 % 3)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "12\n" + ] + } + ], "source": [ "print(-27 % 13)" ] @@ -87,25 +124,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "20\n", + "37\n", + "51\n", + "41\n" + ] + } + ], "source": [ "# Exercise 2\n", "\n", "# remember that % is the modulo operator\n", "prime = 57\n", - "# 44+33\n", - "# 9-29\n", - "# 17+42+49\n", - "# 52-30-38" + "print((44+33) % prime)\n", + "print( (9-29) % prime)\n", + "print((17+42+49) % prime)\n", + "print((52-30-38) % prime)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "from ecc import FieldElement\n", "a = FieldElement(7, 13)\n", @@ -127,9 +183,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + ".\n", + "----------------------------------------------------------------------\n", + "Ran 1 test in 0.001s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# Exercise 3\n", "\n", @@ -328,7 +396,25 @@ ] } ], - "metadata": {}, + "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.9.5" + } + }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/code-ch01/ecc.py b/code-ch01/ecc.py index b5bf616e..dfa141c5 100644 --- a/code-ch01/ecc.py +++ b/code-ch01/ecc.py @@ -22,8 +22,7 @@ def __eq__(self, other): # end::source1[] def __ne__(self, other): - # this should be the inverse of the == operator - raise NotImplementedError + return self.num != other.num # tag::source2[] def __add__(self, other): @@ -36,6 +35,8 @@ def __add__(self, other): def __sub__(self, other): if self.prime != other.prime: raise TypeError('Cannot subtract two numbers in different Fields') + num = (self.num - other.num) % self.prime + return self.__class__(num, self.prime) # self.num and other.num are the actual values # self.prime is what we need to mod against # We return an element of the same class From f3c6479dcb90fabcddad8d45c8298699daff9fa5 Mon Sep 17 00:00:00 2001 From: VASH Date: Sat, 26 Aug 2023 16:26:35 +0200 Subject: [PATCH 2/4] chapter 1 done repasar exponentiation & division --- code-ch01/Chapter1.ipynb | 181 ++++++++++++++++++++++++++++++++------- code-ch01/ecc.py | 13 +++ 2 files changed, 163 insertions(+), 31 deletions(-) diff --git a/code-ch01/Chapter1.ipynb b/code-ch01/Chapter1.ipynb index e08968e2..19992ca0 100644 --- a/code-ch01/Chapter1.ipynb +++ b/code-ch01/Chapter1.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -19,7 +19,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -52,7 +52,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -124,7 +124,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -151,7 +151,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -183,7 +183,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -220,17 +220,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n", + "68\n", + "63\n" + ] + } + ], "source": [ "# Exercise 4\n", "\n", "prime = 97\n", "\n", "# 95*45*31\n", + "print(95*45*31 % prime)\n", "# 17*13*19*44\n", - "# 12**7*77**49" + "print(17*13*19*44 % prime)\n", + "# 12**7*77**49\n", + "print(12**7*77**49 % prime)\n" ] }, { @@ -248,25 +261,63 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "For k = 0: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n", + "For k = 1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]\n", + "For k = 2: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 1, 3, 5, 7, 9, 11, 13, 15, 17]\n", + "For k = 3: [0, 3, 6, 9, 12, 15, 18, 2, 5, 8, 11, 14, 17, 1, 4, 7, 10, 13, 16]\n", + "For k = 4: [0, 4, 8, 12, 16, 1, 5, 9, 13, 17, 2, 6, 10, 14, 18, 3, 7, 11, 15]\n", + "For k = 5: [0, 5, 10, 15, 1, 6, 11, 16, 2, 7, 12, 17, 3, 8, 13, 18, 4, 9, 14]\n", + "For k = 6: [0, 6, 12, 18, 5, 11, 17, 4, 10, 16, 3, 9, 15, 2, 8, 14, 1, 7, 13]\n", + "For k = 7: [0, 7, 14, 2, 9, 16, 4, 11, 18, 6, 13, 1, 8, 15, 3, 10, 17, 5, 12]\n", + "For k = 8: [0, 8, 16, 5, 13, 2, 10, 18, 7, 15, 4, 12, 1, 9, 17, 6, 14, 3, 11]\n", + "For k = 9: [0, 9, 18, 8, 17, 7, 16, 6, 15, 5, 14, 4, 13, 3, 12, 2, 11, 1, 10]\n", + "For k = 10: [0, 10, 1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9]\n", + "For k = 11: [0, 11, 3, 14, 6, 17, 9, 1, 12, 4, 15, 7, 18, 10, 2, 13, 5, 16, 8]\n", + "For k = 12: [0, 12, 5, 17, 10, 3, 15, 8, 1, 13, 6, 18, 11, 4, 16, 9, 2, 14, 7]\n", + "For k = 13: [0, 13, 7, 1, 14, 8, 2, 15, 9, 3, 16, 10, 4, 17, 11, 5, 18, 12, 6]\n", + "For k = 14: [0, 14, 9, 4, 18, 13, 8, 3, 17, 12, 7, 2, 16, 11, 6, 1, 15, 10, 5]\n", + "For k = 15: [0, 15, 11, 7, 3, 18, 14, 10, 6, 2, 17, 13, 9, 5, 1, 16, 12, 8, 4]\n", + "For k = 16: [0, 16, 13, 10, 7, 4, 1, 17, 14, 11, 8, 5, 2, 18, 15, 12, 9, 6, 3]\n", + "For k = 17: [0, 17, 15, 13, 11, 9, 7, 5, 3, 1, 18, 16, 14, 12, 10, 8, 6, 4, 2]\n", + "For k = 18: [0, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n" + ] + } + ], "source": [ "# Exercise 5\n", "\n", "prime = 19\n", - "k = 1 # 3, 7, 13 and 18 are the other possibilities\n", + "# k = 1 # 3, 7, 13 and 18 are the other possibilities\n", "# loop through all possible k's 0 up to prime-1\n", "# calculate k*iterator % prime\n", "\n", - "# Hint - sort!" + "# Hint - sort!\n", + "\n", + "for k in range(prime):\n", + " values = [k * iterator % prime for iterator in range(prime)]\n", + " print(f\"For k = {k}: {values}\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "from ecc import FieldElement\n", "a = FieldElement(3, 13)\n", @@ -288,9 +339,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + ".\n", + "----------------------------------------------------------------------\n", + "Ran 1 test in 0.001s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# Exercise 6\n", "\n", @@ -300,9 +363,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "from ecc import FieldElement\n", "a = FieldElement(3, 13)\n", @@ -323,13 +394,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 1, 1, 1, 1]\n", + "[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n", + "[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n", + "[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n" + ] + } + ], "source": [ "# Exercise 7\n", "\n", - "primes = [7, 11, 17, 31, 43]" + "for prime in (7, 11, 17, 31):\n", + " print([pow(i, prime-1, prime) for i in range(1, prime)])" ] }, { @@ -347,15 +430,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "29\n", + "13\n" + ] + } + ], "source": [ "# Exercise 8\n", - "\n", + "prime=31\n", "# 3/24\n", - "# 17**-3\n", - "# 4**-4*11" + "print(3*pow(24, prime-2, prime) % prime)\n", + "# 4\n", + "# 17^-3\n", + "print(pow(17, prime-4, prime))\n", + "# 29\n", + "# 4^-4 * 11\n", + "print(pow(4, prime-5, prime)*11 % prime)\n", + "# 13" ] }, { @@ -373,9 +472,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + ".\n", + "----------------------------------------------------------------------\n", + "Ran 1 test in 0.001s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# Exercise 9\n", "\n", @@ -385,9 +496,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "from ecc import FieldElement\n", "a = FieldElement(7, 13)\n", diff --git a/code-ch01/ecc.py b/code-ch01/ecc.py index dfa141c5..12d227ae 100644 --- a/code-ch01/ecc.py +++ b/code-ch01/ecc.py @@ -45,6 +45,8 @@ def __sub__(self, other): def __mul__(self, other): if self.prime != other.prime: raise TypeError('Cannot multiply two numbers in different Fields') + num = (self.num * other.num) % self.prime + return self.__class__(num, self.prime) # self.num and other.num are the actual values # self.prime is what we need to mod against # We return an element of the same class @@ -65,6 +67,17 @@ def __truediv__(self, other): # this means: # 1/n == pow(n, p-2, p) # We return an element of the same class + + # Fet per ChatGPT + # Calculate the modular inverse of other using Fermat's Little Theorem + # If other is represented by 'b', the modular inverse is 'b^(p-2) % p' + inverse = pow(other.num, self.prime - 2, self.prime) + # Multiply self by the modular inverse of other + # This is equivalent to dividing by other in the finite field + result_num = (self.num * inverse) % self.prime + + # Create and return a new instance of the same class with the result + return self.__class__(result_num, self.prime) raise NotImplementedError From 2756e09ea7d331d6c66bf440c946062bd0d2911d Mon Sep 17 00:00:00 2001 From: VASH Date: Wed, 6 Sep 2023 21:04:18 +0200 Subject: [PATCH 3/4] chapter 2 finished learn more about derivates --- code-ch02/Chapter2.ipynb | 198 ++++++++++++++++++++++++++++++++++----- code-ch02/ecc.py | 46 ++++----- 2 files changed, 196 insertions(+), 48 deletions(-) diff --git a/code-ch02/Chapter2.ipynb b/code-ch02/Chapter2.ipynb index f658631b..50f6f451 100644 --- a/code-ch02/Chapter2.ipynb +++ b/code-ch02/Chapter2.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ @@ -19,13 +19,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "from ecc import Point\n", "p1 = Point(-1, -1, 5, 7)\n", - "p2 = Point(-1, -2, 5, 7)" + "p2 = Point(-1, -1, 5, 7)" ] }, { @@ -41,14 +41,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 4\n", + "16 25\n", + "False\n", + "-1 -1\n", + "1 1\n", + "True\n", + "18 77\n", + "5929 5929\n", + "True\n", + "5 7\n", + "49 157\n", + "False\n" + ] + } + ], "source": [ "# Exercise 1\n", + "def isOnCurve(x,y):\n", + " print(x,y)\n", + " left = pow(y,2) \n", + " right = pow(x,3) + x *5 + 7\n", + " print(left,right)\n", + " return left == right\n", "\n", "# (2,4), (-1,-1), (18,77), (5,7)\n", - "# equation in python is: y**2 == x**3 + 5*x + 7" + "# equation in python is: y**2 == x**3 + 5*x + 7\n", + "print(isOnCurve(2,4))\n", + "print(isOnCurve(-1,-1))\n", + "print(isOnCurve(18,77))\n", + "print(isOnCurve(5,7))" ] }, { @@ -64,9 +93,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + ".\n", + "----------------------------------------------------------------------\n", + "Ran 1 test in 0.001s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# Exercise 2\n", "\n", @@ -79,6 +120,23 @@ "execution_count": null, "metadata": {}, "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Point(-1,-1)_5_7\n", + "Point(-1,1)_5_7\n", + "Point(infinity)\n" + ] + } + ], "source": [ "from ecc import Point\n", "p1 = Point(-1, -1, 5, 7)\n", @@ -102,9 +160,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + ".\n", + "----------------------------------------------------------------------\n", + "Ran 1 test in 0.001s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# Exercise 3\n", "\n", @@ -123,9 +193,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.0 -7.0\n" + ] + } + ], "source": [ "# Exercise 4\n", "\n", @@ -133,12 +211,30 @@ "\n", "a = 5\n", "b = 7\n", + "# x1, y1 = 2, 5\n", + "# x2, y2 = -1, -1\n", + "# point1 = Point(x1,y1,a,b)\n", + "# point2 = Point(x2,y2,a,b)\n", + "\n", + "# (x1,y1) + (x2,y2)\n", + "# print(point1 + point2)\n", + "\n", + "\n", "x1, y1 = 2, 5\n", "x2, y2 = -1, -1\n", - "\n", - "# (x1,y1) + (x2,y2)" + "s = (y2 - y1) / (x2 - x1)\n", + "x3 = s**2 - x1 - x2\n", + "y3 = s * (x1 - x3) - y1\n", + "print(x3, y3)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -152,9 +248,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + ".\n", + "----------------------------------------------------------------------\n", + "Ran 1 test in 0.001s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# Exercise 5\n", "\n", @@ -173,18 +281,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "18.0 77.0\n", + "Point(18.0,77.0)_5_7\n" + ] + } + ], "source": [ "# Exercise 6\n", "\n", "from ecc import Point\n", "\n", + "# (-1,-1) + (-1,-1)\n", "a = 5\n", "b = 7\n", "x1, y1 = -1, -1\n", - "# (-1,-1) + (-1,-1)" + "s = (3 * x1**2 + a) / (2 * y1)\n", + "x3 = s**2 - 2*x1\n", + "y3 = s*(x1-x3)-y1\n", + "print(x3,y3)\n", + "p1 = Point(-1,-1,5,7)\n", + "p2 = Point(-1,-1,5,7)\n", + "print(p1 + p2)" ] }, { @@ -200,9 +324,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + ".\n", + "----------------------------------------------------------------------\n", + "Ran 1 test in 0.001s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# Exercise 7\n", "\n", @@ -211,7 +347,25 @@ ] } ], - "metadata": {}, + "metadata": { + "kernelspec": { + "display_name": ".venv", + "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.9.5" + } + }, "nbformat": 4, "nbformat_minor": 2 } diff --git a/code-ch02/ecc.py b/code-ch02/ecc.py index eb50c4af..b21353bd 100644 --- a/code-ch02/ecc.py +++ b/code-ch02/ecc.py @@ -142,7 +142,7 @@ def __eq__(self, other): # <2> def __ne__(self, other): # this should be the inverse of the == operator - raise NotImplementedError + return self.x != other.x or self.y != other.y def __repr__(self): if self.x is None: @@ -150,34 +150,28 @@ def __repr__(self): else: return 'Point({},{})_{}_{}'.format(self.x, self.y, self.a, self.b) - # tag::source3[] - def __add__(self, other): # <2> + # tag::source3[] + def __add__(self, other): if self.a != other.a or self.b != other.b: - raise TypeError('Points {}, {} are not on the same curve'.format - (self, other)) - - if self.x is None: # <3> + raise TypeError + if self.x is None: return other - if other.x is None: # <4> + if other.x is None: return self - # end::source3[] - - # Case 1: self.x == other.x, self.y != other.y - # Result is point at infinity - - # Case 2: self.x ≠ other.x - # Formula (x3,y3)==(x1,y1)+(x2,y2) - # s=(y2-y1)/(x2-x1) - # x3=s**2-x1-x2 - # y3=s*(x1-x3)-y1 - - # Case 3: self == other - # Formula (x3,y3)=(x1,y1)+(x1,y1) - # s=(3*x1**2+a)/(2*y1) - # x3=s**2-2*x1 - # y3=s*(x1-x3)-y1 - - raise NotImplementedError + if self.x == other.x and self.y != other.y: + return self.__class__(None, None, self.a, self.b) # No deuría ser punt a infinito desde x? + if self.x != other.x: + s = (other.y - self.y) / (other.x - self.x) + x = s**2 - self.x - other.x + y = s * (self.x - x) - self.y + return self.__class__(x, y, self.a, self.b) + if self == other and self.y == 0 * self.x: + return self.__class(None, None, self.a, self.b) + if self == other: + s = (3 * self.x**2 + self.a) / (2 * self.y) + x = s**2 - 2 * self.x + y = s * (self.x - x) - self.y + return self.__class__(x, y, self.a, self.b) class PointTest(TestCase): From d3c82adc0155b7be5de85d47e49f6e226289aa7f Mon Sep 17 00:00:00 2001 From: VASH Date: Wed, 13 Sep 2023 19:50:05 +0200 Subject: [PATCH 4/4] exercise 3 chap 3 --- code-ch03/Chapter3.ipynb | 193 +++++++++++++++++++++++++++++++++++---- code-ch03/ecc.py | 15 ++- 2 files changed, 187 insertions(+), 21 deletions(-) diff --git a/code-ch03/Chapter3.ipynb b/code-ch03/Chapter3.ipynb index bf0ffe20..28eb6c88 100644 --- a/code-ch03/Chapter3.ipynb +++ b/code-ch03/Chapter3.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -30,24 +30,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "Point(17,56)_0_7 FieldElement(223)\n" + ] + } + ], "source": [ "# Exercise 1\n", - "\n", + "def on_curve(x,y):\n", + " return y**2 == x**3 + a*x + b\n", "prime = 223\n", "a = FieldElement(0, prime)\n", "b = FieldElement(7, prime)\n", + "# (192,105), (17,56), (200,119), (1,193), (42,99)\n", + "x1 = 192 \n", + "y1 = 105\n", + "\n", + "r1 = (FieldElement(192,prime))\n", + "r2 = (FieldElement(105,prime))\n", "\n", - "# (192,105), (17,56), (200,119), (1,193), (42,99)" + "print(on_curve(r1,r2))\n", + "print(Point(FieldElement(17,prime), FieldElement(56,prime),a,b))\n", + "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Point(192,105)_0_7 FieldElement(223)\n" + ] + } + ], "source": [ "from ecc import FieldElement, Point\n", "a = FieldElement(num=0, prime=223)\n", @@ -62,7 +88,15 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Point(170,142)_0_7 FieldElement(223)\n" + ] + } + ], "source": [ "from ecc import FieldElement, Point\n", "prime = 223\n", @@ -92,9 +126,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Point(220,181)_0_7 FieldElement(223)\n" + ] + } + ], "source": [ "# Exercise 2\n", "\n", @@ -104,7 +146,10 @@ "\n", "# (170,142) + (60,139)\n", "# (47,71) + (17,56)\n", - "# (143,98) + (76,66)" + "# (143,98) + (76,66)\n", + "\n", + "res1 = Point(FieldElement(170,prime),FieldElement(142,prime),a,b) + Point(FieldElement(60,prime), FieldElement(139,prime),a,b)\n", + "print(res1)" ] }, { @@ -120,9 +165,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + ".\n", + "----------------------------------------------------------------------\n", + "Ran 1 test in 0.001s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# Exercise 3\n", "\n", @@ -174,7 +231,34 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1*(47,71)=(47,71)\n", + "2*(47,71)=(36,111)\n", + "3*(47,71)=(15,137)\n", + "4*(47,71)=(194,51)\n", + "5*(47,71)=(126,96)\n", + "6*(47,71)=(139,137)\n", + "7*(47,71)=(92,47)\n", + "8*(47,71)=(116,55)\n", + "9*(47,71)=(69,86)\n", + "10*(47,71)=(154,150)\n", + "11*(47,71)=(154,73)\n", + "12*(47,71)=(69,137)\n", + "13*(47,71)=(116,168)\n", + "14*(47,71)=(92,176)\n", + "15*(47,71)=(139,86)\n", + "16*(47,71)=(126,127)\n", + "17*(47,71)=(194,172)\n", + "18*(47,71)=(15,86)\n", + "19*(47,71)=(36,112)\n", + "20*(47,71)=(47,152)\n" + ] + } + ], "source": [ "from ecc import FieldElement, Point\n", "prime = 223\n", @@ -224,7 +308,15 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Point(infinity)\n" + ] + } + ], "source": [ "from ecc import FieldElement, Point\n", "prime = 223\n", @@ -240,7 +332,15 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "gx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\n", "gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8\n", @@ -252,7 +352,15 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Point(infinity)\n" + ] + } + ], "source": [ "from ecc import FieldElement, Point\n", "gx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798\n", @@ -271,7 +379,15 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "S256Point(infinity)\n" + ] + } + ], "source": [ "from ecc import G, N\n", "print(N*G)" @@ -281,7 +397,15 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], "source": [ "from ecc import S256Point, G, N\n", "z = 0xbc62d4b80d9e36da29c16c5d4d9f11731f36052c72401a76c23c0fb5a9b74423\n", @@ -345,7 +469,18 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "S256Point(028d003eab2e428d11983f3e97c3fa0addf3b42740df0d211795ffb3be2f6c52, 0ae987b9ec6ea159c78cb2a937ed89096fb218d9e7594f02b547526d8cd309e2)\n", + "0x231c6f3d980a6b0fb7152f85cee7eb52bf92433d9919b9c5218cb08e79cce78\n", + "0x2b698a0f0a4041b77e63488ad48c23e8e8838dd1fb7520408b121697b782ef22\n", + "0xbb14e602ef9e3f872e25fad328466b34e6734b7a0fcd58b1eb635447ffae8cb9\n" + ] + } + ], "source": [ "from ecc import S256Point, G, N\n", "from helper import hash256\n", @@ -396,7 +531,25 @@ ] } ], - "metadata": {}, + "metadata": { + "kernelspec": { + "display_name": ".venv", + "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.9.5" + } + }, "nbformat": 4, "nbformat_minor": 2 } diff --git a/code-ch03/ecc.py b/code-ch03/ecc.py index 84c72988..283d20e0 100644 --- a/code-ch03/ecc.py +++ b/code-ch03/ecc.py @@ -289,11 +289,24 @@ def test_add(self): (143, 98, 76, 66, 47, 71), ) + for values in additions: + x1, y1, x2, y2, x3, y3 = values + x1 = FieldElement(x1, prime) + y1 = FieldElement(y1, prime) + x2 = FieldElement(x2, prime) + y2 = FieldElement(y2, prime) + x3 = FieldElement(x3, prime) + y3 = FieldElement(y3, prime) + p1 = Point(x1, y1, a, b) + p2 = Point(x2, y2, a, b) + p3 = Point(x3, y3, a, b) + + self.assertEqual(p1 + p2, p3, "Points are not equal") + # loop over additions # initialize x's and y's as FieldElements # create p1, p2 and p3 as Points # check p1+p2==p3 - raise NotImplementedError def test_rmul(self): # tests the following scalar multiplications