rfc6979

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2022 License: MIT Imports: 3 Imported by: 1

README

RFC6979

This package implements RFC6979, titled:

Deterministic Usage of the Digital Signature Algorithm (DSA) and Elliptic Curve Digital Signature Algorithm (ECDSA)

This RFC provides a way to make digital signatures deterministic instead of random. In the traditional DSA or ECDSA protocols, crafting signatures would involve selecting a random nonce value, called k. If selection of k is not perfectly random, then "slight biases in that process may be turned into attacks on the signature schemes."

RFC6979 specifies a procedure for generating k values deterministically, while still providing perfectly uniform distribution (apparent randomness). The k values are determined by four factors:

  • The finite field order, q ('order' means the number of numbers in the finite field). This is usually some very large constant value.
  • The private key being used to sign
  • The hash of the message which the caller wants to sign
  • The hash algorithm used to hash the message

This package provides a Golang implementation of the specification which passes all test vectors included in the RFC document.

Usage

To add this package to your Go module:

go get github.com/kklash/rfc6979

First instantiate a Q struct using the order of your finite field, and call its Nonce method to generate k values when signing data.

package main

import "github.com/kklash/rfc6979"

func main() {
  q := rfc6979.NewQ(big.NewInt(31))
  privateKey := big.NewInt(14)
  messageHash := sha256.Sum256([]byte("this is a message to sign"))

  k := q.Nonce(privateKey, messageHash[:], sha256.New)
  fmt.Println(k) // 27
}

Contributing

Tests can be run with

go test

To run benchmarks as well:

go test -bench=.

Documentation

Overview

Package rfc6979 generates deterministic nonce values for digital signatures.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Q

type Q struct {
	// contains filtered or unexported fields
}

Q represents a finite field order Q.

func NewQ

func NewQ(q *big.Int) *Q

func (*Q) Bits2int added in v1.1.0

func (q *Q) Bits2int(bits []byte) *big.Int

Bits2int converts a byte slice into a big.Int. Excess bits larger than the finite field order are right-shifted off.

func (*Q) Nonce

func (q *Q) Nonce(privkey *big.Int, h1 []byte, hashFn func() hash.Hash) *big.Int

Nonce calculates a deterministic value for K, to be used for digitally signing the given hash h1.

Example
package main

import (
	"crypto/sha256"
	"fmt"
	"math/big"

	"github.com/kklash/rfc6979"
)

func main() {
	q := rfc6979.NewQ(big.NewInt(31))
	privateKey := big.NewInt(14)
	messageHash := sha256.Sum256([]byte("this is a message to sign"))

	k := q.Nonce(privateKey, messageHash[:], sha256.New)
	fmt.Println("k:", k)

}
Output:

k: 27

Jump to

Keyboard shortcuts

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