Tout jusqu'à la fonction dépouillement exclue.

This commit is contained in:
boyreau 2026-11-09 21:00:09 +01:00
commit 6e0e362e7e
2 changed files with 79 additions and 12 deletions

View file

@ -42,7 +42,7 @@ def Decrypt_cc(g: int, q: int, sk: int, c: (int, int)) -> int:
Returns:
The clear message.
"""
return c[1] // pow(c[0], sk, q)
return (c[1] * pow(c[0], -sk, q)) % q
def Encrypt_ce(g: int, q: int, pk: int, m: int) -> (int, int):
@ -85,20 +85,57 @@ def Add_cipher(c: list[(int, int)]) -> int:
return reduce(lambda x, y: x + y[1], c, 0)
def KeyGen_central(g: int, q: int, N: int) -> (int, int):
"""Generate N public/private key pairs.
def KeyGen_central(g: int, q: int, N: int) -> ([int], int):
"""Generate N private key fragments and a public key.
Args:
g: A generator for the group.
q: The order of a group.
N: The number of keypairs to generate.
N: The number of privkey fragment to generate.
Returns:
N public keys and 1 private key.
N private keys fragments and 1 public key.
"""
keys = []
K = 1
for i in range(N):
r = randint(1, q - 1)
keys.push(pow(g, r, q))
K = reduce(lambda x, y: x * y, keys, 1)
keys.push(K)
return keys
pk_i, sk_i = KeyGen(g, q)
keys.append(sk_i)
K = (K * pk_i) % q
return keys, K
def Decrypt_central_cc(g: int, q: int, k: [int], c: (int, int)) -> int:
"""Decipher a secret using multiple private key fragments.
Args:
g: A generator for the group.
q: The order of a group.
k: A list of private key fragments.
c: A cipher to decipher.
Returns:
The deciphered message in cleartext.
"""
Y = 1
for K_i in k:
Y = (Y * pow(c[0], K_i, q)) % q
return (c[1] * pow(Y, -1, q)) % q
def Decrypt_central_ce(g: int, q: int, k: [int], c: (int, int)) -> int:
"""Decipher a secret using multiple private key fragments (exponential).
Args:
g: A generator for the group.
q: The order of a group.
k: A list of private key fragments.
c: A cipher to decipher.
Returns:
The deciphered message in cleartext.
"""
Y = 1
for K_i in k:
Y = (Y * pow(c[0], K_i, q)) % q
M = (c[1] * pow(Y, -1, q)) % q
return discrete_log(q, M, g)