Initial impl
This commit is contained in:
parent
132ff68483
commit
be18e8189e
|
@ -0,0 +1,13 @@
|
|||
# Package
|
||||
|
||||
version = "0.1.0"
|
||||
author = "Status Research & Development GmbH"
|
||||
description = "Ethereum Bloom Filter"
|
||||
license = "Apache2"
|
||||
srcDir = "src"
|
||||
|
||||
# Dependencies
|
||||
|
||||
requires "nim >= 0.17.0",
|
||||
"https://github.com/status-im/nim-keccak-tiny.git >= 0.1.0",
|
||||
"https://github.com/status-im/nim-ttmath >= 0.5.0"
|
|
@ -0,0 +1,33 @@
|
|||
import keccak_tiny, ttmath
|
||||
|
||||
iterator chunksForBloom(h: Hash[256]): array[2, uint8] =
|
||||
yield [h.data[0], h.data[1]]
|
||||
yield [h.data[2], h.data[3]]
|
||||
yield [h.data[4], h.data[5]]
|
||||
|
||||
proc chunkToBloomBits(chunk: array[2, uint8]): Int2048 =
|
||||
let h = chunk[0].int
|
||||
let l = chunk[1].int
|
||||
1.i2048 shl ((l + (h shl 8)) and 2047)
|
||||
|
||||
iterator bloomBits(h: Hash[256]): Int2048 =
|
||||
for chunk in chunksForBloom(h):
|
||||
yield chunkToBloomBits(chunk)
|
||||
|
||||
type BloomFilter* = object
|
||||
value*: Int2048
|
||||
|
||||
proc incl(f: var BloomFilter, h: Hash[256]) = # Should this be public?
|
||||
for bits in bloomBits(h):
|
||||
f.value |= bits
|
||||
|
||||
template incl*(f: var BloomFilter, v: typed) =
|
||||
f.incl(keccak_256(v))
|
||||
|
||||
proc contains(f: BloomFilter, h: Hash[256]): bool = # Should this be public?
|
||||
for bits in bloomBits(h):
|
||||
if isZero(f.value and bits): return false
|
||||
return true
|
||||
|
||||
template contains*(f: BloomFilter, v: typed): bool =
|
||||
f.contains(keccak_256(v))
|
|
@ -0,0 +1,17 @@
|
|||
import eth_bloom, ttmath, unittest
|
||||
|
||||
suite "Simple Bloom Filter tests":
|
||||
test "incl proc":
|
||||
var f: BloomFilter
|
||||
f.incl("hello")
|
||||
check "hello" in f
|
||||
check "hello1" notin f
|
||||
f.incl("hello1")
|
||||
check "hello" in f
|
||||
check "hello1" in f
|
||||
|
||||
test "int value":
|
||||
var f: BloomFilter
|
||||
f.incl("value 1")
|
||||
f.incl("value 2")
|
||||
check f.value == i2048"1090215279796298345343057319992441901006450066263950115824040002588950485497113027143927523755823134941133023716890165043342811041924870874305880232180990464248298835944719578227183672673286106858273952584661686762419935928160959430409028732374024192153399763277382459194254234587232383494962731940352290891816707697788111127980409605093135659121120897102645250001200507634146244124778321795865777525978540960830042468420173693965828992647991129039043403835835590424035347457188427354145120006479590726476620907513681178254852999008485376"
|
|
@ -0,0 +1 @@
|
|||
switch("path", "$projectDir/../src")
|
Loading…
Reference in New Issue