@@ -301,6 +301,62 @@ def homomorphic_multiplicative_scheme(public_key, private_key, c_1, c_2, print_m
301301 return m
302302
303303
304+ # ElGamal homomorphic ciphertext extension
305+ def homomorphic_ciphertext_extension (public_key , private_key , m_1 , a_b , print_matrix = False ,
306+ print_linear_factorization = True ):
307+ print (tabulate ([['Homomorphe Erweiterung des Geheimtextes' ]], tablefmt = 'fancy_grid' ))
308+
309+ # Unpack both keys into its components
310+ p , g , e = public_key
311+ p_v , d = private_key
312+
313+ # Unpack the combined ciphertext into its components
314+ a , b = a_b
315+
316+ # The value of p must be identical in both keys
317+ if p != p_v :
318+ print (f'Die Variablen p = { p } und p_v = { p_v } müssen identisch sein.' )
319+ return - 1
320+
321+ # Calculation of m
322+ a_d = (a ** d ) % p
323+ a_i = modulo_inverse_multiplicative .mim (p , a_d , print_matrix , print_linear_factorization , 1 )
324+ m = (a_i * b ) % p
325+
326+ # Calculation of m_2
327+ m_1_i = modulo_inverse_multiplicative .mim (p , m_1 , print_matrix , print_linear_factorization , 2 )
328+ m_2 = (m * m_1_i ) % p
329+
330+ # Calculation path output
331+ print (
332+ f'Gegeben sind K(pub) = {{p, g, e}} = {{{ p } , { g } , { e } }} und K(priv) = {{p, d}} = {{{ p_v } , { d } }} mit dem aus '
333+ f'Geheimtext 1 und 2 erweiterten Geheimtext a_b = {{a, b}} = {{{ a } , { b } }}. Ebenfalls bekannt ist der zu '
334+ f'Geheimtext 1 zugehörige Klartext m_1 = { m_1 } . Durch die Umkehrung der Geheimtext-Erweiterung soll nun der '
335+ f'Klartext m_2 ermittelt werden.' , end = '\n \n ' )
336+ print (
337+ f'Der zum erweiterten Geheimtext (a, b) gehörende Klartext m ergibt sich aus der Gleichung a^d * m = b mod p '
338+ f'zu:\n '
339+ f'm = b * (a^d)^-1 mod p\n '
340+ f'm = { b } * ({ a } ^{ d } )^-1 mod { p } \n '
341+ f'm = { b } * { a_d } ^-1 mod { p } \n '
342+ f'<AUXILIARY 1>Achtung: Die Namen der Variablen können abweichen!</AUXILIARY 1>\n '
343+ f'm = { b } * { a_i } mod { p } \n '
344+ f'm = { m } ' , end = '\n \n ' )
345+ print (
346+ f'Der Klartext m_2, welcher zur Erweiterung des Klartexts m_1 verwendet wurde, ergibt sich aus:\n '
347+ f'm_2 = m * m_1^-1 mod p\n '
348+ f'm_2 = { m } * { m_1 } ^-1 mod { p } \n '
349+ f'<AUXILIARY 2>Achtung: Die Namen der Variablen können abweichen!</AUXILIARY 2>\n '
350+ f'm_2 = { m } * { m_1_i } mod { p } \n '
351+ f'm_2 = { m_2 } ' , end = '\n \n ' )
352+ print (
353+ f'Verifikation:\n '
354+ f'm = m_1 * m_2 mod p\n '
355+ f'{ m } = { m_1 } * { m_2 } mod { p } \n '
356+ f'{ m } = { (m_1 * m_2 ) % p } ' , end = '\n \n ' )
357+ return m_2
358+
359+
304360# ElGamal homomorphic multiplicative decryption
305361def homomorphic_multiplicative_decryption (public_key , private_key , m_1 , c_1 , c_2 , print_matrix = False ,
306362 print_linear_factorization = True ):
0 commit comments