1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #这份是gpto1写的,不过是基于椭圆曲线的 from ecdsa import SECP256k1, SigningKey, VerifyingKey import hashlib
# 曲线参数 curve = SECP256k1 G = curve.generator # 基点 g n = curve.order # 阶
# 私钥 x(随机生成) x_sk = SigningKey.generate(curve=curve) x = x_sk.privkey.secret_multiplier # x 的数值 # 公钥 h = g^x h_vk = x_sk.verifying_key h = h_vk.pubkey.point
def nizkpok_prove(x): # 证明者生成随机数 r r_sk = SigningKey.generate(curve=curve) r = r_sk.privkey.secret_multiplier # 计算承诺 t = g^r t = r * G # 计算挑战 e = Hash(g || h || t) e = hashlib.sha256() e.update(int(G.x()).to_bytes(32, 'big') + int(G.y()).to_bytes(32, 'big')) e.update(int(h.x()).to_bytes(32, 'big') + int(h.y()).to_bytes(32, 'big')) e.update(int(t.x()).to_bytes(32, 'big') + int(t.y()).to_bytes(32, 'big')) e_int = int(e.hexdigest(), 16) % n # 计算响应 s = r + e * x mod n s = (r + e_int * x) % n return (e_int, s)
def nizkpok_verify(h, proof): e_int, s = proof # 计算 t' = g^s + (-h^e) sG = s * G eH = e_int * h # 获取 eH 的负元 neg_eH = (n - 1) * eH # 计算 t' = sG + (-eH) t_prime = sG + neg_eH # 重新计算挑战 e' = Hash(g || h || t') e_prime = hashlib.sha256() e_prime.update(int(G.x()).to_bytes(32, 'big') + int(G.y()).to_bytes(32, 'big')) e_prime.update(int(h.x()).to_bytes(32, 'big') + int(h.y()).to_bytes(32, 'big')) e_prime.update(int(t_prime.x()).to_bytes(32, 'big') + int(t_prime.y()).to_bytes(32, 'big')) e_prime_int = int(e_prime.hexdigest(), 16) % n # 验证 e 是否等于 e' return e_int == e_prime_int
# 生成证明 proof = nizkpok_prove(x)
# 验证证明 is_valid = nizkpok_verify(h, proof) print("证明是否有效:", is_valid)
|