Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MurmurHash3 Test Vectors

I'm trying to port a C# implementation of MurmurHash3 to VB.Net.

It runs... but can someone provide me with some known Test Vectors to verify correctness?

  • Known string text
  • Seed value
  • Result of MurmurHash3

Thanks in advance.

Edit : I'm limiting the implementation to only the 32-bit MurmurHash3, but if you can also provide vectors for the 64-bit implementation, would also be good.

like image 906
pepoluan Avatar asked Feb 07 '13 09:02

pepoluan


2 Answers

I finally got around to creating a MurMur3 implementation, and i managed to translate the SMHasher test code. My implementation gives the same result as the SMHasher test. That means i can finally give some useful, and assumed to be correct, test vectors.

This is for Murmur3_x86_32 only

| Input        | Seed       | Expected   |
|--------------|------------|------------|
| (no bytes)   | 0          | 0          | with zero data and zero seed, everything becomes zero
| (no bytes)   | 1          | 0x514E28B7 | ignores nearly all the math
| (no bytes)   | 0xffffffff | 0x81F16F39 | make sure your seed uses unsigned 32-bit math
| FF FF FF FF  | 0          | 0x76293B50 | make sure 4-byte chunks use unsigned math
| 21 43 65 87  | 0          | 0xF55B516B | Endian order. UInt32 should end up as 0x87654321
| 21 43 65 87  | 0x5082EDEE | 0x2362F9DE | Special seed value eliminates initial key with xor
| 21 43 65     | 0          | 0x7E4A8634 | Only three bytes. Should end up as 0x654321
| 21 43        | 0          | 0xA0F7B07A | Only two bytes. Should end up as 0x4321
| 21           | 0          | 0x72661CF4 | Only one byte. Should end up as 0x21
| 00 00 00 00  | 0          | 0x2362F9DE | Make sure compiler doesn't see zero and convert to null
| 00 00 00     | 0          | 0x85F0B427 | 
| 00 00        | 0          | 0x30F4C306 |
| 00           | 0          | 0x514E28B7 |

For those of you who will be porting to a language that doesn't have actual arrays, i also have some string based tests. For these tests:

  • all strings are assumed to be UTF-8 encoded
  • and do not include any null terminator

I'll leave these in code form:

TestString("", 0, 0); //empty string with zero seed should give zero
TestString("", 1, 0x514E28B7);
TestString("", 0xffffffff, 0x81F16F39); //make sure seed value is handled unsigned
TestString("\0\0\0\0", 0, 0x2362F9DE); //make sure we handle embedded nulls


TestString("aaaa", 0x9747b28c, 0x5A97808A); //one full chunk
TestString("aaa", 0x9747b28c, 0x283E0130); //three characters
TestString("aa", 0x9747b28c, 0x5D211726); //two characters
TestString("a", 0x9747b28c, 0x7FA09EA6); //one character

//Endian order within the chunks
TestString("abcd", 0x9747b28c, 0xF0478627); //one full chunk
TestString("abc", 0x9747b28c, 0xC84A62DD);
TestString("ab", 0x9747b28c, 0x74875592);
TestString("a", 0x9747b28c, 0x7FA09EA6);

TestString("Hello, world!", 0x9747b28c, 0x24884CBA);

//Make sure you handle UTF-8 high characters. A bcrypt implementation messed this up
TestString("ππππππππ", 0x9747b28c, 0xD58063C1); //U+03C0: Greek Small Letter Pi

//String of 256 characters.
//Make sure you don't store string lengths in a char, and overflow at 255 bytes (as OpenBSD's canonical BCrypt implementation did)
TestString(StringOfChar("a", 256), 0x9747b28c, 0x37405BDC);

I'll post just two of the 11 SHA-2 test vectors that i converted to Murmur3.

TestString("abc", 0, 0xB3DD93FA);
TestString("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 0, 0xEE925B90);

And finally, the big one:

  • Key: "The quick brown fox jumps over the lazy dog"
  • Seed: 0x9747b28c
  • Hash: 0x2FA826CD

If anyone else can confirm any/all of these vectors from their implementations.

And, again, these test vectors come from an implementation that passes the SMHasher 256 iteration loop test from KeySetTest.cpp - VerificationTest(...).

These tests came from my implementation in Delphi. I also created an implementation in Lua (which isn't big on supporting arrays).

Note: Any code released into public domain. No attribution required.

like image 88
Ian Boyd Avatar answered Sep 29 '22 11:09

Ian Boyd


SMHasher uses a little routine to check that the hashes are working, basically it calculates the hashes for the following values, using a decreasing seed value (from 256) for each:

' The comment in the SMHasher code is a little wrong -
' it's missing the first case.
{}, {0}, {0, 1}, {0, 1, 2} ... {0, 1, 2, ... 254}

And appends that to a HASHLENGTH * 256 length array, in other words:

' Where & is a byte array concatenation.
HashOf({}, 256) &
HashOf({0}, 255) &
HashOf({0, 1}, 254) &
...
HashOf({0, 1, ... 254), 1)

It then takes the hash of that big array. The first 4 bytes of the final hash are interpreted as a unsigned 32bit integer and checked against a verification code:

  • MurmurHash3 x86 32 0xB0F57EE3
  • MurmurHash3 x86 128 0xB3ECE62A
  • MurmurHash3 x64 128 0x6384BA69

Unfortunately that's the only public test I could find. I guess the other option would be to write a quick C app and hash some values.

Here is my C# implementation of the verifier.

static void VerificationTest(uint expected)
{
    using (var hash = new Murmur3())
    // Also test that Merkle incremental hashing works.
    using (var cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
    {
        var key = new byte[256];

        for (var i = 0; i < 256; i++)
        {
            key[i] = (byte)i;
            using (var m = new Murmur3(256 - i))
            {
                var computed = m.ComputeHash(key, 0, i);
                // Also check that your implementation deals with incomplete
                // blocks.
                cs.Write(computed, 0, 5);
                cs.Write(computed, 5, computed.Length - 5);
            }
        }

        cs.FlushFinalBlock();
        var final = hash.Hash;
        var verification = ((uint)final[0]) | ((uint)final[1] << 8) | ((uint)final[2] << 16) | ((uint)final[3] << 24);
        if (verification == expected)
            Console.WriteLine("Verification passed.");
        else
            Console.WriteLine("Verification failed, got {0:x8}, expected {1:x8}", verification, expected);
    }
}
like image 25
Jonathan Dickinson Avatar answered Sep 29 '22 13:09

Jonathan Dickinson