Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add combine_keys function to PublicKey #291

Merged
merged 1 commit into from
Jun 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub const ONE_KEY: SecretKey = SecretKey([0, 0, 0, 0, 0, 0, 0, 0,

/// A Secp256k1 public key, used for verification of signatures
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
#[repr(transparent)]
pub struct PublicKey(ffi::PublicKey);

impl fmt::LowerHex for PublicKey {
Expand Down Expand Up @@ -364,14 +365,26 @@ impl PublicKey {
/// the result would be the point at infinity, i.e. we are adding this point
/// to its own negation
pub fn combine(&self, other: &PublicKey) -> Result<PublicKey, Error> {
PublicKey::combine_keys(&[self, other])
}

/// 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
/// a point to its own negation
pub fn combine_keys(keys: &[&PublicKey]) -> Result<PublicKey, Error> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post-merge note: PublicKey is a copy type and I am not sure why we need to have it here by reference

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the reason is because we need a &[&PublicKey] to transmute. It would also be possible to take a &[PublicKey] and do the conversion internally but I guess I preferred to keep the library code minimal. I don't have a strong opinion if people want to change it though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the C code takes pointer to a list of pointers, so we'd have to allocate a vector of pointers and we try to avoid allocations where possible

use core::mem::transmute;
use core::i32::MAX;

debug_assert!(keys.len() < MAX as usize);
unsafe {
let mut ret = ffi::PublicKey::new();
let ptrs = [self.as_c_ptr(), other.as_c_ptr()];
let ptrs : &[*const ffi::PublicKey] =
transmute::<&[&PublicKey], &[*const ffi::PublicKey]>(keys);
if ffi::secp256k1_ec_pubkey_combine(
ffi::secp256k1_context_no_precomp,
&mut ret,
ptrs.as_c_ptr(),
2
keys.len() as i32
) == 1
{
Ok(PublicKey(ret))
Expand Down Expand Up @@ -794,6 +807,29 @@ mod test {
assert_eq!(sum1.unwrap(), exp_sum);
}

#[test]
fn pubkey_combine_keys() {
let compressed1 = PublicKey::from_slice(
&hex!("0241cc121c419921942add6db6482fb36243faf83317c866d2a28d8c6d7089f7ba"),
).unwrap();
let compressed2 = PublicKey::from_slice(
&hex!("02e6642fd69bd211f93f7f1f36ca51a26a5290eb2dd1b0d8279a87bb0d480c8443"),
).unwrap();
let compressed3 = PublicKey::from_slice(
&hex!("03e74897d8644eb3e5b391ca2ab257aec2080f4d1a95cad57e454e47f021168eb0")
).unwrap();
let exp_sum = PublicKey::from_slice(
&hex!("0252d73a47f66cf341e5651542f0348f452b7c793af62a6d8bff75ade703a451ad"),
).unwrap();

let sum1 = PublicKey::combine_keys(&[&compressed1, &compressed2, &compressed3]);
assert!(sum1.is_ok());
let sum2 = PublicKey::combine_keys(&[&compressed1, &compressed2, &compressed3]);
assert!(sum2.is_ok());
assert_eq!(sum1, sum2);
assert_eq!(sum1.unwrap(), exp_sum);
}

#[test]
fn pubkey_equal() {
let pk1 = PublicKey::from_slice(
Expand Down