Skip to content

Commit f12d302

Browse files
committed
Upgrade to secp256k1 v12, bitcoin v16, and crates bitcoin_hashes
1 parent 7fd294d commit f12d302

17 files changed

+225
-237
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ max_level_info = []
2222
max_level_debug = []
2323

2424
[dependencies]
25-
bitcoin = "0.15"
26-
bitcoin_hashes = { git = "https://github.com/TheBlueMatt/bitcoin_hashes", branch = "rust-lightning-dep" }
25+
bitcoin = "0.16"
26+
bitcoin_hashes = "0.2"
2727
rand = "0.4"
28-
secp256k1 = "0.11"
28+
secp256k1 = "0.12"
2929

3030
[dev-dependencies.bitcoin]
31-
version = "0.15"
31+
version = "0.16"
3232
features = ["bitcoinconsensus"]
3333

3434
[dev-dependencies]

fuzz/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ libfuzzer_fuzz = ["libfuzzer-sys"]
1818
[dependencies]
1919
afl = { version = "0.4", optional = true }
2020
lightning = { path = "..", features = ["fuzztarget"] }
21-
bitcoin = { version = "0.15", features = ["fuzztarget"] }
22-
bitcoin_hashes = { git = "https://github.com/TheBlueMatt/bitcoin_hashes", branch = "rust-lightning-dep", features=["fuzztarget"] }
21+
bitcoin = { version = "0.16", features = ["fuzztarget"] }
22+
bitcoin_hashes = { version = "0.2", features=["fuzztarget"] }
2323
hex = "0.3"
2424
honggfuzz = { version = "0.5", optional = true }
25-
secp256k1 = { version = "0.11", features=["fuzztarget"] }
25+
secp256k1 = { version = "0.12", features=["fuzztarget"] }
2626
libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git", optional = true }
2727

2828
[build-dependencies]

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 24 additions & 26 deletions
Large diffs are not rendered by default.

fuzz/fuzz_targets/peer_crypt_target.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use lightning::ln::peer_channel_encryptor::PeerChannelEncryptor;
55
use lightning::util::reset_rng_state;
66

77
use secp256k1::key::{PublicKey,SecretKey};
8-
use secp256k1::Secp256k1;
98

109
#[inline]
1110
fn slice_to_be16(v: &[u8]) -> u16 {
@@ -31,14 +30,13 @@ pub fn do_test(data: &[u8]) {
3130
}
3231
}
3332

