Merge pull request 'main' (#9) from sangge/mimajingsai:main into main

Reviewed-on: #9
This commit is contained in:
ccyj 2023-10-17 21:50:29 +08:00
commit 523bae52a9

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,29 +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], Callable,
Callable, Callable, Callable]:
'''
params:
sec: an init safety param
# 生成元
U = multiply(g, random.randint(0, sm2p256v1.P))
return:
G: sm2 curve
g: generator
U: another generator
use sm3 as hash function
hash2: G^2 -> Zq
hash3: G^3 -> Zq
hash4: G^3 * Zq -> Zq
'''
# 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)
# 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
@ -183,9 +189,6 @@ def Setup(sec: int) -> Tuple[CurveFp, Tuple[int, int],
digest = int.from_bytes(digest, 'big') % sm2p256v1.P
return digest
return G, g, U, hash2, hash3, hash4, KDF
def GenerateKeyPair(
lamda_parma: int,
public_params: tuple
@ -210,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)
@ -273,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, hash2, hash3, hash4, KDF = Setup(sec)
def GenerateReKey(sk_A, pk_B, N: int, T: int) -> list:
'''
param:
@ -347,7 +344,6 @@ def Checkcapsule(capsule:Tuple[Tuple[int,int],Tuple[int,int],int]) -> bool: #
return flag
def ReEncapsulate(kFrag:list,capsule:Tuple[Tuple[int,int],Tuple[int,int],int]) -> Tuple[Tuple[int,int],Tuple[int,int],int,Tuple[int,int]] :
id,rk,Xa,U1 = kFrag
E,V,s = capsule