Skip to content
This repository was archived by the owner on Feb 25, 2021. It is now read-only.

Commit 2f8855f

Browse files
authored
Merge pull request BlockstreamResearch#56 from rust-bitcoin/2018-08-return-pubkey-combine
Revert "remove PublicKey::combine"
2 parents 15655e5 + 6f025a3 commit 2f8855f

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
# 0.11.1 - 2018-08-22
3+
4+
* Put `PublicKey::combine` back because it is currently needed to implement Lightning BOLT 3
5+
26
# 0.11.0 - 2018-08-22
37

48
* Update `rand` to 0.4 and `gcc` 0.3 to `cc` 1.0. (`rand` 0.5 exists but has a lot of breaking changes and no longer compiles with 1.14.0.)

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "secp256k1"
4-
version = "0.11.0"
4+
version = "0.11.1"
55
authors = [ "Dawid Ciężarkiewicz <[email protected]>",
66
"Andrew Poelstra <[email protected]>" ]
77
license = "CC0-1.0"

src/key.rs

+40-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
1818
#[cfg(any(test, feature = "rand"))] use rand::Rng;
1919

20-
use std::fmt;
20+
use std::{fmt, mem};
2121

2222
use super::{Secp256k1};
2323
use super::Error::{self, InvalidPublicKey, InvalidSecretKey};
@@ -273,6 +273,21 @@ impl PublicKey {
273273
}
274274
}
275275
}
276+
277+
/// Adds a second key to this one, returning the sum. Returns an error if
278+
/// the result would be the point at infinity, i.e. we are adding this point
279+
/// to its own negation
280+
pub fn combine<C>(&self, secp: &Secp256k1<C>, other: &PublicKey) -> Result<PublicKey, Error> {
281+
unsafe {
282+
let mut ret = mem::uninitialized();
283+
let ptrs = [self.as_ptr(), other.as_ptr()];
284+
if ffi::secp256k1_ec_pubkey_combine(secp.ctx, &mut ret, ptrs.as_ptr(), 2) == 1 {
285+
Ok(PublicKey(ret))
286+
} else {
287+
Err(InvalidPublicKey)
288+
}
289+
}
290+
}
276291
}
277292

278293
/// Creates a new public key from a FFI public key
@@ -551,6 +566,30 @@ mod test {
551566
assert_eq!(count, COUNT);
552567
}
553568

569+
#[test]
570+
fn pubkey_combine() {
571+
let s = Secp256k1::without_caps();
572+
let compressed1 = PublicKey::from_slice(
573+
&s,
574+
&hex!("0241cc121c419921942add6db6482fb36243faf83317c866d2a28d8c6d7089f7ba"),
575+
).unwrap();
576+
let compressed2 = PublicKey::from_slice(
577+
&s,
578+
&hex!("02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443"),
579+
).unwrap();
580+
let exp_sum = PublicKey::from_slice(
581+
&s,
582+
&hex!("0384526253c27c7aef56c7b71a5cd25bebb66dddda437826defc5b2568bde81f07"),
583+
).unwrap();
584+
585+
let sum1 = compressed1.combine(&s, &compressed2);
586+
assert!(sum1.is_ok());
587+
let sum2 = compressed2.combine(&s, &compressed1);
588+
assert!(sum2.is_ok());
589+
assert_eq!(sum1, sum2);
590+
assert_eq!(sum1.unwrap(), exp_sum);
591+
}
592+
554593
#[test]
555594
fn pubkey_equal() {
556595
let s = Secp256k1::new();

0 commit comments

Comments
 (0)