#!/usr/bin/env python3

import matplotlib.pyplot as plt
from math import sqrt
from Cryptotools.Groups.point import Point
from Cryptotools.Groups.elliptic import Elliptic
import numpy as np

# https://course.ece.cmu.edu/~ece733/lectures/21-intro-ecc.pdf



a = 3
b = 7 
n = 97

E = Elliptic(n, a, b)
E.quadraticResidues()
Ep = E.pointsE()

# In cryptography, where is the public key and where is the private key on the elliptic curve ???

# Need to generate public/private key
## - First, we need to generate the private key. 
## This key must be a random value between d = {1, n - 1}
## - Second, for the public key Q, we need to compute Q = d * G
## - d a random value; Q = {x, y}
##
## For encrypting, 

G = Ep[1]
privkey = 48
pubkey = E.scalar(G, privkey)
if not E.pointExist(pubkey):
    raise ValueError("The public key is not in the Curve")

print(f"Private key: {privkey}")
print(f"Public key: ({pubkey.x}, {pubkey.y})")

# We can brute-force until we found Q
# We know the poing G, because it's in the domain parameters and it's public
# Same for the public key, Q which is public
# And we know the prime number
# Need to find the private key, k

# Here, we brute force until we match with the public key
for x in range(n):
    Q = E.scalar(G, x)
    if Q.x == pubkey.x and Q.y == pubkey.y:
        print(f"We found the private key {privkey}")
        break
