gpt4 book ai didi

python - 椭圆曲线点乘有时会产生错误的结果

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:59 28 4
gpt4 key购买 nike

我正在尝试在 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 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

我已经测试了多个值,有时收到正确的结果,有时收到错误的结果。以下是 secp256k1 曲线的一些示例:

Correct case:

Point:
x: 55066263022277343669578718895168534326250603453777594175500187360389116729240
y: 32670510020758816978083085130507043184471273380659243275938904335757337482424

Scalar:
24917563387128992525586541072555299775466638713533702470001729610242625242518

Result:
x: 30761640594611050927920473896980176618852175787129035469672454552631110981601
y: 71589730992642335000963807008595655528549796860255114743222783656832671116115

Incorrect case:

Point:
x: 55763821349469695143251444157857527262741225298806100934200365505846997750729
y: 108311437762381308673474438597429390041179265557837286648496266777408263200496

Scalar:
14153923515125836933174734651094672686604752309351060890449588899992278900437

Result:
x: 33276244942913720410563273329803338573012270278748134958920446992664092974057
y: 38066195899997010281080208319525128360573241938488491930954474962286948555539

最佳答案

所以我发现了这个问题,但任何人都很难看出,因为数学似乎是正确的,但我犯了一个编程错误,如果你脱离上下文查看该函数,则不容易发现这个错误。

所提供的代码将始终对曲线的生成点进行标量乘法,但不会对 self 实际引用的点进行标量乘法。 Self 是一个点的实例,但在概述的代码中从未直接引用,而是与曲线生成器点相乘。

new_point = self.curve().g()

应替换为

new_point = self

new_point = new_point + self.curve().g()

new_point = new_point + self

我仍然有点不清楚这段代码在某些情况下如何生成正确的示例。

关于python - 椭圆曲线点乘有时会产生错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55891150/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com