github.com/platonnetwork/platon-go@v0.7.6/crypto/rfc6979/rfc6979_test.go (about)

     1  package rfc6979
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"encoding/hex"
     6  	"math/big"
     7  	"testing"
     8  	"fmt"
     9  	"crypto/ecdsa"
    10  	"crypto/rand"
    11  	"github.com/PlatONnetwork/PlatON-Go/crypto/secp256k1"
    12  )
    13  
    14  // https://tools.ietf.org/html/rfc6979#appendix-A.1
    15  func TestGenerate_k(t *testing.T) {
    16  	q, _ := new(big.Int).SetString("4000000000000000000020108A2E0CC0D99F8A5EF", 16)
    17  	x, _ := new(big.Int).SetString("09A4D6792295A7F730FC3F2B49CBC0F62E862272F", 16)
    18  	hash, _ := hex.DecodeString("AF2BDBE1AA9B6EC1E2ADE1D694F41FC71A831D0268E9891562113D8A62ADD1BF")
    19  	expected, _ := new(big.Int).SetString("23AF4074C90A02B3FE61D286D5C87F425E6BDD81B", 16)
    20  	var actual *big.Int
    21  	generate_k(q, x, sha256.New, hash, func(k *big.Int) bool {
    22  		actual = k
    23  		return true
    24  	})
    25  
    26  	if actual.Cmp(expected) != 0 {
    27  		t.Errorf("Expected %x, got %x", expected, actual)
    28  	}
    29  }
    30  
    31  func TestDeterministicNonce(t *testing.T) {
    32  	curve := secp256k1.S256()
    33  	sk, err := ecdsa.GenerateKey(curve, rand.Reader)
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  	fmt.Printf("sk:%x\n",sk.D.Bytes())
    38  	msg := "hello"
    39  	k, err := ECVRF_nonce_generation(sk.D.Bytes(), []byte(msg))
    40  	fmt.Printf("k:%x\n",k.D.Bytes())
    41  
    42  }