From e2e1abf1a3f50daa049faa06dd7250b575f7a055 Mon Sep 17 00:00:00 2001 From: Tibo-lg Date: Thu, 10 Jun 2021 21:06:48 +0900 Subject: [PATCH] Add error type for combine keys + test and doc --- src/key.rs | 15 ++++++++++++--- src/lib.rs | 6 +++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/key.rs b/src/key.rs index 0491b8ea4..dd67056b0 100644 --- a/src/key.rs +++ b/src/key.rs @@ -20,7 +20,7 @@ use core::{fmt, str}; use super::{from_hex, Secp256k1}; -use super::Error::{self, InvalidPublicKey, InvalidSecretKey}; +use super::Error::{self, InvalidPublicKey, InvalidPublicKeySum, InvalidSecretKey}; use Signing; use Verification; use constants; @@ -395,11 +395,15 @@ impl PublicKey { /// Adds the keys in the provided slice together, returning the sum. Returns /// an error if the result would be the point at infinity, i.e. we are adding - /// a point to its own negation + /// a point to its own negation, or if the provided slice has no element in it. pub fn combine_keys(keys: &[&PublicKey]) -> Result { use core::mem::transmute; use core::i32::MAX; + if keys.len() < 1 { + return Err(InvalidPublicKeySum); + } + debug_assert!(keys.len() < MAX as usize); unsafe { let mut ret = ffi::PublicKey::new(); @@ -414,7 +418,7 @@ impl PublicKey { { Ok(PublicKey(ret)) } else { - Err(InvalidPublicKey) + Err(InvalidPublicKeySum) } } } @@ -893,6 +897,11 @@ mod test { assert_eq!(sum1.unwrap(), exp_sum); } + #[cfg_attr(not(fuzzing), test)] + fn pubkey_combine_keys_empty_slice() { + assert!(PublicKey::combine_keys(&[]).is_err()); + } + #[test] fn create_pubkey_combine() { let s = Secp256k1::new(); diff --git a/src/lib.rs b/src/lib.rs index a32a2afe2..53f6c18a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -527,10 +527,10 @@ pub enum Error { InvalidRecoveryId, /// Invalid tweak for add_*_assign or mul_*_assign InvalidTweak, - /// `tweak_add_check` failed on an xonly public key - TweakCheckFailed, /// Didn't pass enough memory to context creation with preallocated memory NotEnoughMemory, + /// Bad set of public keys + InvalidPublicKeySum, } impl Error { @@ -543,8 +543,8 @@ impl Error { Error::InvalidSecretKey => "secp: malformed or out-of-range secret key", Error::InvalidRecoveryId => "secp: bad recovery id", Error::InvalidTweak => "secp: bad tweak", - Error::TweakCheckFailed => "secp: xonly_pubkey_tewak_add_check failed", Error::NotEnoughMemory => "secp: not enough memory allocated", + Error::InvalidPublicKeySum => "secp: the sum of public keys was invalid or the input vector lengths was less than 1", } } }