更好地理解 Kademlia 的 XOR Integer Metric

Better understanding Kademlia's XOR Integer Metric

我试图更好地掌握 Kademlia 的 XOR 距离度量,所以我编写了一个小的虚拟程序来尝试更好地理解。我在这里也没有使用 160 位数字作为我的密钥,而是使用某个用户标识符的 sha256 哈希值。

这是我的异或距离函数。这或多或少是正确的吗?我对每个字节进行异或运算——将其附加到缓冲区 rawBytes 并将该字节缓冲区转换为整数。

func XorDistance(node string, otherNode string) uint64 {
    var rawBytes [32]byte
    for i := 0; i < 32; i++ {
        rawBytes[i] = node[i] ^ otherNode[i]
    }
    distance, _ := binary.Uvarint(rawBytes[:])
    return distance
}

不正确,因为

您必须使用 math/big 包才能像这样使用。这是我对你的代码片段的修改版本:

func xorDistance(node string, otherNode string) *big.Int {
    var rawBytes [32]byte
    for i := 0; i < 32; i++ {
        rawBytes[i] = node[i] ^ otherNode[i]
    }
    return big.NewInt(0).SetBytes(rawBytes[:])
}