github.com/cloudflare/circl@v1.5.0/sign/eddilithium2/eddilithium_test.go (about)

     1  package eddilithium2_test
     2  
     3  import (
     4  	"encoding/binary"
     5  	"testing"
     6  
     7  	"github.com/cloudflare/circl/sign/eddilithium2"
     8  )
     9  
    10  func BenchmarkVerify(b *testing.B) {
    11  	// Note that Dilithium precomputes quite a bit during Unpacking/Keygen
    12  	// instead of at the moment of verification (as compared to the reference
    13  	// implementation.  A fair comparison thus should sum verification
    14  	// times with unpacking times.)
    15  	var seed [32]byte
    16  	var msg [8]byte
    17  	var sig [eddilithium2.SignatureSize]byte
    18  	pk, sk := eddilithium2.NewKeyFromSeed(&seed)
    19  	eddilithium2.SignTo(sk, msg[:], sig[:])
    20  	b.ResetTimer()
    21  	for i := 0; i < b.N; i++ {
    22  		// We should generate a new signature for every verify attempt,
    23  		// as this influences the time a little bit.  This difference, however,
    24  		// is small and generating a new signature in between creates a lot
    25  		// pressure on the allocator which makes an accurate measurement hard.
    26  		eddilithium2.Verify(pk, msg[:], sig[:])
    27  	}
    28  }
    29  
    30  func BenchmarkSign(b *testing.B) {
    31  	// Note that Dilithium precomputes quite a bit during Unpacking/Keygen
    32  	// instead of at the moment of signing (as compared to the reference
    33  	// implementation.  A fair comparison thus should sum sign times with
    34  	// unpacking times.)
    35  	var seed [32]byte
    36  	var msg [8]byte
    37  	var sig [eddilithium2.SignatureSize]byte
    38  	_, sk := eddilithium2.NewKeyFromSeed(&seed)
    39  	b.ResetTimer()
    40  	for i := 0; i < b.N; i++ {
    41  		binary.LittleEndian.PutUint64(msg[:], uint64(i))
    42  		eddilithium2.SignTo(sk, msg[:], sig[:])
    43  	}
    44  }
    45  
    46  func BenchmarkGenerateKey(b *testing.B) {
    47  	var seed [32]byte
    48  	for i := 0; i < b.N; i++ {
    49  		binary.LittleEndian.PutUint64(seed[:], uint64(i))
    50  		eddilithium2.NewKeyFromSeed(&seed)
    51  	}
    52  }
    53  
    54  func BenchmarkPublicFromPrivate(b *testing.B) {
    55  	var seed [32]byte
    56  	for i := 0; i < b.N; i++ {
    57  		b.StopTimer()
    58  		binary.LittleEndian.PutUint64(seed[:], uint64(i))
    59  		_, sk := eddilithium2.NewKeyFromSeed(&seed)
    60  		b.StartTimer()
    61  		sk.Public()
    62  	}
    63  }
    64  
    65  func TestSignThenVerifyAndPkSkPacking(t *testing.T) {
    66  	var seed [eddilithium2.SeedSize]byte
    67  	var sig [eddilithium2.SignatureSize]byte
    68  	var msg [8]byte
    69  	var pkb [eddilithium2.PublicKeySize]byte
    70  	var skb [eddilithium2.PrivateKeySize]byte
    71  	var pk2 eddilithium2.PublicKey
    72  	var sk2 eddilithium2.PrivateKey
    73  	for i := uint64(0); i < 100; i++ {
    74  		binary.LittleEndian.PutUint64(seed[:], i)
    75  		pk, sk := eddilithium2.NewKeyFromSeed(&seed)
    76  		for j := uint64(0); j < 10; j++ {
    77  			binary.LittleEndian.PutUint64(msg[:], j)
    78  			eddilithium2.SignTo(sk, msg[:], sig[:])
    79  			if !eddilithium2.Verify(pk, msg[:], sig[:]) {
    80  				t.Fatal()
    81  			}
    82  		}
    83  		pk.Pack(&pkb)
    84  		pk2.Unpack(&pkb)
    85  		if !pk.Equal(&pk2) {
    86  			t.Fatal()
    87  		}
    88  		sk.Pack(&skb)
    89  		sk2.Unpack(&skb)
    90  		if !sk.Equal(&sk2) {
    91  			t.Fatal()
    92  		}
    93  	}
    94  }
    95  
    96  func TestPublicFromPrivate(t *testing.T) {
    97  	var seed [eddilithium2.SeedSize]byte
    98  	for i := uint64(0); i < 100; i++ {
    99  		binary.LittleEndian.PutUint64(seed[:], i)
   100  		pk, sk := eddilithium2.NewKeyFromSeed(&seed)
   101  		pk2 := sk.Public().(*eddilithium2.PublicKey)
   102  		var pkb1, pkb2 [eddilithium2.PublicKeySize]byte
   103  		pk.Pack(&pkb1)
   104  		pk2.Pack(&pkb2)
   105  		if pkb1 != pkb2 {
   106  			t.Fatal()
   107  		}
   108  	}
   109  }