Python Forum
Support Python loop ECDSA Secp256k1
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Support Python loop ECDSA Secp256k1
#1
Dear all,

I am pretty new to Python code, so please be nice with me: I have started to modify an existing code on ECDSA the secp256k1 to build on it and I am struggling with loop issues- tried with while True, but the loop is not working. Tried to define function but failed miserably Huh

my goal: input a given public key in Hex. Input a private key in decimal minus a value - see example in code below:
Run the main stack code of ECDSA secp256k1

if output address defined in the code as "Add" = to my input, i would like the code to break and return the privKey found, if not, code need to increment private key by +1 till the Add = my input

I used on below code
privKey= i = 110061473103979833750000009551542796534417160777537709002846589079070443665740 (deliberately -3 for the test)
and its pair public Key is 1b6c96e0574c18cc0e598c3b231aeab997ee0e9f = input()

my question is how to create this loop on below code to have a kind of iteration till the Add calculated is equal to my input and then break and return private key ? here expected: 110061473103979833750000009551542796534417160777537709002846589079070443665743

Thank you a lot in advance for your guidance & advise.

Cheers, Smile

Code stack :
   input() # my input to compare Add with = 1b6c96e0574c18cc0e598c3b231aeab997ee0e9f

    # if input not = to Add = output of code stack, then increment privKey +=1 till Add=input() then return privKey

#main code stack
        i = 110061473103979833750000009551542796534417160777537709002846589079070443665740
        privKey = i  # replace with any private key

        Pcurve = 2 ** 256 - 2 ** 32 - 2 ** 9 - 2 ** 8 - 2 ** 7 - 2 ** 6 - 2 ** 4 - 1  # The proven prime
        N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141  # Number of points in the field
        Acurve = 0;
        Bcurve = 7  # These two defines the elliptic curve. y^2 = x^3 + Acurve * x + Bcurve
        Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240
        Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424
        GPoint = (Gx, Gy)  # This is our generator point. Trillions of dif ones possible

        # Individual Transaction/Personal Information


        def modinv(a, n=Pcurve):  # Extended Euclidean Algorithm/'division' in elliptic curves
            lm, hm = 1, 0
            low, high = a % n, n
            while low > 1:
                ratio = high / low
                nm, new = hm - lm * ratio, high - low * ratio
                lm, low, hm, high = nm, new, lm, low
            return lm % n


        def ECadd(a, b):  # Not true addition, invented for EC. Could have been called anything.
            LamAdd = ((b[1] - a[1]) * modinv(b[0] - a[0], Pcurve)) % Pcurve
            x = (LamAdd * LamAdd - a[0] - b[0]) % Pcurve
            y = (LamAdd * (a[0] - x) - a[1]) % Pcurve
            return (x, y)


        def ECdouble(a):  # This is called point doubling, also invented for EC.
            Lam = ((3 * a[0] * a[0] + Acurve) * modinv((2 * a[1]), Pcurve)) % Pcurve
            x = (Lam * Lam - 2 * a[0]) % Pcurve
            y = (Lam * (a[0] - x) - a[1]) % Pcurve
            return (x, y)


        def EccMultiply(GenPoint, ScalarHex):  # Double & add. Not true multiplication
            if ScalarHex == 0 or ScalarHex >= N: raise Exception("Invalid Scalar/Private Key")
            ScalarBin = str(bin(ScalarHex))[2:]
            Q = GenPoint
            for i in range(1, len(ScalarBin)):  # This is invented EC multiplication.
                Q = ECdouble(Q);  # print "DUB", Q[0]; print
                if ScalarBin[i] == "1":
                    Q = ECadd(Q, GenPoint);  # print "ADD", Q[0]; print
            return (Q)


        print;
        print "******* Public Key Generation *********";
        print
        PublicKey = EccMultiply(GPoint, privKey)
        print "the private key:";
        print privKey;
        print
        print "the uncompressed public key (not address):";
        print PublicKey;
        print
        print "the uncompressed public key (HEX):";
        print "04" + "%064x" % PublicKey[0] + "%064x" % PublicKey[1];
        print;
        print "the official Public Key - compressed:";
        if PublicKey[1] % 2 == 1:  # If the Y value for the Public Key is odd.
            print "03" + str(hex(PublicKey[0])[2:-1]).zfill(64)
        else:  # Or else, if the Y value is even.
            print "02" + str(hex(PublicKey[0])[2:-1]).zfill(64)

        PBK = str("04" + "%064x" % PublicKey[0] + "%064x" % PublicKey[1])

        data1 = unhexlify(PBK)
        hash1 = hashlib.sha256(data1).hexdigest()

        print(hash1)

        data2 = unhexlify(hash1)
        hash2 =hashlib.new('RIPEMD160', data2).hexdigest()

        Add = hash2
        print(Add)
Max
buran write Mar-22-2021, 08:34 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [cryptography.io] How to convert DER signature to ECDSA fstefanov 1 2,986 Jul-04-2019, 08:59 AM
Last Post: fstefanov
  is this possible in python? please support anna 3 3,258 Apr-04-2018, 01:16 PM
Last Post: anna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020