make libsecp256k1 available with a build tag.

This commit is contained in:
fiatjaf
2024-09-27 17:35:43 -03:00
parent 7503643e80
commit 47d493b7e8
11 changed files with 189 additions and 249 deletions

View File

@@ -1,33 +0,0 @@
This wraps [libsecp256k1](https://github.com/bitcoin-core/secp256k1) with `cgo`.
It doesn't embed the library or anything smart like that because I don't know how to do it, so you must have it installed in your system.
It is faster than the pure Go version:
```
goos: linux
goarch: amd64
pkg: github.com/nbd-wtf/go-nostr/libsecp256k1
cpu: AMD Ryzen 3 3200G with Radeon Vega Graphics
BenchmarkSignatureVerification/btcec-4 145 7873130 ns/op 127069 B/op 579 allocs/op
BenchmarkSignatureVerification/libsecp256k1-4 502 2314573 ns/op 112241 B/op 392 allocs/op
```
To use it manually, just import. To use it inside the automatic verification that happens for subscriptions, set it up with a `SimplePool`:
```go
pool := nostr.NewSimplePool()
pool.SignatureChecker = func (evt nostr.Event) bool {
ok, _ := libsecp256k1.CheckSignature(evt)
return ok
}
```
Or directly to the `Relay`:
```go
relay := nostr.RelayConnect(context.Background(), "wss://relay.nostr.com", nostr.WithSignatureChecker(func (evt nostr.Event) bool {
ok, _ := libsecp256k1.CheckSignature(evt)
return ok
}))
```

View File

@@ -1,34 +0,0 @@
package libsecp256k1
import (
"encoding/json"
"testing"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/test_common"
)
func BenchmarkSignatureVerification(b *testing.B) {
events := make([]*nostr.Event, len(test_common.NormalEvents))
for i, jevt := range test_common.NormalEvents {
evt := &nostr.Event{}
json.Unmarshal([]byte(jevt), evt)
events[i] = evt
}
b.Run("btcec", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, evt := range events {
evt.CheckSignature()
}
}
})
b.Run("libsecp256k1", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, evt := range events {
CheckSignature(evt)
}
}
})
}

View File

@@ -1,51 +0,0 @@
package libsecp256k1
/*
#cgo LDFLAGS: -lsecp256k1
#include <secp256k1.h>
#include <secp256k1_schnorrsig.h>
#include <secp256k1_extrakeys.h>
*/
import "C"
import (
"crypto/rand"
"errors"
"unsafe"
)
var globalSecp256k1Context *C.secp256k1_context
func init() {
globalSecp256k1Context = C.secp256k1_context_create(C.SECP256K1_CONTEXT_SIGN | C.SECP256K1_CONTEXT_VERIFY)
if globalSecp256k1Context == nil {
panic("failed to create secp256k1 context")
}
}
func Sign(msg [32]byte, sk [32]byte) ([64]byte, error) {
var sig [64]byte
var keypair C.secp256k1_keypair
if C.secp256k1_keypair_create(globalSecp256k1Context, &keypair, (*C.uchar)(unsafe.Pointer(&sk[0]))) != 1 {
return sig, errors.New("failed to parse private key")
}
var random [32]byte
rand.Read(random[:])
if C.secp256k1_schnorrsig_sign32(globalSecp256k1Context, (*C.uchar)(unsafe.Pointer(&sig[0])), (*C.uchar)(unsafe.Pointer(&msg[0])), &keypair, (*C.uchar)(unsafe.Pointer(&random[0]))) != 1 {
return sig, errors.New("failed to sign message")
}
return sig, nil
}
func Verify(msg [32]byte, sig [64]byte, pk [32]byte) bool {
var xonly C.secp256k1_xonly_pubkey
if C.secp256k1_xonly_pubkey_parse(globalSecp256k1Context, &xonly, (*C.uchar)(unsafe.Pointer(&pk[0]))) != 1 {
return false
}
return C.secp256k1_schnorrsig_verify(globalSecp256k1Context, (*C.uchar)(unsafe.Pointer(&sig[0])), (*C.uchar)(unsafe.Pointer(&msg[0])), 32, &xonly) == 1
}

View File

@@ -1,20 +0,0 @@
package libsecp256k1
import (
"encoding/json"
"testing"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/test_common"
"github.com/stretchr/testify/assert"
)
func TestEventVerification(t *testing.T) {
for _, jevt := range test_common.NormalEvents {
evt := &nostr.Event{}
json.Unmarshal([]byte(jevt), evt)
ok, _ := CheckSignature(evt)
shouldBe, _ := evt.CheckSignature()
assert.Equal(t, ok, shouldBe, "%s signature must be %s", jevt, shouldBe)
}
}

View File

@@ -1,26 +0,0 @@
package libsecp256k1
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/nbd-wtf/go-nostr"
)
func CheckSignature(evt *nostr.Event) (bool, error) {
var pk [32]byte
_, err := hex.Decode(pk[:], []byte(evt.PubKey))
if err != nil {
return false, fmt.Errorf("event pubkey '%s' is invalid hex: %w", evt.PubKey, err)
}
var sig [64]byte
_, err = hex.Decode(sig[:], []byte(evt.Sig))
if err != nil {
return false, fmt.Errorf("event signature '%s' is invalid hex: %w", evt.Sig, err)
}
msg := sha256.Sum256(evt.Serialize())
return Verify(msg, sig, pk), nil
}