-
Notifications
You must be signed in to change notification settings - Fork 52
/
bloom.py
55 lines (39 loc) · 1.16 KB
/
bloom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import random
try: #
shathree = __import__('sha3').sha3_256
except:
shathree = __import__('python_sha3').sha3_256
params = {
"size": 256,
"pecks": 32
}
def decode_int(x):
o = 0
for a in x:
o = o * 256 + ord(a)
return o
def sha3(x):
return shathree(x).digest()
def bloom_insert(params, bloom, val):
k = decode_int(sha3(val)) * (3**160 + 112)
for i in range(params["pecks"]):
bloom |= 1 << (k % params["size"])
k //= params["size"]
return bloom
def bloom_query(params, bloom, val):
o = bloom_insert(params, 0, val)
return (bloom & o) == o
def test_params(size, pecks, objcount):
params = {"size": size, "pecks": pecks}
count = 0
for i in range(100):
objs = [str(random.randrange(2**40)) for i in range(objcount)]
bloom = 0
for o in objs:
bloom = bloom_insert(params, bloom, o)
for o in objs:
assert bloom_query(params, bloom, o)
for i in range(100):
if bloom_query(params, bloom, str(random.randrange(2**40))):
count += 1
print 'False positive rate: %f' % (count / 10000.)