Merge pull request #56 from rust-bitcoin/2018-08-return-pubkey-combine

Revert "remove PublicKey::combine"
This commit is contained in:
Andrew Poelstra 2018-08-22 21:16:33 +00:00 committed by GitHub
commit 2f8855f8fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 2 deletions

View File

@ -1,4 +1,8 @@
# 0.11.1 - 2018-08-22
* Put `PublicKey::combine` back because it is currently needed to implement Lightning BOLT 3
# 0.11.0 - 2018-08-22 # 0.11.0 - 2018-08-22
* 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.) * 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.)

View File

@ -1,7 +1,7 @@
[package] [package]
name = "secp256k1" name = "secp256k1"
version = "0.11.0" version = "0.11.1"
authors = [ "Dawid Ciężarkiewicz <dpc@ucore.info>", authors = [ "Dawid Ciężarkiewicz <dpc@ucore.info>",
"Andrew Poelstra <apoelstra@wpsoftware.net>" ] "Andrew Poelstra <apoelstra@wpsoftware.net>" ]
license = "CC0-1.0" license = "CC0-1.0"

View File

@ -17,7 +17,7 @@
#[cfg(any(test, feature = "rand"))] use rand::Rng; #[cfg(any(test, feature = "rand"))] use rand::Rng;
use std::fmt; use std::{fmt, mem};
use super::{Secp256k1}; use super::{Secp256k1};
use super::Error::{self, InvalidPublicKey, InvalidSecretKey}; use super::Error::{self, InvalidPublicKey, InvalidSecretKey};
@ -273,6 +273,21 @@ impl PublicKey {
} }
} }
} }
/// Adds a second key to this one, returning the sum. Returns an error if
/// the result would be the point at infinity, i.e. we are adding this point
/// to its own negation
pub fn combine<C>(&self, secp: &Secp256k1<C>, other: &PublicKey) -> Result<PublicKey, Error> {
unsafe {
let mut ret = mem::uninitialized();
let ptrs = [self.as_ptr(), other.as_ptr()];
if ffi::secp256k1_ec_pubkey_combine(secp.ctx, &mut ret, ptrs.as_ptr(), 2) == 1 {
Ok(PublicKey(ret))
} else {
Err(InvalidPublicKey)
}
}
}
} }
/// Creates a new public key from a FFI public key /// Creates a new public key from a FFI public key
@ -551,6 +566,30 @@ mod test {
assert_eq!(count, COUNT); assert_eq!(count, COUNT);
} }
#[test]
fn pubkey_combine() {
let s = Secp256k1::without_caps();
let compressed1 = PublicKey::from_slice(
&s,
&hex!("0241cc121c419921942add6db6482fb36243faf83317c866d2a28d8c6d7089f7ba"),
).unwrap();
let compressed2 = PublicKey::from_slice(
&s,
&hex!("02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443"),
).unwrap();
let exp_sum = PublicKey::from_slice(
&s,
&hex!("0384526253c27c7aef56c7b71a5cd25bebb66dddda437826defc5b2568bde81f07"),
).unwrap();
let sum1 = compressed1.combine(&s, &compressed2);
assert!(sum1.is_ok());
let sum2 = compressed2.combine(&s, &compressed1);
assert!(sum2.is_ok());
assert_eq!(sum1, sum2);
assert_eq!(sum1.unwrap(), exp_sum);
}
#[test] #[test]
fn pubkey_equal() { fn pubkey_equal() {
let s = Secp256k1::new(); let s = Secp256k1::new();