update benchmark
This commit is contained in:
		@@ -1,5 +1,7 @@
 | 
			
		||||
import ecc_rs
 | 
			
		||||
import time
 | 
			
		||||
import ecc_py
 | 
			
		||||
import random
 | 
			
		||||
 | 
			
		||||
point = tuple[int, int]
 | 
			
		||||
# Example point coordinates for P1 and P2 as tuples (x1, y1) and (x2, y2)
 | 
			
		||||
@@ -58,19 +60,44 @@ def add(a: point, b: point) -> point:
 | 
			
		||||
    return result
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
random_mul_times = random.randint(0, sm2p256v1.N)
 | 
			
		||||
 | 
			
		||||
start_time = time.time()  # 获取开始时间
 | 
			
		||||
for i in range(10):
 | 
			
		||||
    result = multiply(g, 10000)  # 执行函数
 | 
			
		||||
    result = multiply(g, random_mul_times)  # 执行函数
 | 
			
		||||
end_time = time.time()  # 获取结束时间
 | 
			
		||||
elapsed_time = end_time - start_time  # 计算执行时间
 | 
			
		||||
print(f"rust multiply 执行时间: {elapsed_time:.6f} 秒")
 | 
			
		||||
rs_mul_elapsed_time = end_time - start_time  # 计算执行时间
 | 
			
		||||
print(f"rust multiply 执行时间: {rs_mul_elapsed_time:.6f} 秒")
 | 
			
		||||
# rust multiply 执行时间: 0.000849 秒
 | 
			
		||||
 | 
			
		||||
start_time = time.time()  # 获取开始时间
 | 
			
		||||
for i in range(10):
 | 
			
		||||
    result = ecc_py.multiply(g, random_mul_times)  # 执行函数
 | 
			
		||||
end_time = time.time()  # 获取结束时间
 | 
			
		||||
py_mul_elapsed_time = end_time - start_time  # 计算执行时间
 | 
			
		||||
print(f"ecc_py multiply 执行时间: {py_mul_elapsed_time:.6f} 秒")
 | 
			
		||||
# ecc_py multiply 执行时间: 0.001374 秒
 | 
			
		||||
 | 
			
		||||
mul_improve = (py_mul_elapsed_time - rs_mul_elapsed_time) / py_mul_elapsed_time * 100
 | 
			
		||||
 | 
			
		||||
print(f"rust 实现 mul 提速 {mul_improve:.3f}%")
 | 
			
		||||
 | 
			
		||||
start_time = time.time()  # 获取开始时间
 | 
			
		||||
for i in range(10):
 | 
			
		||||
    result = add(g, g)  # 执行函数
 | 
			
		||||
end_time = time.time()  # 获取结束时间
 | 
			
		||||
elapsed_time = end_time - start_time  # 计算执行时间
 | 
			
		||||
print(f"rust add 执行时间: {elapsed_time:.6f} 秒")
 | 
			
		||||
rs_add_elapsed_time = end_time - start_time  # 计算执行时间
 | 
			
		||||
print(f"rust add 执行时间: {rs_add_elapsed_time:.6f} 秒")
 | 
			
		||||
# rust add 执行时间: 0.000378 秒
 | 
			
		||||
 | 
			
		||||
start_time = time.time()  # 获取开始时间
 | 
			
		||||
for i in range(10):
 | 
			
		||||
    result = ecc_py.add(g, g)  # 执行函数
 | 
			
		||||
end_time = time.time()  # 获取结束时间
 | 
			
		||||
py_add_elapsed_time = end_time - start_time  # 计算执行时间
 | 
			
		||||
print(f"ecc_py add 执行时间: {py_add_elapsed_time:.6f} 秒")
 | 
			
		||||
# ecc_py add 执行时间: 0.000510 秒
 | 
			
		||||
 | 
			
		||||
add_improve = (py_add_elapsed_time - rs_add_elapsed_time) / py_add_elapsed_time * 100
 | 
			
		||||
 | 
			
		||||
print(f"rust 实现 add 提速 {add_improve:.3f}%")
 | 
			
		||||
							
								
								
									
										132
									
								
								ecc_py.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										132
									
								
								ecc_py.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,132 @@
 | 
			
		||||
from typing import Tuple
 | 
			
		||||
 | 
			
		||||
point = Tuple[int, int]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# 生成密钥对模块
 | 
			
		||||
