macula_routing_nodeid (macula v0.20.5)
View SourceNode ID utilities for Kademlia DHT. 256-bit node identifiers with XOR distance metric.
Summary
Functions
Calculate bucket index for a node relative to local node. Returns leading zero count of XOR distance (0..255). Special case: distance 0 (same node) returns 256. Normalizes inputs to 32 bytes if needed.
Check if NodeA is closer to Target than NodeB. Normalizes inputs to 32 bytes if needed.
Compare distances of NodeA and NodeB to Target. Returns: less (A closer), equal (same distance), greater (B closer). Normalizes inputs to 32 bytes if needed.
Calculate XOR distance between two node IDs. Normalizes inputs to 32 bytes if needed.
Create node ID from binary (validates size).
Parse node ID from hex string. Crashes on invalid hex or wrong length - exposes bugs in validation logic.
Generate a random 256-bit node ID.
Count leading zero bits in binary.
Normalize any binary to a 32-byte node ID. If already 32 bytes, returns as-is. Otherwise, hashes with SHA-256.
Convert node ID to hex string.
Types
-type node_id() :: binary().
32 bytes (256 bits)
Functions
Calculate bucket index for a node relative to local node. Returns leading zero count of XOR distance (0..255). Special case: distance 0 (same node) returns 256. Normalizes inputs to 32 bytes if needed.
Check if NodeA is closer to Target than NodeB. Normalizes inputs to 32 bytes if needed.
Compare distances of NodeA and NodeB to Target. Returns: less (A closer), equal (same distance), greater (B closer). Normalizes inputs to 32 bytes if needed.
Calculate XOR distance between two node IDs. Normalizes inputs to 32 bytes if needed.
Create node ID from binary (validates size).
Parse node ID from hex string. Crashes on invalid hex or wrong length - exposes bugs in validation logic.
-spec generate() -> node_id().
Generate a random 256-bit node ID.
-spec leading_zeros(binary()) -> 0..256.
Count leading zero bits in binary.
Normalize any binary to a 32-byte node ID. If already 32 bytes, returns as-is. Otherwise, hashes with SHA-256.
Convert node ID to hex string.