Skip to content

Update to rust-secp256k1 v0.11 and rust-bitcoin v0.14 #123

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

Merged
merged 5 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ max_level_info = []
max_level_debug = []

[dependencies]
bitcoin = "0.13"
bitcoin = "0.14"
rust-crypto = "0.2"
rand = "0.4"
secp256k1 = "0.9"
secp256k1 = "0.11"

[build-dependencies]
cc = "1.0"

[dev-dependencies.bitcoin]
version = "0.13"
version = "0.14"
features = ["bitcoinconsensus"]

[dev-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ honggfuzz_fuzz = ["honggfuzz"]
[dependencies]
afl = { version = "0.4", optional = true }
lightning = { path = "..", features = ["fuzztarget"] }
bitcoin = { version = "0.13", features = ["fuzztarget"] }
bitcoin = { version = "0.14", features = ["fuzztarget"] }
hex = "0.3"
honggfuzz = { version = "0.5", optional = true }
rust-crypto = "0.2"
secp256k1 = { version = "0.9", features=["fuzztarget"] }
secp256k1 = { version = "0.11", features=["fuzztarget"] }

[build-dependencies]
cc = "1.0"
Expand Down
26 changes: 13 additions & 13 deletions fuzz/fuzz_targets/full_stack_target.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/chain/chaininterface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl ChainWatchInterfaceUtil {
for input in tx.input.iter() {
for outpoint in watched.1.iter() {
let &(outpoint_hash, outpoint_index) = outpoint;
if outpoint_hash == input.prev_hash && outpoint_index == input.prev_index {
if outpoint_hash == input.previous_output.txid && outpoint_index == input.previous_output.vout {
return true;
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/chain/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use bitcoin::util::hash::Sha256dHash;
use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;

/// A reference to a transaction output.
/// Differs from bitcoin::blockdata::transaction::TxOutRef as the index is a u16 instead of usize
/// Differs from bitcoin::blockdata::transaction::OutPoint as the index is a u16 instead of u32
/// due to LN's restrictions on index values. Should reduce (possibly) unsafe conversions this way.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct OutPoint {
Expand All @@ -25,6 +26,13 @@ impl OutPoint {
res[31] ^= ((self.index >> 0) & 0xff) as u8;
res
}

pub fn into_bitcoin_outpoint(self) -> BitcoinOutPoint {
BitcoinOutPoint {
txid: self.txid,
vout: self.index as u32,
}
}
}

#[cfg(test)]
Expand Down
26 changes: 14 additions & 12 deletions src/ln/chan_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bitcoin::blockdata::script::{Script,Builder};
use bitcoin::blockdata::opcodes;
use bitcoin::blockdata::transaction::{TxIn,TxOut,Transaction};
use bitcoin::blockdata::transaction::{TxIn,TxOut,OutPoint,Transaction};
use bitcoin::util::hash::{Hash160,Sha256dHash};

use secp256k1::key::{PublicKey,SecretKey};
Expand Down Expand Up @@ -32,10 +32,10 @@ pub fn build_commitment_secret(commitment_seed: [u8; 32], idx: u64) -> [u8; 32]
res
}

pub fn derive_private_key(secp_ctx: &Secp256k1, per_commitment_point: &PublicKey, base_secret: &SecretKey) -> Result<SecretKey, secp256k1::Error> {
pub fn derive_private_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_secret: &SecretKey) -> Result<SecretKey, secp256k1::Error> {
let mut sha = Sha256::new();
sha.input(&per_commitment_point.serialize());
sha.input(&PublicKey::from_secret_key(&secp_ctx, &base_secret).unwrap().serialize());
sha.input(&PublicKey::from_secret_key(&secp_ctx, &base_secret).serialize());
let mut res = [0; 32];
sha.result(&mut res);

Expand All @@ -44,21 +44,21 @@ pub fn derive_private_key(secp_ctx: &Secp256k1, per_commitment_point: &PublicKey
Ok(key)
}

pub fn derive_public_key(secp_ctx: &Secp256k1, per_commitment_point: &PublicKey, base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
pub fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
let mut sha = Sha256::new();
sha.input(&per_commitment_point.serialize());
sha.input(&base_point.serialize());
let mut res = [0; 32];
sha.result(&mut res);

let hashkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &res)?).unwrap();
let hashkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &res)?);
base_point.combine(&secp_ctx, &hashkey)
}

/// Derives a revocation key from its constituent parts
pub fn derive_private_revocation_key(secp_ctx: &Secp256k1, per_commitment_secret: &SecretKey, revocation_base_secret: &SecretKey) -> Result<SecretKey, secp256k1::Error> {
let revocation_base_point = PublicKey::from_secret_key(&secp_ctx, &revocation_base_secret).unwrap();
let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret).unwrap();
pub fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_secret: &SecretKey, revocation_base_secret: &SecretKey) -> Result<SecretKey, secp256k1::Error> {
let revocation_base_point = PublicKey::from_secret_key(&secp_ctx, &revocation_base_secret);
let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);

let rev_append_commit_hash_key = {
let mut sha = Sha256::new();
Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn derive_private_revocation_key(secp_ctx: &Secp256k1, per_commitment_secret
Ok(part_a)
}

pub fn derive_public_revocation_key(secp_ctx: &Secp256k1, per_commitment_point: &PublicKey, revocation_base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, revocation_base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
let rev_append_commit_hash_key = {
let mut sha = Sha256::new();
sha.input(&revocation_base_point.serialize());
Expand Down Expand Up @@ -124,7 +124,7 @@ pub struct TxCreationKeys {
}

impl TxCreationKeys {
pub fn new(secp_ctx: &Secp256k1, per_commitment_point: &PublicKey, a_delayed_payment_base: &PublicKey, a_htlc_base: &PublicKey, b_revocation_base: &PublicKey, b_payment_base: &PublicKey, b_htlc_base: &PublicKey) -> Result<TxCreationKeys, secp256k1::Error> {
pub fn new<T: secp256k1::Signing + secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, a_delayed_payment_base: &PublicKey, a_htlc_base: &PublicKey, b_revocation_base: &PublicKey, b_payment_base: &PublicKey, b_htlc_base: &PublicKey) -> Result<TxCreationKeys, secp256k1::Error> {
Ok(TxCreationKeys {
per_commitment_point: per_commitment_point.clone(),
revocation_key: derive_public_revocation_key(&secp_ctx, &per_commitment_point, &b_revocation_base)?,
Expand Down Expand Up @@ -241,8 +241,10 @@ pub fn get_htlc_redeemscript(htlc: &HTLCOutputInCommitment, keys: &TxCreationKey
pub fn build_htlc_transaction(prev_hash: &Sha256dHash, feerate_per_kw: u64, to_self_delay: u16, htlc: &HTLCOutputInCommitment, a_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
let mut txins: Vec<TxIn> = Vec::new();
txins.push(TxIn {
prev_hash: prev_hash.clone(),
prev_index: htlc.transaction_output_index,
previous_output: OutPoint {
txid: prev_hash.clone(),
vout: htlc.transaction_output_index,
},
script_sig: Script::new(),
sequence: 0,
witness: Vec::new(),
Expand Down
Loading