github.com/MetalBlockchain/metalgo@v1.11.9/utils/crypto/secp256k1/rfc6979_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package secp256k1
     5  
     6  import (
     7  	"encoding/hex"
     8  	"fmt"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  // See: https://bitcointalk.org/index.php?topic=285142.msg3300992#msg3300992 as
    15  // the source of these test vectors.
    16  var rfc6979Tests = []test{
    17  	{
    18  		skHex: "0000000000000000000000000000000000000000000000000000000000000001",
    19  		msg:   "Everything should be made as simple as possible, but not simpler.",
    20  		rsHex: "33a69cd2065432a30f3d1ce4eb0d59b8ab58c74f27c41a7fdb5696ad4e6108c96f807982866f785d3f6418d24163ddae117b7db4d5fdf0071de069fa54342262",
    21  	},
    22  	{
    23  		skHex: "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140",
    24  		msg:   "Equations are more important to me, because politics is for the present, but an equation is something for eternity.",
    25  		rsHex: "54c4a33c6423d689378f160a7ff8b61330444abb58fb470f96ea16d99d4a2fed07082304410efa6b2943111b6a4e0aaa7b7db55a07e9861d1fb3cb1f421044a5",
    26  	},
    27  	{
    28  		skHex: "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140",
    29  		msg:   "Not only is the Universe stranger than we think, it is stranger than we can think.",
    30  		rsHex: "ff466a9f1b7b273e2f4c3ffe032eb2e814121ed18ef84665d0f515360dab3dd06fc95f5132e5ecfdc8e5e6e616cc77151455d46ed48f5589b7db7771a332b283",
    31  	},
    32  	{
    33  		skHex: "0000000000000000000000000000000000000000000000000000000000000001",
    34  		msg:   "How wonderful that we have met with a paradox. Now we have some hope of making progress.",
    35  		rsHex: "c0dafec8251f1d5010289d210232220b03202cba34ec11fec58b3e93a85b91d375afdc06b7d6322a590955bf264e7aaa155847f614d80078a90292fe205064d3",
    36  	},
    37  	{
    38  		skHex: "69ec59eaa1f4f2e36b639716b7c30ca86d9a5375c7b38d8918bd9c0ebc80ba64",
    39  		msg:   "Computer science is no more about computers than astronomy is about telescopes.",
    40  		rsHex: "7186363571d65e084e7f02b0b77c3ec44fb1b257dee26274c38c928986fea45d0de0b38e06807e46bda1f1e293f4f6323e854c86d58abdd00c46c16441085df6",
    41  	},
    42  	{
    43  		skHex: "00000000000000000000000000007246174ab1e92e9149c6e446fe194d072637",
    44  		msg:   "...if you aren't, at any given time, scandalized by code you wrote five or even three years ago, you're not learning anywhere near enough",
    45  		rsHex: "fbfe5076a15860ba8ed00e75e9bd22e05d230f02a936b653eb55b61c99dda4870e68880ebb0050fe4312b1b1eb0899e1b82da89baa5b895f612619edf34cbd37",
    46  	},
    47  	{
    48  		skHex: "000000000000000000000000000000000000000000056916d0f9b31dc9b637f3",
    49  		msg:   "The question of whether computers can think is like the question of whether submarines can swim.",
    50  		rsHex: "cde1302d83f8dd835d89aef803c74a119f561fbaef3eb9129e45f30de86abbf906ce643f5049ee1f27890467b77a6a8e11ec4661cc38cd8badf90115fbd03cef",
    51  	},
    52  }
    53  
    54  type test struct {
    55  	skHex string
    56  	msg   string
    57  	rsHex string
    58  }
    59  
    60  func TestRFC6979Compliance(t *testing.T) {
    61  	for i, tt := range rfc6979Tests {
    62  		t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
    63  			require := require.New(t)
    64  
    65  			skBytes, err := hex.DecodeString(tt.skHex)
    66  			require.NoError(err)
    67  
    68  			sk, err := ToPrivateKey(skBytes)
    69  			require.NoError(err)
    70  
    71  			msgBytes := []byte(tt.msg)
    72  			sigBytes, err := sk.Sign(msgBytes)
    73  			require.NoError(err)
    74  
    75  			expectedRSBytes, err := hex.DecodeString(tt.rsHex)
    76  			require.NoError(err)
    77  
    78  			// sigBytes is returned in [R || S || V] format, so we drop last
    79  			// byte to get [R || S]
    80  			rsBytes := sigBytes[:len(sigBytes)-1]
    81  			require.Equal(expectedRSBytes, rsBytes)
    82  		})
    83  	}
    84  }