Skip to content

Simplify + clarify random-bytes-fetching from KeysInterface #674

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

Conversation

TheBlueMatt
Copy link
Collaborator

Due to a desire to be able to override temporary channel IDs and
onion keys, KeysInterface had two separate fetch-random-32-bytes
interfaces - an onion-key specific version which fetched 2 random
32 byte strings and a temporary-channel-id specific version.

It turns out, we never actually need to override both at once (as
creating a new channel and sending an outbound payment are always
separate top-level calls), so there's no reason to add two
functions to the interface when both really do the same thing.

@TheBlueMatt TheBlueMatt mentioned this pull request Aug 23, 2020
9 tasks
@codecov
Copy link

codecov bot commented Aug 23, 2020

Codecov Report

Merging #674 into master will increase coverage by 0.09%.
The diff coverage is 95.45%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #674      +/-   ##
==========================================
+ Coverage   91.36%   91.45%   +0.09%     
==========================================
  Files          35       35              
  Lines       21703    22198     +495     
==========================================
+ Hits        19828    20302     +474     
- Misses       1875     1896      +21     
Impacted Files Coverage Δ
lightning/src/util/test_utils.rs 85.77% <90.00%> (-0.37%) ⬇️
lightning/src/chain/keysinterface.rs 94.84% <100.00%> (-0.20%) ⬇️
lightning/src/ln/channel.rs 87.23% <100.00%> (+0.03%) ⬆️
lightning/src/ln/channelmanager.rs 85.26% <100.00%> (+<0.01%) ⬆️
lightning/src/ln/functional_tests.rs 97.18% <100.00%> (+0.17%) ⬆️
lightning/src/ln/msgs.rs 91.43% <0.00%> (+1.11%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8ac0992...6497465. Read the comment docs.

/// transaction is created, at which point they will use the outpoint in the funding
/// transaction.
fn get_channel_id(&self) -> [u8; 32];
/// Gets a unique, cryptographically-secure, random 32 byte value. This is used for encrypting
Copy link

Choose a reason for hiding this comment

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

Is this an API requirement change to switch from using a PRNG to CSPRNG ? This is better, but if so rename prng_seed mentions.

Copy link
Collaborator Author

@TheBlueMatt TheBlueMatt Aug 23, 2020

Choose a reason for hiding this comment

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

I only see one doc comment mentioning RNG (which now says CSRNG).

let child_ix = self.channel_id_child_index.fetch_add(1, Ordering::AcqRel);
let child_privkey = self.channel_id_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32).expect("key space exhausted")).expect("Your RNG is busted");
let child_ix = self.rand_bytes_child_index.fetch_add(1, Ordering::AcqRel);
let child_privkey = self.rand_bytes_master_key.ckd_priv(&self.secp_ctx, ChildNumber::from_hardened_idx(child_ix as u32).expect("key space exhausted")).expect("Your RNG is busted");
Copy link

Choose a reason for hiding this comment

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

"Your CSRNG is busted" ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We have "RNG is busted" in a ton of places. I don't think its really all that critical to explicitly say CSRNG in a panic message (which even a biased RNG should suffice to not hit), as long as the docs (which hopefully is the only thing users ever see) are good.

let override_channel_id = self.override_channel_id_priv.lock().unwrap();
let override_session_key = self.override_session_priv.lock().unwrap();
if override_channel_id.is_some() && override_session_key.is_some() {
panic!("We don't know which override key to use!");
Copy link

Choose a reason for hiding this comment

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

By your commit message, you mean the reason to have both functions to the interface was to potentially override both keys in test framework, which we never need to do so actually ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Pretty much, yes, but it was also just a historical quirk - we had different ways to get data into the KeyInterface and the easiest way to keep that the way it was was to have multiple override hooks.

Due to a desire to be able to override temporary channel IDs and
onion keys, KeysInterface had two separate fetch-random-32-bytes
interfaces - an onion-key specific version which fetched 2 random
32 byte strings and a temporary-channel-id specific version.

It turns out, we never actually need to override both at once (as
creating a new channel and sending an outbound payment are always
separate top-level calls), so there's no reason to add two
functions to the interface when both really do the same thing.
@TheBlueMatt TheBlueMatt force-pushed the 2020-08-keyif-rand-names branch from cad08d1 to 6497465 Compare August 23, 2020 23:40
@ariard
Copy link

ariard commented Aug 26, 2020

Code Review ACK 6497465

@TheBlueMatt TheBlueMatt merged commit af69fae into lightningdevkit:master Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants