# Arup Guha
# 11/9/2026

# Returns the RHS of the elliptic curve E(p,a,b)
def f(x,a,b,p):
    return (x**3 + a*x + b)%p

# Converts the plaintext which is numbits number of bits so
# less than (1<<numbits) to a point on the elliptic curve
# E(p,a,b). If it fails, returns null. You need to make sure
# that p has a few extra bits more than numbits.
def convert(plaintext,numbits,a,b,p):

    tryval = plaintext

    # We loop until we get out of range.
    while tryval < p:

        # Echo the value we are trying.
        print("Trying x =",tryval)
        
        # Get RHS of curve equation.
        c = f(tryval,a,b,p)

        print("Corresponding c =",c)

        # Euler test for Legendre symbols.
        expComp = pow(c, (p-1)//2, p)

        print("Legendre symbol is",expComp)

        # c is a quadratic residue, so we'll find a point!
        if expComp == 1:
            print("Returning point",tryval,pow(c, (p+1)//4, p))
            print()
            return (tryval, pow(c, (p+1)//4, p))

        # Increment if we fail.
        tryval += (1<<numbits)
        print("This failed. Next value to try is",tryval)
        print()

    print("Nothing worked!")
    print()
    return None

# Test for all possible plaintexts on curve E79(3,4).
for msg in range(16):
    pt = convert(msg, 4, 3, 4, 79)
    print(pt)
    print("----------------------------")









