Skip to content

Instantly share code, notes, and snippets.

@ChristianOConnor
Created October 24, 2023 02:48
Show Gist options
  • Save ChristianOConnor/b9a6e1771420df87f2f9f9a2b76df4ac to your computer and use it in GitHub Desktop.
Save ChristianOConnor/b9a6e1771420df87f2f9f9a2b76df4ac to your computer and use it in GitHub Desktop.
package main
import (
"encoding/hex"
"fmt"
"sort"
"github.com/cloudflare/circl/sign/dilithium"
)
func main() {
// Check supported modes
modes := dilithium.ModeNames()
sort.Strings(modes)
fmt.Printf("Supported modes: %v\n", modes)
// Pick Dilithium mode III.
mode := dilithium.ModeByName("Dilithium5")
if mode == nil {
panic("Mode5 not supported")
}
// Alternatively one could simply write
//
// mode := dilithium.Mode3
// Generates a keypair.
pk, sk, err := mode.GenerateKey(nil)
if err != nil {
panic(err)
}
// (Alternatively one can derive a keypair from a seed,
// see mode.NewKeyFromSeed().)
// Packs public and private key
packedSk := sk.Bytes()
packedPk := pk.Bytes()
// Load it again
sk2 := mode.PrivateKeyFromBytes(packedSk)
pk2 := mode.PublicKeyFromBytes(packedPk)
fmt.Printf(hex.EncodeToString(sk2.Bytes()))
fmt.Printf("\n")
fmt.Printf("\n")
fmt.Printf(hex.EncodeToString(pk2.Bytes()))
// Creates a signature on our message with the generated private key.
msg := []byte("Some message")
signature := mode.Sign(sk2, msg)
// Checks whether a signature is correct
if !mode.Verify(pk2, msg, signature) {
panic("incorrect signature")
}
fmt.Printf("O.K.")
// Output:
// Supported modes: [Dilithium2 Dilithium2-AES Dilithium3 Dilithium3-AES Dilithium5 Dilithium5-AES]
// O.K.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment