update secp256k1 to latest version with minimized dependencies

This commit is contained in:
Andrew Poelstra 2018-02-18 15:45:35 +00:00
parent 23a2c6bc9a
commit 066c49305f
4 changed files with 18 additions and 15 deletions

View File

@ -27,7 +27,10 @@ num = "0.1"
rand = "0.3" rand = "0.3"
rust-crypto = "0.2" rust-crypto = "0.2"
rustc-serialize = "0.3" rustc-serialize = "0.3"
secp256k1 = "0.6"
serde = "0.6" serde = "0.6"
strason = "0.3" strason = "0.3"
[dependencies.secp256k1]
version = "0.8"
features = [ "rand", "serde" ]

View File

@ -55,11 +55,14 @@ impl Address {
/// Creates an address from a public key /// Creates an address from a public key
#[inline] #[inline]
pub fn from_key(network: Network, pk: &PublicKey, compressed: bool) -> Address { pub fn from_key(network: Network, pk: &PublicKey, compressed: bool) -> Address {
let secp = Secp256k1::without_caps();
Address { Address {
ty: Type::PubkeyHash, ty: Type::PubkeyHash,
network: network, network: network,
hash: Hash160::from_data(&pk.serialize_vec(&secp, compressed)[..]) hash: if compressed {
Hash160::from_data(&pk.serialize()[..])
} else {
Hash160::from_data(&pk.serialize_uncompressed()[..])
}
} }
} }

View File

@ -209,7 +209,7 @@ impl ExtendedPrivKey {
ChildNumber::Normal(n) => { ChildNumber::Normal(n) => {
if n >= (1 << 31) { return Err(Error::InvalidChildNumber(i)) } if n >= (1 << 31) { return Err(Error::InvalidChildNumber(i)) }
// Non-hardened key: compute public data and use that // Non-hardened key: compute public data and use that
hmac.input(&PublicKey::from_secret_key(secp, &self.secret_key).unwrap().serialize_vec(secp, true)[..]); hmac.input(&PublicKey::from_secret_key(secp, &self.secret_key).unwrap().serialize()[..]);
BigEndian::write_u32(&mut be_n, n); BigEndian::write_u32(&mut be_n, n);
} }
ChildNumber::Hardened(n) => { ChildNumber::Hardened(n) => {
@ -243,7 +243,7 @@ impl ExtendedPrivKey {
let pk = ExtendedPubKey::from_private(secp, self); let pk = ExtendedPubKey::from_private(secp, self);
// Do SHA256 of just the ECDSA pubkey // Do SHA256 of just the ECDSA pubkey
let mut sha2 = Sha256::new(); let mut sha2 = Sha256::new();
sha2.input(&pk.public_key.serialize_vec(secp, true)[..]); sha2.input(&pk.public_key.serialize()[..]);
sha2.result(&mut sha2_res); sha2.result(&mut sha2_res);
// do RIPEMD160 // do RIPEMD160
let mut ripemd = Ripemd160::new(); let mut ripemd = Ripemd160::new();
@ -284,7 +284,7 @@ impl ExtendedPubKey {
} }
ChildNumber::Normal(n) => { ChildNumber::Normal(n) => {
let mut hmac = Hmac::new(Sha512::new(), &self.chain_code[..]); let mut hmac = Hmac::new(Sha512::new(), &self.chain_code[..]);
hmac.input(&self.public_key.serialize_vec(secp, true)[..]); hmac.input(&self.public_key.serialize()[..]);
let mut be_n = [0; 4]; let mut be_n = [0; 4];
BigEndian::write_u32(&mut be_n, n); BigEndian::write_u32(&mut be_n, n);
hmac.input(&be_n); hmac.input(&be_n);
@ -317,12 +317,11 @@ impl ExtendedPubKey {
/// Returns the HASH160 of the chaincode /// Returns the HASH160 of the chaincode
pub fn identifier(&self) -> [u8; 20] { pub fn identifier(&self) -> [u8; 20] {
let s = Secp256k1::with_caps(secp256k1::ContextFlag::None);
let mut sha2_res = [0; 32]; let mut sha2_res = [0; 32];
let mut ripemd_res = [0; 20]; let mut ripemd_res = [0; 20];
// Do SHA256 of just the ECDSA pubkey // Do SHA256 of just the ECDSA pubkey
let mut sha2 = Sha256::new(); let mut sha2 = Sha256::new();
sha2.input(&self.public_key.serialize_vec(&s, true)[..]); sha2.input(&self.public_key.serialize()[..]);
sha2.result(&mut sha2_res); sha2.result(&mut sha2_res);
// do RIPEMD160 // do RIPEMD160
let mut ripemd = Ripemd160::new(); let mut ripemd = Ripemd160::new();
@ -395,7 +394,6 @@ impl FromBase58 for ExtendedPrivKey {
impl ToBase58 for ExtendedPubKey { impl ToBase58 for ExtendedPubKey {
fn base58_layout(&self) -> Vec<u8> { fn base58_layout(&self) -> Vec<u8> {
let s = Secp256k1::with_caps(secp256k1::ContextFlag::None);
let mut ret = Vec::with_capacity(78); let mut ret = Vec::with_capacity(78);
ret.extend(match self.network { ret.extend(match self.network {
Network::Bitcoin => [0x04u8, 0x88, 0xB2, 0x1E], Network::Bitcoin => [0x04u8, 0x88, 0xB2, 0x1E],
@ -414,7 +412,7 @@ impl ToBase58 for ExtendedPubKey {
} }
ret.extend(be_n.iter().cloned()); ret.extend(be_n.iter().cloned());
ret.extend(self.chain_code[..].iter().cloned()); ret.extend(self.chain_code[..].iter().cloned());
ret.extend(self.public_key.serialize_vec(&s, true)[..].iter().cloned()); ret.extend(self.public_key.serialize()[..].iter().cloned());
ret ret
} }
} }

View File

@ -17,7 +17,7 @@
//! at http://blockstream.com/sidechains.pdf for details of //! at http://blockstream.com/sidechains.pdf for details of
//! what this does. //! what this does.
use secp256k1::{self, ContextFlag, Secp256k1}; use secp256k1::{self, Secp256k1};
use secp256k1::key::{PublicKey, SecretKey}; use secp256k1::key::{PublicKey, SecretKey};
use blockdata::{opcodes, script}; use blockdata::{opcodes, script};
use crypto::{hmac, sha2}; use crypto::{hmac, sha2};
@ -109,7 +109,6 @@ pub struct Template(Vec<TemplateElement>);
impl Template { impl Template {
/// Instantiate a template /// Instantiate a template
pub fn to_script(&self, keys: &[PublicKey]) -> Result<script::Script, Error> { pub fn to_script(&self, keys: &[PublicKey]) -> Result<script::Script, Error> {
let secp = Secp256k1::with_caps(ContextFlag::None);
let mut key_index = 0; let mut key_index = 0;
let mut ret = script::Builder::new(); let mut ret = script::Builder::new();
for elem in &self.0 { for elem in &self.0 {
@ -120,7 +119,7 @@ impl Template {
return Err(Error::TooFewKeys(key_index)); return Err(Error::TooFewKeys(key_index));
} }
key_index += 1; key_index += 1;
ret.push_slice(&keys[key_index - 1].serialize_vec(&secp, true)[..]) ret.push_slice(&keys[key_index - 1].serialize()[..])
} }
} }
} }
@ -171,7 +170,7 @@ pub fn tweak_keys(secp: &Secp256k1, keys: &[PublicKey], contract: &[u8]) -> Resu
let mut ret = Vec::with_capacity(keys.len()); let mut ret = Vec::with_capacity(keys.len());
for mut key in keys.iter().cloned() { for mut key in keys.iter().cloned() {
let mut hmac_raw = [0; 32]; let mut hmac_raw = [0; 32];
let mut hmac = hmac::Hmac::new(sha2::Sha256::new(), &key.serialize_vec(secp, true)); let mut hmac = hmac::Hmac::new(sha2::Sha256::new(), &key.serialize());
hmac.input(contract); hmac.input(contract);
hmac.raw_result(&mut hmac_raw); hmac.raw_result(&mut hmac_raw);
let hmac_sk = try!(SecretKey::from_slice(secp, &hmac_raw).map_err(Error::BadTweak)); let hmac_sk = try!(SecretKey::from_slice(secp, &hmac_raw).map_err(Error::BadTweak));
@ -184,7 +183,7 @@ pub fn tweak_keys(secp: &Secp256k1, keys: &[PublicKey], contract: &[u8]) -> Resu
/// Compute a tweak from some given data for the given public key /// Compute a tweak from some given data for the given public key
pub fn compute_tweak(secp: &Secp256k1, pk: &PublicKey, contract: &[u8]) -> Result<SecretKey, Error> { pub fn compute_tweak(secp: &Secp256k1, pk: &PublicKey, contract: &[u8]) -> Result<SecretKey, Error> {
let mut hmac_raw = [0; 32]; let mut hmac_raw = [0; 32];
let mut hmac = hmac::Hmac::new(sha2::Sha256::new(), &pk.serialize_vec(secp, true)); let mut hmac = hmac::Hmac::new(sha2::Sha256::new(), &pk.serialize());
hmac.input(contract); hmac.input(contract);
hmac.raw_result(&mut hmac_raw); hmac.raw_result(&mut hmac_raw);
SecretKey::from_slice(secp, &hmac_raw).map_err(Error::BadTweak) SecretKey::from_slice(secp, &hmac_raw).map_err(Error::BadTweak)