5,955
社区成员
import numpy as np
import matplotlib.pyplot as plt
# 将16进制字符串转为整数
a_hex = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000000000000000FFFFFFFC"
b_hex = "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93"
# a_hex = "FF"
# b_hex = "28"
# 转换为整数
a = int(a_hex, 16)
b = int(b_hex, 16)
# a=10
# b=5
# 设定 x 的范围
#x_values = np.linspace(-2**32, 2**32, 1000) # 选择合适的范围
#x_values = np.linspace(-2000, 2000, 250)
x_values = np.linspace(-10, 200, 500)
# 计算 y^2 = x^3 + ax + b
y_squared = x_values**3 + a * x_values + b
print(y_squared)
# 计算 y,去掉负值和无效值
# y_positive = np.where(y_squared >= 0, np.sqrt(y_squared), np.nan)
# y_negative = np.where(y_squared >= 0, -np.sqrt(y_squared), np.nan)
y_positive = np.sqrt(np.maximum(0, y_squared))
y_negative = -y_positive
# 绘制椭圆曲线
plt.figure(figsize=(10, 10))
plt.plot(x_values, y_positive, label=r'$y^2 = x^3 + ax + b, color='blue')
plt.plot(x_values, y_negative, label='y = -√(x? + ax + b)',color='red')
# 设置图的范围
plt.xlim(-10, 200)
plt.ylim(-10**42, 10**42)
# plt.ylim(-30, 30) # 限制 y 轴的范围
# plt.xlim(-20, 20) # 限制 x 轴的范围
plt.axhline(0, color='black', linewidth=0.5, linestyle='--')
plt.axvline(0, color='black', linewidth=0.5, linestyle='--')
plt.grid()
plt.title(r'Elliptic Curve: $y^2 = x^3 + ' + hex(a)[2:] + 'x + ' + hex(b)[2:] + ')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
有时候报:
AttributeError: 'float' object has no attribute 'sqrt'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\myProgram\pythonProgram\myfindprime\测试一下\国密sm2\官方-用官方的a和b.py", line 28, in <module>
y_positive = np.where(y_squared > 0, np.sqrt(y_squared), np.nan)
TypeError: loop of ufunc does not support argument 0 of type float which has no callable sqrt method
有时候报:
报异常:AttributeError: 'int' object has no attribute 'sqrt'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\myProgram\pythonProgram\myfindprime\测试一下\国密sm2\官方-用官方的a和b.py", line 24, in <module>
y_positive = np.sqrt(np.maximum(0, y_squared))
TypeError: loop of ufunc does not support argument 0 of type int which has no callable sqrt method
如果把a和b设小一点(比如a=10,b=5),就可以画出来。这个异常莫名其妙,chatgpt也解决不了。盼大牛看看,谢谢