event.CheckID()
absurd performance increase benefit over a naïve id comparison! goos: linux goarch: amd64 pkg: github.com/nbd-wtf/go-nostr cpu: AMD Ryzen 3 3200G with Radeon Vega Graphics BenchmarkIDCheck/naïve-4 1478547 802.9 ns/op 488 B/op 15 allocs/op BenchmarkIDCheck/big_brain-4 1673341 715.3 ns/op 358 B/op 12 allocs/op
This commit is contained in:
22
event.go
22
event.go
@@ -35,6 +35,28 @@ func (evt *Event) GetID() string {
|
||||
return hex.EncodeToString(h[:])
|
||||
}
|
||||
|
||||
// CheckID checks if the implied ID matches the given ID
|
||||
func (evt *Event) CheckID() bool {
|
||||
ser := evt.Serialize()
|
||||
h := sha256.Sum256(ser)
|
||||
|
||||
const hextable = "0123456789abcdef"
|
||||
|
||||
for i := 0; i < 32; i++ {
|
||||
b := hextable[h[i]>>4]
|
||||
if b != evt.ID[i*2] {
|
||||
return false
|
||||
}
|
||||
|
||||
b = hextable[h[i]&0x0f]
|
||||
if b != evt.ID[i*2+1] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Serialize outputs a byte array that can be hashed/signed to identify/authenticate.
|
||||
// JSON encoding as defined in RFC4627.
|
||||
func (evt *Event) Serialize() []byte {
|
||||
|
||||
Reference in New Issue
Block a user