-
Notifications
You must be signed in to change notification settings - Fork 577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NIP-76 Relay Read Permissions #1497
Open
vitorpamplona
wants to merge
10
commits into
nostr-protocol:master
Choose a base branch
from
vitorpamplona:read-permission
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a784792
read permissions
vitorpamplona bad139e
adjustments
vitorpamplona c4c2061
typos
vitorpamplona c1649e4
typos
vitorpamplona e71e390
improves wording
vitorpamplona 92d8bcc
fixes pseudo code
vitorpamplona 3c7429b
Merge branch 'read-permission' of https://github.com/vitorpamplona/ni…
vitorpamplona 36f9a2e
Fixes the pseudo code
vitorpamplona d60412c
breaks the `rp` keys into multiple tags instead of a single list
vitorpamplona c47f5c0
adds salt
vitorpamplona File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,91 @@ | ||
NIP-76 | ||
====== | ||
|
||
Relay Read Permissions | ||
---------------------- | ||
|
||
`draft` `optional` | ||
|
||
Tags `rp` (read permission) and `prp` (probabilistic read permission) specify which keys are authorized to download an event from the relay. | ||
|
||
Events tagged with `rp` or `prp` require AUTH for download. | ||
|
||
## Read Permission | ||
|
||
The `rp` tag takes a pubkey in lowercase hex as value. | ||
|
||
```json | ||
["rp", "<pubkey1>"] | ||
["rp", "<pubkey2>"] | ||
["rp", "<pubkey3>"] | ||
``` | ||
|
||
When responding to `REQ`s, if an event contains `rp` tags, relays MUST verify that the authenticated user is either the event's author or one of the keys in the `rp` set before delivering it to the client. | ||
|
||
## Probabilistic Read Permissions | ||
|
||
Probabilistic permissions are implemented using Bloom filters that represent a set of authorized pubkeys. These permissions are expressed as a colon-separated value comprising: | ||
1. the number of bits in the bit array, | ||
2. the number of hash rounds applied, and | ||
3. the bit array encoded in Base64. | ||
4. the salt encoded in Base64. | ||
|
||
```json | ||
["prp", "<bits>:<rounds>:<base64>:<salt>"] | ||
``` | ||
|
||
Bloom filters MUST use `SHA256` functions applied to the concatenation of the key, salt, and index, as demonstrated in the pseudocode below: | ||
|
||
```js | ||
class BloomFilter(size: Int, rounds: Int, buffer: ByteArray, salt: ByteArray) { | ||
val bits = BitArray(buffer) | ||
|
||
fun bitIndex(value: ByteArray, index: Byte) { | ||
return BigInt(sha256(value || salt || index)) % size | ||
} | ||
|
||
fun add(pubkey: HexKey) { | ||
val value = pubkey.hexToByteArray() | ||
|
||
for (index in 0 until rounds) { | ||
bits[bitIndex(value, index)] = true | ||
} | ||
} | ||
|
||
fun mightContains(pubkey: HexKey): Boolean { | ||
val value = pubkey.hexToByteArray() | ||
|
||
for (index in 0 until rounds) { | ||
if (!bits[bitIndex(value, index)]) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
fun encode() { | ||
return size + ":" + rounds + ":" + base64Encode(bits.toByteArray()) + ":" + base64Encode(salt) | ||
} | ||
|
||
fun decode(str: String): BloomFilter { | ||
val [sizeStr, roundsStr, bufferB64, saltB64] = str.split(":") | ||
return BloomFilter(sizeStr.toInt(), roundsStr.toInt(), base64Decode(bufferB64), base64Decode(saltB64)) | ||
} | ||
} | ||
``` | ||
|
||
When responding to `REQ`s, if an event contains `prp` tags, relays MUST verify that the authenticated user is either the event's author or matches any of the filters before delivering it to the client. | ||
|
||
If both `rp` and `prp` tags are present, the authenticated user MUST either be in the `rp` set or match any `prp` filter. | ||
|
||
### Test cases | ||
|
||
The filter below has 100 bits and uses 10 rounds of hashing, which should be capable of handling up to 10,000,000 keys without producing any false positives. | ||
|
||
```json | ||
["prp", "100:10:AAAkAQANcYQFCQoB:hZkZYqqdxcE="] | ||
``` | ||
|
||
It includes keys `ca29c211f1c72d5b6622268ff43d2288ea2b2cb5b9aa196ff9f1704fc914b71b` and `460c25e682fda7832b52d1f22d3d22b3176d972f60dcdc3212ed8c92ef85065c` | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @jooray mentions, the math must be off here. Also the wording is wrong.
A filter by itself has no false positives ever. It has a false positive rate (FPR).
So what the nip author meant was 10 million npubs not colliding randomly with a bloom filter that has 2 elements, 100 bits and 10 rounds.
With the chosen parameters, the FPR is of the order E-8 - 1 in 26 million, so yes, with 10 million accounts, it's more likely none of them collides with any specific such filter than not but there is no guarantee about this being collision-free.
That said, 10 rounds are not a good choice of parameters for 2 elements. I'm not an expert on this and only plaid around with https://hur.st/bloomfilter/ but that tool suggests that with the same filter size but 35 rounds you would get an FPR of 1 in 27 billion.
Edit: As base64 encodes 6bit/char, 100bit is a bad choice, too. You can get 2 more bits for free. This might not sound like much but it brings the FPR down to 1 in 44 billion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test code is here if you want to play around :)
https://github.com/vitorpamplona/amethyst/blob/428ed464afbcbdb2c50eb224d276ff87ae3836e5/quartz/src/androidTest/java/com/vitorpamplona/quartz/bloom/BloomFilter.kt
I hope i didn't mess the code up :)