github.com/cloudflare/circl@v1.5.0/sign/dilithium/dilithium_test.go (about)

     1  package dilithium
     2  
     3  import (
     4  	"encoding/hex"
     5  	"testing"
     6  
     7  	"github.com/cloudflare/circl/sign/schemes"
     8  
     9  	"github.com/cloudflare/circl/internal/sha3"
    10  )
    11  
    12  func hexHash(in []byte) string {
    13  	var ret [16]byte
    14  	h := sha3.NewShake256()
    15  	_, _ = h.Write(in[:])
    16  	_, _ = h.Read(ret[:])
    17  	return hex.EncodeToString(ret[:])
    18  }
    19  
    20  func TestNewKeyFromSeed(t *testing.T) {
    21  	// Test vectors generated from reference implementation
    22  	for _, tc := range []struct {
    23  		name string
    24  		esk  string
    25  		epk  string
    26  	}{
    27  		{
    28  			"Dilithium2", "afe2e91f5f5899354230744c18410498",
    29  			"7522162619f3329b5312322d3ee45b87",
    30  		},
    31  		{
    32  			"Dilithium3", "8ad3142e08b718b33f7c2668cd9d053c",
    33  			"3562fc184dce1a10aad099051705b5d3",
    34  		},
    35  		{
    36  			"Dilithium5", "3956d812a7961af6e5dad16af15c736c",
    37  			"665388291aa01e12e7f94bdc7769db18",
    38  		},
    39  	} {
    40  		t.Run(tc.name, func(t *testing.T) {
    41  			mode := schemes.ByName(tc.name)
    42  			if mode == nil {
    43  				t.Fatal()
    44  			}
    45  			var seed [32]byte
    46  			pk, sk := mode.DeriveKey(seed[:])
    47  
    48  			ppk, err := pk.MarshalBinary()
    49  			if err != nil {
    50  				t.Fatal(err)
    51  			}
    52  			psk, err := sk.MarshalBinary()
    53  			if err != nil {
    54  				t.Fatal(err)
    55  			}
    56  
    57  			pkh := hexHash(ppk)
    58  			skh := hexHash(psk)
    59  			if pkh != tc.epk {
    60  				t.Fatalf("%s expected pk %s, got %s", tc.name, tc.epk, pkh)
    61  			}
    62  			if skh != tc.esk {
    63  				t.Fatalf("%s expected pk %s, got %s", tc.name, tc.esk, skh)
    64  			}
    65  		})
    66  	}
    67  }