项目中用到的函数简介:https://github.com/EveandBob/Introduction-to-some-functions-in-elliptic-curves-not-projects-
ECDSA的实现
def ECDSA():
PA=[0,0]
def ECDSA_sign(msg):
nonlocal PA
M = msg.encode()
dA, PA = get_key()
k = random.randrange(1, n)
R = calculate_np(Gx, Gy, k, a, b, p)
r = R[0] % n
e = int(sha1(M).hexdigest(), 16)
s = (get_inverse(k, n) * (e + dA * r)) % n
print("获得的签名为:")
print(r,s)
return r, s
def ECDSA_verif_sign(msg, sign):
r, s = sign
M = msg.encode()
e = int(sha1(M).hexdigest(), 16)
w = get_inverse(s, n)
temp1 = calculate_np(Gx, Gy, e * w, a, b, p)
temp2 = calculate_np(PA[0], PA[1], r * w, a, b, p)
R, S = calculate_p_q(temp1[0], temp1[1], temp2[0], temp2[1], a, b, p)
if (r == R):
print("结果正确")
return True
else:
print("结果错误")
return False
return ECDSA_verif_sign(msg,ECDSA_sign(msg))
