...

Source file src/github.com/cloudflare/circl/sign/dilithium/example_test.go

Documentation: github.com/cloudflare/circl/sign/dilithium

     1  package dilithium_test
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  
     7  	"github.com/cloudflare/circl/sign/dilithium"
     8  )
     9  
    10  func Example() {
    11  	// Check supported modes
    12  	modes := dilithium.ModeNames()
    13  	sort.Strings(modes)
    14  	fmt.Printf("Supported modes: %v\n", modes)
    15  
    16  	// Pick Dilithium mode III.
    17  	mode := dilithium.ModeByName("Dilithium3")
    18  	if mode == nil {
    19  		panic("Mode3 not supported")
    20  	}
    21  
    22  	// Alternatively one could simply write
    23  	//
    24  	//  mode := dilithium.Mode3
    25  
    26  	// Generates a keypair.
    27  	pk, sk, err := mode.GenerateKey(nil)
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  	// (Alternatively one can derive a keypair from a seed,
    32  	// see mode.NewKeyFromSeed().)
    33  
    34  	// Packs public and private key
    35  	packedSk := sk.Bytes()
    36  	packedPk := pk.Bytes()
    37  
    38  	// Load it again
    39  	sk2 := mode.PrivateKeyFromBytes(packedSk)
    40  	pk2 := mode.PublicKeyFromBytes(packedPk)
    41  
    42  	// Creates a signature on our message with the generated private key.
    43  	msg := []byte("Some message")
    44  	signature := mode.Sign(sk2, msg)
    45  
    46  	// Checks whether a signature is correct
    47  	if !mode.Verify(pk2, msg, signature) {
    48  		panic("incorrect signature")
    49  	}
    50  
    51  	fmt.Printf("O.K.")
    52  
    53  	// Output:
    54  	// Supported modes: [Dilithium2 Dilithium2-AES Dilithium3 Dilithium3-AES Dilithium5 Dilithium5-AES]
    55  	// O.K.
    56  }
    57  

View as plain text