diff --git a/.gitignore b/.gitignore index c938ebd..5b6c206 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ jiangchao -__pycache__ \ No newline at end of file +__pycache__ + +example.py + +ReEncrypt.py \ No newline at end of file diff --git a/src/tpre.py b/src/tpre.py index f75c1df..df6c14d 100644 --- a/src/tpre.py +++ b/src/tpre.py @@ -1,6 +1,5 @@ from gmssl import * #pylint: disable = e0401 from typing import Tuple, Callable -import random # 生成密钥对模块 class CurveFp: @@ -29,7 +28,7 @@ def multiply(a: Tuple[int, int], n: int) -> Tuple[int, int]: P = sm2p256v1.P return fromJacobian(jacobianMultiply(toJacobian(a), n, N, A, P), P) -def add(a: Tuple[int, int], b: Tuple[int, int]) -> Tuple[int, int]: +def add(a: Tuple[int, int], b: Tuple[int, int], A: int, P: int) -> Tuple[int, int]: A = sm2p256v1.A P = sm2p256v1.P return fromJacobian(jacobianAdd(toJacobian(a), toJacobian(b), A, P), P) @@ -117,44 +116,22 @@ 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]: +def Setup(sec: int) -> Tuple[int, int, int, Callable, Callable, Callable, Callable]: ''' params: sec: an init safety param return: - G: sm2 curve - g: generator - U: another generator - sm3 - hash2: G^2 -> Zq - hash3: G^3 -> Zq - hash4: G^3 * Zq -> Zq + G: + ''' - G = sm2p256v1 - - g = (sm2p256v1.Gx, sm2p256v1.Gy) - - tmp_u = random.randint(0, sm2p256v1.P) - U = multiply(g, tmp_u) - - hash2 = Sm3() #pylint: disable=e0602 - - hash3 = Sm3() #pylint: disable=e0602 - - hash4 = Sm3() #pylint: disable=e0602 - - KDF = Sm3() #pylint: disable=e0602 - return G, g, U, hash2, hash3, hash4, KDF def GenerateKeyPair( lamda_parma: int, public_params: tuple - ) -> Tuple[Tuple[int, int], int]: + ) -> Tuple[Tuple[bytes, bytes], bytes]: ''' params: lamda_param: an init safety param @@ -163,14 +140,16 @@ def GenerateKeyPair( return: public_key, secret_key ''' - sm2 = Sm2Key() #pylint: disable=e0602 + sm2 = Sm2Key() sm2.generate_key() - - public_key_x = int.from_bytes(bytes(sm2.public_key.x),"big") - public_key_y = int.from_bytes(bytes(sm2.public_key.y),"big") + public_key_x = bytes(sm2.public_key.x) + public_key_y = bytes(sm2.public_key.y) public_key = (public_key_x, public_key_y) + secret_key = bytes(sm2.private_key) + print(private_key) + + - secret_key = int.from_bytes(bytes(sm2.private_key),"big") return public_key, secret_key