perf: 注释掉Setup函数并将G, g, U转化为全局变量;添加demo.py

This commit is contained in:
dqy 2023-10-17 21:02:55 +08:00
parent 1f83cb2140
commit c2eba8a0bf

View File

@ -23,6 +23,12 @@ sm2p256v1 = CurveFp(
Gy=0xBC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0
)
# 椭圆曲线
G = sm2p256v1
# 生成元
g = (sm2p256v1.Gx, sm2p256v1.Gy)
def multiply(a: Tuple[int, int], n: int) -> Tuple[int, int]:
N = sm2p256v1.N
A = sm2p256v1.A
@ -117,26 +123,29 @@ def jacobianMultiply(
return jacobianAdd(jacobianDouble(jacobianMultiply((Xp, Yp, Zp), n // 2, N, A, P), A, P), (Xp, Yp, Zp), A, P)
raise ValueError("jacobian Multiply error")
def Setup(sec: int) -> Tuple[CurveFp, Tuple[int, int],
Tuple[int, int]]:
'''
params:
sec: an init safety param
# 生成元
U = multiply(g, random.randint(0, sm2p256v1.P))
return:
G: sm2 curve
g: generator
U: another generator
'''
# def Setup(sec: int) -> Tuple[CurveFp, Tuple[int, int],
# Tuple[int, int]]:
# '''
# params:
# sec: an init safety param
G = sm2p256v1
# return:
# G: sm2 curve
# g: generator
# U: another generator
# '''
g = (sm2p256v1.Gx, sm2p256v1.Gy)
# G = sm2p256v1
tmp_u = random.randint(0, sm2p256v1.P)
U = multiply(g, tmp_u)
# g = (sm2p256v1.Gx, sm2p256v1.Gy)
return G, g, U
# tmp_u = random.randint(0, sm2p256v1.P)
# U = multiply(g, tmp_u)
# return G, g, U
def hash2(double_G: Tuple[Tuple[int, int], Tuple[int, int]]) -> int:
sm3 = Sm3() #pylint: disable=e0602
@ -204,6 +213,10 @@ def GenerateKeyPair(
return public_key, secret_key
# 生成A和B的公钥和私钥
pk_A, sk_A = GenerateKeyPair(0, ())
pk_B, sk_B = GenerateKeyPair(0, ())
def Encrypt(pk: Tuple[int, int], m: int) -> Tuple[Tuple[
Tuple[int, int],Tuple[int, int], int], int]:
enca = Encapsulate(pk)
@ -267,16 +280,6 @@ def f(x: int, f_modulus: list, T: int) -> int:
res += f_modulus[i] * pow(x, i)
return res
# 生成A和B的公钥和私钥
pk_A, sk_A = GenerateKeyPair(0, ())
pk_B, sk_B = GenerateKeyPair(0, ())
# sec需要重新设置
sec = 256
# 调用Setup函数
G, g, U= Setup(sec)
def GenerateReKey(sk_A, pk_B, N: int, T: int) -> list:
'''
param: