ELLIPTIC CURVES
BACKGROUND
椭圆曲线的含义
Weierstrass方程:
\(E:Y^2=X^3+aX+b\)
椭圆曲线有一个非常有趣的特性,也可以称之为一种运算——“点加法”。这个运算接受椭圆曲线上的两个点作为输入,输出是椭圆曲线上的第三个点。通过将椭圆曲线上的所有点都纳入这种运算中,可以构成一个阿贝尔群结构。
我们可以把一个点与标量的乘法理解为:将该点重复添加多次。原来,标量乘法其实是一种“陷门函数”。ECC正是利用给定P、Q后,很难找到\(Q=[n]P\)的n。
从几何学角度,以一条椭圆曲线为例,先在曲线上标出两个点P,Q,并记录下他们的坐标x,y。接着画一条穿过这两个点的直线。继续延长这条直线,直到它再次与椭圆曲线相交。将这个新的交点标记为R。最后,沿着方向y将其反射,得到点R'=R(x,-y)。这样,点的加法运算的结果就是P + Q =R'。

同时也可以将两个相同的点连在一起,以P+P为例。我们无法通过一个点来画出一条唯一的直线,但我们可以继续算出该点处曲线的切线。直线与曲线在点R处相交。接着将R点反射得到点P+P=R'=R(x,-y)。
如果没有第三个点的话,即这条直线不再与曲线相交。这是我们称这条直线与点(O)相交。点(O)实际上是一个位于无穷远处的点,也就是每条垂直线的终点。
点(O)在该群中起着“单位元”的作用,即P+O=P和P+(-P)=O都相当于与O相乘的结果。
定义:椭圆曲线E是指满足Weierstrass方程的解的集合。
再加上无穷远点处的那个点O。常数a,b必须满足相应的关系式。
确保曲线上不存在奇点。
正式来说E作为一个椭圆曲线,那么点加法具有以下性质:
(a)P+O=O+P=P
(b)P+(-P)=O
(c)(P+Q)+R=P+(Q+R)
(d)P+Q=Q+P
Starter
Point Negation
题目
\(E:Y^2=X^3+497X+1768 \pmod{9739}\)
利用上述曲线以及点P(8045,6936),找出满足P+Q=O条件的点Q(x,y)。
在椭圆曲线有限域中,P(x,y),则-P(x,-y %p)。
crypto{8045,2803}
Parameter Choice
Smooth Criminal
题目
from Crypto.Cipher import AES
from Crypto.Util.number import inverse
from Crypto.Util.Padding import pad, unpad
from collections import namedtuple
from random import randint
import hashlib
import os
# Create a simple Point class to represent the affine points.
Point = namedtuple("Point", "x y")
# The point at infinity (origin for the group law).
O = 'Origin'
FLAG = b'crypto{??????????????????????????????}'
def check_point(P: tuple):
if P == O:
return True
else:
return (P.y**2 - (P.x**3 + a*P.x + b)) % p == 0 and 0 <= P.x < p and 0 <= P.y < p
def point_inverse(P: tuple):
if P == O:
return P
return Point(P.x, -P.y % p)
#计算P+Q
def point_addition(P: tuple, Q: tuple):
# based of algo. in ICM
if P == O:
return Q
elif Q == O:
return P
elif Q == point_inverse(P):
return O
else:
if P == Q:
lam = (3*P.x**2 + a)*inverse(2*P.y, p)
lam %= p
else:
lam = (Q.y - P.y) * inverse((Q.x - P.x), p)
lam %= p
Rx = (lam**2 - P.x - Q.x) % p
Ry = (lam*(P.x - Rx) - P.y) % p
R = Point(Rx, Ry)
assert check_point(R)
return R
#点加倍如计算R = nP
def double_and_add(P: tuple, n: int):
# based of algo. in ICM
Q = P
R = O
while n > 0:
if n % 2 == 1:
R = point_addition(R, Q) #=Q
Q = point_addition(Q, Q)
n = n // 2
assert check_point(R)
return R
#只取横坐标 S=nQ
def gen_shared_secret(Q: tuple, n: int):
# Bob's Public key, my secret int
S = double_and_add(Q, n)
return S.x
def encrypt_flag(shared_secret: int):
# Derive AES key from shared secret
sha1 = hashlib.sha1()
sha1.update(str(shared_secret).encode('ascii'))
key = sha1.digest()[:16]
# Encrypt flag
iv = os.urandom(16)
cipher = AES.new(key, AES.MODE_CBC, iv)
ciphertext = cipher.encrypt(pad(FLAG, 16))
# Prepare data to send
data = {}
data['iv'] = iv.hex()
data['encrypted_flag'] = ciphertext.hex()
return data
# Define the curve
p = 310717010502520989590157367261876774703
a = 2
b = 3
# Generator
g_x = 179210853392303317793440285562762725654
g_y = 105268671499942631758568591033409611165
G = Point(g_x, g_y)
# My secret int, different every time!!
n = randint(1, p)
# Send this to Bob!
public = double_and_add(G, n)
print(public)
# Bob's public key
b_x = 272640099140026426377756188075937988094
b_y = 51062462309521034358726608268084433317
B = Point(b_x, b_y)
# Calculate Shared Secret
shared_secret = gen_shared_secret(B, n)
# Send this to Bob!
ciphertext = encrypt_flag(shared_secret)
print(ciphertext)
shared_secret=[n]B。所以只需要求出n即可。由pubilc = [n]G。我们可以求出n的值,利用脚本
# 填入你的参数
p = 310717010502520989590157367261876774703
a = 2
b = 3
E = EllipticCurve(GF(p), [a,b])
G = E(179210853392303317793440285562762725654, 105268671499942631758568591033409611165)
P = E(280810182131414898730378982766101210916, 291506490768054478159835604632710368904)
# 求离散对数 n,满足 P = n*G
n = G.discrete_log(P)
print(n)
#n =47836431801801373761601790722388100620
之后添加解密函数
def decrypt_flag(shared_secret: int):
# Derive AES key from shared secret
sha1 = hashlib.sha1()
sha1.update(str(shared_secret).encode('ascii'))
key = sha1.digest()[:16]
# Decrypt flag
iv = bytes.fromhex('07e2628b590095a5e332d397b8a59aa7')
encrypted_flag = bytes.fromhex('8220b7c47b36777a737f5ef9caa2814cf20c1c1ef496ec21a9b4833da24a008d0870d3ac3a6ad80065c138a2ed6136af')
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = unpad(cipher.decrypt(encrypted_flag), 16)
return plaintext
解得b'crypto{n07_4ll_curv3s_4r3_s4f3_curv3s}'

浙公网安备 33010602011771号