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