class CurveFp:
 | 
			
		||||
    def __init__(self, A, B, P, N, Gx, Gy, name):
 | 
			
		||||
        self.A = A
 | 
			
		||||
        self.B = B
 | 
			
		||||
        self.P = P
 | 
			
		||||
        self.N = N
 | 
			
		||||
        self.Gx = Gx
 | 
			
		||||
        self.Gy = Gy
 | 
			
		||||
        self.name = name
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
sm2p256v1 = CurveFp(
 | 
			
		||||
    name="sm2p256v1",
 | 
			
		||||
    A=0xFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC,
 | 
			
		||||
    B=0x28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93,
 | 
			
		||||
    P=0xFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF,
 | 
			
		||||
    N=0xFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123,
 | 
			
		||||
    Gx=0x32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7,
 | 
			
		||||
    Gy=0xBC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def multiply(a: point, n: int) -> point:
 | 
			
		||||
 | 
			
		||||
    N = sm2p256v1.N
 | 
			
		||||
    A = sm2p256v1.A
 | 
			
		||||
    P = sm2p256v1.P
 | 
			
		||||
    return fromJacobian(jacobianMultiply(toJacobian(a), n, N, A, P), P)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def add(
 | 
			
		||||
    a: point,
 | 
			
		||||
    b: point,
 | 
			
		||||
) -> point:
 | 
			
		||||
    A = sm2p256v1.A
 | 
			
		||||
    P = sm2p256v1.P
 | 
			
		||||
    return fromJacobian(jacobianAdd(toJacobian(a), toJacobian(b), A, P), P)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def inv(a: int, n: int) -> int:
 | 
			
		||||
    if a == 0:
 | 
			
		||||
        return 0
 | 
			
		||||
    lm, hm = 1, 0
 | 
			
		||||
    low, high = a % n, n
 | 
			
		||||
    while low > 1:
 | 
			
		||||
        r = high // low
 | 
			
		||||
        nm, new = hm - lm * r, high - low * r
 | 
			
		||||
        lm, low, hm, high = nm, new, lm, low
 | 
			
		||||
    return lm % n
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def toJacobian(Xp_Yp: point) -> Tuple[int, int, int]:
 | 
			
		||||
    Xp, Yp = Xp_Yp
 | 
			
		||||
    return (Xp, Yp, 1)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def fromJacobian(Xp_Yp_Zp: Tuple[int, int, int], P: int) -> point:
 | 
			
		||||
    Xp, Yp, Zp = Xp_Yp_Zp
 | 
			
		||||
    z = inv(Zp, P)
 | 
			
		||||
    return ((Xp * z**2) % P, (Yp * z**3) % P)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def jacobianDouble(
 | 
			
		||||
    Xp_Yp_Zp: Tuple[int, int, int], A: int, P: int
 | 
			
		||||
) -> Tuple[int, int, int]:
 | 
			
		||||
    Xp, Yp, Zp = Xp_Yp_Zp
 | 
			
		||||
    if not Yp:
 | 
			
		||||
        return (0, 0, 0)
 | 
			
		||||
    ysq = (Yp**2) % P
 | 
			
		||||
    S = (4 * Xp * ysq) % P
 | 
			
		||||
    M = (3 * Xp**2 + A * Zp**4) % P
 | 
			
		||||
    nx = (M**2 - 2 * S) % P
 | 
			
		||||
    ny = (M * (S - nx) - 8 * ysq**2) % P
 | 
			
		||||
    nz = (2 * Yp * Zp) % P
 | 
			
		||||
    return (nx, ny, nz)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def jacobianAdd(
 | 
			
		||||
    Xp_Yp_Zp: Tuple[int, int, int], Xq_Yq_Zq: Tuple[int, int, int], A: int, P: int
 | 
			
		||||
) -> Tuple[int, int, int]:
 | 
			
		||||
    Xp, Yp, Zp = Xp_Yp_Zp
 | 
			
		||||
    Xq, Yq, Zq = Xq_Yq_Zq
 | 
			
		||||
    if not Yp:
 | 
			
		||||
        return (Xq, Yq, Zq)
 | 
			
		||||
    if not Yq:
 | 
			
		||||
        return (Xp, Yp, Zp)
 | 
			
		||||
    U1 = (Xp * Zq**2) % P
 | 
			
		||||
    U2 = (Xq * Zp**2) % P
 | 
			
		||||
    S1 = (Yp * Zq**3) % P
 | 
			
		||||
    S2 = (Yq * Zp**3) % P
 | 
			
		||||
    if U1 == U2:
 | 
			
		||||
        if S1 != S2:
 | 
			
		||||
            return (0, 0, 1)
 | 
			
		||||
        return jacobianDouble((Xp, Yp, Zp), A, P)
 | 
			
		||||
    H = U2 - U1
 | 
			
		||||
    R = S2 - S1
 | 
			
		||||
    H2 = (H * H) % P
 | 
			
		||||
    H3 = (H * H2) % P
 | 
			
		||||
    U1H2 = (U1 * H2) % P
 | 
			
		||||
    nx = (R**2 - H3 - 2 * U1H2) % P
 | 
			
		||||
    ny = (R * (U1H2 - nx) - S1 * H3) % P
 | 
			
		||||
    nz = (H * Zp * Zq) % P
 | 
			
		||||
    return (nx, ny, nz)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def jacobianMultiply(
 | 
			
		||||
    Xp_Yp_Zp: Tuple[int, int, int], n: int, N: int, A: int, P: int
 | 
			
		||||
) -> Tuple[int, int, int]:
 | 
			
		||||
    Xp, Yp, Zp = Xp_Yp_Zp
 | 
			
		||||
    if Yp == 0 or n == 0:
 | 
			
		||||
        return (0, 0, 1)
 | 
			
		||||
    if n == 1:
 | 
			
		||||
        return (Xp, Yp, Zp)
 | 
			
		||||
    if n < 0 or n >= N:
 | 
			
		||||
        return jacobianMultiply((Xp, Yp, Zp), n % N, N, A, P)
 | 
			
		||||
    if (n % 2) == 0:
 | 
			
		||||
        return jacobianDouble(jacobianMultiply((Xp, Yp, Zp), n // 2, N, A, P), A, P)
 | 
			
		||||
    if (n % 2) == 1:
 | 
			
		||||
        return jacobianAdd(
 | 
			
		||||
            jacobianDouble(jacobianMultiply((Xp, Yp, Zp), n // 2, N, A, P), A, P),
 | 
			
		||||
            (Xp, Yp, Zp),
 | 
			
		||||
            A,
 | 
			
		||||
            P,
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    raise ValueError("jacobian Multiply error")
 | 
			
		||||
@@ -10,6 +10,11 @@ classifiers = [
 | 
			
		||||
  "Programming Language :: Python :: Implementation :: CPython",
 | 
			
		||||
]
 | 
			
		||||
dynamic = ["version"]
 | 
			
		||||
authors = [{ name = "Liang Junyong", email = "2251250136@qq.com" }]
 | 
			
		||||
readme = "readme.md"
 | 
			
		||||
 | 
			
		||||
[project.urls]
 | 
			
		||||
Homepage = "https://git.mamahaha.work/sangge/ecc_rs"
 | 
			
		||||
 | 
			
		||||
[tool.maturin]
 | 
			
		||||
features = ["pyo3/extension-module"]
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										26
									
								
								readme.md
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								readme.md
									
									
									
									
									
								
							@@ -2,3 +2,29 @@
 | 
			
		||||
 | 
			
		||||
a simple rust implementation of SM2 binding for python.  
 | 
			
		||||
powered by pyo3.
 | 
			
		||||
 | 
			
		||||
useage
 | 
			
		||||
 | 
			
		||||
```python
 | 
			
		||||
import ecc_rs
 | 
			
		||||
 | 
			
		||||
point = tuple[int, int]
 | 
			
		||||
# Example point coordinates for P1 and P2 as tuples (x1, y1) and (x2, y2)
 | 
			
		||||
p1 = (
 | 
			
		||||
    1234567890123456789012345678901234567890123456789012345678901234,
 | 
			
		||||
    9876543210987654321098765432109876543210987654321098765432109876,
 | 
			
		||||
)
 | 
			
		||||
p2 = (
 | 
			
		||||
    2234567890123456789012345678901234567890123456789012345678901234,
 | 
			
		||||
    2876543210987654321098765432109876543210987654321098765432109876,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# Add the two points
 | 
			
		||||
result_x, result_y = ecc_rs.add(p1, p2)
 | 
			
		||||
print(f"Resulting Point: x = {result_x}, y = {result_y}")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
result = ecc_rs.multiply(p1, 2)
 | 
			
		||||
print(f"Resulting Point: x = {result[0]}, y = {result[1]}")
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user