def verify_dash_signature(generator, address, message, signature): compressed = False G = generator curve = G.curve() order = G.order() _a, _b, _p = curve.a(), curve.b(), curve.p() sig = base64.b64decode(signature) if len(sig) != 65: raise Exception("vmB", "Bad signature") hb = ord(sig[0]) r, s = map(str_to_long, [sig[1:33], sig[33:65]]) if hb < 27 or hb >= 35: raise Exception("vmB", "Bad first byte") if hb >= 31: compressed = True hb -= 4 recid = hb - 27 x = (r + (recid / 2) * order) % _p y2 = (pow(x, 3, _p) + _a * x + _b) % _p yomy = numbertheory.modular_sqrt(y2, _p) if (yomy - recid) % 2 == 0: y = yomy else: y = _p - yomy R = ellipticcurve.Point(curve, x, y, order) e = str_to_long(message) minus_e = -e % order inv_r = numbertheory.inverse_mod(r, order) Q = inv_r * (R * s + G * minus_e) key = Key(public_pair=(Q.x(), Q.y()), netcode='DASH') return key.address(use_uncompressed=not compressed) == address
def uncompress_pubkey(cpk): '''将压缩版公钥转换为完整版公钥''' p = 0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF a = -3 b = 0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B prefix = cpk[:2] x = int(cpk[2:],16) y_squared = (x**3 + a*x + b)%p y = modular_sqrt(y_squared, p) y_hex = '%x' % y if (1==int(y_hex[-1],16)%2 and '02' == prefix) or (0==int(y_hex[-1],16)%2 and '03' == prefix): y = p - y return '04%064x%064x' % (x,y)
def getFullPubKeyFromCompressed(x_str: str): prefix = x_str[0:2] print("prefix = %s" % (prefix)) x_str = x_str[2:] x = int(x_str, 16) print("x = \t\t%x" % (x)) p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F y_squared = (x**3 + 7) % p y = modular_sqrt(y_squared, p) y_str = "%x" % y print("y_str before = \t%s" % (y_str)) y_is_even = (int(y_str[-1], 16) % 2 == 0) if prefix == "02" and y_is_even == False or prefix == "03" and y_is_even == True: y = p - y y_str = "%x" % y if len(y_str) % 2 == 1: y_str = "0" + y_str print("y_str after = \t%s" % (y_str)) return "04" + x_str + y_str