Skip to content

Commit e2e1abf

Browse files
committed
Add error type for combine keys + test and doc
1 parent bb25ed4 commit e2e1abf

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/key.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use core::{fmt, str};
2121

2222
use super::{from_hex, Secp256k1};
23-
use super::Error::{self, InvalidPublicKey, InvalidSecretKey};
23+
use super::Error::{self, InvalidPublicKey, InvalidPublicKeySum, InvalidSecretKey};
2424
use Signing;
2525
use Verification;
2626
use constants;
@@ -395,11 +395,15 @@ impl PublicKey {
395395

396396
/// Adds the keys in the provided slice together, returning the sum. Returns
397397
/// an error if the result would be the point at infinity, i.e. we are adding
398-
/// a point to its own negation
398+
/// a point to its own negation, or if the provided slice has no element in it.
399399
pub fn combine_keys(keys: &[&PublicKey]) -> Result<PublicKey, Error> {
400400
use core::mem::transmute;
401401
use core::i32::MAX;
402402

403+
if keys.len() < 1 {
404+
return Err(InvalidPublicKeySum);
405+
}
406+
403407
debug_assert!(keys.len() < MAX as usize);
404408
unsafe {
405409
let mut ret = ffi::PublicKey::new();
@@ -414,7 +418,7 @@ impl PublicKey {
414418
{
415419
Ok(PublicKey(ret))
416420
} else {
417-
Err(InvalidPublicKey)
421+
Err(InvalidPublicKeySum)
418422
}
419423
}
420424
}
@@ -893,6 +897,11 @@ mod test {
893897
assert_eq!(sum1.unwrap(), exp_sum);
894898
}
895899

900+
#[cfg_attr(not(fuzzing), test)]
901+
fn pubkey_combine_keys_empty_slice() {
902+
assert!(PublicKey::combine_keys(&[]).is_err());
903+
}
904+
896905
#[test]
897906
fn create_pubkey_combine() {
898907
let s = Secp256k1::new();

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,10 @@ pub enum Error {
527527
InvalidRecoveryId,
528528
/// Invalid tweak for add_*_assign or mul_*_assign
529529
InvalidTweak,
530-
/// `tweak_add_check` failed on an xonly public key
531-
TweakCheckFailed,
532530
/// Didn't pass enough memory to context creation with preallocated memory
533531
NotEnoughMemory,
532+
/// Bad set of public keys
533+
InvalidPublicKeySum,
534534
}
535535

536536
impl Error {
@@ -543,8 +543,8 @@ impl Error {
543543
Error::InvalidSecretKey => "secp: malformed or out-of-range secret key",
544544
Error::InvalidRecoveryId => "secp: bad recovery id",
545545
Error::InvalidTweak => "secp: bad tweak",
546-
Error::TweakCheckFailed => "secp: xonly_pubkey_tewak_add_check failed",
547546
Error::NotEnoughMemory => "secp: not enough memory allocated",
547+
Error::InvalidPublicKeySum => "secp: the sum of public keys was invalid or the input vector lengths was less than 1",
548548
}
549549
}
550550
}

0 commit comments

Comments
 (0)