Use stint instead of ttmath

This commit is contained in:
Yuriy Glukhov 2018-05-08 17:45:13 +03:00
parent c58f025de1
commit 9904a03920
3 changed files with 10 additions and 8 deletions

View File

@ -10,8 +10,8 @@ srcDir = "src"
requires "nim >= 0.18", requires "nim >= 0.18",
"nimcrypto", "nimcrypto",
"ttmath" "stint"
task test, "Run tests": task test, "Run tests":
--run --run
setCommand "cpp", "tests/test1.nim" setCommand "c", "tests/test1.nim"

View File

@ -1,4 +1,6 @@
import ttmath, nimcrypto import stint, nimcrypto
type UInt2048 = StUint[2048]
iterator chunksForBloom(h: MDigest[256]): array[2, uint8] = iterator chunksForBloom(h: MDigest[256]): array[2, uint8] =
yield [h.data[0], h.data[1]] yield [h.data[0], h.data[1]]
@ -8,7 +10,7 @@ iterator chunksForBloom(h: MDigest[256]): array[2, uint8] =
proc chunkToBloomBits(chunk: array[2, uint8]): UInt2048 = proc chunkToBloomBits(chunk: array[2, uint8]): UInt2048 =
let h = chunk[0].int let h = chunk[0].int
let l = chunk[1].int let l = chunk[1].int
1.u2048 shl ((l + (h shl 8)) and 2047).uint32 one(UInt2048) shl ((l + (h shl 8)) and 2047)
iterator bloomBits(h: MDigest[256]): UInt2048 = iterator bloomBits(h: MDigest[256]): UInt2048 =
for chunk in chunksForBloom(h): for chunk in chunksForBloom(h):
@ -19,7 +21,7 @@ type BloomFilter* = object
proc incl(f: var BloomFilter, h: MDigest[256]) = # Should this be public? proc incl(f: var BloomFilter, h: MDigest[256]) = # Should this be public?
for bits in bloomBits(h): for bits in bloomBits(h):
f.value |= bits f.value = f.value or bits
# TODO: The following 2 procs should be one genric, but it doesn't compile. Nim bug? # TODO: The following 2 procs should be one genric, but it doesn't compile. Nim bug?
proc incl*(f: var BloomFilter, v: string) = f.incl(keccak256.digest(v)) proc incl*(f: var BloomFilter, v: string) = f.incl(keccak256.digest(v))
@ -27,7 +29,7 @@ proc incl*(f: var BloomFilter, v: openarray[byte]) = f.incl(keccak256.digest(v))
proc contains(f: BloomFilter, h: MDigest[256]): bool = # Should this be public? proc contains(f: BloomFilter, h: MDigest[256]): bool = # Should this be public?
for bits in bloomBits(h): for bits in bloomBits(h):
if isZero(f.value and bits): return false if (f.value and bits).isZero: return false
return true return true
template contains*[T](f: BloomFilter, v: openarray[T]): bool = template contains*[T](f: BloomFilter, v: openarray[T]): bool =

View File

@ -1,4 +1,4 @@
import eth_bloom, ttmath, unittest import eth_bloom, stint, unittest
suite "Simple Bloom Filter tests": suite "Simple Bloom Filter tests":
test "incl proc": test "incl proc":
@ -14,4 +14,4 @@ suite "Simple Bloom Filter tests":
var f: BloomFilter var f: BloomFilter
f.incl("value 1") f.incl("value 1")
f.incl("value 2") f.incl("value 2")
check f.value == u2048"1090215279796298345343057319992441901006450066263950115824040002588950485497113027143927523755823134941133023716890165043342811041924870874305880232180990464248298835944719578227183672673286106858273952584661686762419935928160959430409028732374024192153399763277382459194254234587232383494962731940352290891816707697788111127980409605093135659121120897102645250001200507634146244124778321795865777525978540960830042468420173693965828992647991129039043403835835590424035347457188427354145120006479590726476620907513681178254852999008485376" check f.value == parse("1090215279796298345343057319992441901006450066263950115824040002588950485497113027143927523755823134941133023716890165043342811041924870874305880232180990464248298835944719578227183672673286106858273952584661686762419935928160959430409028732374024192153399763277382459194254234587232383494962731940352290891816707697788111127980409605093135659121120897102645250001200507634146244124778321795865777525978540960830042468420173693965828992647991129039043403835835590424035347457188427354145120006479590726476620907513681178254852999008485376", StUint[2048])