简体   繁体   English

Python - 如何将单词分成 64 位块

[英]Python - How to break word into 64bit chunks

I am doing an assignment where I have to institute the Diffie-Hellman key exchange.我正在执行一项任务,我必须建立 Diffie-Hellman 密钥交换。 In order to speed things up I am using bit-operators in Python, and everything is working fine programming wise, but I have to perform a Parity Checksum, and I don't think I have the proper understanding of what this is or how it works.为了加快速度,我在 Python 中使用了位运算符,并且一切都在编程方面运行良好,但我必须执行奇偶校验和,而且我认为我对这是什么或如何正确理解它作品。

Basically I need to be able to take a key of variable length (up to 2048 bits), break it into 64 bit words, and perform Checksum.基本上我需要能够获取可变长度(最多 2048 位)的密钥,将其分解为 64 位字,然后执行校验和。 I am unsure what this means exactly.我不确定这到底意味着什么。 To break a word into 64 bit words using Python, how would one go about that?要使用 Python 将一个单词分解为 64 位单词,该怎么做呢? I think once I do that I should be able to just perform an XOR operation on the words to get a 64bit output.我想一旦我这样做了,我应该能够对单词执行 XOR 操作以获得 64 位输出。 At the moment though I am stuck on exactly how one break up a word in 64 bit chunks in Python appropriately?目前,虽然我被困在如何在 Python 中正确地将一个单词分解为 64 位块?

A parity checksum is just the xor of all the bits in the word.奇偶校验和只是字中所有位的异或。 The most efficient way to do this will have log(nbits) operations, because you can halve the number of bits you are dealing with on each iteration.最有效的方法是使用 log(nbits) 操作,因为您可以将每次迭代处理的位数减半。 For example:例如:

def parity(word, nbits):
    if nbits & (nbits - 1):
        raise ValueError("nbits must be power of two")

    while nbits > 1:
        nbits >>= 1
        word ^= (word >> nbits)
    return word & 1

A longitudinal parity check is a bit different, because you stop when you get to a given word-size, at which point, your parity should be all zeros or all ones, rather than a single one or zero.纵向奇偶校验有点不同,因为当你到达给定的字长时你会停下来,此时你的奇偶校验应该是全零或全一,而不是单个一或零。 I don't know whether you want odd or even parity, so this is a bit more general:我不知道您是要奇偶校验还是偶校验,所以这更笼统:

def longitudinal_parity(data, total_bits, word_bits, expected_parity=1):
    """
    Performs longitudinal parity check
    """
    for nbits in (total_bits, word_bits):
        if nbits & (nbits - 1):
            raise ValueError("bit size must be power of two")

    mask = (1 << total_bits) - 1

    while total_bits > word_bits:
        total_bits >>= 1
        data ^= (data >> total_bits)
        mask >>= total_bits
        data &= mask
    return data == (mask if expected_parity else 0)

So for your example, the first parameter would be a 2048 bit integer, the total_bits would be 2048, the word_bits would be 64, and the desired parity would be 0 or 1.因此,对于您的示例,第一个参数是 2048 位整数,total_bits 是 2048,word_bits 是 64,所需的奇偶校验是 0 或 1。

I don't know anything about Diffie-Hellman's parity check, but if your parity is provided separately (seems likely), then you are comparing against a separate parity word rather than all ones or all zeroes.我对 Diffie-Hellman 的奇偶校验一无所知,但如果您的奇偶校验是单独提供的(似乎很可能),那么您将与一个单独的奇偶校验字进行比较,而不是与全 1 或全零进行比较。 This is a minor tweak:这是一个小调整:

def longitudinal_parity(data, total_bits, word_bits, expected_parity):
    """
    Performs longitudinal parity check
    """
    for nbits in (total_bits, word_bits):
        if nbits & (nbits - 1):
            raise ValueError("bit size must be power of two")

    mask = (1 << total_bits) - 1

    while total_bits > word_bits:
        total_bits >>= 1
        data ^= (data >> total_bits)
        mask >>= total_bits
        data &= mask
    return data == expected_parity

There are plenty of possible optimizations here, such as precalculating masks, starting the mask off at a smaller number, etc. Hopefully the code is readable.这里有很多可能的优化,例如预先计算掩码,以较小的数字开始掩码等。希望代码是可读的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM