MurmurHash3 Test Vectors
Asked Answered
D

3

7

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.

Disjointed answered 7/2, 2013 at 9:9 Comment(3)
I wrote my own implementation and noticed that these are the same values that I'm getting: github.com/karanlyons/murmurHash3.js/blob/master/tests.htmlSewan
@KaiSellgren I validated the x86 32bit Tests against my own implementation. It would be useful if the javascript test also checked that Javascript strings are utf-8 encoded (e.g. "ππππππππ"), as well as that it supports embedded nulls (e.g. "\0\0\0\0")Tarriance
@IanBoyd for what it's worth, here's my implementation in Rust with a few unit tests at the bottom: github.com/kaisellgren/comp_sci.rs/blob/master/src/algorithms/…Sewan
T
19

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.

Tarriance answered 10/8, 2015 at 21:31 Comment(4)
Thanks! Whoa, it's quite an effort there. I'll mark yours as 'the' answer.Disjointed
"If anyone else can confirm any/all of these vectors from their implementations." -- confirmed, github.com/airbreather/AirBreather.Common/blob/… is all passing in my C# implementation. Thanks!Michelle
Quick copy/paste for C++Compellation
Here's the test cases in JSON formatRothmuller
C
7

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);
    }
}
Cyanide answered 28/5, 2013 at 15:39 Comment(1)
Oh my god; i'd kill for a test vector. Because now i need a test to test my test code that is a copy of his test codeTarriance
D
3

I've improved the life-saving code from Jonathan. Your Murmur3 must implement ICryptoTransform for this method to work. You can find one on github that implements this interface.

public static  void VerificationTest(uint expected)
{
    using (var hash = new Murmur32ManagedX86())
    {
        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 mur = new Murmur32ManagedX86((uint)(256 - i)))
                {
                    var computed = mur.ComputeHash(key, 0,i);
                    cs.Write(computed, 0, 4);
                }
            }
            cs.FlushFinalBlock();
            var testBoy = hash.Seed;

            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);
        }
    }
}

If you use an implementation that doesn't have ICryptoTransform interface but just processes the bytes and returns int (can be easily modified to work with byte[] too). Here is the test function for that:

public static void VerificationTest(uint expected)
{
    using (var stream = new MemoryStream())
    {
        var key = new byte[256];

        for (var i = 0; i < 256; i++)
        {
            key[i] = (byte)i;
            var hasher = new MurMurHash3((uint)(256 - i));

            int computed = hasher.ComputeBytesFast(key.Take(i).ToArray());
            stream.Write(BitConverter.GetBytes(computed), 0, 4);
        }
        var finalHasher = new MurMurHash3(0); //initial seed = 0
        int result = finalHasher.ComputeBytesFast2(stream.GetBuffer());
        if (result == (int)expected)
            Console.WriteLine("Verification passed.");
        else
            Console.WriteLine("Verification failed, got {0:x8}, expected {1:x8}", verification, expected);
    }
}
Discant answered 18/3, 2016 at 23:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.