Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging My Exercises #284

Closed
wants to merge 18 commits into from
506 changes: 466 additions & 40 deletions code-ch01/Chapter1.ipynb

Large diffs are not rendered by default.

18 changes: 7 additions & 11 deletions code-ch01/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 not (self == other)

# tag::source2[]
def __add__(self, other):
Expand All @@ -36,18 +35,14 @@ def __add__(self, other):
def __sub__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot subtract two numbers in different Fields')
# 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
raise NotImplementedError
num = (self.num - other.num) % self.prime
return self.__class__(num, self.prime)

def __mul__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot multiply two numbers in different Fields')
# 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
raise NotImplementedError
num = (self.num * other.num) % self.prime
return self.__class__(num, self.prime)

# tag::source3[]
def __pow__(self, exponent):
Expand All @@ -63,8 +58,9 @@ def __truediv__(self, other):
# self.num**(p-1) % p == 1
# this means:
# 1/n == pow(n, p-2, p)
num = (self.num * pow(other.num,self.prime-2,self.prime)) % self.prime
# We return an element of the same class
raise NotImplementedError
return self.__class__(num, self.prime)


class FieldElementTest(TestCase):
Expand Down
185 changes: 161 additions & 24 deletions code-ch02/Chapter2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -19,9 +19,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 46,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "ValueError",
"evalue": "(-1, -2) is not on the curve",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[46], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mecc\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Point\n\u001b[1;32m 2\u001b[0m p1 \u001b[38;5;241m=\u001b[39m Point(\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m7\u001b[39m)\n\u001b[0;32m----> 3\u001b[0m p2 \u001b[38;5;241m=\u001b[39m \u001b[43mPoint\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m7\u001b[39;49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/Documents/Projects/programmingbitcoin/code-ch02/ecc.py:136\u001b[0m, in \u001b[0;36mPoint.__init__\u001b[0;34m(self, x, y, a, b)\u001b[0m\n\u001b[1;32m 133\u001b[0m \u001b[38;5;66;03m# end::source2[]\u001b[39;00m\n\u001b[1;32m 134\u001b[0m \u001b[38;5;66;03m# tag::source1[]\u001b[39;00m\n\u001b[1;32m 135\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39my\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m2\u001b[39m \u001b[38;5;241m!=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mx\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m3\u001b[39m \u001b[38;5;241m+\u001b[39m a \u001b[38;5;241m*\u001b[39m x \u001b[38;5;241m+\u001b[39m b: \u001b[38;5;66;03m# <1>\u001b[39;00m\n\u001b[0;32m--> 136\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m(\u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m, \u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m) is not on the curve\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mformat(x, y))\n",
"\u001b[0;31mValueError\u001b[0m: (-1, -2) is not on the curve"
]
}
],
"source": [
"from ecc import Point\n",
"p1 = Point(-1, -1, 5, 7)\n",
Expand All @@ -41,14 +54,32 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 47,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2, 4) is not on the curve\n",
"(-1,-1) is on the curve\n",
"(18,77) is on the curve\n",
"(5, 7) is not on the curve\n"
]
}
],
"source": [
"# Exercise 1\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",
"from ecc import Point\n",
"for p in [(2,4), (-1,-1), (18,77), (5,7)]:\n",
" try:\n",
" p = Point(p[0], p[1], 5, 7)\n",
" print(\"({0},{1}) is on the curve\".format(p.x,p.y))\n",
" except ValueError as e:\n",
" print(e)\n"
]
},
{
Expand All @@ -64,9 +95,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 48,
"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",
Expand All @@ -76,9 +119,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 49,
"metadata": {},
"outputs": [],
"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",
Expand All @@ -102,9 +155,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 50,
"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",
Expand All @@ -123,9 +188,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 51,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2,5) + (-1,-1) = (3.0,-7.0)\n"
]
}
],
"source": [
"# Exercise 4\n",
"\n",
Expand All @@ -136,7 +209,11 @@
"x1, y1 = 2, 5\n",
"x2, y2 = -1, -1\n",
"\n",
"# (x1,y1) + (x2,y2)"
"# (x1,y1) + (x2,y2)\n",
"p1 = Point(x1, y1, 5, 7)\n",
"p2 = Point(x2, y2, 5, 7)\n",
"p3 = p1+p2\n",
"print(\"(2,5) + (-1,-1) = ({0},{1})\".format(p3.x,p3.y))"
]
},
{
Expand All @@ -152,9 +229,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 52,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
".\n",
"----------------------------------------------------------------------\n",
"Ran 1 test in 0.003s\n",
"\n",
"OK\n"
]
}
],
"source": [
"# Exercise 5\n",
"\n",
Expand All @@ -173,9 +262,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 53,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" (-1,-1) + (-1,-1) = (18.0,77.0)\n"
]
}
],
"source": [
"# Exercise 6\n",
"\n",
Expand All @@ -184,7 +281,10 @@
"a = 5\n",
"b = 7\n",
"x1, y1 = -1, -1\n",
"# (-1,-1) + (-1,-1)"
"# (-1,-1) + (-1,-1)\n",
"p1 = Point(x1, y1, a, b)\n",
"p2 = p1+p1\n",
"print(\" (-1,-1) + (-1,-1) = ({0},{1})\".format(p2.x,p2.y))"
]
},
{
Expand All @@ -200,18 +300,55 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 44,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
".\n",
"----------------------------------------------------------------------\n",
"Ran 1 test in 0.003s\n",
"\n",
"OK\n"
]
}
],
"source": [
"# Exercise 7\n",
"\n",
"reload(ecc)\n",
"run(ecc.PointTest(\"test_add2\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.11.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
31 changes: 25 additions & 6 deletions code-ch02/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ def __eq__(self, other): # <2>

def __ne__(self, other):
# this should be the inverse of the == operator
raise NotImplementedError
return not self == other
#return self.x != other.x or self.y != other.y \
# or self.a != other.a or self.b != other.b

def __repr__(self):
if self.x is None:
Expand All @@ -164,21 +166,38 @@ def __add__(self, other): # <2>

# Case 1: self.x == other.x, self.y != other.y
# Result is point at infinity

if self.x == other.x and self.y != other.y:
return self.__class__(None,None, self.a, self.b)

# 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

if self.x != other.x:
x1 = self.x
x2 = other.x
y1 = self.y
y2 = other.y
s = (y2-y1)/(x2-x1)
x3 = s**2-x1-x2
y3 = s*(x1-x3)-y1
return self.__class__(x3,y3, self.a, self.b)

# 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 == other:
x1 = self.x
x2 = other.x
y1 = self.y
y2 = other.y
s=(3*x1**2+self.a)/(2*y1)
x3=s**2-2*x1
y3=s*(x1-x3)-y1
return self.__class__(x3,y3, self.a, self.b)

class PointTest(TestCase):

Expand Down
Loading