X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fscid_utils.rs;h=fbbcc69a133e0966e8bd937d1838ac8dc4fd5c17;hb=62fd36d795f99887260aa962a0e8f30e3b5504fa;hp=f85e843d8184e5c55c53fec6553d30dfc074cb62;hpb=e1c33d49f03b08e979e873b3842c5413fd9cb0e0;p=rust-lightning diff --git a/lightning/src/util/scid_utils.rs b/lightning/src/util/scid_utils.rs index f85e843d..fbbcc69a 100644 --- a/lightning/src/util/scid_utils.rs +++ b/lightning/src/util/scid_utils.rs @@ -20,7 +20,7 @@ pub const MAX_SCID_TX_INDEX: u64 = 0x00ffffff; pub const MAX_SCID_VOUT_INDEX: u64 = 0xffff; /// A `short_channel_id` construction error -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Eq)] pub enum ShortChannelIdError { BlockOverflow, TxIndexOverflow, @@ -60,6 +60,199 @@ pub fn scid_from_parts(block: u64, tx_index: u64, vout_index: u64) -> Result(&self, highest_seen_blockheight: u32, chain_hash: &ChainHash, fake_scid_rand_bytes: &[u8; 32], entropy_source: &ES) -> u64 + where ES::Target: EntropySource, + { + // Ensure we haven't created a namespace that doesn't fit into the 3 bits we've allocated for + // namespaces. + assert!((*self as u8) < MAX_NAMESPACES); + let rand_bytes = entropy_source.get_secure_random_bytes(); + + let segwit_activation_height = segwit_activation_height(chain_hash); + let mut blocks_since_segwit_activation = highest_seen_blockheight.saturating_sub(segwit_activation_height); + + // We want to ensure that this fake channel won't conflict with any transactions we haven't + // seen yet, in case `highest_seen_blockheight` is updated before we get full information + // about transactions confirmed in the given block. + blocks_since_segwit_activation = blocks_since_segwit_activation.saturating_sub(MAX_SCID_BLOCKS_FROM_NOW); + + let rand_for_height = u32::from_be_bytes(rand_bytes[..4].try_into().unwrap()); + let fake_scid_height = segwit_activation_height + rand_for_height % (blocks_since_segwit_activation + 1); + + let rand_for_tx_index = u32::from_be_bytes(rand_bytes[4..8].try_into().unwrap()); + let fake_scid_tx_index = rand_for_tx_index % MAX_TX_INDEX; + + // Put the scid in the given namespace. + let fake_scid_vout = self.get_encrypted_vout(fake_scid_height, fake_scid_tx_index, fake_scid_rand_bytes); + scid_utils::scid_from_parts(fake_scid_height as u64, fake_scid_tx_index as u64, fake_scid_vout as u64).unwrap() + } + + /// We want to ensure that a 3rd party can't identify a payment as belong to a given + /// `Namespace`. Therefore, we encrypt it using a random bytes provided by `ChannelManager`. + fn get_encrypted_vout(&self, block_height: u32, tx_index: u32, fake_scid_rand_bytes: &[u8; 32]) -> u8 { + let mut salt = [0 as u8; 8]; + let block_height_bytes = block_height.to_be_bytes(); + salt[0..4].copy_from_slice(&block_height_bytes); + let tx_index_bytes = tx_index.to_be_bytes(); + salt[4..8].copy_from_slice(&tx_index_bytes); + + let mut chacha = ChaCha20::new(fake_scid_rand_bytes, &salt); + let mut vout_byte = [*self as u8]; + chacha.process_in_place(&mut vout_byte); + vout_byte[0] & NAMESPACE_ID_BITMASK + } + } + + fn segwit_activation_height(chain_hash: &ChainHash) -> u32 { + if *chain_hash == ChainHash::using_genesis_block(Network::Bitcoin) { + MAINNET_SEGWIT_ACTIVATION_HEIGHT + } else { + TEST_SEGWIT_ACTIVATION_HEIGHT + } + } + + /// Returns whether the given fake scid falls into the phantom namespace. + pub fn is_valid_phantom(fake_scid_rand_bytes: &[u8; 32], scid: u64, chain_hash: &ChainHash) -> bool { + let block_height = scid_utils::block_from_scid(&scid); + let tx_index = scid_utils::tx_index_from_scid(&scid); + let namespace = Namespace::Phantom; + let valid_vout = namespace.get_encrypted_vout(block_height, tx_index, fake_scid_rand_bytes); + block_height >= segwit_activation_height(chain_hash) + && valid_vout == scid_utils::vout_from_scid(&scid) as u8 + } + + /// Returns whether the given fake scid falls into the intercept namespace. + pub fn is_valid_intercept(fake_scid_rand_bytes: &[u8; 32], scid: u64, chain_hash: &ChainHash) -> bool { + let block_height = scid_utils::block_from_scid(&scid); + let tx_index = scid_utils::tx_index_from_scid(&scid); + let namespace = Namespace::Intercept; + let valid_vout = namespace.get_encrypted_vout(block_height, tx_index, fake_scid_rand_bytes); + block_height >= segwit_activation_height(chain_hash) + && valid_vout == scid_utils::vout_from_scid(&scid) as u8 + } + + #[cfg(test)] + mod tests { + use bitcoin::blockdata::constants::ChainHash; + use bitcoin::network::constants::Network; + use crate::util::scid_utils::fake_scid::{is_valid_intercept, is_valid_phantom, MAINNET_SEGWIT_ACTIVATION_HEIGHT, MAX_TX_INDEX, MAX_NAMESPACES, Namespace, NAMESPACE_ID_BITMASK, segwit_activation_height, TEST_SEGWIT_ACTIVATION_HEIGHT}; + use crate::util::scid_utils; + use crate::util::test_utils; + use crate::sync::Arc; + + #[test] + fn namespace_identifier_is_within_range() { + let phantom_namespace = Namespace::Phantom; + assert!((phantom_namespace as u8) < MAX_NAMESPACES); + assert!((phantom_namespace as u8) <= NAMESPACE_ID_BITMASK); + + let intercept_namespace = Namespace::Intercept; + assert!((intercept_namespace as u8) < MAX_NAMESPACES); + assert!((intercept_namespace as u8) <= NAMESPACE_ID_BITMASK); + } + + #[test] + fn test_segwit_activation_height() { + let mainnet_genesis = ChainHash::using_genesis_block(Network::Bitcoin); + assert_eq!(segwit_activation_height(&mainnet_genesis), MAINNET_SEGWIT_ACTIVATION_HEIGHT); + + let testnet_genesis = ChainHash::using_genesis_block(Network::Testnet); + assert_eq!(segwit_activation_height(&testnet_genesis), TEST_SEGWIT_ACTIVATION_HEIGHT); + + let signet_genesis = ChainHash::using_genesis_block(Network::Signet); + assert_eq!(segwit_activation_height(&signet_genesis), TEST_SEGWIT_ACTIVATION_HEIGHT); + + let regtest_genesis = ChainHash::using_genesis_block(Network::Regtest); + assert_eq!(segwit_activation_height(®test_genesis), TEST_SEGWIT_ACTIVATION_HEIGHT); + } + + #[test] + fn test_is_valid_phantom() { + let namespace = Namespace::Phantom; + let fake_scid_rand_bytes = [0; 32]; + let testnet_genesis = ChainHash::using_genesis_block(Network::Testnet); + let valid_encrypted_vout = namespace.get_encrypted_vout(0, 0, &fake_scid_rand_bytes); + let valid_fake_scid = scid_utils::scid_from_parts(1, 0, valid_encrypted_vout as u64).unwrap(); + assert!(is_valid_phantom(&fake_scid_rand_bytes, valid_fake_scid, &testnet_genesis)); + let invalid_fake_scid = scid_utils::scid_from_parts(1, 0, 12).unwrap(); + assert!(!is_valid_phantom(&fake_scid_rand_bytes, invalid_fake_scid, &testnet_genesis)); + } + + #[test] + fn test_is_valid_intercept() { + let namespace = Namespace::Intercept; + let fake_scid_rand_bytes = [0; 32]; + let testnet_genesis = ChainHash::using_genesis_block(Network::Testnet); + let valid_encrypted_vout = namespace.get_encrypted_vout(0, 0, &fake_scid_rand_bytes); + let valid_fake_scid = scid_utils::scid_from_parts(1, 0, valid_encrypted_vout as u64).unwrap(); + assert!(is_valid_intercept(&fake_scid_rand_bytes, valid_fake_scid, &testnet_genesis)); + let invalid_fake_scid = scid_utils::scid_from_parts(1, 0, 12).unwrap(); + assert!(!is_valid_intercept(&fake_scid_rand_bytes, invalid_fake_scid, &testnet_genesis)); + } + + #[test] + fn test_get_fake_scid() { + let mainnet_genesis = ChainHash::using_genesis_block(Network::Bitcoin); + let seed = [0; 32]; + let fake_scid_rand_bytes = [1; 32]; + let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet)); + let namespace = Namespace::Phantom; + let fake_scid = namespace.get_fake_scid(500_000, &mainnet_genesis, &fake_scid_rand_bytes, &keys_manager); + + let fake_height = scid_utils::block_from_scid(&fake_scid); + assert!(fake_height >= MAINNET_SEGWIT_ACTIVATION_HEIGHT); + assert!(fake_height <= 500_000); + + let fake_tx_index = scid_utils::tx_index_from_scid(&fake_scid); + assert!(fake_tx_index <= MAX_TX_INDEX); + + let fake_vout = scid_utils::vout_from_scid(&fake_scid); + assert!(fake_vout < MAX_NAMESPACES as u16); + } + } +} + #[cfg(test)] mod tests { use super::*;