Python 椭圆曲线点乘有时会产生错误的结果
我试图在Python中用标量乘法实现椭圆曲线点,但遇到了一个问题,在某些情况下,我得到了错误的结果,并且正在努力找出可能的错误 以下是执行计算的函数:Python 椭圆曲线点乘有时会产生错误的结果,python,math,cryptography,elliptic-curve,ecdsa,Python,Math,Cryptography,Elliptic Curve,Ecdsa,我试图在Python中用标量乘法实现椭圆曲线点,但遇到了一个问题,在某些情况下,我得到了错误的结果,并且正在努力找出可能的错误 以下是执行计算的函数: def __mul__(self, other): """ Scalar multiplication of a point with a integer The point gets added to itself other times This can be efficiently computed usin
def __mul__(self, other):
"""
Scalar multiplication of a point with a integer
The point gets added to itself other times
This can be efficiently computed using binary
representation of the scalar
:param other: int number to multiply the point with
:return: Point point after applying the multiplication
"""
if other < 1 or other > self.order():
raise Exception("Scalar for point mult is out of range")
# Convert into binary representation
scalar_bin = str(bin(other))[2:]
new_point = self.curve().g()
# Iterate through every bit of the scalar
# double the current point and if it is a 1 bit
# we add the generator point
for i in range(1, len(scalar_bin)):
new_point = new_point + new_point
if scalar_bin[i] == "1":
new_point = new_point + self.curve().g()
return new_point
因此,我发现了这个问题,很难让任何人看到,因为数学似乎是正确的,但相反,我犯了一个编程错误,如果你断章取义地看函数,这是不容易看到的 给出的代码将始终对曲线的生成器点进行标量乘法,但不会对self实际引用的点进行标量乘法。Self是一个点的实例,但在概述的代码中从未直接引用,而是将曲线生成器点相乘
new_point = self.curve().g()
应该由
new_point = self
及
借
对于我来说,这段代码在某些情况下如何生成正确的示例仍然有点不清楚。因此我解决了这个问题,很难让任何人看到,因为数学似乎是正确的,但相反,我犯了一个编程错误,如果你断章取义地看函数,很难看到这个错误 给出的代码将始终对曲线的生成器点进行标量乘法,但不会对self实际引用的点进行标量乘法。Self是一个点的实例,但在概述的代码中从未直接引用,而是将曲线生成器点相乘
new_point = self.curve().g()
应该由
new_point = self
及
借
在某些情况下,我仍然有点不清楚该代码如何生成正确的示例。您确定(x,y)真的是曲线上的一个点吗?这真的不是一个问题。堆栈溢出上的任何人都不可能运行此代码。您确定(x,y)真的是曲线上的一个点吗?这真的不是一个点。堆栈溢出上的任何人都不可能运行此代码。
new_point = new_point + self