diff --git a/py_ecc/fields/field_elements.py b/py_ecc/fields/field_elements.py index cd8db3fe..5d223b8b 100644 --- a/py_ecc/fields/field_elements.py +++ b/py_ecc/fields/field_elements.py @@ -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, @@ -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": @@ -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 ) @@ -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 ) diff --git a/py_ecc/fields/optimized_field_elements.py b/py_ecc/fields/optimized_field_elements.py index 9e48ce82..a501726f 100644 --- a/py_ecc/fields/optimized_field_elements.py +++ b/py_ecc/fields/optimized_field_elements.py @@ -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, @@ -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": @@ -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 ) @@ -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 )