gpt4 book ai didi

go - 更好地理解 Kademlia 的 XOR Integer Metric

转载 作者:IT王子 更新时间:2023-10-29 01:44:01 24 4
gpt4 key购买 nike

我试图更好地掌握 Kademlia 的 XOR 距离度量,因此我编写了一个小的虚拟程序来尝试更好地理解。我在这里也没有使用 160 位数字作为我的 key ,而是使用某个用户标识符的 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[:])
}

关于go - 更好地理解 Kademlia 的 XOR Integer Metric,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53166625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com