Skip to content

Instantly share code, notes, and snippets.

@itzmeanjan
Last active October 11, 2023 09:49
Show Gist options
  • Save itzmeanjan/a32eab0244af55eba2847c6472337535 to your computer and use it in GitHub Desktop.
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
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")
@itzmeanjan
Copy link
Author

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.

  1. Clone git repository, holding reference implementation of Multimixer-128.
git clone https://github.com/Parisaa/Multimixer.git
pushd Multimixer/
git checkout 797bbba9d2b3c3ec15403cbf3d7ef588ec16d81d # Important step
popd
  1. Clone git repository, holding above git patch.
git clone https://gist.github.com/a32eab0244af55eba2847c6472337535.git
pushd a32eab0244af55eba2847c6472337535
sha256sum git.patch # Must be 5b23db7ea62dc1a6c38c7cb67c92b65f5e74b1e491b45bfd1ecb12d30f9ffbf8
popd
  1. Apply git patch on reference implementation of Multimixer-128.
cp a32eab0244af55eba2847c6472337535/git.patch Multimixer/
pushd Multimixer/
git apply git.patch
git diff # Optional, but shows the changes applied
popd
  1. Generate Known Answer Tests.
pushd Multimixer/
python3 ReferenceCode/Multimixer-128.py | tee multimixer128.kat
sha256sum multimixer128.kat # Must be 88bb132696e986dd859af44a0d343d0e62a8e70feb0c159d6b69aee5dfe83094
popd

Generated KAT file (multimixer128.kat) lives in the root directory of the reference implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment