Abstract
This BEP specifies an extension to the BitTorrent DHT (BEP 5) to support scrapes via distributed counting based on bloom filters.

Rationale
Scrapes are an important part of the BitTorrent ecosystem as they allow users to assess the state of a torrent and clients to determine which swarm to select next in their seeding queue, without having to participate in the swarm first.

These options are currently not available in the absence of a tracker, this BEP aims to rectify this.


function estimateCount(array $bloom): float
{
   $m = 256 * 8;
   $k = 2;
   $c = $m - 1;
   for ($i = 0; $i < count($bloom); $i++) {
       $c -= ($bloom[$i] === 0) ? 8 : substr_count(decbin($bloom[$i]), '0');
   }
   $c = max(1, $c);
   $size = log($c / $m) / ($k * log(1 - 1 / $m));
   return $size;
}
function insertIP(array $ip, array &$bloom): void
{
   $hash = sha1(implode('.', $ip), true);
   $index1 = unpack('v', substr($hash, 0, 2))[1];
   $index2 = unpack('v', substr($hash, 2, 2))[1];
   $m = 256 * 8;
   $index1 %= $m;
   $index2 %= $m;
   $bloom[$index1 >> 3] |= (1 << ($index1 & 7));
   $bloom[$index2 >> 3] |= (1 << ($index2 & 7));
}


The insertIP function takes the bloom filter as an input and returns the estimated count of items in the bloom filter. The function first initializes the parameters m and k. It then calculates the number of zero bits in the bloom filter and uses it to estimate the count using the provided equation.

The estimateCount function takes the bloom filter as an input and returns the estimated count of items in the bloom filter. The function first initializes the parameters m and k. It then calculates the number of zero bits in the bloom filter and uses it to estimate the count using the provided equation.