Documentation ¶
Overview ¶
Package gcs provides an API for building and using a Golomb-coded set filter.
A Golomb-Coded Set (GCS) is a space-efficient probabilistic data structure that is used to test set membership with a tunable false positive rate while simultaneously preventing false negatives. In other words, items that are in the set will always match, but items that are not in the set will also sometimes match with the chosen false positive rate.
This package currently implements two different versions for backwards compatibility. Version 1 is deprecated and therefore should no longer be used.
Version 2 is the GCS variation that follows the specification details in DCP0005: https://github.com/decred/dcps/blob/master/dcp-0005/dcp-0005.mediawiki#golomb-coded-sets.
Version 2 sets do not permit empty items (data of zero length) to be added and are parameterized by the following:
* A parameter `B` that defines the remainder code bit size * A parameter `M` that defines the false positive rate as `1/M` * A key for the SipHash-2-4 function * The items to include in the set
Errors ¶
Errors returned by this package are of type gcs.Error. This allows the caller to programmatically determine the specific error by examining the ErrorKind field of the type asserted gcs.Error while still providing rich error messages with contextual information. See ErrorKind in the package documentation for a full list.
GCS use in Decred ¶
GCS is used as a mechanism for storing, transmitting, and committing to per-block filters. Consensus-validating full nodes commit to a single filter for every block and serve the filter to SPV clients that match against the filter locally to determine if the block is potentially relevant. The required parameters for Decred are defined by the blockcf2 package.
For more details, see the Block Filters section of DCP0005: https://github.com/decred/dcps/blob/master/dcp-0005/dcp-0005.mediawiki#block-filters
Index ¶
Constants ¶
const ( // ErrNTooBig signifies that the filter can't handle N items. ErrNTooBig = ErrorKind("ErrNTooBig") // ErrPTooBig signifies that the filter can't handle `1/2**P` // collision probability. ErrPTooBig = ErrorKind("ErrPTooBig") // ErrBTooBig signifies that the provided Golomb coding bin size is larger // than the maximum allowed value. ErrBTooBig = ErrorKind("ErrBTooBig") // ErrMisserialized signifies a filter was misserialized and is missing the // N and/or P parameters of a serialized filter. ErrMisserialized = ErrorKind("ErrMisserialized") )
These constants are used to identify a specific RuleError.
const KeySize = 16
KeySize is the size of the byte array required for key material for the SipHash keyed hash function.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Error ¶
Error identifies an error related to gcs filters. It has full support for errors.Is and errors.As, so the caller can ascertain the specific reason for the error by checking the underlying error.
type ErrorKind ¶
type ErrorKind string
ErrorKind identifies a kind of error. It has full support for errors.Is and errors.As, so the caller can directly check against an error kind when determining the reason for an error.
type FilterV1 ¶
type FilterV1 struct {
// contains filtered or unexported fields
}
FilterV1 describes an immutable filter that can be built from a set of data elements, serialized, deserialized, and queried in a thread-safe manner. The serialized form is compressed as a Golomb Coded Set (GCS) along with the number of members of the set. The hash function used is SipHash, a keyed function. The key used in building the filter is required in order to match filter values and is not included in the serialized form.
func FromBytesV1 ¶
FromBytesV1 deserializes a version 1 GCS filter from a known P and serialized filter as returned by Bytes().
func NewFilterV1 ¶
NewFilterV1 builds a new version 1 GCS filter with a collision probability of 1 / 2^P for the given key and data.
func (*FilterV1) Bytes ¶
func (f *FilterV1) Bytes() []byte
Bytes returns the serialized format of the GCS filter which includes N, but does not include other parameters such as the false positive rate or the key.
func (*FilterV1) Match ¶
Match checks whether a []byte value is likely (within collision probability) to be a member of the set represented by the filter.
func (*FilterV1) MatchAny ¶
MatchAny checks whether any []byte value is likely (within collision probability) to be a member of the set represented by the filter faster than calling Match() for each value individually.
type FilterV2 ¶
type FilterV2 struct {
// contains filtered or unexported fields
}
FilterV2 describes an immutable filter that can be built from a set of data elements, serialized, deserialized, and queried in a thread-safe manner. The serialized form is compressed as a Golomb Coded Set (GCS) along with the number of members of the set. The hash function used is SipHash, a keyed function. The key used in building the filter is required in order to match filter values and is not included in the serialized form.
Version 2 filters differ from version 1 filters in four ways:
- Support for independently specifying the false positive rate and Golomb coding bin size which allows minimizing the filter size
- A faster (incompatible with version 1) reduction function
- A more compact serialization for the number of members in the set
- Deduplication of all hash collisions prior to reducing and serializing the deltas
func FromBytesV2 ¶
FromBytesV2 deserializes a version 2 GCS filter from a known B, M, and serialized filter as returned by Bytes().
func NewFilterV2 ¶
NewFilterV2 builds a new GCS filter with the provided tunable parameters that contains every item of the passed data as a member of the set.
B is the tunable bits parameter for constructing the filter that is used as the bin size in the underlying Golomb coding with a value of 2^B. The optimal value of B to minimize the size of the filter for a given false positive rate 1/M is floor(log_2(M) - 0.055256). The maximum allowed value for B is 32. An error will be returned for larger values.
M is the inverse of the target false positive rate for the filter. The optimal value of M to minimize the size of the filter for a given B is ceil(1.497137 * 2^B).
key is a key used in the SipHash function used to hash each data element prior to inclusion in the filter. This helps thwart would be attackers attempting to choose elements that intentionally cause false positives.
The general process for determining optimal parameters for B and M to minimize the size of the filter is to start with the desired false positive rate and calculate B per the aforementioned formula accordingly. Then, if the application permits the false positive rate to be varied, calculate the optimal value of M via the formula provided under the description of M.
func (*FilterV2) B ¶
B returns the tunable bits parameter that was used to construct the filter. It represents the bin size in the underlying Golomb coding with a value of 2^B.
func (*FilterV2) Bytes ¶
func (f *FilterV2) Bytes() []byte
Bytes returns the serialized format of the GCS filter which includes N, but does not include other parameters such as the false positive rate or the key.
func (*FilterV2) Match ¶
Match checks whether a []byte value is likely (within collision probability) to be a member of the set represented by the filter.
Directories ¶
Path | Synopsis |
---|---|
Package blockcf2 provides functions for building committed filters for blocks using Golomb-coded sets in a way that is useful for light clients such as SPV wallets.
|
Package blockcf2 provides functions for building committed filters for blocks using Golomb-coded sets in a way that is useful for light clients such as SPV wallets. |