Skip to content

Commit

Permalink
changed comments from FieldElement class
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmysong committed Nov 6, 2018
1 parent 4d96707 commit 0b6cad1
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 139 deletions.
23 changes: 9 additions & 14 deletions code-ch02/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,27 @@ def __add__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __sub__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __mul__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __pow__(self, exponent):
Expand All @@ -62,14 +59,13 @@ def __truediv__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add two numbers in different Fields')
# self.num and other.num are the actual values
num = (self.num * pow(other.num, self.prime - 2, self.prime)) % self.prime
# self.prime is what you'll need to mod against
# self.prime is what we need to mod against
# use fermat's little theorem:
# self.num**(p-1) % p == 1
# this means:
# 1/n == pow(n, p-2, p)
# You need to return an element of the same class
# use: self.__class__(num, prime)
num = (self.num * pow(other.num, self.prime - 2, self.prime)) % self.prime
# We return an element of the same class
return self.__class__(num, self.prime)


Expand Down Expand Up @@ -123,7 +119,6 @@ def test_div(self):


class Point:
zero = 0

def __init__(self, x, y, a, b):
self.a = a
Expand Down
22 changes: 9 additions & 13 deletions code-ch03/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,27 @@ def __add__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __sub__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __mul__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __pow__(self, exponent):
Expand All @@ -66,14 +63,13 @@ def __truediv__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add two numbers in different Fields')
# self.num and other.num are the actual values
num = (self.num * pow(other.num, self.prime - 2, self.prime)) % self.prime
# self.prime is what you'll need to mod against
# self.prime is what we need to mod against
# use fermat's little theorem:
# self.num**(p-1) % p == 1
# this means:
# 1/n == pow(n, p-2, p)
# You need to return an element of the same class
# use: self.__class__(num, prime)
num = (self.num * pow(other.num, self.prime - 2, self.prime)) % self.prime
# We return an element of the same class
return self.__class__(num, self.prime)

def __rmul__(self, coefficient):
Expand Down
23 changes: 9 additions & 14 deletions code-ch04/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,27 @@ def __add__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __sub__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __mul__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __pow__(self, exponent):
Expand All @@ -69,14 +66,13 @@ def __truediv__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add two numbers in different Fields')
# self.num and other.num are the actual values
num = (self.num * pow(other.num, self.prime - 2, self.prime)) % self.prime
# self.prime is what you'll need to mod against
# self.prime is what we need to mod against
# use fermat's little theorem:
# self.num**(p-1) % p == 1
# this means:
# 1/n == pow(n, p-2, p)
# You need to return an element of the same class
# use: self.__class__(num, prime)
num = (self.num * pow(other.num, self.prime - 2, self.prime)) % self.prime
# We return an element of the same class
return self.__class__(num, self.prime)

def __rmul__(self, coefficient):
Expand Down Expand Up @@ -139,7 +135,6 @@ def test_div(self):


class Point:
zero = 0

def __init__(self, x, y, a, b):
self.a = a
Expand Down
23 changes: 9 additions & 14 deletions code-ch05/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,27 @@ def __add__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __sub__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __mul__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __pow__(self, exponent):
Expand All @@ -69,14 +66,13 @@ def __truediv__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add two numbers in different Fields')
# self.num and other.num are the actual values
num = (self.num * pow(other.num, self.prime - 2, self.prime)) % self.prime
# self.prime is what you'll need to mod against
# self.prime is what we need to mod against
# use fermat's little theorem:
# self.num**(p-1) % p == 1
# this means:
# 1/n == pow(n, p-2, p)
# You need to return an element of the same class
# use: self.__class__(num, prime)
num = (self.num * pow(other.num, self.prime - 2, self.prime)) % self.prime
# We return an element of the same class
return self.__class__(num, self.prime)

def __rmul__(self, coefficient):
Expand Down Expand Up @@ -139,7 +135,6 @@ def test_div(self):


class Point:
zero = 0

def __init__(self, x, y, a, b):
self.a = a
Expand Down
23 changes: 9 additions & 14 deletions code-ch07/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,27 @@ def __add__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __sub__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __mul__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __pow__(self, exponent):
Expand All @@ -69,14 +66,13 @@ def __truediv__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add two numbers in different Fields')
# self.num and other.num are the actual values
num = (self.num * pow(other.num, self.prime - 2, self.prime)) % self.prime
# self.prime is what you'll need to mod against
# self.prime is what we need to mod against
# use fermat's little theorem:
# self.num**(p-1) % p == 1
# this means:
# 1/n == pow(n, p-2, p)
# You need to return an element of the same class
# use: self.__class__(num, prime)
num = (self.num * pow(other.num, self.prime - 2, self.prime)) % self.prime
# We return an element of the same class
return self.__class__(num, self.prime)

def __rmul__(self, coefficient):
Expand Down Expand Up @@ -139,7 +135,6 @@ def test_div(self):


class Point:
zero = 0

def __init__(self, x, y, a, b):
self.a = a
Expand Down
23 changes: 9 additions & 14 deletions code-ch08/ecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,27 @@ def __add__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __sub__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __mul__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add 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
# self.prime is what you'll need to mod against
# You need to return an element of the same class
# use: self.__class__(num, prime)
# We return an element of the same class
return self.__class__(num, self.prime)

def __pow__(self, exponent):
Expand All @@ -69,14 +66,13 @@ def __truediv__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add two numbers in different Fields')
# self.num and other.num are the actual values
num = (self.num * pow(other.num, self.prime - 2, self.prime)) % self.prime
# self.prime is what you'll need to mod against
# self.prime is what we need to mod against
# use fermat's little theorem:
# self.num**(p-1) % p == 1
# this means:
# 1/n == pow(n, p-2, p)
# You need to return an element of the same class
# use: self.__class__(num, prime)
num = (self.num * pow(other.num, self.prime - 2, self.prime)) % self.prime
# We return an element of the same class
return self.__class__(num, self.prime)

def __rmul__(self, coefficient):
Expand Down Expand Up @@ -139,7 +135,6 @@ def test_div(self):


class Point:
zero = 0

def __init__(self, x, y, a, b):
self.a = a
Expand Down
Loading

0 comments on commit 0b6cad1

Please sign in to comment.