34-
let secp_ctx = Secp256k1::new();
35-
let our_network_key = match SecretKey::from_slice(&secp_ctx, get_slice!(32)) {
33+
let our_network_key = match SecretKey::from_slice(get_slice!(32)) {
3634
Ok(key) => key,
3735
Err(_) => return,
3836
};
3937

4038
let mut crypter = if get_slice!(1)[0] != 0 {
41-
let their_pubkey = match PublicKey::from_slice(&secp_ctx, get_slice!(33)) {
39+
let their_pubkey = match PublicKey::from_slice(get_slice!(33)) {
4240
Ok(key) => key,
4341
Err(_) => return,
4442
};

fuzz/fuzz_targets/router_target.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use lightning::util::logger::Logger;
1515
use lightning::util::ser::Readable;
1616

1717
use secp256k1::key::PublicKey;
18-
use secp256k1::Secp256k1;
1918

2019
mod utils;
2120

@@ -146,10 +145,9 @@ pub fn do_test(data: &[u8]) {
146145
}
147146
}
148147

149-
let secp_ctx = Secp256k1::new();
150148
macro_rules! get_pubkey {
151149
() => {
152-
match PublicKey::from_slice(&secp_ctx, get_slice!(33)) {
150+
match PublicKey::from_slice(get_slice!(33)) {
153151
Ok(key) => key,
154152
Err(_) => return,
155153
}

src/chain/keysinterface.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ impl KeysManager {
133133
/// RNG is busted) this may panic.
134134
pub fn new(seed: &[u8; 32], network: Network, logger: Arc<Logger>) -> KeysManager {
135135
let secp_ctx = Secp256k1::new();
136-
match ExtendedPrivKey::new_master(&secp_ctx, network.clone(), seed) {
136+
match ExtendedPrivKey::new_master(network.clone(), seed) {
137137
Ok(master_key) => {
138138
let node_secret = master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(0)).expect("Your RNG is busted").secret_key;
139139
let destination_script = match master_key.ckd_priv(&secp_ctx, ChildNumber::from_hardened_idx(1)) {
140140
Ok(destination_key) => {
141141
let pubkey_hash160 = Hash160::hash(&ExtendedPubKey::from_private(&secp_ctx, &destination_key).public_key.serialize()[..]);
142-
Builder::new().push_opcode(opcodes::All::OP_PUSHBYTES_0)
142+
Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
143143
.push_slice(&pubkey_hash160.into_inner())
144144
.into_script()
145145
},
@@ -215,7 +215,7 @@ impl KeysInterface for KeysManager {
215215
sha.input(&seed);
216216
sha.input(&$prev_key[..]);
217217
sha.input(&$info[..]);
218-
SecretKey::from_slice(&self.secp_ctx, &Sha256::from_engine(sha).into_inner()).expect("SHA-256 is busted")
218+
SecretKey::from_slice(&Sha256::from_engine(sha).into_inner()).expect("SHA-256 is busted")
219219
}}
220220
}
221221
let funding_key = key_step!(b"funding key", commitment_seed);
@@ -244,6 +244,6 @@ impl KeysInterface for KeysManager {
244244
let child_ix = self.session_child_index.fetch_add(1, Ordering::AcqRel);
245245
let child_privkey = self.session_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32)).expect("Your RNG is busted");
246246
sha.input(&child_privkey.secret_key[..]);
247-
SecretKey::from_slice(&self.secp_ctx, &Sha256::from_engine(sha).into_inner()).expect("Your RNG is busted")
247+
SecretKey::from_slice(&Sha256::from_engine(sha).into_inner()).expect("Your RNG is busted")
248248
}
249249
}

src/ln/chan_utils.rs

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn derive_private_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_co
3939
let res = Sha256::from_engine(sha).into_inner();
4040

4141
let mut key = base_secret.clone();
42-
key.add_assign(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &res)?)?;
42+
key.add_assign(&res)?;
4343
Ok(key)
4444
}
4545

@@ -49,8 +49,8 @@ pub fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_com
4949
sha.input(&base_point.serialize());
5050
let res = Sha256::from_engine(sha).into_inner();
5151

52-
let hashkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &res)?);
53-
base_point.combine(&secp_ctx, &hashkey)
52+
let hashkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&res)?);
53+
base_point.combine(&hashkey)
5454
}
5555

5656
/// Derives a revocation key from its constituent parts
@@ -63,21 +63,21 @@ pub fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1
6363
sha.input(&revocation_base_point.serialize());
6464
sha.input(&per_commitment_point.serialize());
6565

66-
SecretKey::from_slice(&secp_ctx, &Sha256::from_engine(sha).into_inner())?
66+
Sha256::from_engine(sha).into_inner()
6767
};
6868
let commit_append_rev_hash_key = {
6969
let mut sha = Sha256::engine();
7070
sha.input(&per_commitment_point.serialize());
7171
sha.input(&revocation_base_point.serialize());
7272

73-
SecretKey::from_slice(&secp_ctx, &Sha256::from_engine(sha).into_inner())?
73+
Sha256::from_engine(sha).into_inner()
7474
};
7575

7676
let mut part_a = revocation_base_secret.clone();
77-
part_a.mul_assign(&secp_ctx, &rev_append_commit_hash_key)?;
77+
part_a.mul_assign(&rev_append_commit_hash_key)?;
7878
let mut part_b = per_commitment_secret.clone();
79-
part_b.mul_assign(&secp_ctx, &commit_append_rev_hash_key)?;
80-
part_a.add_assign(&secp_ctx, &part_b)?;
79+
part_b.mul_assign(&commit_append_rev_hash_key)?;
80+
part_a.add_assign(&part_b[..])?;
8181
Ok(part_a)
8282
}
8383

@@ -87,21 +87,21 @@ pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp2
8787
sha.input(&revocation_base_point.serialize());
8888
sha.input(&per_commitment_point.serialize());
8989

90-
SecretKey::from_slice(&secp_ctx, &Sha256::from_engine(sha).into_inner())?
90+
Sha256::from_engine(sha).into_inner()
9191
};
9292
let commit_append_rev_hash_key = {
9393
let mut sha = Sha256::engine();
9494
sha.input(&per_commitment_point.serialize());
9595
sha.input(&revocation_base_point.serialize());
9696

97-
SecretKey::from_slice(&secp_ctx, &Sha256::from_engine(sha).into_inner())?
97+
Sha256::from_engine(sha).into_inner()
9898
};
9999

