2fe8982068436301d1a96f33b20eb2c87f6ee071
[mLib] / hash / t / unihash-testgen.py
1 #! /usr/bin/python
2 ### -*-python-*-
3 ###
4 ### Generate test vectors for universal hashing.
5
6 MOD = 0x104c11db7
7
8 def gfmul(x, y):
9 a = 0
10 while y > 0:
11 if y & 1: a ^= x
12 if x & 0x80000000: x = (x << 1) ^ MOD
13 else: x <<= 1
14 y >>= 1
15 return a
16
17 def hashtest(k, m):
18 h = k
19 for ch in m: h = gfmul(h ^ ord(ch), k)
20 print ' 0x%08x "%s" 0x%08x;' % (k, m, h)
21
22 print '''\
23 ### Test vectors for universal hashing
24 ### [generated]
25
26 hash {'''
27
28 for k, m in [(0x00000000, 'anything you like'),
29 (0x12345678, 'an exaple test string'),
30 (0xb8a171f0, 'The quick brown fox jumps over the lazy dog.'),
31 (0x2940521b, 'A man, a plan, a canal: Panama!')]:
32 hashtest(k, m)
33
34 k, m = 0x94b22a73, 0xbb7b1fef
35 for i in xrange(48):
36 hashtest(k, "If we don't succeed, we run the risk of failure.")
37 k = gfmul(k, m)
38
39 print '}'