-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bloom filter acceleration for deposit processing
- Loading branch information
Showing
9 changed files
with
105 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# beacon_chain | ||
# Copyright (c) 2024 Status Research & Development GmbH | ||
# Licensed and distributed under either of | ||
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). | ||
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). | ||
# at your option. This file may not be copied, modified, or distributed except according to those terms. | ||
|
||
{.push raises: [].} | ||
|
||
import "."/spec/crypto | ||
|
||
from "."/spec/datatypes/base import Validator, pubkey | ||
from "."/spec/helpers import bytes_to_uint32 | ||
|
||
const | ||
# https://hur.st/bloomfilter/?n=4M&p=&m=8MiB&k= | ||
pubkeyBloomFilterScale = 23 # 21 too small, 22 borderline, 24 also ok | ||
|
||
type | ||
PubkeyBloomFilter* = object | ||
bloomFilter: array[1 shl pubkeyBloomFilterScale, byte] | ||
|
||
from stew/bitops2 import getBit, setBit | ||
|
||
iterator bloomFilterHashes(pubkey: ValidatorPubKey): auto = | ||
const pubkeyBloomFilterMask = (1 shl pubkeyBloomFilterScale) - 1 | ||
for r in countup(0'u32, 20'u32, 4'u32): | ||
# ValidatorPubKeys have fairly uniform entropy; using enough hash | ||
# functions also reduces risk of low-entropy portions | ||
yield pubkey.blob.toOpenArray(r, r+3).bytes_to_uint32 and | ||
pubkeyBloomFilterMask | ||
|
||
func constructBloomFilter*(x: openArray[Validator]): auto = | ||
let res = new PubkeyBloomFilter | ||
for m in x: | ||
for bloomFilterHash in bloomFilterHashes(m.pubkey): | ||
setBit(res[].bloomFilter, bloomFilterHash) | ||
res | ||
|
||
func mightContain*(bf: PubkeyBloomFilter, pubkey: ValidatorPubKey): bool = | ||
# Might return false positive, but never false negative | ||
for bloomFilterHash in bloomFilterHashes(pubkey): | ||
if not getBit(bf.bloomFilter, bloomFilterHash): | ||
return false | ||
|
||
true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters