From 46681bbcac3ef26150c14db725521e09380b88c8 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Fri, 10 Jun 2016 18:36:15 +0000 Subject: [PATCH] contracthash: expose key tweak in new function Needed for applications where the tweak and the secret key material are on different devices (and the one with the secret material does not want to know how to compute the tweak itself). --- Cargo.toml | 2 +- src/util/contracthash.rs | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 968cd4f..091e662 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "bitcoin" -version = "0.5.10" +version = "0.5.11" authors = ["Andrew Poelstra "] license = "CC0-1.0" homepage = "https://github.com/apoelstra/rust-bitcoin/" diff --git a/src/util/contracthash.rs b/src/util/contracthash.rs index 9c6d7e7..4c0bfac 100644 --- a/src/util/contracthash.rs +++ b/src/util/contracthash.rs @@ -181,16 +181,21 @@ pub fn tweak_keys(secp: &Secp256k1, keys: &[PublicKey], contract: &[u8]) -> Resu Ok(ret) } -/// Tweak a secret key using some arbitrary data -pub fn tweak_secret_key(secp: &Secp256k1, key: &SecretKey, contract: &[u8]) -> Result { - // Compute public key - let pk = try!(PublicKey::from_secret_key(secp, &key).map_err(Error::Secp)); - // Compute HMAC tweak +/// Compute a tweak from some given data for the given public key +pub fn compute_tweak(secp: &Secp256k1, pk: &PublicKey, contract: &[u8]) -> Result { let mut hmac_raw = [0; 32]; let mut hmac = hmac::Hmac::new(sha2::Sha256::new(), &pk.serialize_vec(&secp, true)); hmac.input(contract); hmac.raw_result(&mut hmac_raw); - let hmac_sk = try!(SecretKey::from_slice(&secp, &hmac_raw).map_err(Error::BadTweak)); + SecretKey::from_slice(&secp, &hmac_raw).map_err(Error::BadTweak) +} + +/// Tweak a secret key using some arbitrary data (calls `compute_tweak` internally) +pub fn tweak_secret_key(secp: &Secp256k1, key: &SecretKey, contract: &[u8]) -> Result { + // Compute public key + let pk = try!(PublicKey::from_secret_key(secp, &key).map_err(Error::Secp)); + // Compute tweak + let hmac_sk = try!(compute_tweak(secp, &pk, contract)); // Execute the tweak let mut key = *key; try!(key.add_assign(&secp, &hmac_sk).map_err(Error::Secp));