# ECC

## Elliptic Curve

An **elliptic curve** is a plane curve defined by an equation of the form:

$$
y^{2}=x^{3}+ax+b
$$

after a linear change of variables ($$a$$ and $$b$$ are real numbers). This type of equation is called a **Weierstrass equation**.

The definition of elliptic curve also requires that the curve is non-singular. Geometrically, this means that the graph has no cusps, self-intersections, or isolated points. Algebraically, this holds if and only if the **discriminant**:

$$
\Delta =-16(4a^{3}+27b^{2})
$$

is not equal to zero. (Although the factor −16 is irrelevant to whether or not the curve is non-singular, this definition of the discriminant is useful in a more advanced study of elliptic curves.)

The (real) graph of a non-singular curve has two components if its discriminant is positive, and one component if it is negative. For example, in the graphs shown in figure below, the discriminant in the first case is 64, and in the second case is −368:

![Graphs of curves y^2 = x^3 − x and y^2 = x^3 − x + 1](https://3988450783-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MWVjG_njKgBtvmnKaJh%2Fuploads%2FlSSUyjJH6gwpe8ud3Cb6%2Fimage.png?alt=media\&token=9b439bb9-ca72-4c40-8ccb-c5a2e146d57a)

## Arithmetics on Elliptic Curves

{% embed url="<https://cryptohack.org/challenges/ecc/>" %}
Elliptic Curves - CryptoHack
{% endembed %}

### Point Negation

#### Algorithm

The rule is: if $$P = (x\_P,y\_P)$$, then $$P + (x\_P, x\_P+y\_P) = O$$. In other word, $$-P = (x\_P, x\_P+y\_P)$$.

#### Implementation

In this challenge we are given $$P = (8045, 6936)$$, hence $$Q = -P = (8045, (8045 + 6936) \mod 9739) = (8045, 2803)$$

### Point Addition

#### Algorithm

1. If $$P = O$$, then $$P + Q = Q$$.
2. Otherwise, if $$Q = O$$, then $$P + Q = P$$.
3. Otherwise, write $$P = (x\_1, y\_1)$$ and $$Q = (x\_2, y\_2)$$.
4. If $$x\_1 = x\_2$$ and $$y\_1 = −y\_2$$, then $$P + Q = O$$.
5. Otherwise:
   * if $$P \neq Q$$: $$\lambda = (y\_2 - y\_1) / (x\_2 - x\_1)$$
   * if $$P = Q$$: $$\lambda = (3x\_1^2 + a) / 2y\_1$$
6. $$x\_3 = \lambda\_2 − x\_1 − x2$$ and $$y\_3 = \lambda(x\_1 − x\_3) − y\_1$$
7. $$P + Q = (x\_3, y\_3)$$

#### Implementation

```python
#!/usr/bin/env python3
from Crypto.Util.number import inverse

#--------Data--------#

a = 497
b = 1768
p = 9739

P = (493, 5564)
Q = (1539, 4742) 
R = (4403,5202)

#--------Addition--------#

def point_addition(P, Q):
    # Define zero
    O = (0, 0)

    # If P = O, then P + Q = Q
    if P == O:
        return Q
    # If Q = O, then P + Q = P
    if Q == O:
        return P

    # Otherwise, write P = (x1, y1) and Q = (x2, y2)
    x1, y1 = P[0], P[1]
    x2, y2 = Q[0], Q[1]

    # If x1 = x2 and y1 = -y2, then P + Q = O
    if x1 == x2 and y1 == -y2:
        return O

    # Otherwise, if P ≠ Q: λ = (y2 - y1) / (x2 - x1)
    if P != Q:
        lam = ((y2 - y1) * inverse(x2 - x1, p)) % p
    # If P = Q: λ = (3 * x1**2 + a) / 2 * y1
    else:
        lam = ((3 * x1**2 + a) * inverse(2 * y1, p)) % p

    # x3 = λ**2 - x1 - x2, y3 = λ *( x1 - x3) - y1
    x3 = (lam**2 - x1 - x2) % p
    y3 = (lam * (x1 - x3) - y1) % p

    # P + Q = (x3, y3)
    summation = (x3, y3)

    return summation

#--------Testing--------#

# X = (5274, 2841) 
# Y = (8669, 740)
# print(point_addition(X, Y))
# print(point_addition(X, X))

# S = P + P + Q + R
S = point_addition(point_addition(point_addition(P, P), Q), R)
print(S)
```

### Scalar Multiplication

#### Algorithm

Input: $$P$$ in $$E(F\_p)$$ and an integer $$n > 0$$.

1. Set $$Q = P$$ and $$R = O$$.
2. Loop while $$n > 0$$.
   * If $$n \equiv 1 \mod 2$$, set $$R = R + Q$$.
   * Set $$Q = 2Q$$ and $$n = \left\lfloor n/2 \right\rfloor$$.
   * If $$n > 0$$, continue with loop at Step 2.
3. Return the point $$R$$, which equals $$nP$$.

#### Implementation

```python
#!/usr/bin/env python3
from Crypto.Util.number import inverse

#--------Data--------#

a = 497
b = 1768
p = 9739

P = (2339, 2213)

#--------Functions--------#

def point_addition(P, Q):
    # Define zero
    O = (0, 0)

    # If P = O, then P + Q = Q
    if P == O:
        return Q
    # If Q = O, then P + Q = P
    if Q == O:
        return P

    # Otherwise, write P = (x1, y1) and Q = (x2, y2)
    x1, y1 = P[0], P[1]
    x2, y2 = Q[0], Q[1]

    # If x1 = x2 and y1 = -y2, then P + Q = O
    if x1 == x2 and y1 == -y2:
        return O

    # Otherwise, if P ≠ Q: λ = (y2 - y1) / (x2 - x1)
    if P != Q:
        lam = ((y2 - y1) * inverse(x2 - x1, p)) % p
    # If P = Q: λ = (3 * x1**2 + a) / 2 * y1
    else:
        lam = ((3 * x1**2 + a) * inverse(2 * y1, p)) % p

    # x3 = λ**2 - x1 - x2, y3 = λ *( x1 - x3) - y1
    x3 = (lam**2 - x1 - x2) % p
    y3 = (lam * (x1 - x3) - y1) % p

    # P + Q = (x3, y3)
    summation = (x3, y3)

    return summation

def scalar_multiplication(n, P):
    # Define zero
    O = (0, 0)
    # Set Q = P and R = O
    Q, R = P, O

    while n > 0:
        # If n ≡ 1 mod 2, set R = R + Q
        if n % 2 == 1:
            R = point_addition(R, Q)
        # Set Q = 2 Q and n = ⌊n/2⌋.
        Q = point_addition(Q, Q)
        n //= 2

    return R

#--------Testing--------#

# X = (5323, 5438)
# print(scalar_multiplication(1337, X))

# Q = 7863 P
print(scalar_multiplication(7863, P))
```

### Curves and Logs

### Algorithm

1. Alice and Bob agree on a curve $$E$$, a prime $$p$$ and a generator point $$G$$.
2. Alice generates a secret random integer $$n\_A$$ and calculates $$Q\_A = n\_AG$$.
3. Bob generates a secret random integer $$n\_B$$ and calculates $$Q\_B = n\_BG$$.
4. Alice sends Bob $$Q\_A$$*, and Bob sends Alice $$Q\_B$$. Due to the hardness of ECDLP, an onlooker Eve is unable to calculate*$$n{A/B}$$ in reasonable time.
5. Alice then calculates $$n\_AQ\_B$$, and Bob calculates $$n\_BQ\_A$$.

Due to the associativity of scalar multiplication, $$S = n\_AQ\_B = n\_BQ\_A$$. Alice and Bob can use $$S$$ as their shared secret.

### Implementation

```python
#!/usr/bin/env python3
from Crypto.Util.number import inverse
from hashlib import sha1

#--------Data--------#

a = 497
b = 1768
p = 9739

G = (1804,5368)
Q_A = (815, 3190)
n_B = 1829

#--------Functions--------#

def point_addition(P, Q):
    # Define zero
    O = (0, 0)

    # If P = O, then P + Q = Q
    if P == O:
        return Q
    # If Q = O, then P + Q = P
    if Q == O:
        return P

    # Otherwise, write P = (x1, y1) and Q = (x2, y2)
    x1, y1 = P[0], P[1]
    x2, y2 = Q[0], Q[1]

    # If x1 = x2 and y1 = -y2, then P + Q = O
    if x1 == x2 and y1 == -y2:
        return O

    # Otherwise, if P ≠ Q: λ = (y2 - y1) / (x2 - x1)
    if P != Q:
        lam = ((y2 - y1) * inverse(x2 - x1, p)) % p
    # If P = Q: λ = (3 * x1**2 + a) / 2 * y1
    else:
        lam = ((3 * x1**2 + a) * inverse(2 * y1, p)) % p

    # x3 = λ**2 - x1 - x2, y3 = λ *( x1 - x3) - y1
    x3 = (lam**2 - x1 - x2) % p
    y3 = (lam * (x1 - x3) - y1) % p

    # P + Q = (x3, y3)
    summation = (x3, y3)

    return summation

def scalar_multiplication(n, P):
    # Define zero
    O = (0, 0)
    # Set Q = P and R = O
    Q, R = P, O

    # Loop while n > 0
    while n > 0:
        # If n ≡ 1 mod 2, set R = R + Q
        if n % 2 == 1:
            R = point_addition(R, Q)
        # Set Q = 2 Q and n = ⌊n/2⌋.
        Q = point_addition(Q, Q)
        n //= 2

    return R

#--------ECDH--------#

S = scalar_multiplication(n_B, Q_A)
key = sha1(str(S[0]).encode()).hexdigest()

print(key)
```

## Lab

{% embed url="<https://cryptohack.org/challenges/ecc>" %}
Elliptic Curves - CryptoHack
{% endembed %}

## Reference

{% embed url="<https://en.wikipedia.org/wiki/Elliptic_curve>" %}
Elliptic curve - Wikipedia
{% endembed %}

{% embed url="<https://hgarrereyn.gitbooks.io/th3g3ntl3man-ctf-writeups/content/2017/picoCTF_2017/problems/cryptography/ECC2/ECC2.html>" %}
picoCTF 2017 ECC 2 - 200 (Cryptography) Writeup
{% endembed %}
