Skip to content

Commit

Permalink
redo exercises and code for ch01
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmysong committed Nov 28, 2018
1 parent 440ac0c commit e9093ce
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 4 deletions.
16 changes: 13 additions & 3 deletions appa.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ class FieldElement:
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
num = (self.num - other.num) % self.prime
# We return an element of the same class
return self.__class__(num, self.prime)
----

Expand Down Expand Up @@ -114,8 +117,11 @@ class FieldElement:
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
num = (self.num * other.num) % self.prime
# We return an element of the same class
return self.__class__(num, self.prime)
----

==== Exercise {counter:ch1exercise}
Expand Down Expand Up @@ -162,10 +168,14 @@ Write the corresponding $$__truediv__$$ method which defines the division of two
----
class FieldElement:
...
def __truediv__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot divide two numbers in different Fields')
# use fermat's little theorem:
# self.num**(p-1) % p == 1
# this means:
# 1/n == pow(n, p-2, p)
# We return an element of the same class
num = self.num * pow(other.num, self.prime-2, self.prime) % self.prime
return self.__class__(num, self.prime)
----
Expand Down
3 changes: 2 additions & 1 deletion ch01.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ We want to represent each finite field element and in Python, we'll be creating
The class represents an actual element in a field F~prime~. The bare bones of the class is pretty intuitive:

[source,python]
.ecc.py
----
class FieldElement:
Expand Down Expand Up @@ -114,6 +113,8 @@ True

Python allows us to override the $$==$$ operator with the $$__eq__$$ method, which is something we'll be taking advantage of going forward.

You can see this in action in the code that accompanies this book. Once you've set up Jupyter Notebook (see Preface), you can navigate to code-ch01/Chapter1.ipynb and run the code to see the results. For the next exercise, you'll want to open up ecc.py by clicking the link in the Exercise 1 box.

==== Exercise {counter:exercise}

Write the corresponding method $$__ne__$$ which checks if two `FieldElement` objects are _not equal_ to each other.
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions code-ch01/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ 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

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

def __pow__(self, exponent):
Expand All @@ -47,6 +53,11 @@ def __pow__(self, exponent):
def __truediv__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot divide two numbers in different Fields')
# use fermat's little theorem:
# self.num**(p-1) % p == 1
# this means:
# 1/n == pow(n, p-2, p)
# We return an element of the same class
raise NotImplementedError


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e9093ce

Please sign in to comment.