Skip to content

Commit

Permalink
Remove calling normalize_FQ_point function for performance reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhargavasomu committed Feb 27, 2019
1 parent d31e990 commit cd2aa80
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 42 deletions.
51 changes: 30 additions & 21 deletions py_ecc/fields/field_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,6 @@
IntOrFQ = Union[int, "FQ"]


def normalize_FQ_point(value: IntOrFQ) -> int:
"""
Normalize FQ Point or any integer to integer
"""
if isinstance(value, int):
return value
elif isinstance(value, FQ):
return value.n
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(value))
)


class FQ(object):
"""
A class for field elements in FQ. Wrap a number in this class,
Expand Down Expand Up @@ -59,11 +44,19 @@ def __init__(self, val: IntOrFQ) -> None:
)

def __add__(self, other: IntOrFQ) -> "FQ":
on = normalize_FQ_point(other)
try:
on = other.n
except Exception:
on = other

return type(self)((self.n + on) % self.field_modulus)

def __mul__(self, other: IntOrFQ) -> "FQ":
on = normalize_FQ_point(other)
try:
on = other.n
except Exception:
on = other

return type(self)((self.n * on) % self.field_modulus)

def __rmul__(self, other: IntOrFQ) -> "FQ":
Expand All @@ -73,15 +66,27 @@ def __radd__(self, other: IntOrFQ) -> "FQ":
return self + other

def __rsub__(self, other: IntOrFQ) -> "FQ":
on = normalize_FQ_point(other)
try:
on = other.n
except Exception:
on = other

return type(self)((on - self.n) % self.field_modulus)

def __sub__(self, other: IntOrFQ) -> "FQ":
on = normalize_FQ_point(other)
try:
on = other.n
except Exception:
on = other

return type(self)((self.n - on) % self.field_modulus)

def __div__(self, other: IntOrFQ) -> "FQ":
on = normalize_FQ_point(other)
try:
on = other.n
except Exception:
on = other

return type(self)(
self.n * prime_field_inv(on, self.field_modulus) % self.field_modulus
)
Expand All @@ -90,7 +95,11 @@ def __truediv__(self, other: IntOrFQ) -> "FQ":
return self.__div__(other)

def __rdiv__(self, other: IntOrFQ) -> "FQ":
on = normalize_FQ_point(other)
try:
on = other.n
except Exception:
on = other

return type(self)(
prime_field_inv(self.n, self.field_modulus) * on % self.field_modulus
)
Expand Down
51 changes: 30 additions & 21 deletions py_ecc/fields/optimized_field_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,6 @@
IntOrFQ = Union[int, "FQ"]


def normalize_FQ_point(value: IntOrFQ) -> int:
"""
Normalize FQ Point or any integer to integer
"""
if isinstance(value, int):
return value
elif isinstance(value, FQ):
return value.n
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(value))
)


class FQ(object):
"""
A class for field elements in FQ. Wrap a number in this class,
Expand Down Expand Up @@ -59,11 +44,19 @@ def __init__(self, val: IntOrFQ) -> None:
)

def __add__(self, other: IntOrFQ) -> "FQ":
on = normalize_FQ_point(other)
try:
on = other.n
except Exception:
on = other

return type(self)((self.n + on) % self.field_modulus)

def __mul__(self, other: IntOrFQ) -> "FQ":
on = normalize_FQ_point(other)
try:
on = other.n
except Exception:
on = other

return type(self)((self.n * on) % self.field_modulus)

def __rmul__(self, other: IntOrFQ) -> "FQ":
Expand All @@ -73,18 +66,30 @@ def __radd__(self, other: IntOrFQ) -> "FQ":
return self + other

def __rsub__(self, other: IntOrFQ) -> "FQ":
on = normalize_FQ_point(other)
try:
on = other.n
except Exception:
on = other

return type(self)((on - self.n) % self.field_modulus)

def __sub__(self, other: IntOrFQ) -> "FQ":
on = normalize_FQ_point(other)
try:
on = other.n
except Exception:
on = other

return type(self)((self.n - on) % self.field_modulus)

def __mod__(self, other: IntOrFQ) -> "FQ":
raise NotImplementedError("Modulo Operation not yet supported by fields")

def __div__(self, other: IntOrFQ) -> "FQ":
on = normalize_FQ_point(other)
try:
on = other.n
except Exception:
on = other

return type(self)(
self.n * prime_field_inv(on, self.field_modulus) % self.field_modulus
)
Expand All @@ -93,7 +98,11 @@ def __truediv__(self, other: IntOrFQ) -> "FQ":
return self.__div__(other)

def __rdiv__(self, other: IntOrFQ) -> "FQ":
on = normalize_FQ_point(other)
try:
on = other.n
except Exception:
on = other

return type(self)(
prime_field_inv(self.n, self.field_modulus) * on % self.field_modulus
)
Expand Down

0 comments on commit cd2aa80

Please sign in to comment.