Of course! You've run into a common Go pattern that can be a bit tricky. The issue is with how you are initializing and using the sig slice with the SignTo function.
The Problem
The function mode3.SignTo is designed to write the signature into a pre-existing byte slice that you provide. It does not allocate memory for the slice itself.
In your code:
var sig []byte declares a nil slice. A nil slice has a length and capacity of 0.
mode3.SignTo(privk, tempbytes, sig) is then called. It tries to write the signature data into sig, but sig has no space. The function will not write anything, so sig remains empty/nil.
mode3.Verify(pubk, tempbytes, sig) then tries to verify an empty signature, which will always fail.
Solution 1: The Easy Way (Recommended)
The circl library provides a more straightforward function, Sign, which handles the memory allocation for you and returns the signature. This is the idiomatic and recommended approach unless you have very specific performance needs.
package main
import (
"fmt"
"github.com/cloudflare/circl/sign/dilithium/mode3"
"log"
)
func main() {
pubk, privk, err := mode3.GenerateKey(nil)
if err != nil {
log.Fatal("keygen error", err)
}
tempMsg := "what the sigma"
tempbytes := []byte(tempMsg)
// Use the Sign function, which allocates and returns the signature slice.
sig := mode3.Sign(privk, tempbytes)
// The signature is now a valid, non-empty slice.
fmt.Printf("Signature (length %d)\n", len(sig))
fmt.Println("Checking sig")
if mode3.Verify(pubk, tempbytes, sig) {
fmt.Println("Signature verified successfully!")
} else {
fmt.Println("Signature not verified.")
}
}
Solution 2: The Correct Way to Use SignTo
If you want to use SignTo (for example, to reuse a buffer and avoid memory allocations in a hot loop), you must first create a slice of the correct size. The dilithium/mode3 package provides a constant, mode3.SignatureSize, for this exact purpose.
package main
import (
"fmt"
"github.com/cloudflare/circl/sign/dilithium/mode3"
"log"
)
func main() {
pubk, privk, err := mode3.GenerateKey(nil)
if err != nil {
log.Fatal("keygen error", err)
}
tempMsg := "what the sigma"
tempbytes := []byte(tempMsg)
// Create a slice with the exact size needed for the signature.
sig := make([]byte, mode3.SignatureSize)
// Now, SignTo can write the signature data into the buffer you provided.
mode3.SignTo(privk, tempbytes, sig)
fmt.Printf("Signature (length %d)\n", len(sig))
fmt.Println("Checking sig")
if mode3.Verify(pubk, tempbytes, sig) {
fmt.Println("Signature verified successfully!")
} else {
fmt.Println("Signature not verified.")
}
}
Summary: Sign vs. SignTo
Sign(privateKey, message): Easier to use. It allocates a new slice of the correct size, fills it with the signature, and returns it. Use this unless you have a good reason not to.
SignTo(privateKey, message, destinationSlice): More performant. It writes the signature into a slice you provide (destinationSlice). It's your responsibility to make sure the slice is large enough. Use this if you are managing memory carefully to improve performance.