Как умножить экземпляра класса на число в эллиптических кривых над конечными полями?
Ошибка умножения экземпляра класса на число в эллиптических кривых над конечными полями.
Всем привет! Столкнулся с проблемой, из-за которой не могу перемножить вышеописанное. Вводимый код для тестирования:
prime = 223
a = FieldElement(num=0, prime=prime)
b = FieldElement(num=7, prime=prime)
x = FieldElement(num=47, prime=prime)
y = FieldElement(num=71, prime=prime)
p1 = Point(x, y, a, b)
print(8*p1)
Перегрузки операторов умножения и сложения описаны в коде класса FieldElement:
def __add__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot add numbers in different Fields')
num = (self.num + other.num) % self.prime
return self.__class__(num, self.prime)
def __mul__(self, other):
if self.prime != other.prime:
raise TypeError('Cannot multiply numbers in different Fields')
mul = (self.num * other.num) % self.prime
return self.__class__(mul, self.prime)
В классе Point:
def __add__(self, other):
if self.a != other.a or self.b != other.b:
raise TypeError(f'{self} {other} are not on the same Curve')
if self.x is None:
return other
if other.x is None:
return self
if self.x == other.x and self.y != other.y: # vertical invertibility
return self.__class__(None, None, self.a, self.b)
if self.x != other.x: # tangent
s = (other.y - self.y) / (other.x - self.x)
x = s ** 2 - self.x - other.x
y = s * (self.x - x) - self.y
return self.__class__(x, y, self.a, self.b)
if self == other: # p1=p2
s = (3 * self.x ** 2 + self.a) / (2 * self.y)
x = s ** 2 - 2 * self.x
y = s * (self.x - x) - self.y
return self.__class__(x, y, self.a, self.b)
if self == other and self.y == 0 * self.x: # infinity vertical tangent with 0 divider
return self.__class__(None, None, self.a, self.b)
def __rmul__(self, coef):
current = self
result = self.__class__(None, None, self.a, self.b) # <2>
while coef:
if coef & 1:
result += current
current += current
coef >>= 1
return result
Ну и сама ошибка:
Traceback (most recent call last):
File test.py line 135, in <module>
print(8*p1)
File ecc.py, line 109, in __rmul__
current += current
File ecc.py, line 95, in __add__
s = (3 * self.x ** 2 + self.a) / (2 * self.y)
TypeError: unsupported operand type(s) for *: 'int' and 'FieldElement'
Благодарю за помощь!