Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does hash() compute the hash of a tuple?

How does the function hash() compute the hash value of a tuple? For example:

t = (1,2,3)
print(hash(t))

Gives an output

-378539185
like image 978
Aditya Mandal Avatar asked Dec 23 '22 05:12

Aditya Mandal


1 Answers

You can check the implementation of this function in C if you're familiar with C programming and some advanced math. It seems that algorithm XORs hashes of each element in tuple and adds some magic.

static Py_hash_t
tuplehash(PyTupleObject *v)
{
    Py_uhash_t x;  /* Unsigned for defined overflow behavior. */
    Py_hash_t y;
    Py_ssize_t len = Py_SIZE(v);
    PyObject **p;
    Py_uhash_t mult = _PyHASH_MULTIPLIER;
    x = 0x345678UL;
    p = v->ob_item;
    while (--len >= 0) {
        y = PyObject_Hash(*p++);
        if (y == -1)
            return -1;
        x = (x ^ y) * mult;
        /* the cast might truncate len; that doesn't change hash stability */
        mult += (Py_hash_t)(82520UL + len + len);
    }
    x += 97531UL;
    if (x == (Py_uhash_t)-1)
        x = -2;
    return x;
}

Note that this is CPython's current implementation. Other Python interpreters and even other versions of CPython may have different hash functions. This particular implementation, called SipHash, has been in use since 2013. See PEP 456 -- Secure and interchangeable hash algorithm for a detailed explanation.

SipHash is a cryptographic pseudo random function with a 128-bit seed and 64-bit output.... SipHash is a family of pseudorandom functions (a.k.a. keyed hash functions) optimized for speed on short messages. Target applications include network traffic authentication and defense against hash-flooding DoS attacks.

like image 163
Lev Zakharov Avatar answered Dec 25 '22 20:12

Lev Zakharov