@@ -65,6 +65,7 @@ pub const ONE_KEY: SecretKey = SecretKey([0, 0, 0, 0, 0, 0, 0, 0,
65
65
66
66
/// A Secp256k1 public key, used for verification of signatures
67
67
#[ derive( Copy , Clone , PartialEq , Eq , Debug , Hash ) ]
68
+ #[ repr( transparent) ]
68
69
pub struct PublicKey ( ffi:: PublicKey ) ;
69
70
70
71
impl fmt:: LowerHex for PublicKey {
@@ -364,14 +365,24 @@ impl PublicKey {
364
365
/// the result would be the point at infinity, i.e. we are adding this point
365
366
/// to its own negation
366
367
pub fn combine ( & self , other : & PublicKey ) -> Result < PublicKey , Error > {
368
+ PublicKey :: combine_keys ( & [ self , other] )
369
+ }
370
+
371
+ /// Adds the keys in the provided slice together, returning the sum. Returns
372
+ /// an error if the result would be the point at infinity, i.e. we are adding
373
+ /// a point to its own negation
374
+ #[ cfg( all( feature = "std" ) ) ]
375
+ pub fn combine_keys ( keys : & [ & PublicKey ] ) -> Result < PublicKey , Error > {
376
+ use std:: mem:: transmute;
367
377
unsafe {
368
378
let mut ret = ffi:: PublicKey :: new ( ) ;
369
- let ptrs = [ self . as_c_ptr ( ) , other. as_c_ptr ( ) ] ;
379
+ let ptrs : & [ * const ffi:: PublicKey ] =
380
+ transmute :: < & [ & PublicKey ] , & [ * const ffi:: PublicKey ] > ( keys) ;
370
381
if ffi:: secp256k1_ec_pubkey_combine (
371
382
ffi:: secp256k1_context_no_precomp,
372
383
& mut ret,
373
384
ptrs. as_c_ptr ( ) ,
374
- 2
385
+ keys . len ( ) as i32
375
386
) == 1
376
387
{
377
388
Ok ( PublicKey ( ret) )
@@ -794,6 +805,29 @@ mod test {
794
805
assert_eq ! ( sum1. unwrap( ) , exp_sum) ;
795
806
}
796
807
808
+ #[ test]
809
+ fn pubkey_combine_keys ( ) {
810
+ let compressed1 = PublicKey :: from_slice (
811
+ & hex ! ( "0241cc121c419921942add6db6482fb36243faf83317c866d2a28d8c6d7089f7ba" ) ,
812
+ ) . unwrap ( ) ;
813
+ let compressed2 = PublicKey :: from_slice (
814
+ & hex ! ( "02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443" ) ,
815
+ ) . unwrap ( ) ;
816
+ let compressed3 = PublicKey :: from_slice (
817
+ & hex ! ( "03e74897d8644eb3e5b391ca2ab257aec2080f4d1a95cad57e454e47f021168eb0" )
818
+ ) . unwrap ( ) ;
819
+ let exp_sum = PublicKey :: from_slice (
820
+ & hex ! ( "0252d73a47f66cf341e5651542f0348f452b7c793af62a6d8bff75ade703a451ad" ) ,
821
+ ) . unwrap ( ) ;
822
+
823
+ let sum1 = PublicKey :: combine_keys ( & [ & compressed1, & compressed2, & compressed3] ) ;
824
+ assert ! ( sum1. is_ok( ) ) ;
825
+ let sum2 = PublicKey :: combine_keys ( & [ & compressed1, & compressed2, & compressed3] ) ;
826
+ assert ! ( sum2. is_ok( ) ) ;
827
+ assert_eq ! ( sum1, sum2) ;
828
+ assert_eq ! ( sum1. unwrap( ) , exp_sum) ;
829
+ }
830
+
797
831
#[ test]
798
832
fn pubkey_equal ( ) {
799
833
let pk1 = PublicKey :: from_slice (
0 commit comments