Code Examples
package main
import (
"crypto/mldsa"
"fmt"
"log"
)
func main() {
// The signer generates a new ML-DSA-44 key pair.
sk, err := mldsa.GenerateKey(mldsa.MLDSA44())
if err != nil {
log.Fatal(err)
}
// The signer publishes the public key encoding.
publicKey := sk.PublicKey().Bytes()
fmt.Printf("public key: %d bytes\n", len(publicKey))
// The signer signs a message and publishes the signature.
msg := []byte("hello, world")
sig, err := sk.Sign(nil, msg, &mldsa.Options{Context: "example"})
if err != nil {
log.Fatal(err)
}
fmt.Printf("signature: %d bytes\n", len(sig))
// The verifier reconstructs the public key and checks the signature.
// The context string must match the one used by the signer.
pk, err := mldsa.NewPublicKey(mldsa.MLDSA44(), publicKey)
if err != nil {
log.Fatal(err)
}
if err := mldsa.Verify(pk, msg, sig, &mldsa.Options{Context: "example"}); err != nil {
log.Fatal("invalid signature: ", err)
}
}
Package-Level Type Names (total 4)
/* sort by: | */
Options contains additional options for signing and verifying ML-DSA signatures.
Context can be used to distinguish signatures created for different
purposes. It must be at most 255 bytes long, and it is empty by default.
The same context must be used when signing and verifying a signature. HashFunc returns zero, to implement the [crypto.SignerOpts] interface.
*Options : crypto.SignerOpts
func Verify(pk *PublicKey, message []byte, signature []byte, opts *Options) error
Parameters represents one of the fixed parameter sets defined in FIPS 204.
Most applications should use [MLDSA44].
Multiple invocations of [MLDSA44], [MLDSA65], or [MLDSA87] will return the
same respective value, which can be used for equality checks and switch
statements. The returned value is safe for concurrent use. PublicKeySize returns the size of public keys for this parameter set, in bytes. SignatureSize returns the size of signatures for this parameter set, in bytes. String returns the name of the parameter set, e.g. "ML-DSA-44".
Parameters : expvar.Var
Parameters : fmt.Stringer
func MLDSA44() Parameters
func MLDSA65() Parameters
func MLDSA87() Parameters
func (*PublicKey).Parameters() Parameters
func GenerateKey(params Parameters) (*PrivateKey, error)
func NewPrivateKey(params Parameters, seed []byte) (*PrivateKey, error)
func NewPublicKey(params Parameters, encoding []byte) (*PublicKey, error)
PrivateKey is an in-memory ML-DSA private key. It implements [crypto.Signer]
and the informal extended [crypto.PrivateKey] interface.
A PrivateKey is safe for concurrent use. Bytes returns the private key seed. Equal reports whether sk and x are the same key (i.e. they are derived from
the same seed).
If x is not a *PrivateKey, Equal returns false. Public returns the corresponding [PublicKey] for this private key.
It implements the [crypto.Signer] interface. PublicKey returns the corresponding [PublicKey] for this private key. Sign returns a signature of the given message using this private key.
If opts is nil or opts.HashFunc returns zero, the message is signed directly.
If opts.HashFunc returns [crypto.MLDSAMu], the provided message must be a
[pre-hashed μ message representative]. opts can be of type *[Options] if a
context string is desired along with a directly-signed message. The io.Reader
argument is ignored. SignDeterministic works like [PrivateKey.Sign], but the signature is
deterministic.
*PrivateKey : crypto.Signer
*PrivateKey : github.com/apache/arrow-go/v18/internal/hashing.ByteSlice
func GenerateKey(params Parameters) (*PrivateKey, error)
func NewPrivateKey(params Parameters, seed []byte) (*PrivateKey, error)
PublicKey is an ML-DSA public key. It implements the informal extended
[crypto.PublicKey] interface.
A PublicKey is safe for concurrent use. Bytes returns the public key encoding. Equal reports whether pk and x are the same key (i.e. they have the same
encoding).
If x is not a *PublicKey, Equal returns false. Parameters returns the parameters associated with this public key.
*PublicKey : github.com/apache/arrow-go/v18/internal/hashing.ByteSlice
func NewPublicKey(params Parameters, encoding []byte) (*PublicKey, error)
func (*PrivateKey).PublicKey() *PublicKey
func Verify(pk *PublicKey, message []byte, signature []byte, opts *Options) error
Package-Level Functions (total 7)
GenerateKey generates a new random ML-DSA private key.
MLDSA44 returns the ML-DSA-44 parameter set defined in FIPS 204.
MLDSA65 returns the ML-DSA-65 parameter set defined in FIPS 204.
MLDSA87 returns the ML-DSA-87 parameter set defined in FIPS 204.
NewPrivateKey decodes an ML-DSA private key from the given seed.
The seed must be exactly [PrivateKeySize] bytes long.
NewPublicKey creates a new ML-DSA public key from the given encoding.
Verify reports whether signature is a valid signature of message by pk.
If opts is nil, it's equivalent to the zero value of Options.
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.