@@ -364,14 +364,21 @@ impl PublicKey {
364364 /// the result would be the point at infinity, i.e. we are adding this point
365365 /// to its own negation
366366 pub fn combine ( & self , other : & PublicKey ) -> Result < PublicKey , Error > {
367+ PublicKey :: combine_keys ( & [ self , other] )
368+ }
369+
370+ /// Adds the keys in the provided slice together, returning the sum. Returns
371+ /// an error if the result would be the point at infinity, i.e. we are adding
372+ /// a point to its own negation
373+ pub fn combine_keys ( keys : & [ & PublicKey ] ) -> Result < PublicKey , Error > {
367374 unsafe {
368375 let mut ret = ffi:: PublicKey :: new ( ) ;
369- let ptrs = [ self . as_c_ptr ( ) , other . as_c_ptr ( ) ] ;
376+ let ptrs : Vec < * const secp256k1_sys :: PublicKey > = keys . iter ( ) . map ( |k| k . as_c_ptr ( ) ) . collect ( ) ;
370377 if ffi:: secp256k1_ec_pubkey_combine (
371378 ffi:: secp256k1_context_no_precomp,
372379 & mut ret,
373380 ptrs. as_c_ptr ( ) ,
374- 2
381+ keys . len ( ) as i32
375382 ) == 1
376383 {
377384 Ok ( PublicKey ( ret) )
@@ -794,6 +801,29 @@ mod test {
794801 assert_eq ! ( sum1. unwrap( ) , exp_sum) ;
795802 }
796803
804+ #[ test]
805+ fn pubkey_combine_keys ( ) {
806+ let compressed1 = PublicKey :: from_slice (
807+ & hex ! ( "0241cc121c419921942add6db6482fb36243faf83317c866d2a28d8c6d7089f7ba" ) ,
808+ ) . unwrap ( ) ;
809+ let compressed2 = PublicKey :: from_slice (
810+ & hex ! ( "02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443" ) ,
811+ ) . unwrap ( ) ;
812+ let compressed3 = PublicKey :: from_slice (
813+ & hex ! ( "03e74897d8644eb3e5b391ca2ab257aec2080f4d1a95cad57e454e47f021168eb0" )
814+ ) . unwrap ( ) ;
815+ let exp_sum = PublicKey :: from_slice (
816+ & hex ! ( "0252d73a47f66cf341e5651542f0348f452b7c793af62a6d8bff75ade703a451ad" ) ,
817+ ) . unwrap ( ) ;
818+
819+ let sum1 = PublicKey :: combine_keys ( & [ & compressed1, & compressed2, & compressed3] ) ;
820+ assert ! ( sum1. is_ok( ) ) ;
821+ let sum2 = PublicKey :: combine_keys ( & [ & compressed1, & compressed2, & compressed3] ) ;
822+ assert ! ( sum2. is_ok( ) ) ;
823+ assert_eq ! ( sum1, sum2) ;
824+ assert_eq ! ( sum1. unwrap( ) , exp_sum) ;
825+ }
826+
797827 #[ test]
798828 fn pubkey_equal ( ) {
799829 let pk1 = PublicKey :: from_slice (
0 commit comments