Source

pep-456-benchmarks / sipvalues.py

Full commit
#!/usr/bin/env python3
"""Script to create SipHash24 test values for Python test.test_hash

Requires https://github.com/majek/pysiphash
"""
from siphash import SipHash24


def lcg(x, length=16):
    out = bytearray(length)
    if x == 0:
        return bytes(out)
    for i in range(length):
         x = (214013 * x + 2531011) & 0x7fffffff
         out[i] = (x >> 16) & 0xff
    return bytes(out)


def to32m(h):
    """Hash as mangled signed int32"""
    h ^= h >> 32
    h &= 0xffffffff
    if h > (1 << 31) - 1:
        h -= 1 << 32
    return h

def to32(h):
    """Hash as signed int32"""
    h &= 0xffffffff
    if h > (1 << 31) - 1:
        h -= 1 << 32
    return h

def to64(h):
    """Hash as signed int64"""
    if h > (1 << 63) - 1:
            h -= 1 << 64
    return h

def testvalues():
    values = [(0, 'abc', False), 
              (42, 'abc', False),
              (42, 'abcdefghijk', False),
              (0, 'äú∑ℇ', True),
              (42, 'äú∑ℇ', True),]

    for seed, s, endian in values:
        secret = lcg(seed)
        if endian:
            le = SipHash24(secret, s.encode("utf-16-le")).hash()
            be = SipHash24(secret, s.encode("utf-16-be")).hash()
        else:
            le = be = SipHash24(secret, s.encode("utf-8")).hash()

        print("            # seed {}, '{}'".format(seed, s))
        print("            [{}, {}, {}, {}],".format(
              to32(le), to64(le), to32(be), to64(be)))


if __name__ == "__main__":
    testvalues()