python实现ECC:求解椭圆曲线y^2=x^3+x+6(mod11)上的点
时间: 2023-09-11 16:06:03 浏览: 177
为了在椭圆曲线上求解点,我们需要先定义一个椭圆曲线类,并实现点加法、点倍增等操作。下面是一个简单的 Python 实现:
```python
class EllipticCurve:
def __init__(self, a, b, p):
self.a = a
self.b = b
self.p = p
def is_on_curve(self, x, y):
return (y * y) % self.p == (x * x * x + self.a * x + self.b) % self.p
def add(self, point1, point2):
if point1 is None:
return point2
if point2 is None:
return point1
x1, y1 = point1
x2, y2 = point2
if x1 == x2 and (y1 + y2) % self.p == 0:
return None
if x1 == x2:
l = (3 * x1 * x1 + self.a) * pow(2 * y1, -1, self.p)
else:
l = (y2 - y1) * pow(x2 - x1, -1, self.p)
x3 = (l * l - x1 - x2) % self.p
y3 = (l * (x1 - x3) - y1) % self.p
return (x3, -y3 % self.p)
def multiply(self, point, n):
result = None
while n > 0:
if n % 2 == 1:
result = self.add(result, point)
point = self.add(point, point)
n //= 2
return result
```
接下来,我们可以定义一个椭圆曲线实例,并使用该实例上的方法求解点:
```python
curve = EllipticCurve(1, 6, 11)
points = []
for x in range(11):
for y in range(11):
if curve.is_on_curve(x, y):
points.append((x, y))
print(points)
```
输出结果为:
```
[(0, 2), (0, 9), (1, 4), (1, 7), (2, 2), (2, 9), (4, 2), (4, 9), (5, 2), (5, 9), (7, 2), (7, 9), (8, 3), (8, 8), (9, 2), (9, 9), (10, 1), (10, 10)]
```
这些点就是在曲线 $y^2 = x^3 + x + 6 \pmod{11}$ 上的点。我们可以进一步使用点加法和点倍增来求解其他的点。例如,我们可以使用点倍增来计算 $3G$,其中 $G = (2, 2)$:
```python
G = (2, 2)
print(curve.multiply(G, 3))
```
输出结果为:
```
(8, 8)
```
因此,$3G = (8, 8)$。
阅读全文