...

Source file src/github.com/decred/dcrd/dcrec/secp256k1/v4/schnorr/example_test.go

Documentation: github.com/decred/dcrd/dcrec/secp256k1/v4/schnorr

     1  // Copyright (c) 2020-2021 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package schnorr_test
     6  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  
    11  	"github.com/decred/dcrd/crypto/blake256"
    12  	"github.com/decred/dcrd/dcrec/secp256k1/v4"
    13  	"github.com/decred/dcrd/dcrec/secp256k1/v4/schnorr"
    14  )
    15  
    16  // This example demonstrates signing a message with the EC-Schnorr-DCRv0 scheme
    17  // using a secp256k1 private key that is first parsed from raw bytes and
    18  // serializing the generated signature.
    19  func ExampleSign() {
    20  	// Decode a hex-encoded private key.
    21  	pkBytes, err := hex.DecodeString("22a47fa09a223f2aa079edf85a7c2d4f8720ee6" +
    22  		"3e502ee2869afab7de234b80c")
    23  	if err != nil {
    24  		fmt.Println(err)
    25  		return
    26  	}
    27  	privKey := secp256k1.PrivKeyFromBytes(pkBytes)
    28  
    29  	// Sign a message using the private key.
    30  	message := "test message"
    31  	messageHash := blake256.Sum256([]byte(message))
    32  	signature, err := schnorr.Sign(privKey, messageHash[:])
    33  	if err != nil {
    34  		fmt.Println(err)
    35  		return
    36  	}
    37  
    38  	// Serialize and display the signature.
    39  	fmt.Printf("Serialized Signature: %x\n", signature.Serialize())
    40  
    41  	// Verify the signature for the message using the public key.
    42  	pubKey := privKey.PubKey()
    43  	verified := signature.Verify(messageHash[:], pubKey)
    44  	fmt.Printf("Signature Verified? %v\n", verified)
    45  
    46  	// Output:
    47  	// Serialized Signature: 970603d8ccd2475b1ff66cfb3ce7e622c5938348304c5a7bc2e6015fb98e3b457d4e912fcca6ca87c04390aa5e6e0e613bbbba7ffd6f15bc59f95bbd92ba50f0
    48  	// Signature Verified? true
    49  }
    50  
    51  // This example demonstrates verifying an EC-Schnorr-DCRv0 signature against a
    52  // public key that is first parsed from raw bytes.  The signature is also parsed
    53  // from raw bytes.
    54  func ExampleSignature_Verify() {
    55  	// Decode hex-encoded serialized public key.
    56  	pubKeyBytes, err := hex.DecodeString("02a673638cb9587cb68ea08dbef685c6f2d" +
    57  		"2a751a8b3c6f2a7e9a4999e6e4bfaf5")
    58  	if err != nil {
    59  		fmt.Println(err)
    60  		return
    61  	}
    62  	pubKey, err := schnorr.ParsePubKey(pubKeyBytes)
    63  	if err != nil {
    64  		fmt.Println(err)
    65  		return
    66  	}
    67  
    68  	// Decode hex-encoded serialized signature.
    69  	sigBytes, err := hex.DecodeString("970603d8ccd2475b1ff66cfb3ce7e622c59383" +
    70  		"48304c5a7bc2e6015fb98e3b457d4e912fcca6ca87c04390aa5e6e0e613bbbba7ffd" +
    71  		"6f15bc59f95bbd92ba50f0")
    72  	if err != nil {
    73  		fmt.Println(err)
    74  		return
    75  	}
    76  	signature, err := schnorr.ParseSignature(sigBytes)
    77  	if err != nil {
    78  		fmt.Println(err)
    79  		return
    80  	}
    81  
    82  	// Verify the signature for the message using the public key.
    83  	message := "test message"
    84  	messageHash := blake256.Sum256([]byte(message))
    85  	verified := signature.Verify(messageHash[:], pubKey)
    86  	fmt.Println("Signature Verified?", verified)
    87  
    88  	// Output:
    89  	// Signature Verified? true
    90  }
    91  

View as plain text