github.com/klaytn/klaytn@v1.12.1/crypto/bls/blst/secret_key_test.go (about)

     1  // Copyright 2023 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package blst
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/klaytn/klaytn/common"
    23  	"github.com/klaytn/klaytn/crypto/bls/types"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  var (
    28  	// https://github.com/ethereum/bls12-381-tests
    29  	// sign/sign_case_142f678a8d05fcd1.json
    30  	testSecretKeyBytes = common.FromHex("0x47b8192d77bf871b62e87859d653922725724a5c031afeabc60bcef5ff665138")
    31  
    32  	// Standard test mnemonic test..junk
    33  	testEcPrivateKeyBytes = common.FromHex("0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
    34  )
    35  
    36  func TestGenerateKey(t *testing.T) {
    37  	sk1, err := GenerateKey(testEcPrivateKeyBytes)
    38  	assert.Nil(t, err)
    39  
    40  	sk2, err := GenerateKey(testEcPrivateKeyBytes)
    41  	assert.Nil(t, err)
    42  
    43  	// GenerateKey is deterministic
    44  	assert.Equal(t, sk1.Marshal(), sk2.Marshal())
    45  }
    46  
    47  func TestRandKey(t *testing.T) {
    48  	sk, err := RandKey()
    49  	assert.Nil(t, err)
    50  	assert.Equal(t, types.SecretKeyLength, len(sk.Marshal()))
    51  }
    52  
    53  func TestSecretKeyFromBytes(t *testing.T) {
    54  	b := testSecretKeyBytes
    55  	sk, err := SecretKeyFromBytes(b)
    56  	assert.Nil(t, err)
    57  	assert.Equal(t, b, sk.Marshal())
    58  
    59  	_, err = SecretKeyFromBytes([]byte{1, 2, 3, 4})
    60  	assert.NotNil(t, err)
    61  
    62  	// Valid secret key must be 1 <= SK < r
    63  	// as per draft-irtf-cfrg-bls-signature-05#2.3. KeyGen
    64  	zero := make([]byte, types.SecretKeyLength)
    65  	_, err = SecretKeyFromBytes(zero)
    66  	assert.Equal(t, types.ErrSecretKeyUnmarshal, err)
    67  
    68  	order := common.FromHex(types.CurveOrderHex)
    69  	_, err = SecretKeyFromBytes(order)
    70  	assert.Equal(t, types.ErrSecretKeyUnmarshal, err)
    71  }
    72  
    73  func TestSecretKeyPublicKey(t *testing.T) {
    74  	var (
    75  		// https://github.com/ethereum/bls12-381-tests
    76  		// sign/sign_case_84d45c9c7cca6b92.json
    77  		// verify/verify_valid_case_195246ee3bd3b6ec.json
    78  		skb = common.FromHex("0x328388aff0d4a5b7dc9205abd374e7e98f3cd9f3418edb4eafda5fb16473d216")
    79  		pkb = common.FromHex("0xb53d21a4cfd562c469cc81514d4ce5a6b577d8403d32a394dc265dd190b47fa9f829fdd7963afdf972e5e77854051f6f")
    80  	)
    81  
    82  	sk, err := SecretKeyFromBytes(skb)
    83  	assert.Nil(t, err)
    84  
    85  	pk := sk.PublicKey()
    86  	assert.Equal(t, pkb, pk.Marshal())
    87  }