El Gamal Cryptosystem Proof, Step-by-Step Explanation Needed. I feel like I don't understand what I have on this proof Can someone provide a good and thorough explanation of the El Gamal proof? Basically, I need a step-by-step breakdown of what is happening at each important part in the algorithm.

I haven't been able to find any good notes on El Gamala that explain the proof in an easy to understand format.

Here is the proof in this image:
First, we should make clear what we're proving here. The derivation you're showing is part of a proof of correctness of ElGamal encryption, not security.

Perfect correctness (also referred to as completeness) of a public key encryption scheme is defined as follows.

Let $(\mathsf{Gen},\mathsf{Enc},\mathsf{Dec})$ be a public key encryption scheme. The scheme is said to be perfectly correct if it holds that for any security parameter $n\in\mathbb{N}$, any key pair $(\mathsf{ek},\mathsf{dk})\gets\mathsf{Gen}(1^n)$, any message $m$ from the message space, and any ciphertext $c \gets \mathsf{Enc}(\mathsf{ek},m)$ it holds that $\mathsf{Dec}(\mathsf{dk},c)=m$.

To see whether ElGamal encryption is correct, we first recall the definition of ElGamal encryption. I will try to follow the notation in your notes as close as possible. From the notes it's not clear what kind of group is being used. But $e_1$ is a generator of some subgroup of $\mathbb{Z}_p^*$. Let $q$ be the order of that subgroup. (If $e_1$ is a generator of $\mathbb{Z}_p^*$, then $q=p-1$, if we are in a safe prime setting, then $q=(p-1)/2$ and prime.

\begin{align}
&amp;\mathsf{Gen}(1^n) &amp;&amp; \mathsf{Enc}(e_2,P) &amp;&amp; \mathsf{Dec}(d,(c_1,c_2))\\
&amp;d\gets\mathbb{Z}_q&amp;&amp;r \gets \mathbb{Z}_q&amp;&amp;P' := c_2\cdot (c_1^d)^{-1} \bmod p\\
&amp;e_2 := e_1^d \bmod p&amp;&amp;c_1:= e_1^r\bmod p&amp;&amp;\text{return }P'\\
&amp;\text{return } (e_2,d)&amp;&amp;c_2 := P\cdot e_2^r\bmod p&amp;\\
&amp;&amp;&amp;\text{return } (c_1,c_2)&amp;&amp;\\
\end{align}

To verify that the scheme is correct, we need to verify that for any choice of key pair, and message it always holds that $\mathsf{Dec}(d,\mathsf{Enc}(e_2,P)) = P$. This is where the derivation you're looking at comes in.

\begin{align}
 P' =&amp; c_2\cdot (c_1^d)^{-1} \bmod p \tag{1}\\
=&amp; c_2\cdot (e_1^{rd})^{-1} \bmod p\tag{2}\\
=&amp; P\cdot e_2^r\cdot (e_1^{rd})^{-1} \bmod p\tag{3}\\
=&amp; P\cdot e_1^{rd}\cdot (e_1^{rd})^{-1} \bmod p\tag{4}\\
=&amp; P\cdot e_1^{rd}\cdot e_1^{-rd} \bmod p\tag{5}\\
=&amp; P\cdot e_1^{rd-rd} \bmod p\tag{6}\\
=&amp; P \bmod p\tag{7}\\
\end{align}

Now let's go through this line by line.


In line (1) we simply have the definition of the decryption algorithm.
In line (2) we use the definition of $c_1 = e_1^r\bmod p$ from the encryption algorithm and replace $c_1$ by its definition.
In line (3) we do exactly the same with $c_2$ and replace it with its definition $c_2 := P\cdot e_2^r\bmod p$ from the encryption algorithm.
In line (4) we now look at the key generation algorithm and replace $e_2$ by its definition from there $e_2 := e_1^d \bmod p$. Note that we have not changed anything. We have simply replaced the variables $c_1,c_2,e_2$ by their respective definitions.
In line (5) we use a basic rule of exponentiation, namely that $(x^a)^b = x^{a\cdot b}$, therefore $(e_1^{rd})^{-1} = e_1^{-rd}$.
In line (6) we again use a basic rule of exponentiation, namely that $x^a\cdot x^b = x^{a+ b}$, therefore $e_1^{rd}\cdot e_1^{-rd} = e_1^{rd-rd}$.
Since $rd-rd=0$ and anything raised to the $0$th power equals the identity, we are left with $P\cdot 1 \bmod p$ and thereby, since $1$ is the identity of the group with $P$ in line (7).


Since this derivation makes no assumptions whatsoever about $P,d,e_2,c_1$ and $c_2$, besides that they were generated according to the above definition of ElGamal encryption, it holds for any choice of key-pair and any plaintext $P$.

The derivation thus shows that decryption always works and results in the same plaintext that was encrypted, thus showing that the encryption scheme is in fact correct.

What it does not show is anything about the security of the scheme.