github.com/cloudflare/circl@v1.5.0/sign/schemes/schemes_test.go (about)

     1  package schemes_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/cloudflare/circl/sign"
     8  	"github.com/cloudflare/circl/sign/schemes"
     9  )
    10  
    11  func TestCaseSensitivity(t *testing.T) {
    12  	if schemes.ByName("ed25519") != schemes.ByName("Ed25519") {
    13  		t.Fatal()
    14  	}
    15  }
    16  
    17  func TestApi(t *testing.T) {
    18  	allSchemes := schemes.All()
    19  	for _, scheme := range allSchemes {
    20  		t.Run(scheme.Name(), func(t *testing.T) {
    21  			if scheme == nil {
    22  				t.Fatal()
    23  			}
    24  
    25  			pk, sk, err := scheme.GenerateKey()
    26  			if err != nil {
    27  				t.Fatal()
    28  			}
    29  
    30  			packedPk, err := pk.MarshalBinary()
    31  			if err != nil {
    32  				t.Fatal()
    33  			}
    34  
    35  			if len(packedPk) != scheme.PublicKeySize() {
    36  				t.Fatal()
    37  			}
    38  
    39  			packedSk, err := sk.MarshalBinary()
    40  			if err != nil {
    41  				t.Fatal(err)
    42  			}
    43  
    44  			if len(packedSk) != scheme.PrivateKeySize() {
    45  				t.Fatal()
    46  			}
    47  
    48  			pk2, err := scheme.UnmarshalBinaryPublicKey(packedPk)
    49  			if err != nil {
    50  				t.Fatal(err)
    51  			}
    52  
    53  			sk2, err := scheme.UnmarshalBinaryPrivateKey(packedSk)
    54  			if err != nil {
    55  				t.Fatal(err)
    56  			}
    57  
    58  			if !sk.Equal(sk2) {
    59  				t.Fatal()
    60  			}
    61  
    62  			if !pk.Equal(pk2) {
    63  				t.Fatal()
    64  			}
    65  
    66  			msg := []byte(fmt.Sprintf("Signing with %s", scheme.Name()))
    67  			opts := &sign.SignatureOpts{}
    68  			if scheme.SupportsContext() {
    69  				opts.Context = "A context"
    70  			}
    71  			sig := scheme.Sign(sk, msg, opts)
    72  
    73  			if scheme.SignatureSize() != len(sig) {
    74  				t.Fatal()
    75  			}
    76  
    77  			if !scheme.Verify(pk2, msg, sig, opts) {
    78  				t.Fatal()
    79  			}
    80  
    81  			if scheme.SupportsContext() {
    82  				opts2 := opts
    83  				opts2.Context = "Wrong context"
    84  				if scheme.Verify(pk2, msg, sig, opts2) {
    85  					t.Fatal()
    86  				}
    87  			}
    88  
    89  			sig[0]++
    90  			if scheme.Verify(pk2, msg, sig, opts) {
    91  				t.Fatal()
    92  			}
    93  
    94  			scheme2 := schemes.ByName(scheme.Name())
    95  			if scheme2 == nil || scheme2 != scheme {
    96  				t.Fatal()
    97  			}
    98  
    99  			if pk.Scheme() != scheme {
   100  				t.Fatal()
   101  			}
   102  
   103  			if sk.Scheme() != scheme {
   104  				t.Fatal()
   105  			}
   106  		})
   107  	}
   108  }
   109  
   110  func Example() {
   111  	for _, sch := range schemes.All() {
   112  		fmt.Println(sch.Name())
   113  	}
   114  	// Output:
   115  	// Ed25519
   116  	// Ed448
   117  	// Ed25519-Dilithium2
   118  	// Ed448-Dilithium3
   119  	// Dilithium2
   120  	// Dilithium3
   121  	// Dilithium5
   122  	// ML-DSA-44
   123  	// ML-DSA-65
   124  	// ML-DSA-87
   125  }
   126  
   127  func BenchmarkGenerateKeyPair(b *testing.B) {
   128  	allSchemes := schemes.All()
   129  	for _, scheme := range allSchemes {
   130  		b.Run(scheme.Name(), func(b *testing.B) {
   131  			for i := 0; i < b.N; i++ {
   132  				_, _, _ = scheme.GenerateKey()
   133  			}
   134  		})
   135  	}
   136  }
   137  
   138  func BenchmarkSign(b *testing.B) {
   139  	allSchemes := schemes.All()
   140  	opts := &sign.SignatureOpts{}
   141  	for _, scheme := range allSchemes {
   142  		msg := []byte(fmt.Sprintf("Signing with %s", scheme.Name()))
   143  		_, sk, _ := scheme.GenerateKey()
   144  		b.Run(scheme.Name(), func(b *testing.B) {
   145  			for i := 0; i < b.N; i++ {
   146  				_ = scheme.Sign(sk, msg, opts)
   147  			}
   148  		})
   149  	}
   150  }
   151  
   152  func BenchmarkVerify(b *testing.B) {
   153  	allSchemes := schemes.All()
   154  	opts := &sign.SignatureOpts{}
   155  	for _, scheme := range allSchemes {
   156  		msg := []byte(fmt.Sprintf("Signing with %s", scheme.Name()))
   157  		pk, sk, _ := scheme.GenerateKey()
   158  		sig := scheme.Sign(sk, msg, opts)
   159  		b.Run(scheme.Name(), func(b *testing.B) {
   160  			for i := 0; i < b.N; i++ {
   161  				_ = scheme.Verify(pk, msg, sig, opts)
   162  			}
   163  		})
   164  	}
   165  }