Skip to content

Commit 24a9c9c

Browse files
authored
Merge pull request #304 from p2pderivatives/combine-keys-test-and-doc
Add error type for combine keys + test and doc
2 parents cd62343 + 674cc79 commit 24a9c9c

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/key.rs

+13-4
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;
@@ -425,12 +425,16 @@ impl PublicKey {
425425

426426
/// Adds the keys in the provided slice together, returning the sum. Returns
427427
/// an error if the result would be the point at infinity, i.e. we are adding
428-
/// a point to its own negation
428+
/// a point to its own negation, if the provided slice has no element in it,
429+
/// or if the number of element it contains is greater than i32::MAX.
429430
pub fn combine_keys(keys: &[&PublicKey]) -> Result<PublicKey, Error> {
430431
use core::mem::transmute;
431432
use core::i32::MAX;
432433

433-
debug_assert!(keys.len() < MAX as usize);
434+
if keys.is_empty() || keys.len() > MAX as usize {
435+
return Err(InvalidPublicKeySum);
436+
}
437+
434438
unsafe {
435439
let mut ret = ffi::PublicKey::new();
436440
let ptrs : &[*const ffi::PublicKey] =
@@ -444,7 +448,7 @@ impl PublicKey {
444448
{
445449
Ok(PublicKey(ret))
446450
} else {
447-
Err(InvalidPublicKey)
451+
Err(InvalidPublicKeySum)
448452
}
449453
}
450454
}
@@ -923,6 +927,11 @@ mod test {
923927
assert_eq!(sum1.unwrap(), exp_sum);
924928
}
925929

930+
#[cfg_attr(not(fuzzing), test)]
931+
fn pubkey_combine_keys_empty_slice() {
932+
assert!(PublicKey::combine_keys(&[]).is_err());
933+
}
934+
926935
#[test]
927936
fn create_pubkey_combine() {
928937
let s = Secp256k1::new();

src/lib.rs

+3-3
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)