# Exercise 1.1frombloomfilterimportBIP37_CONSTANTfromhelperimport (
murmur3,
bit_field_to_bytes
)
field_size=10function_count=5tweak=99items= (b'Hello World', b'Goodbye!')
# bit_field_size is 8 * field_size# create a bit field with the appropriate size# for each item you want to add to the filter# iterate function_count number of times# BIP0037 spec seed is i*BIP37_CONSTANT + tweak# get the murmur3 hash given that seed# set the bit to be h mod the bit_field_size# set the bit_field at the index bit to be 1# print the bit field converted to bytes using bit_field_to_bytes in hex
Test Driven Exercise
# Exercise 1.2fromhelperimportmurmur3frombloomfilterimport (
BloomFilter,
BIP37_CONSTANT
)
classBloomFilter(BloomFilter):
defadd(self, item):
'''Add an item to the filter'''# iterate self.function_count number of times# BIP0037 spec seed is i*BIP37_CONSTANT + self.tweak# get the murmur3 hash given that seed# set the bit at the hash mod the bitfield size (self.size*8)# set the bit field at bit to be 1pass