Skip to content

Commit 9c8593d

Browse files
sipasdaftuar
authored andcommitted
Implement SipHash in Python
1 parent 56c87e9 commit 9c8593d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2016 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#
7+
# siphash.py - Specialized SipHash-2-4 implementations
8+
#
9+
# This implements SipHash-2-4 for 256-bit integers.
10+
11+
def rotl64(n, b):
12+
return n >> (64 - b) | (n & ((1 << (64 - b)) - 1)) << b
13+
14+
def siphash_round(v0, v1, v2, v3):
15+
v0 = (v0 + v1) & ((1 << 64) - 1)
16+
v1 = rotl64(v1, 13)
17+
v1 ^= v0
18+
v0 = rotl64(v0, 32)
19+
v2 = (v2 + v3) & ((1 << 64) - 1)
20+
v3 = rotl64(v3, 16)
21+
v3 ^= v2
22+
v0 = (v0 + v3) & ((1 << 64) - 1)
23+
v3 = rotl64(v3, 21)
24+
v3 ^= v0
25+
v2 = (v2 + v1) & ((1 << 64) - 1)
26+
v1 = rotl64(v1, 17)
27+
v1 ^= v2
28+
v2 = rotl64(v2, 32)
29+
return (v0, v1, v2, v3)
30+
31+
def siphash256(k0, k1, h):
32+
n0 = h & ((1 << 64) - 1)
33+
n1 = (h >> 64) & ((1 << 64) - 1)
34+
n2 = (h >> 128) & ((1 << 64) - 1)
35+
n3 = (h >> 192) & ((1 << 64) - 1)
36+
v0 = 0x736f6d6570736575 ^ k0
37+
v1 = 0x646f72616e646f6d ^ k1
38+
v2 = 0x6c7967656e657261 ^ k0
39+
v3 = 0x7465646279746573 ^ k1 ^ n0
40+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
41+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
42+
v0 ^= n0
43+
v3 ^= n1
44+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
45+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
46+
v0 ^= n1
47+
v3 ^= n2
48+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
49+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
50+
v0 ^= n2
51+
v3 ^= n3
52+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
53+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
54+
v0 ^= n3
55+
v3 ^= 0x2000000000000000
56+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
57+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
58+
v0 ^= 0x2000000000000000
59+
v2 ^= 0xFF
60+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
61+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
62+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
63+
v0, v1, v2, v3 = siphash_round(v0, v1, v2, v3)
64+
return v0 ^ v1 ^ v2 ^ v3

src/test/hash_tests.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ BOOST_AUTO_TEST_CASE(siphash)
122122
hasher3.Write(uint64_t(x)|(uint64_t(x+1)<<8)|(uint64_t(x+2)<<16)|(uint64_t(x+3)<<24)|
123123
(uint64_t(x+4)<<32)|(uint64_t(x+5)<<40)|(uint64_t(x+6)<<48)|(uint64_t(x+7)<<56));
124124
}
125+
126+
CHashWriter ss(SER_DISK, CLIENT_VERSION);
127+
ss << CTransaction();
128+
BOOST_CHECK_EQUAL(SipHashUint256(1, 2, ss.GetHash()), 0x79751e980c2a0a35ULL);
125129
}
126130

127131
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)