# ElGamal Encryption

## ElGamal Encryption

Given a **cyclic group** $$\mathbb{G}$$ of order $$q$$ and its **generator** $$g$$, the encryption and decryption process for a message $$M$$ is defined as follows.

### **ElGamal Encryption Process**

#### $$\mathsf{ElGamal.Setup}() \rightarrow (x, y)$$**:**

* Select a **secret key** $$x$$ randomly from the range $$1 \leq x < q-1$$.
* Compute the **public key** $$y$$ as follows:

$$
y = g^x
$$

#### $$\mathsf{ElGamal.Encrypt}(M, y) \rightarrow (c\_1, c\_2)$$:

* Map the message $$M$$ to an element $$m \in \mathbb{G}$$.
* Select a **random value** $$k$$ from the range $$1 \leq k < q-1$$.
* Compute the **shared secret** $$s$$:

$$
s = y^k
$$

* Generate the ciphertext $$(c\_1, c\_2)$$ as follows:

$$
c\_1 = g^k, \quad c\_2 = m \cdot s
$$

#### $$\mathsf{ElGamal.Decrypt}(c\_1, c\_2, x) \rightarrow M$$:

* Recover the shared secret $$s$$ (This can only be done by the owner of $$x$$):&#x20;

$$
s = c\_1^x = (g^k)^x = (g^x)^k = y^k
$$

* Retrieve the original message $$m$$:

$$
m = c\_2 \cdot s^{-1} = (m \cdot s) \cdot s^{-1}
$$

* Convert $$m$$ back to the message $$M$$.

## References

* <https://en.wikipedia.org/wiki/ElGamal_encryption>

> Written by [Ryan Kim](mailto:undefined) of Fractalyze


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fractalyze.gitbook.io/intro/primitives/encryption-scheme/elgamal-encryption.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
