diff options
| author | fiatjaf <fiatjaf@gmail.com> | 2024-11-03 16:49:02 -0300 |
|---|---|---|
| committer | fiatjaf <fiatjaf@gmail.com> | 2026-02-04 19:22:23 -0300 |
| commit | cd60c2e9cf4600641bcd85efca15eccd50d0c417 (patch) | |
| tree | cc279729e244b25a69daeb2913686e673a6887ac /45.md | |
| parent | 88960829f1d134e918052513051ccfd3190e45cc (diff) | |
nip45: negate pow attacks on hyperloglog using a stupid hack.
Diffstat (limited to '45.md')
| -rw-r--r-- | 45.md | 15 |
1 files changed, 8 insertions, 7 deletions
| @@ -41,13 +41,14 @@ This is so it enables merging results from multiple relays and yielding a reason | |||
| 41 | 41 | ||
| 42 | ### Algorithm | 42 | ### Algorithm |
| 43 | 43 | ||
| 44 | The HLL value must be calculated with a precision of `8`, i.e. with 256 registers. | 44 | This section describes the steps a relay should take in order to return HLL values to clients. |
| 45 | 45 | ||
| 46 | To compute HLL values, first initi the 256 registers to `0` each; then, for on every event to be counted, | 46 | 1. Upon receiving a filter, if it has a single `#e`, `#p`, `#a` or `#q` item, read its 32th ascii character as a byte and take its modulo over 24 to obtain an `offset` -- in the unlikely case that the filter doesn't meet these conditions, set `offset` to the number 16; |
| 47 | 47 | 2. Initialize 256 registers to 0 for the HLL value; | |
| 48 | 1. take byte `16` of the `id` and use it to determine the register index; | 48 | 3. For all the events that are to be counted according to the filter, do this: |
| 49 | 2. count the number of leading zero bits in the following bytes `17..24` of the `id`; | 49 | 1. Read byte at position `offset` of the event `pubkey`, its value will be the register index `ri`; |
| 50 | 3. if the number of leading zeros is bigger than what was previously stored in that register, overwrite it. | 50 | 2. Count the number of leading zero bits starting at position `offset+1` of the event `pubkey`; |
| 51 | 3. Compare that with the value stored at register `ri`, if the new number is bigger, store it. | ||
| 51 | 52 | ||
| 52 | That is all that has to be done on the relay side, and therefore the only part needed for interoperability. | 53 | That is all that has to be done on the relay side, and therefore the only part needed for interoperability. |
| 53 | 54 | ||