Merge pull request #304 from p2pderivatives/combine-keys-test-and-doc

Add error type for combine keys + test and doc
This commit is contained in:
Andrew Poelstra 2021-09-24 22:37:19 +00:00 committed by GitHub
commit 24a9c9c765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 7 deletions

View File

@ -20,7 +20,7 @@
use core::{fmt, str}; use core::{fmt, str};
use super::{from_hex, Secp256k1}; use super::{from_hex, Secp256k1};
use super::Error::{self, InvalidPublicKey, InvalidSecretKey}; use super::Error::{self, InvalidPublicKey, InvalidPublicKeySum, InvalidSecretKey};
use Signing; use Signing;
use Verification; use Verification;
use constants; use constants;
@ -425,12 +425,16 @@ impl PublicKey {
/// Adds the keys in the provided slice together, returning the sum. Returns /// 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 /// 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, if the provided slice has no element in it,
/// or if the number of element it contains is greater than i32::MAX.
pub fn combine_keys(keys: &[&PublicKey]) -> Result<PublicKey, Error> { pub fn combine_keys(keys: &[&PublicKey]) -> Result<PublicKey, Error> {
use core::mem::transmute; use core::mem::transmute;
use core::i32::MAX; use core::i32::MAX;
debug_assert!(keys.len() < MAX as usize); if keys.is_empty() || keys.len() > MAX as usize {
return Err(InvalidPublicKeySum);
}
unsafe { unsafe {
let mut ret = ffi::PublicKey::new(); let mut ret = ffi::PublicKey::new();
let ptrs : &[*const ffi::PublicKey] = let ptrs : &[*const ffi::PublicKey] =
@ -444,7 +448,7 @@ impl PublicKey {
{ {
Ok(PublicKey(ret)) Ok(PublicKey(ret))
} else { } else {
Err(InvalidPublicKey) Err(InvalidPublicKeySum)
} }
} }
} }
@ -923,6 +927,11 @@ mod test {
assert_eq!(sum1.unwrap(), exp_sum); assert_eq!(sum1.unwrap(), exp_sum);
} }
#[cfg_attr(not(fuzzing), test)]
fn pubkey_combine_keys_empty_slice() {
assert!(PublicKey::combine_keys(&[]).is_err());
}
#[test] #[test]
fn create_pubkey_combine() { fn create_pubkey_combine() {
let s = Secp256k1::new(); let s = Secp256k1::new();

View File

@ -527,10 +527,10 @@ pub enum Error {
InvalidRecoveryId, InvalidRecoveryId,
/// Invalid tweak for add_*_assign or mul_*_assign /// Invalid tweak for add_*_assign or mul_*_assign
InvalidTweak, InvalidTweak,
/// `tweak_add_check` failed on an xonly public key
TweakCheckFailed,
/// Didn't pass enough memory to context creation with preallocated memory /// Didn't pass enough memory to context creation with preallocated memory
NotEnoughMemory, NotEnoughMemory,
/// Bad set of public keys
InvalidPublicKeySum,
} }
impl Error { impl Error {
@ -543,8 +543,8 @@ impl Error {
Error::InvalidSecretKey => "secp: malformed or out-of-range secret key", Error::InvalidSecretKey => "secp: malformed or out-of-range secret key",
Error::InvalidRecoveryId => "secp: bad recovery id", Error::InvalidRecoveryId => "secp: bad recovery id",
Error::InvalidTweak => "secp: bad tweak", Error::InvalidTweak => "secp: bad tweak",
Error::TweakCheckFailed => "secp: xonly_pubkey_tewak_add_check failed",
Error::NotEnoughMemory => "secp: not enough memory allocated", 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",
} }
} }
} }