Last active
October 11, 2023 09:49
-
-
Save itzmeanjan/a32eab0244af55eba2847c6472337535 to your computer and use it in GitHub Desktop.
Known Answer Tests for Multimixer-128: Universal Keyed Hashing, based on Integer Multiplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/ReferenceCode/Multimixer-128.py b/ReferenceCode/Multimixer-128.py | |
index 2f9b11e..54cb0a6 100644 | |
--- a/ReferenceCode/Multimixer-128.py | |
+++ b/ReferenceCode/Multimixer-128.py | |
@@ -96,9 +96,70 @@ def Int_multimix(M,K): | |
#256 = block size of Multimixer-128 | |
-l = int(input("Enter message Length: ")) | |
+def gen_rand_bytes(l: int) -> bytes: | |
+ return bytes([random.randint(0x00, 0xff) for i in range(l)]) | |
-Msg = rand_key(l*256) | |
-Key = rand_key(l*256) | |
+def bytes_to_bitstring(data: bytes, word_size_bits: int = 32) -> str: | |
+ word_size_bytes = word_size_bits // 8 | |
+ word_cnt = len(data) // word_size_bytes | |
-print(Int_multimix(Msg, Key)) | |
+ assert word_size_bytes in [4, 8], "Word size must be 32, 64 -bits !" | |
+ | |
+ bs = '' | |
+ for i in range(word_cnt): | |
+ block = data[i * word_size_bytes:(i + 1) * word_size_bytes] | |
+ word = int.from_bytes(block, byteorder='little') | |
+ | |
+ if word_size_bytes == 4: | |
+ bits = '{:032b}'.format(word) | |
+ else: | |
+ bits = '{:064b}'.format(word) | |
+ | |
+ bs += bits | |
+ return bs | |
+ | |
+def bitstring_to_bytes(bs: str, word_size_bits: int = 64) -> bytes: | |
+ word_size_bytes = word_size_bits // 8 | |
+ word_cnt = len(bs) // word_size_bits | |
+ | |
+ data = b'' | |
+ for i in range(word_cnt): | |
+ bits = bs[i * word_size_bits:(i + 1) * word_size_bits] | |
+ word = int(bits, base=2) | |
+ block = word.to_bytes(word_size_bytes, byteorder='little') | |
+ data += block | |
+ return data | |
+ | |
+if __name__ == '__main__': | |
+ BLOCK_SIZE_BYTES = 32 | |
+ KAT_COUNT = 256 | |
+ | |
+ IN_WORD_SIZE_BITS = 32 | |
+ OUT_WORD_SIZE_BITS = IN_WORD_SIZE_BITS*2 | |
+ | |
+ IN_WORD_SIZE_BYTES = IN_WORD_SIZE_BITS/ 8 | |
+ OUT_WORD_SIZE_BYTES = OUT_WORD_SIZE_BITS/ 8 | |
+ | |
+ SEED = 1 | |
+ random.seed(SEED) | |
+ | |
+ for i in range(KAT_COUNT): | |
+ mlen = (i+1) * BLOCK_SIZE_BYTES | |
+ | |
+ key = gen_rand_bytes(mlen) | |
+ msg = gen_rand_bytes(mlen) | |
+ | |
+ key_bits = bytes_to_bitstring(key, word_size_bits=IN_WORD_SIZE_BITS) | |
+ msg_bits = bytes_to_bitstring(msg, word_size_bits=IN_WORD_SIZE_BITS) | |
+ | |
+ assert bitstring_to_bytes(key_bits, word_size_bits=IN_WORD_SIZE_BITS) == key | |
+ assert bitstring_to_bytes(msg_bits, word_size_bits=IN_WORD_SIZE_BITS) == msg | |
+ | |
+ md_bits = Int_multimix(msg_bits, key_bits) | |
+ md = bitstring_to_bytes(md_bits, word_size_bits=OUT_WORD_SIZE_BITS) | |
+ assert bytes_to_bitstring(md, word_size_bits=OUT_WORD_SIZE_BITS) == md_bits | |
+ | |
+ print(f"mlen = {mlen}") | |
+ print(f"key = {key.hex()}") | |
+ print(f"msg = {msg.hex()}") | |
+ print(f"md = {md.hex()}\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Steps for Reproducibly Generating KAT Files for Multimixer-128
Multimixer-128 is a universal keyed hashing scheme, based on 32 -bit integer addition and multiplication, which was defined in paper https://eprint.iacr.org/2023/1357.pdf. I implemented Multimixer-128 as a Rust library in https://github.com/itzmeanjan/multimixer-128. But I couldn't find any KAT files, to ensure functional correctness and conformance of my library implementation, so I took the venture of creating a few, using the reference implementation of Multimixer-128, that was supplied by authors.
Generated KAT file (
multimixer128.kat
) lives in the root directory of the reference implementation.