[Python] How do I edit my point addition function so that it can directly add G to itself k...

Discussão em 'Python' iniciado por Stack, Outubro 25, 2024.

  1. Stack

    Stack Membro Participativo

    In my python function, I have a point addition mathematical formula going that can properly calculate a public key from a private key integer. In order to calculate the public key properly, the program just needs to add G to itself k times so it can get to the coordinates of the correct public key.

    I just want a mathematical formula that can add G to itself k times instantly.

    The code I'm using so far is:

    def modinv(a, p):
    """Compute modular inverse of a mod p using extended Euclidean algorithm."""
    return pow(a, p-2, p)


    def point_add(P1, P2, p, k):
    """Add two points P1 and P2 on the elliptic curve over the field of size p.
    Optionally, add P2 k times to P1."""
    # If adding P2 k times, set initial result to P1
    result = P1

    # Loop to add P2 k times
    x1, y1 = result
    x2, y2 = P2

    if x1 == x2 and y1 != y2:
    return None # This means the points add up to infinity (inverse points)

    if x1 != x2:
    # Point addition
    m = (y2 - y1) * modinv(x2 - x1, p) % p
    else:
    # Point doubling
    m = (3 * x1 ** 2) * modinv(2 * y1, p) % p

    x3 = (m ** 2 - x1 - x2) % p
    y3 = (m * (x1 - x3) - y1) % p
    result = (x3, y3) # Update the result for the next addition

    return result


    As you can see in this, the point_add function is just using a formula to add G to itself but it only does it once. When I turn the function into a for loop, it is able to run the function again and again so it will calculate the new public key depending on how many times the function was ran.

    I want a mathematical formula that can do this for me, I want the mathematical formula to be able to run this function "k" times instantly (since the private key k is an integer), with the mathematical formula.

    I already tried running the function again and again but I don't want to do this. I want to manipulate the code so that it can be in the mathematical formula to multiply itself by how many times it needs.

    Continue reading...

Compartilhe esta Página