bip340

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2022 License: MIT Imports: 10 Imported by: 2

README

GoDoc Build Status Go Report Card License Latest tag

bip340

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 or safely).

Usage

Install using:

go get -u github.com/fiatjaf/bip340

In your code:

import "github.com/fiatjaf/bip340"

signature, err := bip340.Sign(privateKey, message)
result, err := bip340.Verify(publicKey, message, signature)

Credits

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Zero holds a big integer of 0
	Zero = new(big.Int)
	// One holds a big integer of 1
	One = new(big.Int).SetInt64(1)
	// Two holds a big integer of 2
	Two = new(big.Int).SetInt64(2)
	// Three holds a big integer of 3
	Three = new(big.Int).SetInt64(3)
	// Four holds a big integer of 4
	Four = new(big.Int).SetInt64(4)
	// Seven holds a big integer of 7
	Seven = new(big.Int).SetInt64(7)
	// N2 holds a big integer of N-2
	N2 = new(big.Int).Sub(Curve.N, Two)
)
View Source
var Curve = btcec.S256()

Functions

func GeneratePrivateKey added in v1.1.1

func GeneratePrivateKey() *big.Int

func GetPublicKey

func GetPublicKey(privateKey *big.Int) [32]byte

func ParsePrivateKey added in v1.1.0

func ParsePrivateKey(hexKey string) (*big.Int, error)

func ParsePublicKey added in v1.1.0

func ParsePublicKey(hexKey string) ([32]byte, error)

func Sign

func Sign(privateKey *big.Int, message [32]byte, aux []byte) ([64]byte, error)

Sign a 32 byte message with the private key, returning a 64 byte signature. Calling with a nil aux will cause the function to use a deterministic nonce. https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki#signing

Example
var message [32]byte

privateKey, _ := new(big.Int).SetString("B7E151628AED2A6ABF7158809CF4F3C762E7160F38B4DA56A784D9045190CFEF", 16)
msg, _ := hex.DecodeString("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
copy(message[:], msg)

signature, err := Sign(privateKey, message, nil)
if err != nil {
	fmt.Printf("The signing is failed: %v\n", err)
}
fmt.Printf("The signature is: %x\n", signature)
Output:

The signature is: 2a298dacae57395a15d0795ddbfd1dcb564da82b0f269bc70a74f8220429ba1d96ef2be1af1cae22bf6736fa9650de69e7da1d37f92c4a92fbc93cc28fdbdb84

func Verify

func Verify(publicKey [32]byte, message [32]byte, signature [64]byte) (bool, error)

Verify a 64 byte signature of a 32 byte message against the public key. Returns an error if verification fails. https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki#verification

Example
var (
	publicKey [32]byte
	message   [32]byte
	signature [64]byte
)

pk, _ := hex.DecodeString("dff1d77f2a671c5f36183726db2341be58feae1da2deced843240f7b502ba659")
copy(publicKey[:], pk)
msg, _ := hex.DecodeString("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
copy(message[:], msg)
sig, _ := hex.DecodeString("2a298dacae57395a15d0795ddbfd1dcb564da82b0f269bc70a74f8220429ba1d96ef2be1af1cae22bf6736fa9650de69e7da1d37f92c4a92fbc93cc28fdbdb84")
copy(signature[:], sig)

if result, err := Verify(publicKey, message, signature); err != nil {
	fmt.Printf("The signature verification failed: %v\n", err)
} else if result {
	fmt.Println("The signature is valid.")
}
Output:

The signature is valid.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL