libp2p-hs-0.1.0.0: Haskell implementation of the libp2p networking stack
Safe HaskellNone
LanguageGHC2021

Network.LibP2P.DHT.Distance

Description

XOR distance metric for the Kademlia DHT.

All distance computations operate on 256-bit SHA-256 keys. Distance is the XOR of two keys interpreted as a 256-bit unsigned integer.

Synopsis

Documentation

peerIdToKey :: PeerId -> DHTKey Source #

Convert a Peer ID to its DHT key by hashing with SHA-256.

xorDistance :: DHTKey -> DHTKey -> DHTKey Source #

Compute XOR distance between two DHT keys.

commonPrefixLength :: DHTKey -> DHTKey -> Int Source #

Count the number of leading zero bits (common prefix length). Same key → 256. First bit differs → 0.

compareDistance :: DHTKey -> DHTKey -> DHTKey -> Ordering Source #

Compare two keys by distance to a target. Returns LT if a is closer to target than b, GT if farther, EQ if equal.

sortByDistance :: DHTKey -> [BucketEntry] -> [BucketEntry] Source #

Sort bucket entries by ascending XOR distance to a target key.