diff --git a/ElGamal.py b/ElGamal.py index c00a6ad..73071d0 100644 --- a/ElGamal.py +++ b/ElGamal.py @@ -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) diff --git a/test_ElGamal.py b/test_ElGamal.py index 9cd78a6..87af361 100644 --- a/test_ElGamal.py +++ b/test_ElGamal.py @@ -1,5 +1,16 @@ -from ElGamal import KeyGen, Encrypt_cc, Decrypt_cc, Encrypt_ce, Decrypt_ce, Add_cipher -from sympy import randprime, primitive_root +from sympy import primitive_root, randprime + +from ElGamal import ( + Add_cipher, + Decrypt_cc, + Decrypt_ce, + Encrypt_cc, + Encrypt_ce, + KeyGen, + KeyGen_central, + Decrypt_central_cc, + Decrypt_central_ce, +) if __name__ == "__main__": # On prend le groupe de (randprime - 1) @@ -25,3 +36,22 @@ if __name__ == "__main__": ciphers = [(1, 1), (1, 2), (1, 3)] assert Add_cipher(ciphers) == 1 + 2 + 3 + + # Le chiffrement par clé publique est déchiffrable par la clé privée reconstituée. + privkeys, pubkey = KeyGen_central(gen, ordre, 2) + clear = 12 + print(f"Clés privées : {privkeys}") + print(f"Clé publique : {pubkey}") + crypt = Encrypt_cc(gen, ordre, pubkey, clear) + assert clear == Decrypt_central_cc(gen, ordre, privkeys, crypt) + + assert clear != Decrypt_cc( + gen, ordre, privkeys[0], Encrypt_cc(gen, ordre, pubkey, clear) + ) + assert clear != Decrypt_cc( + gen, ordre, privkeys[1], Encrypt_cc(gen, ordre, pubkey, clear) + ) + + crypt = Encrypt_ce(gen, ordre, pubkey, clear) + assert clear == Decrypt_central_ce(gen, ordre, privkeys, crypt) + assert clear != Decrypt_central_ce(gen, ordre, [privkeys[0]], crypt)