Skip to content

Commit

Permalink
Add normalize_FQ_point function to FQ
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhargavasomu committed Jan 25, 2019
1 parent 9c80ebd commit ae2e221
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 120 deletions.
81 changes: 21 additions & 60 deletions py_ecc/fields/field_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@
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 @@ -49,29 +64,11 @@ def __init__(self, val: IntOrFQ) -> None:
)

def __add__(self, other: IntOrFQ) -> "FQ":
if isinstance(other, FQ):
on = other.n
elif isinstance(other, int):
on = other
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(other))
)

on = normalize_FQ_point(other)
return type(self)((self.n + on) % self.field_modulus)

def __mul__(self, other: IntOrFQ) -> "FQ":
if isinstance(other, FQ):
on = other.n
elif isinstance(other, int):
on = other
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(other))
)

on = normalize_FQ_point(other)
return type(self)((self.n * on) % self.field_modulus)

def __rmul__(self, other: IntOrFQ) -> "FQ":
Expand All @@ -81,42 +78,15 @@ def __radd__(self, other: IntOrFQ) -> "FQ":
return self + other

def __rsub__(self, other: IntOrFQ) -> "FQ":
if isinstance(other, FQ):
on = other.n
elif isinstance(other, int):
on = other
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(other))
)

on = normalize_FQ_point(other)
return type(self)((on - self.n) % self.field_modulus)

def __sub__(self, other: IntOrFQ) -> "FQ":
if isinstance(other, FQ):
on = other.n
elif isinstance(other, int):
on = other
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(other))
)

on = normalize_FQ_point(other)
return type(self)((self.n - on) % self.field_modulus)

def __div__(self, other: IntOrFQ) -> "FQ":
if isinstance(other, FQ):
on = other.n
elif isinstance(other, int):
on = other
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(other))
)

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

def __rdiv__(self, other: IntOrFQ) -> "FQ":
if isinstance(other, FQ):
on = other.n
elif isinstance(other, int):
on = other
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(other))
)

on = normalize_FQ_point(other)
return type(self)(
prime_field_inv(self.n, self.field_modulus) * on % self.field_modulus
)
Expand Down
81 changes: 21 additions & 60 deletions py_ecc/fields/optimized_field_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@
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 @@ -48,29 +63,11 @@ def __init__(self, val: IntOrFQ) -> None:
)

def __add__(self, other: IntOrFQ) -> "FQ":
if isinstance(other, FQ):
on = other.n
elif isinstance(other, int):
on = other
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(other))
)

on = normalize_FQ_point(other)
return type(self)((self.n + on) % self.field_modulus)

def __mul__(self, other: IntOrFQ) -> "FQ":
if isinstance(other, FQ):
on = other.n
elif isinstance(other, int):
on = other
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(other))
)

on = normalize_FQ_point(other)
return type(self)((self.n * on) % self.field_modulus)

def __rmul__(self, other: IntOrFQ) -> "FQ":
Expand All @@ -80,45 +77,18 @@ def __radd__(self, other: IntOrFQ) -> "FQ":
return self + other

def __rsub__(self, other: IntOrFQ) -> "FQ":
if isinstance(other, FQ):
on = other.n
elif isinstance(other, int):
on = other
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(other))
)

on = normalize_FQ_point(other)
return type(self)((on - self.n) % self.field_modulus)

def __sub__(self, other: IntOrFQ) -> "FQ":
if isinstance(other, FQ):
on = other.n
elif isinstance(other, int):
on = other
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(other))
)

on = normalize_FQ_point(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":
if isinstance(other, FQ):
on = other.n
elif isinstance(other, int):
on = other
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(other))
)

on = normalize_FQ_point(other)
return type(self)(
self.n * prime_field_inv(on, self.field_modulus) % self.field_modulus
)
Expand All @@ -127,16 +97,7 @@ def __truediv__(self, other: IntOrFQ) -> "FQ":
return self.__div__(other)

def __rdiv__(self, other: IntOrFQ) -> "FQ":
if isinstance(other, FQ):
on = other.n
elif isinstance(other, int):
on = other
else:
raise TypeError(
"Expected an int or FQ object, but got object of type {}"
.format(type(other))
)

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

0 comments on commit ae2e221

Please sign in to comment.