This is a simple and naïve Go implementation of the standard 64-byte Schnorr signature scheme over the elliptic curve secp256k1 defined by BIP340. It only implements simple signing and verifying.
The current version passes all test vectors provided with the BIP (but the author does not give any guarantees that the algorithm is implemented correctly for every edge case).
Install using:
go get -u github.com/fiatjaf/schnorr
In your code:
import "github.com/fiatjaf/schnorr"
signature, err := schnorr.Sign(privateKey, message)
result, err := schnorr.Verify(publicKey, message, signature)
Requiring the module gives an object with 2 methods:
Sign a 32-byte message with the private key, returning a 64-byte signature. Read more
- privateKey (*big.Int): The integer secret key in the range 1..n-1.
- message ([32]byte): The 32-byte array message.
([64]byte, error): A 64-byte array signature. An error if signing fails.
var message [32]byte
privateKey, _ := new(big.Int).SetString("B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF", 16)
msg, _ := hex.DecodeString("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
copy(message[:], msg)
signature, err := schnorr.Sign(privateKey, message)
if err != nil {
fmt.Printf("The signing is failed: %v\n", err)
}
fmt.Printf("The signature is: %x\n", signature)
Verify a 64-byte signature of a 32-byte message against the public key. Read more.
- publicKey ([32]byte): The 32-byte array public key.
- message ([32]byte): The 32-byte array message.
- signature ([64]byte): The 64-byte array signature.
(bool, error): True if signature is valid, An error if verification fails.
var (
publicKey [32]byte
message [32]byte
signature [64]byte
)
pk, _ := hex.DecodeString("02DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B502BA659")
copy(publicKey[:], pk)
msg, _ := hex.DecodeString("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
copy(message[:], msg)
sig, _ := hex.DecodeString("2A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D1E51A22CCEC35599B8F266912281F8365FFC2D035A230434A1A64DC59F7013FD")
copy(signature[:], sig)
if result, err := schnorr.Verify(publicKey, message, signature); err != nil {
fmt.Printf("The signature verification failed: %v\n", err)
} else if result {
fmt.Println("The signature is valid.")
}