use sha1::{Digest, Sha1};
use std::net::{Ipv4Addr, Ipv6Addr};
use std::str::FromStr;
pub fn compute_fast_set(
ip: &str,
num_pieces: u32,
info_hash: &[u8; 20],
set_size: usize,
) -> Vec<u32> {
if num_pieces == 0 || set_size == 0 {
return Vec::new();
}
let ip_bytes = match resolve_ip_bytes(ip) {
Some(bytes) => bytes,
None => return Vec::new(),
};
let masked_ip = mask_ip(ip_bytes);
let mut tx = [0u8; 24];
tx[..4].copy_from_slice(&masked_ip);
tx[4..].copy_from_slice(info_hash);
let mut x = sha1_digest(&tx);
let k = set_size.min(num_pieces as usize);
let mut fast_set = Vec::with_capacity(k);
while fast_set.len() < k {
for i in 0..5 {
if fast_set.len() >= k {
break;
}
let offset = i * 4;
let y = u32::from_be_bytes([x[offset], x[offset + 1], x[offset + 2], x[offset + 3]]);
let index = y % num_pieces;
if !fast_set.contains(&index) {
fast_set.push(index);
}
}
x = sha1_digest(&x);
}
fast_set
}
fn resolve_ip_bytes(ip: &str) -> Option<[u8; 4]> {
if let Ok(v4) = Ipv4Addr::from_str(ip) {
return Some(v4.octets());
}
if let Ok(v6) = Ipv6Addr::from_str(ip) {
let hash = sha1_digest(&v6.octets());
Some([hash[0], hash[1], hash[2], hash[3]])
} else {
None
}
}
fn mask_ip(mut ip: [u8; 4]) -> [u8; 4] {
if (ip[0] & 0x80) == 0 || (ip[0] & 0x40) == 0 {
ip[2] = 0;
ip[3] = 0;
} else {
ip[3] = 0;
}
ip
}
fn sha1_digest(data: &[u8]) -> [u8; 20] {
let mut hasher = Sha1::new();
hasher.update(data);
let result = hasher.finalize();
let mut out = [0u8; 20];
out.copy_from_slice(&result);
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cpp_vector_192_168_0_1() {
let mut info_hash = [0u8; 20];
info_hash[0] = 0xFF;
let fast = compute_fast_set("192.168.0.1", 1000, &info_hash, 10);
let expected: Vec<u32> = vec![686, 459, 278, 200, 404, 834, 64, 203, 760, 950];
assert_eq!(fast, expected);
}
#[test]
fn test_cpp_vector_10_0_0_1() {
let mut info_hash = [0u8; 20];
info_hash[0] = 0xFF;
let fast = compute_fast_set("10.0.0.1", 1000, &info_hash, 10);
let expected: Vec<u32> = vec![568, 188, 466, 452, 550, 662, 109, 226, 398, 11];
assert_eq!(fast, expected);
}
#[test]
fn test_cpp_vector_fewer_pieces_than_set_size() {
let mut info_hash = [0u8; 20];
info_hash[0] = 0xFF;
let fast = compute_fast_set("10.0.0.1", 9, &info_hash, 10);
let expected: Vec<u32> = vec![8, 6, 7, 5, 1, 4, 0, 2, 3];
assert_eq!(fast, expected);
}
#[test]
fn test_zero_pieces_returns_empty() {
let info_hash = [0u8; 20];
let fast = compute_fast_set("192.168.0.1", 0, &info_hash, 10);
assert!(fast.is_empty());
}
#[test]
fn test_zero_set_size_returns_empty() {
let info_hash = [0u8; 20];
let fast = compute_fast_set("192.168.0.1", 100, &info_hash, 0);
assert!(fast.is_empty());
}
#[test]
fn test_invalid_ip_returns_empty() {
let info_hash = [0u8; 20];
let fast = compute_fast_set("not-an-ip", 100, &info_hash, 10);
assert!(fast.is_empty());
}
#[test]
fn test_single_piece() {
let info_hash = [0u8; 20];
let fast = compute_fast_set("192.168.0.1", 1, &info_hash, 10);
assert_eq!(fast, vec![0]);
}
#[test]
fn test_all_indices_within_range() {
let info_hash = [0u8; 20];
let num_pieces = 500u32;
let fast = compute_fast_set("192.168.0.1", num_pieces, &info_hash, 10);
assert_eq!(fast.len(), 10);
for &idx in &fast {
assert!(
idx < num_pieces,
"index {} >= num_pieces {}",
idx,
num_pieces
);
}
}
#[test]
fn test_no_duplicates() {
let info_hash = [0u8; 20];
let fast = compute_fast_set("192.168.0.1", 1000, &info_hash, 10);
let mut seen = std::collections::HashSet::new();
for &idx in &fast {
assert!(seen.insert(idx), "duplicate index {}", idx);
}
}
#[test]
fn test_set_size_capped_to_num_pieces() {
let info_hash = [0u8; 20];
let fast = compute_fast_set("192.168.0.1", 5, &info_hash, 100);
assert!(fast.len() <= 5);
for &idx in &fast {
assert!(idx < 5);
}
}
#[test]
fn test_mask_class_a_ip() {
let result = mask_ip([10, 0, 0, 1]);
assert_eq!(result, [10, 0, 0, 0]);
}
#[test]
fn test_mask_class_b_ip() {
let result = mask_ip([172, 16, 0, 1]);
assert_eq!(result, [172, 16, 0, 0]);
}
#[test]
fn test_mask_class_c_ip() {
let result = mask_ip([192, 168, 0, 1]);
assert_eq!(result, [192, 168, 0, 0]);
}
#[test]
fn test_mask_preserves_subnet_for_class_c() {
let a = mask_ip([192, 168, 1, 10]);
let b = mask_ip([192, 168, 1, 200]);
assert_eq!(a, b);
}
#[test]
fn test_ipv6_returns_nonempty() {
let info_hash = [0u8; 20];
let fast = compute_fast_set("::1", 1000, &info_hash, 10);
assert!(!fast.is_empty());
assert!(fast.len() <= 10);
}
#[test]
fn test_ipv6_all_indices_within_range() {
let info_hash = [0u8; 20];
let fast = compute_fast_set("2001:db8::1", 500, &info_hash, 10);
for &idx in &fast {
assert!(idx < 500);
}
}
#[test]
fn test_ipv6_deterministic() {
let info_hash = [0u8; 20];
let a = compute_fast_set("::1", 1000, &info_hash, 10);
let b = compute_fast_set("::1", 1000, &info_hash, 10);
assert_eq!(a, b);
}
#[test]
fn test_ipv6_different_addresses_different_sets() {
let info_hash = [0u8; 20];
let a = compute_fast_set("::1", 1000, &info_hash, 10);
let b = compute_fast_set("::2", 1000, &info_hash, 10);
assert!(!a.is_empty());
assert!(!b.is_empty());
}
#[test]
fn test_deterministic_same_inputs_same_output() {
let info_hash = [0xAB; 20];
let a = compute_fast_set("192.168.1.1", 2000, &info_hash, 10);
let b = compute_fast_set("192.168.1.1", 2000, &info_hash, 10);
assert_eq!(a, b);
}
#[test]
fn test_different_info_hash_different_set() {
let hash_a = [0x00; 20];
let hash_b = [0xFF; 20];
let a = compute_fast_set("192.168.0.1", 1000, &hash_a, 10);
let b = compute_fast_set("192.168.0.1", 1000, &hash_b, 10);
assert!(!a.is_empty());
assert!(!b.is_empty());
}
#[test]
fn test_resolve_ipv4() {
let bytes = resolve_ip_bytes("192.168.0.1").unwrap();
assert_eq!(bytes, [192, 168, 0, 1]);
}
#[test]
fn test_resolve_ipv6_loopback() {
let bytes = resolve_ip_bytes("::1");
assert!(bytes.is_some());
}
#[test]
fn test_resolve_invalid_ip() {
assert!(resolve_ip_bytes("invalid").is_none());
}
#[test]
fn test_sha1_known_vector() {
let hash = sha1_digest(b"");
assert_eq!(
hex::encode(hash),
"da39a3ee5e6b4b0d3255bfef95601890afd80709"
);
}
#[test]
fn test_sha1_abc() {
let hash = sha1_digest(b"abc");
assert_eq!(
hex::encode(hash),
"a9993e364706816aba3e25717850c26c9cd0d89d"
);
}
}