Skip to content
forked from hbakhtiyor/schnorr

Go implementation of the BIP Schnorr signature scheme.

License

Notifications You must be signed in to change notification settings

fiatjaf/schnorr

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoDoc Build Status Go Report Card License Latest tag

schnorr

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

Table of Contents

Usage

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)

API

Requiring the module gives an object with 2 methods:

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

Sign a 32-byte message with the private key, returning a 64-byte signature. Read more

Arguments
  1. privateKey (*big.Int): The integer secret key in the range 1..n-1.
  2. message ([32]byte): The 32-byte array message.
Returns

([64]byte, error): A 64-byte array signature. An error if signing fails.

Examples
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(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. Read more.

Arguments
  1. publicKey ([32]byte): The 32-byte array public key.
  2. message ([32]byte): The 32-byte array message.
  3. signature ([64]byte): The 64-byte array signature.
Returns

(bool, error): True if signature is valid, An error if verification fails.

Examples
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.")
}

Credits

About

Go implementation of the BIP Schnorr signature scheme.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Go 100.0%