From 6f5484d82c06ee3f76d2fb4c126c20f3a8833efb Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Tue, 18 Aug 2026 14:21:25 +0000 Subject: [PATCH] secp256k1 extmod: raise `ValueError` or `RuntimeError` as appropriate --- extmod/secp256k1mod.c | 16 ++++++++-------- test/modtest_d/ecc.py | 14 +++++++------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/extmod/secp256k1mod.c b/extmod/secp256k1mod.c index 1e762ee6..c3c1dfc4 100755 --- a/extmod/secp256k1mod.c +++ b/extmod/secp256k1mod.c @@ -243,7 +243,7 @@ static PyObject * sign_msghash(PyObject *Py_UNUSED(self), PyObject *args) { } if (msghash_bytes_len != 32) { - PyErr_SetString(PyExc_RuntimeError, "Invalid message hash length (not 32 bytes)"); + PyErr_SetString(PyExc_ValueError, "Invalid message hash length (not 32 bytes)"); return NULL; } @@ -257,11 +257,11 @@ static PyObject * sign_msghash(PyObject *Py_UNUSED(self), PyObject *args) { int recid; if (!secp256k1_ecdsa_sign_recoverable(ctx, &rsig, msghash_bytes, privkey_bytes, NULL, NULL)) { - PyErr_SetString(PyExc_ValueError, "Unable to sign message hash"); + PyErr_SetString(PyExc_RuntimeError, "Unable to sign message hash"); return NULL; } if (!secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, rsig_serialized, &recid, &rsig)) { - PyErr_SetString(PyExc_ValueError, "Unable to serialize signature"); + PyErr_SetString(PyExc_RuntimeError, "Unable to serialize signature"); return NULL; } /* truncate serialized sig to 64 bytes */ @@ -291,11 +291,11 @@ static PyObject * verify_sig(PyObject *Py_UNUSED(self), PyObject *args) { } if (sig_bytes_len != 64) { - PyErr_SetString(PyExc_RuntimeError, "Invalid signature length (not 64 bytes)"); + PyErr_SetString(PyExc_ValueError, "Invalid signature length (not 64 bytes)"); return NULL; } if (msghash_bytes_len != 32) { - PyErr_SetString(PyExc_RuntimeError, "Invalid message hash length (not 32 bytes)"); + PyErr_SetString(PyExc_ValueError, "Invalid message hash length (not 32 bytes)"); return NULL; } @@ -341,15 +341,15 @@ static PyObject * pubkey_recover(PyObject *Py_UNUSED(self), PyObject *args) { } if (recid < 0 || recid > 3) { - PyErr_SetString(PyExc_RuntimeError, "Invalid recovery ID (not in range 0-3)"); + PyErr_SetString(PyExc_ValueError, "Invalid recovery ID (not in range 0-3)"); return NULL; } if (sig_bytes_len != 64) { - PyErr_SetString(PyExc_RuntimeError, "Invalid signature length (not 64 bytes)"); + PyErr_SetString(PyExc_ValueError, "Invalid signature length (not 64 bytes)"); return NULL; } if (msghash_bytes_len != 32) { - PyErr_SetString(PyExc_RuntimeError, "Invalid message hash length (not 32 bytes)"); + PyErr_SetString(PyExc_ValueError, "Invalid message hash length (not 32 bytes)"); return NULL; } diff --git a/test/modtest_d/ecc.py b/test/modtest_d/ecc.py index 46498cb8..bcd0e731 100755 --- a/test/modtest_d/ecc.py +++ b/test/modtest_d/ecc.py @@ -81,19 +81,19 @@ class unit_tests: bad_data = ( ('sign: bad args', 'ValueError', 'Unable to parse', sign1), - ('sign: bad msghash len', 'RuntimeError', 'hash length', sign2), + ('sign: bad msghash len', 'ValueError', 'hash length', sign2), ('sign: privkey=0', 'ValueError', 'Private key not in allowable', sign3), ('verify: bad args', 'ValueError', 'Unable to parse', verify1), ('verify: bad pubkey', 'RuntimeError', 'Failed to parse', verify2), ('verify: bad sig', 'AssertionError', 'bad signature', verify3), ('verify: good sig', 'AssertionError', 'good signature', verify4), - ('verify: bad msghash len', 'RuntimeError', 'message hash length', verify5), - ('verify: bad sig len', 'RuntimeError', 'Invalid signature length', verify6), + ('verify: bad msghash len', 'ValueError', 'message hash length', verify5), + ('verify: bad sig len', 'ValueError', 'Invalid signature length', verify6), ('recover: bad args', 'ValueError', 'Unable to parse', recov1), - ('recover: bad recid', 'RuntimeError', 'Invalid recovery ID', recov2), - ('recover: bad recid', 'RuntimeError', 'Invalid recovery ID', recov3), - ('recover: bad sig len', 'RuntimeError', 'Invalid signature length', recov4), - ('recover: bad msghash len', 'RuntimeError', 'message hash length', recov5), + ('recover: bad recid', 'ValueError', 'Invalid recovery ID', recov2), + ('recover: bad recid', 'ValueError', 'Invalid recovery ID', recov3), + ('recover: bad sig len', 'ValueError', 'Invalid signature length', recov4), + ('recover: bad msghash len', 'ValueError', 'message hash length', recov5), ('recover: bad pubkey', 'AssertionError', 'bad pubkey', recov6), ('recover: bad pubkey', 'AssertionError', 'good pubkey', recov7), )