示例#1
0
def verify_recover_secp256k1_bc_native(signature,
                                       req,
                                       hasher=coincurve.utils.sha256,
                                       context=GLOBAL_CONTEXT):
    # Compact
    native_rec_sig = ffi.new("secp256k1_ecdsa_recoverable_signature *")
    raw_sig, recovery_id = signature[:64], coincurve.utils.bytes_to_int(
        signature[64:])
    lib.secp256k1_ecdsa_recoverable_signature_parse_compact(
        context.ctx, native_rec_sig, raw_sig, recovery_id)

    # Recover public key
    native_public_key = ffi.new("secp256k1_pubkey *")
    msg_hash = hasher(req) if hasher is not None else req
    lib.secp256k1_ecdsa_recover(context.ctx, native_public_key, native_rec_sig,
                                msg_hash)

    # Convert
    native_standard_sig = ffi.new("secp256k1_ecdsa_signature *")
    lib.secp256k1_ecdsa_recoverable_signature_convert(context.ctx,
                                                      native_standard_sig,
                                                      native_rec_sig)

    # Verify
    ret = lib.secp256k1_ecdsa_verify(context.ctx, native_standard_sig,
                                     msg_hash, native_public_key)
示例#2
0
def verify_recover_secp256k1_bc(signature,
                                req,
                                hasher=coincurve.utils.sha256,
                                context=GLOBAL_CONTEXT):
    msg_hash = hasher(req) if hasher is not None else req
    rec_sig = coincurve.ecdsa.deserialize_recoverable(signature)
    public_key = coincurve.PublicKey(coincurve.ecdsa.recover(req, rec_sig))
    n_sig = coincurve.ecdsa.recoverable_convert(rec_sig)

    if not lib.secp256k1_ecdsa_verify(context.ctx, n_sig, msg_hash,
                                      public_key.public_key):
        raise RuntimeError("Failed to verify SECP256K1 bitcoin signature")