dongling2545 2018-11-06 06:15
浏览 89
已采纳

更好地了解Kademlia的XOR整数指标

I'm trying to better-grasp Kademlia's XOR distance metric so I've written a small dummy program to try and understand better. I'm also not using a 160-bit number as my key here, but rather a sha256 hash of some user identifier.

Here's my xor distance function. Is this more or less correct? I'm XORing each byte– appending that to a buffer rawBytes and converting that byte buffer into an integer.

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
}
  • 写回答

1条回答 默认 最新

  • douya7121 2018-11-06 11:16
    关注

    It's not correct because

    You have to use the math/big package for usage like that. Here is my revised version of your snippet:

    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[:])
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程