100100
let mut part_a = revocation_base_point.clone();
101101
part_a.mul_assign(&secp_ctx, &rev_append_commit_hash_key)?;
102102
let mut part_b = per_commitment_point.clone();
103103
part_b.mul_assign(&secp_ctx, &commit_append_rev_hash_key)?;
104-
part_a.combine(&secp_ctx, &part_b)
104+
part_a.combine(&part_b)
105105
}
106106

107107
pub struct TxCreationKeys {
@@ -129,15 +129,15 @@ impl TxCreationKeys {
129129
/// Gets the "to_local" output redeemscript, ie the script which is time-locked or spendable by
130130
/// the revocation key
131131
pub fn get_revokeable_redeemscript(revocation_key: &PublicKey, to_self_delay: u16, delayed_payment_key: &PublicKey) -> Script {
132-
Builder::new().push_opcode(opcodes::All::OP_IF)
132+
Builder::new().push_opcode(opcodes::all::OP_IF)
133133
.push_slice(&revocation_key.serialize())
134-
.push_opcode(opcodes::All::OP_ELSE)
134+
.push_opcode(opcodes::all::OP_ELSE)
135135
.push_int(to_self_delay as i64)
136136
.push_opcode(opcodes::OP_CSV)
137-
.push_opcode(opcodes::All::OP_DROP)
137+
.push_opcode(opcodes::all::OP_DROP)
138138
.push_slice(&delayed_payment_key.serialize())
139-
.push_opcode(opcodes::All::OP_ENDIF)
140-
.push_opcode(opcodes::All::OP_CHECKSIG)
139+
.push_opcode(opcodes::all::OP_ENDIF)
140+
.push_opcode(opcodes::all::OP_CHECKSIG)
141141
.into_script()
142142
}
143143

@@ -154,63 +154,63 @@ pub struct HTLCOutputInCommitment {
154154
pub fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, a_htlc_key: &PublicKey, b_htlc_key: &PublicKey, revocation_key: &PublicKey) -> Script {
155155
let payment_hash160 = Ripemd160::hash(&htlc.payment_hash.0[..]).into_inner();
156156
if htlc.offered {
157-
Builder::new().push_opcode(opcodes::All::OP_DUP)
158-
.push_opcode(opcodes::All::OP_HASH160)
157+
Builder::new().push_opcode(opcodes::all::OP_DUP)
158+
.push_opcode(opcodes::all::OP_HASH160)
159159
.push_slice(&Hash160::hash(&revocation_key.serialize())[..])
160-
.push_opcode(opcodes::All::OP_EQUAL)
161-
.push_opcode(opcodes::All::OP_IF)
162-
.push_opcode(opcodes::All::OP_CHECKSIG)
163-
.push_opcode(opcodes::All::OP_ELSE)
160+
.push_opcode(opcodes::all::OP_EQUAL)
161+
.push_opcode(opcodes::all::OP_IF)
162+
.push_opcode(opcodes::all::OP_CHECKSIG)
163+
.push_opcode(opcodes::all::OP_ELSE)
164164
.push_slice(&b_htlc_key.serialize()[..])
165-
.push_opcode(opcodes::All::OP_SWAP)
166-
.push_opcode(opcodes::All::OP_SIZE)
165+
.push_opcode(opcodes::all::OP_SWAP)
166+
.push_opcode(opcodes::all::OP_SIZE)
167167
.push_int(32)
168-
.push_opcode(opcodes::All::OP_EQUAL)
169-
.push_opcode(opcodes::All::OP_NOTIF)
170-
.push_opcode(opcodes::All::OP_DROP)
168+
.push_opcode(opcodes::all::OP_EQUAL)
169+
.push_opcode(opcodes::all::OP_NOTIF)
170+
.push_opcode(opcodes::all::OP_DROP)
171171
.push_int(2)
172-
.push_opcode(opcodes::All::OP_SWAP)
172+
.push_opcode(opcodes::all::OP_SWAP)
173173
.push_slice(&a_htlc_key.serialize()[..])
174174
.push_int(2)
175-
.push_opcode(opcodes::All::OP_CHECKMULTISIG)
176-
.push_opcode(opcodes::All::OP_ELSE)
177-
.push_opcode(opcodes::All::OP_HASH160)
175+
.push_opcode(opcodes::all::OP_CHECKMULTISIG)
176+
.push_opcode(opcodes::all::OP_ELSE)
177+
.push_opcode(opcodes::all::OP_HASH160)
178178
.push_slice(&payment_hash160)
179-
.push_opcode(opcodes::All::OP_EQUALVERIFY)
180-
.push_opcode(opcodes::All::OP_CHECKSIG)
181-
.push_opcode(opcodes::All::OP_ENDIF)
182-
.push_opcode(opcodes::All::OP_ENDIF)
179+
.push_opcode(opcodes::all::OP_EQUALVERIFY)
180+
.push_opcode(opcodes::all::OP_CHECKSIG)
181+
.push_opcode(opcodes::all::OP_ENDIF)
182+
.push_opcode(opcodes::all::OP_ENDIF)
183183
.into_script()
184184
} else {
185-
Builder::new().push_opcode(opcodes::All::OP_DUP)
186-
.push_opcode(opcodes::All::OP_HASH160)
185+
Builder::new().push_opcode(opcodes::all::OP_DUP)
186+
.push_opcode(opcodes::all::OP_HASH160)
187187
.push_slice(&Hash160::hash(&revocation_key.serialize())[..])
188-
.push_opcode(opcodes::All::OP_EQUAL)
189-
.push_opcode(opcodes::All::OP_IF)
190-
.push_opcode(opcodes::All::OP_CHECKSIG)
191-
.push_opcode(opcodes::All::OP_ELSE)
188+
.push_opcode(opcodes::all::OP_EQUAL)
189+
.push_opcode(opcodes::all::OP_IF)
190+
.push_opcode(opcodes::all::OP_CHECKSIG)
191+
.push_opcode(opcodes::all::OP_ELSE)
192192
.push_slice(&b_htlc_key.serialize()[..])
193-
.push_opcode(opcodes::All::OP_SWAP)
194-
.push_opcode(opcodes::All::OP_SIZE)
193+
.push_opcode(opcodes::all::OP_SWAP)
194+
.push_opcode(opcodes::all::OP_SIZE)
195195
.push_int(32)
196-
.push_opcode(opcodes::All::OP_EQUAL)
197-
.push_opcode(opcodes::All::OP_IF)
198-
.push_opcode(opcodes::All::OP_HASH160)
196+
.push_opcode(opcodes::all::OP_EQUAL)
197+
.push_opcode(opcodes::all::OP_IF)
198+
.push_opcode(opcodes::all::OP_HASH160)
199199
.push_slice(&payment_hash160)
200-
.push_opcode(opcodes::All::OP_EQUALVERIFY)
200+
.push_opcode(opcodes::all::OP_EQUALVERIFY)
201201
.push_int(2)
202-
.push_opcode(opcodes::All::OP_SWAP)
202+
.push_opcode(opcodes::all::OP_SWAP)
203203
.push_slice(&a_htlc_key.serialize()[..])
204204
.push_int(2)
205-
.push_opcode(opcodes::All::OP_CHECKMULTISIG)
206-
.push_opcode(opcodes::All::OP_ELSE)
207-
.push_opcode(opcodes::All::OP_DROP)
205+
.push_opcode(opcodes::all::OP_CHECKMULTISIG)
206+
.push_opcode(opcodes::all::OP_ELSE)
207+
.push_opcode(opcodes::all::OP_DROP)
208208
.push_int(htlc.cltv_expiry as i64)
209209
.push_opcode(opcodes::OP_CLTV)
210-
.push_opcode(opcodes::All::OP_DROP)
211-
.push_opcode(opcodes::All::OP_CHECKSIG)
212-
.push_opcode(opcodes::All::OP_ENDIF)
213-
.push_opcode(opcodes::All::OP_ENDIF)
210+
.push_opcode(opcodes::all::OP_DROP)
211+
.push_opcode(opcodes::all::OP_CHECKSIG)
212+
.push_opcode(opcodes::all::OP_ENDIF)
213+
.push_opcode(opcodes::all::OP_ENDIF)
214214
.into_script()
215215
}
216216
}

0 commit comments

Comments
 (0)