{"id":"89672f04e9b5b6a91e10e230f78cbd1a3ddf385b7018c7899bcb0694aeddd349","pubkey":"fd208ee8c8f283780a9552896e4823cc9dc6bfd442063889577106940fd927c1","created_at":1779472885,"kind":30817,"tags":[["d","nip-13"],["title","NIP-13"],["i","https://github.com/nostr-protocol/nips/blob/master/13.md","fork"],["client","nostrhub.io"]],"content":"NIP-13\n======\n\nProof of Work\n-------------\n\n`draft` `optional` `relay`\n\nThis NIP defines a way to generate and interpret Proof of Work for nostr notes. Proof of Work (PoW) is a way to add a proof of computational work to a note. This is a bearer proof that all relays and clients can universally validate with a small amount of code. This proof can be used as a means of spam deterrence.\n\n`difficulty` is defined to be the number of leading zero bits in the `NIP-01` id. For example, an id of `000000000e9d97a1ab09fc381030b346cdd7a142ad57e6df0b46dc9bef6c7e2d` has a difficulty of `36` with `36` leading 0 bits.\n\n`002f...` is `0000 0000 0010 1111...` in binary, which has 10 leading zeroes. Do not forget to count leading zeroes for hex digits <= `7`.\n\nMining\n------\n\nTo generate PoW for a `NIP-01` note, a `nonce` tag is used:\n\n```json\n{\"content\": \"It's just me mining my own business\", \"tags\": [[\"nonce\", \"1\", \"21\"]]}\n```\n\nWhen mining, the second entry to the nonce tag is updated, and then the id is recalculated (see [NIP-01](./01.md)). If the id has the desired number of leading zero bits, the note has been mined. It is recommended to update the `created_at` as well during this process.\n\nThe third entry to the nonce tag `SHOULD` contain the target difficulty. This allows clients to protect against situations where bulk spammers targeting a lower difficulty get lucky and match a higher difficulty. For example, if you require 40 bits to reply to your thread and see a committed target of 30, you can safely reject it even if the note has 40 bits difficulty. Without a committed target difficulty you could not reject it. Committing to a target difficulty is something all honest miners should be ok with, and clients `MAY` reject a note matching a target difficulty if it is missing a difficulty commitment.\n\nExample mined note\n------------------\n\n```json\n{\n  \"id\": \"000006d8c378af1779d2feebc7603a125d99eca0ccf1085959b307f64e5dd358\",\n  \"pubkey\": \"a48380f4cfcc1ad5378294fcac36439770f9c878dd880ffa94bb74ea54a6f243\",\n  \"created_at\": 1651794653,\n  \"kind\": 1,\n  \"tags\": [\n    [\"nonce\", \"776797\", \"20\"]\n  ],\n  \"content\": \"It's just me mining my own business\",\n  \"sig\": \"284622fc0a3f4f1303455d5175f7ba962a3300d136085b9566801bc2e0699de0c7e31e44c81fb40ad9049173742e904713c3594a1da0fc5d2382a25c11aba977\"\n}\n```\n\nValidating\n----------\n\nHere is some reference C code for calculating the difficulty (aka number of leading zero bits) in a nostr event id:\n\n```c\nint zero_bits(unsigned char b)\n{\n        int n = 0;\n\n        if (b == 0)\n                return 8;\n\n        while (b >>= 1)\n                n++;\n\n        return 7-n;\n}\n\n/* find the number of leading zero bits in a hash */\nint count_leading_zero_bits(unsigned char *hash)\n{\n        int bits, total, i;\n        for (i = 0, total = 0; i < 32; i++) {\n                bits = zero_bits(hash[i]);\n                total += bits;\n                if (bits != 8)\n                        break;\n        }\n        return total;\n}\n```\n\nHere is some JavaScript code for doing the same thing:\n\n```javascript\n// hex should be a hexadecimal string (with no 0x prefix)\nfunction countLeadingZeroes(hex) {\n  let count = 0;\n\n  for (let i = 0; i < hex.length; i++) {\n    const nibble = parseInt(hex[i], 16);\n    if (nibble === 0) {\n      count += 4;\n    } else {\n      count += Math.clz32(nibble) - 28;\n      break;\n    }\n  }\n\n  return count;\n}\n```\n\nDelegated Proof of Work\n-----------------------\n\nSince the `NIP-01` note id does not commit to any signature, PoW can be outsourced to PoW providers, perhaps for a fee. This provides a way for clients to get their messages out to PoW-restricted relays without having to do any work themselves, which is useful for energy-constrained devices like mobile phones.","sig":"2aa808fdbbf6adedd38900aaaf4adb956fd90427bb5fcc0cc711c3a57aa4a1b9e83ab355e691af9331fb4dda5b4883d59d94ac14773c5822710063c97a103480"}