expose ec_public_key_strict_verify to verify non-hybrid pubkeys
What changed, and why it matters
This commit adds a new public function that lets callers check whether a Bitcoin-style public key is valid and is not an old, rarely-used 'hybrid' key format. It does not change existing behavior; it only exposes a new, stricter verification option. The change itself is defensive and not a vulnerability fix, though it may help downstream code reject hybrid keys that some modern protocols consider invalid.
No immediate action required. Treat as a routine defensive API addition. Downstream projects that need to reject hybrid public keys should migrate to wally_ec_public_key_strict_verify where appropriate. Review whether any existing internal callers should switch to the strict verifier, but that is outside the scope of this commit.
Security signals we found
New strict public-key validation API rejecting hybrid (0x06/0x07 prefixed uncompressed) public keys
Hybrid keys are still accepted by the pre-existing wally_ec_public_key_verify; only the new strict call rejects them
No buffer overflow, memory corruption, or cryptographic flaw visible in the diff
Commit message frames the change as enabling modern contexts to exclude hybrid keys
Evidence from the diff
The patch introduces wally_ec_public_key_strict_verify and bindings for C++, Java, Python, WASM/JS, and TypeScript. The C implementation first calls the existing wally_ec_public_key_verify, then additionally rejects uncompressed keys whose first byte is not 0x04 (i.e., hybrid keys with 0x06/0x07 prefixes). Tests confirm that previously accepted hybrid keys now return WALLY_EINVAL under the strict verifier. No existing function is modified; this is purely additive API surface.
Changed components
src/sign.cinclude/wally_crypto.hinclude/wally.hppsrc/swig_java/swig.isrc/test/test_sign.pysrc/test/util.pysrc/wasm_package/src/functions.jssrc/wasm_package/src/index.d.tstools/wasm_exports.shInspect captured patch +39 / −0
### include/wally.hpp
@@ -767,6 +767,12 @@ inline int ec_public_key_negate(const PUB_KEY& pub_key, BYTES_OUT& bytes_out) {
return detail::check_ret(__FUNCTION__, ret);
}
+template <class PUB_KEY>
+inline bool ec_public_key_strict_verify(const PUB_KEY& pub_key) {
+ int ret = ::wally_ec_public_key_strict_verify(pub_key.data(), pub_key.size());
+ return ret == WALLY_OK;
+}
+
template <class PUB_KEY, class TWEAK, class BYTES_OUT>
inline int ec_public_key_tweak(const PUB_KEY& pub_key, const TWEAK& tweak, BYTES_OUT& bytes_out) {
int ret = ::wally_ec_public_key_tweak(pub_key.data(), pub_key.size(), tweak.data(), tweak.size(), bytes_out.data(), bytes_out.size());
### include/wally_crypto.h
@@ -402,6 +402,17 @@ WALLY_CORE_API int wally_ec_public_key_verify(
const unsigned char *pub_key,
size_t pub_key_len);
+/**
+ * Verify that a public key is valid, and not a hybrid public key.
+ *
+ * :param pub_key: The public key to validate.
+ * :param pub_key_len: The length of ``pub_key`` in bytes. Must be
+ *| `EC_PUBLIC_KEY_LEN` or `EC_PUBLIC_KEY_UNCOMPRESSED_LEN`.
+ */
+WALLY_CORE_API int wally_ec_public_key_strict_verify(
+ const unsigned char *pub_key,
+ size_t pub_key_len);
+
/**
* Verify that an x-only public key is valid.
*
### src/sign.c
@@ -58,6 +58,15 @@ int wally_ec_public_key_verify(const unsigned char *pub_key, size_t pub_key_len)
return WALLY_OK;
}
+int wally_ec_public_key_strict_verify(const unsigned char *pub_key, size_t pub_key_len)
+{
+ int ret = wally_ec_public_key_verify(pub_key, pub_key_len);
+ if (ret == WALLY_OK && pub_key_len == EC_PUBLIC_KEY_UNCOMPRESSED_LEN &&
+ pub_key[0] != 0x04)
+ ret = WALLY_EINVAL; /* Hybrid pubkey: reject */
+ return ret;
+}
+
int wally_ec_xonly_public_key_verify(const unsigned char *pub_key, size_t pub_key_len)
{
secp256k1_xonly_pubkey pub;
### src/swig_java/swig.i
@@ -617,6 +617,7 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) {
%returns_array_(wally_ec_public_key_decompress, 3, 4, EC_PUBLIC_KEY_UNCOMPRESSED_LEN);
%returns_array_(wally_ec_public_key_from_private_key, 3, 4, EC_PUBLIC_KEY_LEN);
%returns_array_(wally_ec_public_key_negate, 3, 4, EC_PUBLIC_KEY_LEN);
+%returns_void__(wally_ec_public_key_strict_verify);
%returns_array_(wally_ec_public_key_tweak, 5, 6, EC_PUBLIC_KEY_LEN);
%returns_void__(wally_ec_public_key_verify);
%returns_size_t(wally_ec_sig_from_bytes_aux_len);
### src/test/test_sign.py
@@ -132,6 +132,14 @@ def test_der_convert(self):
ret = wally_ec_sig_from_der(der, der_len, out_buf, out_len)
self.assertEqual(ret, WALLY_EINVAL)
+ def test_hybrid_pubkeys(self):
+ even, even_len = make_cbuffer('0679be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8')
+ odd, odd_len = make_cbuffer('0779be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798b7c52588d95c3b9aa25b0403f1eef75702e84bb7597aabe663b82f6f04ef2777')
+
+ for pk, pk_len in [(even, even_len), (odd, odd_len)]:
+ self.assertEqual(wally_ec_public_key_verify(pk, pk_len), WALLY_OK)
+ self.assertEqual(wally_ec_public_key_strict_verify(pk, pk_len), WALLY_EINVAL)
+
def test_invalid_inputs(self):
out_buf, out_len = make_cbuffer('00' * EC_SIGNATURE_LEN)
### src/test/util.py
@@ -360,6 +360,7 @@ class wally_psbt(Structure):
('wally_ec_public_key_decompress', c_int, [c_void_p, c_size_t, c_void_p, c_size_t]),
('wally_ec_public_key_from_private_key', c_int, [c_void_p, c_size_t, c_void_p, c_size_t]),
('wally_ec_public_key_negate', c_int, [c_void_p, c_size_t, c_void_p, c_size_t]),
+ ('wally_ec_public_key_strict_verify', c_int, [c_void_p, c_size_t]),
('wally_ec_public_key_tweak', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t]),
('wally_ec_public_key_verify', c_int, [c_void_p, c_size_t]),
('wally_ec_scalar_add', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t]),
### src/wasm_package/src/functions.js
@@ -198,6 +198,7 @@ export const ec_public_key_compress = wrap('wally_ec_public_key_compress', [T.By
export const ec_public_key_decompress = wrap('wally_ec_public_key_decompress', [T.Bytes, T.DestPtrSized(T.Bytes, C.EC_PUBLIC_KEY_UNCOMPRESSED_LEN)]);
export const ec_public_key_from_private_key = wrap('wally_ec_public_key_from_private_key', [T.Bytes, T.DestPtrSized(T.Bytes, C.EC_PUBLIC_KEY_LEN)]);
export const ec_public_key_negate = wrap('wally_ec_public_key_negate', [T.Bytes, T.DestPtrSized(T.Bytes, C.EC_PUBLIC_KEY_LEN)]);
+export const ec_public_key_strict_verify = wrap('wally_ec_public_key_strict_verify', [T.Bytes]);
export const ec_public_key_tweak = wrap('wally_ec_public_key_tweak', [T.Bytes, T.Bytes, T.DestPtrSized(T.Bytes, C.EC_PUBLIC_KEY_LEN)]);
export const ec_public_key_verify = wrap('wally_ec_public_key_verify', [T.Bytes]);
export const ec_scalar_add = wrap('wally_ec_scalar_add', [T.Bytes, T.Bytes, T.DestPtrSized(T.Bytes, C.EC_SCALAR_LEN)]);
### src/wasm_package/src/index.d.ts
@@ -158,6 +158,7 @@ export function ec_public_key_compress(pub_key: Buffer|Uint8Array|null): Buffer;
export function ec_public_key_decompress(pub_key: Buffer|Uint8Array|null): Buffer;
export function ec_public_key_from_private_key(priv_key: Buffer|Uint8Array|null): Buffer;
export function ec_public_key_negate(pub_key: Buffer|Uint8Array|null): Buffer;
+export function ec_public_key_strict_verify(pub_key: Buffer|Uint8Array|null): void;
export function ec_public_key_tweak(pub_key: Buffer|Uint8Array|null, tweak: Buffer|Uint8Array|null): Buffer;
export function ec_public_key_verify(pub_key: Buffer|Uint8Array|null): void;
export function ec_scalar_add(scalar: Buffer|Uint8Array|null, operand: Buffer|Uint8Array|null): Buffer;
### tools/wasm_exports.sh
@@ -126,6 +126,7 @@ EXPORTED_FUNCTIONS="['_malloc','_free','_bip32_key_free' \
,'_wally_ec_public_key_decompress' \
,'_wally_ec_public_key_from_private_key' \
,'_wally_ec_public_key_negate' \
+,'_wally_ec_public_key_strict_verify' \
,'_wally_ec_public_key_tweak' \
,'_wally_ec_public_key_verify' \
,'_wally_ec_scalar_add' \Why this scored 23/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.