nip13: fix leading zeroes check (remainings from hex era).

This commit is contained in:
fiatjaf
2025-05-12 05:59:21 -03:00
parent 94d29f1230
commit 3f436d2a86

View File

@@ -3,7 +3,6 @@ package nip13
import ( import (
"context" "context"
"crypto/sha256" "crypto/sha256"
"encoding/hex"
"errors" "errors"
"math/bits" "math/bits"
"runtime" "runtime"
@@ -39,16 +38,12 @@ func CommittedDifficulty(event nostr.Event) int {
// Difficulty counts the number of leading zero bits in an event ID. // Difficulty counts the number of leading zero bits in an event ID.
func Difficulty(id nostr.ID) int { func Difficulty(id nostr.ID) int {
var zeros int var zeros int
var b [1]byte for i := 0; i < 32; i++ {
for i := 0; i < 32; i += 2 {
if id[i] == 0 { if id[i] == 0 {
zeros += 8 zeros += 8
continue continue
} }
if _, err := hex.Decode(b[:], []byte{id[i], id[i+1]}); err != nil { zeros += bits.LeadingZeros8(id[i])
return -1
}
zeros += bits.LeadingZeros8(b[0])
break break
} }
return zeros return zeros