Post
Topic
Board Bitcoin Discussion
Re: == Bitcoin challenge transaction: ~100 BTC total bounty to solvers! ==UPDATED==
by
supika
on 08/02/2020, 17:45:04 UTC
I didn't want to say it here by either delete your video or blur out your code. If really don't want to share it yet.



Doesn't have to delete any video. The first part of the script is useless/generic.

import collections
import time
import gmpy2
import random

EllipticCurve = collections.namedtuple('EllipticCurve', 'name p a b g n h')

curve = EllipticCurve(
    'secp256k1',
    p=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F,
    a=0,
    b=7,
    g=(0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8),
    n=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141,
    h=1,
)

def pubkey_point(pubkey):

    if len(pubkey) == 130:
        hexpubX = pubkey[2:66]
        hexpubY = pubkey[67:130]
        X = int(hexpubX,16)
        Y = int(hexpubY,16)
        return (X, Y)
    else:
        hexpub = pubkey[2:66]
        prefix = pubkey[1:2]
        X = int(hexpub,16)
        Y = pow(int(X**3+7), int((curve.p+1)//4), int(curve.p))
        if Y%2 !=0:
            if int(prefix)%2 !=0:
                Y = Y
            else:
                Y = curve.p - Y
        else:
            if int(prefix)%2 ==0:
                Y = Y
            else:
                Y = curve.p - Y
        return (X, Y)

def point_neg(point):

    if point is None:
        return None
    x, y = point
    result = (x, -y % curve.p)
    return result

def point_add(point1, point2):

    if point1 is None:
        return point2
    if point2 is None:
        return point1
    x1, y1 = point1
    x2, y2 = point2
   
    if x1 == x2 and y1 != y2:
        return None
    if x1 == x2:
        #m = (3 * x1 *x1 + curve.a) * inverse_mod(2 * y1, curve.p)
        m = (3 * x1 *x1 + curve.a) * gmpy2.invert(2 * y1, curve.p)
    else:
        m = (y1 - y2) * gmpy2.invert(x1-x2, curve.p)
        #m = (y1 - y2) * inverse_mod(x1-x2, curve.p)
    x3 = m * m - x1 - x2
    y3 = y1 + m * (x3 - x1)
    result = (x3 % curve.p, -y3 % curve.p)
    return result

def scalar_mult(k, point):
    if k % curve.n == 0 or point is None:
        return None
    if k < 0:
        return scalar_mult(-k, point_neg(point))
    result = None
    power = ' '
    addend = point
   
    while k:
        if k & 1:
            result = point_add(result, addend)
        addend = point_add(addend, addend)
        k >>= 1
    return result

def make_keypair(intkey, point=curve.g):
    public_key = scalar_mult(intkey, point)   #random.randrange(1, curve.n)
    return public_key

if __name__ == '__main__':
   
    start = time.time()
    target_bit = 45
   
    while True:
        tb = random.randrange(1, 2**target_bit)
        if tb.bit_length()            pass
        else:
            target_pnt = make_keypair(tb)
            break
    print ('Target random:', tb)
    print ('Target random hex:', hex(tb))
   
    target_pnt = '026ecabd2d22fdb737be21975ce9a694e108eb94f3649c586cc7461c8abf5da71a'
    target_pnt = pubkey_point(target_pnt)