bip340 – github.com/fiatjaf/bip340 Index | Examples | Files

package bip340

import "github.com/fiatjaf/bip340"

Index

Examples

Variables

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)
)
var Curve = btcec.S256()

Functions

func GeneratePrivateKey

func GeneratePrivateKey() *big.Int

func GetPublicKey

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

func ParsePrivateKey

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

func ParsePublicKey

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

Code:

{
	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
}

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

Code:

{
	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.
}

Output:

The signature is valid.

Source Files

bip340.go secp256k1.go

Version
v1.1.1 (latest)
Published
Jan 6, 2022
Platform
linux/amd64
Imports
10 packages
Last checked
3 days ago

Tools for package owners.