Skip to content

Commit

Permalink
nip13: speed improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Oct 22, 2024
1 parent b04e0d1 commit b18510b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
11 changes: 8 additions & 3 deletions nip13.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { test, expect } from 'bun:test'
import { getPow, minePow } from './nip13.ts'

test('identifies proof-of-work difficulty', async () => {
const id = '000006d8c378af1779d2feebc7603a125d99eca0ccf1085959b307f64e5dd358'
const difficulty = getPow(id)
expect(difficulty).toEqual(21)
;[
['000006d8c378af1779d2feebc7603a125d99eca0ccf1085959b307f64e5dd358', 21],
['6bf5b4f434813c64b523d2b0e6efe18f3bd0cbbd0a5effd8ece9e00fd2531996', 1],
['00003479309ecdb46b1c04ce129d2709378518588bed6776e60474ebde3159ae', 18],
['01a76167d41add96be4959d9e618b7a35f26551d62c43c11e5e64094c6b53c83', 7],
['ac4f44bae06a45ebe88cfbd3c66358750159650a26c0d79e8ccaa92457fca4f6', 0],
['0000000000000000006cfbd3c66358750159650a26c0d79e8ccaa92457fca4f6', 73],
].forEach(([id, diff]) => expect(getPow(id as string)).toEqual(diff as number))
})

test('mines POW for an event', async () => {
Expand Down
24 changes: 16 additions & 8 deletions nip13.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { type UnsignedEvent, type Event, getEventHash } from './pure.ts'
import { bytesToHex } from '@noble/hashes/utils'
import { type UnsignedEvent, type Event } from './pure.ts'
import { sha256 } from '@noble/hashes/sha256'

import { utf8Encoder } from './utils.ts'

/** Get POW difficulty from a Nostr hex ID. */
export function getPow(hex: string): number {
let count = 0

for (let i = 0; i < hex.length; i++) {
const nibble = parseInt(hex[i], 16)
for (let i = 0; i < 64; i += 8) {
const nibble = parseInt(hex.substring(i, i + 8), 16)
if (nibble === 0) {
count += 4
count += 32
} else {
count += Math.clz32(nibble) - 28
count += Math.clz32(nibble)
break
}
}
Expand All @@ -20,8 +24,6 @@ export function getPow(hex: string): number {
/**
* Mine an event with the desired POW. This function mutates the event.
* Note that this operation is synchronous and should be run in a worker context to avoid blocking the main thread.
*
* Adapted from Snort: https://git.v0l.io/Kieran/snort/src/commit/4df6c19248184218c4c03728d61e94dae5f2d90c/packages/system/src/pow-util.ts#L14-L36
*/
export function minePow(unsigned: UnsignedEvent, difficulty: number): Omit<Event, 'sig'> {
let count = 0
Expand All @@ -41,7 +43,7 @@ export function minePow(unsigned: UnsignedEvent, difficulty: number): Omit<Event

tag[1] = (++count).toString()

event.id = getEventHash(event)
event.id = fastEventHash(event)

if (getPow(event.id) >= difficulty) {
break
Expand All @@ -50,3 +52,9 @@ export function minePow(unsigned: UnsignedEvent, difficulty: number): Omit<Event

return event
}

export function fastEventHash(evt: UnsignedEvent): string {
return bytesToHex(
sha256(utf8Encoder.encode(JSON.stringify([0, evt.pubkey, evt.created_at, evt.kind, evt.tags, evt.content]))),
)
}

0 comments on commit b18510b

Please sign in to comment.