| 1 |
import filtercascade |
| 2 |
import hashlib |
| 3 |
from pathlib import Path |
| 4 |
|
| 5 |
import sys |
| 6 |
import logging |
| 7 |
|
| 8 |
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) |
| 9 |
|
| 10 |
|
| 11 |
def predictable_serial_gen(start, end): |
| 12 |
counter = start |
| 13 |
while counter < end: |
| 14 |
counter += 1 |
| 15 |
m = hashlib.sha256() |
| 16 |
m.update(counter.to_bytes(4, byteorder="big")) |
| 17 |
yield m.hexdigest() |
| 18 |
|
| 19 |
|
| 20 |
def store(fc, path): |
| 21 |
if path.exists(): |
| 22 |
path.unlink() |
| 23 |
with open(path, "wb") as f: |
| 24 |
fc.tofile(f) |
| 25 |
|
| 26 |
|
| 27 |
small_set = list(set(predictable_serial_gen(0, 500))) |
| 28 |
large_set = set(predictable_serial_gen(500, 10_000)) |
| 29 |
|
| 30 |
# filter parameters |
| 31 |
growth_factor = 1.0 |
| 32 |
min_filter_length = 177 # 177 * 1.44 ~ 256, so smallest filter will have 256 bits |
| 33 |
|
| 34 |
print("--- v2_sha256l32_with_salt ---") |
| 35 |
v2_sha256l32_with_salt = filtercascade.FilterCascade( |
| 36 |
[], |
| 37 |
defaultHashAlg=filtercascade.fileformats.HashAlgorithm.SHA256, |
| 38 |
salt=b"nacl", |
| 39 |
growth_factor=growth_factor, |
| 40 |
min_filter_length=min_filter_length, |
| 41 |
) |
| 42 |
v2_sha256l32_with_salt.initialize( |
| 43 |
include=[b"this", b"that"] + small_set, exclude=large_set | set([b"other"]) |
| 44 |
) |
| 45 |
store(v2_sha256l32_with_salt, Path("test_v2_sha256l32_salt_mlbf")) |
| 46 |
|
| 47 |
print("--- v2_sha256l32 ---") |
| 48 |
v2_sha256l32 = filtercascade.FilterCascade( |
| 49 |
[], |
| 50 |
defaultHashAlg=filtercascade.fileformats.HashAlgorithm.SHA256, |
| 51 |
growth_factor=growth_factor, |
| 52 |
min_filter_length=min_filter_length, |
| 53 |
) |
| 54 |
v2_sha256l32.initialize( |
| 55 |
include=[b"this", b"that"] + small_set, exclude=large_set | set([b"other"]) |
| 56 |
) |
| 57 |
store(v2_sha256l32, Path("test_v2_sha256l32_mlbf")) |
| 58 |
|
| 59 |
print("--- v2_murmur ---") |
| 60 |
v2_murmur = filtercascade.FilterCascade( |
| 61 |
[], |
| 62 |
defaultHashAlg=filtercascade.fileformats.HashAlgorithm.MURMUR3, |
| 63 |
growth_factor=growth_factor, |
| 64 |
min_filter_length=min_filter_length, |
| 65 |
) |
| 66 |
v2_murmur.initialize( |
| 67 |
include=[b"this", b"that"] + small_set, exclude=large_set | set([b"other"]) |
| 68 |
) |
| 69 |
store(v2_murmur, Path("test_v2_murmur_mlbf")) |
| 70 |
|
| 71 |
print("--- v2_murmur_inverted ---") |
| 72 |
v2_murmur_inverted = filtercascade.FilterCascade( |
| 73 |
[], |
| 74 |
defaultHashAlg=filtercascade.fileformats.HashAlgorithm.MURMUR3, |
| 75 |
growth_factor=growth_factor, |
| 76 |
min_filter_length=min_filter_length, |
| 77 |
) |
| 78 |
v2_murmur_inverted.initialize( |
| 79 |
include=large_set | set([b"this", b"that"]), exclude=[b"other"] + small_set |
| 80 |
) |
| 81 |
store(v2_murmur_inverted, Path("test_v2_murmur_inverted_mlbf")) |
| 82 |
|
| 83 |
print("--- v2_sha256l32_inverted ---") |
| 84 |
v2_sha256l32_inverted = filtercascade.FilterCascade( |
| 85 |
[], |
| 86 |
defaultHashAlg=filtercascade.fileformats.HashAlgorithm.SHA256, |
| 87 |
growth_factor=growth_factor, |
| 88 |
min_filter_length=min_filter_length, |
| 89 |
) |
| 90 |
v2_sha256l32_inverted.initialize( |
| 91 |
include=large_set | set([b"this", b"that"]), exclude=[b"other"] + small_set |
| 92 |
) |
| 93 |
store(v2_sha256l32_inverted, Path("test_v2_sha256l32_inverted_mlbf")) |
| 94 |
|
| 95 |
print("--- v2_sha256ctr_with_salt ---") |
| 96 |
v2_sha256ctr_with_salt = filtercascade.FilterCascade( |
| 97 |
[], |
| 98 |
defaultHashAlg=filtercascade.fileformats.HashAlgorithm.SHA256CTR, |
| 99 |
salt=b"nacl", |
| 100 |
growth_factor=growth_factor, |
| 101 |
min_filter_length=min_filter_length, |
| 102 |
) |
| 103 |
v2_sha256ctr_with_salt.initialize( |
| 104 |
include=[b"this", b"that"] + small_set, exclude=large_set | set([b"other"]) |
| 105 |
) |
| 106 |
store(v2_sha256ctr_with_salt, Path("test_v2_sha256ctr_salt_mlbf")) |