single data works
This commit is contained in:
@@ -9,7 +9,7 @@ class SentinelSystemProvider:
|
||||
"""生成全新的系统公私钥对"""
|
||||
private_key = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=2048
|
||||
key_size=4096
|
||||
)
|
||||
public_key = private_key.public_key()
|
||||
|
||||
|
||||
@@ -3,6 +3,9 @@ import secrets
|
||||
from mnemonic import Mnemonic # 仅用于标准的助记词转换
|
||||
|
||||
class SentinelKeyEngine:
|
||||
# 使用第 13 个梅森素数 (2^521 - 1),远大于 128-bit 熵,确保有限域安全
|
||||
PRIME = 2**521 - 1
|
||||
|
||||
def __init__(self):
|
||||
self.mnemo = Mnemonic("english")
|
||||
|
||||
@@ -18,18 +21,19 @@ class SentinelKeyEngine:
|
||||
"""
|
||||
2. SSS (3,2) 门限分片逻辑
|
||||
公式: f(x) = S + a*x (直线方程,S为秘密,a为随机斜率)
|
||||
我们将秘密 S 分成 3 份,任选 2 份即可恢复
|
||||
我们将秘密 S 分成 3 份,任选 2 份即可恢复。
|
||||
注意:必须在有限域 GF(PRIME) 下进行运算以保证完善保密性。
|
||||
"""
|
||||
# 将熵转换为大整数
|
||||
secret_int = int.from_bytes(entropy, 'big')
|
||||
|
||||
# 生成一个随机系数 a (安全性需与秘密强度一致)
|
||||
# 这里使用 secrets 保证加密级随机
|
||||
a = int.from_bytes(secrets.token_bytes(16), 'big')
|
||||
# a 必须在 [0, PRIME-1] 范围内
|
||||
a = secrets.randbelow(self.PRIME)
|
||||
|
||||
# 定义 3 个点: x=1, x=2, x=3
|
||||
# Share = (x, f(x))
|
||||
def f(x): return secret_int + a * x
|
||||
def f(x): return (secret_int + a * x) % self.PRIME
|
||||
|
||||
share1 = (1, f(1)) # 手机分片
|
||||
share2 = (2, f(2)) # 云端分片
|
||||
@@ -42,15 +46,31 @@ class SentinelKeyEngine:
|
||||
3. 恢复逻辑:拉格朗日插值还原
|
||||
已知 (x1, y1) 和 (x2, y2),求 f(0) 即秘密 S
|
||||
公式: S = (x2*y1 - x1*y2) / (x2 - x1)
|
||||
在有限域下,除法变为乘以模逆: S = (x2*y1 - x1*y2) * (x2 - x1)^-1 mod P
|
||||
"""
|
||||
x1, y1 = share_a
|
||||
x2, y2 = share_b
|
||||
|
||||
# 计算分子
|
||||
numerator = (x2 * y1 - x1 * y2) % self.PRIME
|
||||
# 计算分母的模逆 (x2 - x1)
|
||||
denominator = (x2 - x1) % self.PRIME
|
||||
inv_denominator = pow(denominator, -1, self.PRIME)
|
||||
|
||||
# 还原常数项 S
|
||||
secret_int = (x2 * y1 - x1 * y2) // (x2 - x1)
|
||||
secret_int = (numerator * inv_denominator) % self.PRIME
|
||||
|
||||
# 转回字节并生成助记词
|
||||
recovered_entropy = secret_int.to_bytes(16, 'big')
|
||||
# 注意:secret_int 可能略小于 16 字节(高位为0),需要补齐
|
||||
# 但由于 entropy 原始就是 16 字节,这里直接转换即可
|
||||
try:
|
||||
recovered_entropy = secret_int.to_bytes(16, 'big')
|
||||
except OverflowError:
|
||||
# 理论上不应发生,除非计算出的 secret_int 大于 128 bit (即原始 entropy 大于 128 bit)
|
||||
# 这里为了健壮性,如果原始 entropy 是 16 字节,这里应该也是。
|
||||
# 如果 PRIME 很大,secret_int 还是原来的值。
|
||||
recovered_entropy = secret_int.to_bytes((secret_int.bit_length() + 7) // 8, 'big')
|
||||
|
||||
return self.mnemo.to_mnemonic(recovered_entropy)
|
||||
if __name__ == "__main__":
|
||||
# --- Sentinel 协议业务流程模拟 ---
|
||||
|
||||
@@ -5,9 +5,10 @@ from Crypto.Protocol.KDF import PBKDF2
|
||||
from Crypto.Util.Padding import pad, unpad
|
||||
|
||||
class SentinelVault:
|
||||
def __init__(self, salt=b'Sentinel_Salt_2026'): # 固定的盐值,用于增加派生强度
|
||||
def __init__(self, salt=None):
|
||||
self.mnemo = Mnemonic("english")
|
||||
self.salt = salt
|
||||
# 默认盐值仅用于演示,生产环境建议每个用户随机生成并存储
|
||||
self.salt = salt if salt else b'Sentinel_Salt_2026'
|
||||
|
||||
def derive_key(self, mnemonic_phrase):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user