[Python] How do I directly add G to itself K times in a summation to calculate a public key...

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

  1. Stack

    Stack Membro Participativo

    I have a small script in python that lets me calculate an ethereum public key from a private key integer. I want to use a summation to directly add G to itself k times (basic multiplication) so that I can generate a public key from this.

    I know that there is something called the double and add method that calculates a public key from a private key and this is how it cryptograpically normally works, but I already tried doing this manually by multiplying g k times and it still works fine like this. I would like to do this but in a summation so it can do it instantly for 1 private key value.

    The code I'm using so far is:

    def scalar_multiply(k, G, p):
    """Multiply point G by scalar k by directly adding G k times."""
    result = None # This will represent the point at infinity

    # Add G to result k times
    for _ in range(k):
    result = point_add(result, G, p)


    #The code below is what I want you guys to change for me. It's based off of the for loop above.
    y = symbols('y')
    #result = G
    result2 = summation(point_add(None,G,p),(n,0,k)) # just a placeholder, someone will have to fix this

    return result


    As you can see in my code this is the formula that properly calculates a public key from a private key. The point_add formula just sets it up for me but the scalar_multiply function is where the problem I'm having is.

    I want you guys to look at how the scalar_multiply function is before the #result = G and create a summation with sympy based on this so I can use it in my code. Don't worry about the unknown code like the point_add function. So basically just show me how I can use a summation instead of a for loop for k using the point_add function.

    Just a note. When I try to use result2 and evaluate it with sympy it returns the same public key each time. There's something wrong with the summation code.

    Continue reading...

Compartilhe esta Página