algorithm

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package algorithm provides algorithm agility for Signet's cryptographic operations. It defines algorithm constants and a registry of algorithm implementations, allowing callers to select signing algorithms by name.

Index

Constants

View Source
const DefaultAlgorithm = Ed25519

DefaultAlgorithm is the algorithm used when none is specified.

Variables

This section is empty.

Functions

func MarshalPublicKey

func MarshalPublicKey(pub crypto.PublicKey) ([]byte, error)

MarshalPublicKey serializes a public key using the appropriate algorithm. Dispatches deterministically via MatchesPublicKey.

func Register

func Register(alg Algorithm, ops AlgorithmOps)

Register adds an algorithm implementation to the registry. Called by init() functions in algorithm-specific files.

Panics if the new algorithm's key types overlap with an already-registered algorithm, since dispatch functions rely on exactly one match.

func UnmarshalPublicKey

func UnmarshalPublicKey(alg Algorithm, data []byte) (crypto.PublicKey, error)

UnmarshalPublicKey deserializes a public key using the named algorithm. The algorithm must be specified because raw bytes are ambiguous.

func Verify

func Verify(pub crypto.PublicKey, message, signature []byte) (bool, error)

Verify checks a signature using the appropriate algorithm for the given public key. Dispatches deterministically via MatchesPublicKey.

func ZeroizePrivateKey

func ZeroizePrivateKey(key crypto.PrivateKey)

ZeroizePrivateKey securely zeros private key material using the appropriate algorithm. Dispatches deterministically via MatchesPrivateKey.

Panics if no registered algorithm matches the key type. For a security-critical operation, silently ignoring an unrecognized key would mask bugs.

Types

type Algorithm

type Algorithm string

Algorithm identifies a signing algorithm.

const (
	// Ed25519 is the default signing algorithm (RFC 8032).
	Ed25519 Algorithm = "ed25519"

	// MLDSA44 is the ML-DSA-44 post-quantum signing algorithm (FIPS 204).
	// Implemented via github.com/cloudflare/circl.
	// Key sizes: public 1,312B, signature 2,420B.
	MLDSA44 Algorithm = "ml-dsa-44"
)

func (Algorithm) String

func (a Algorithm) String() string

String returns the algorithm name.

func (Algorithm) Valid

func (a Algorithm) Valid() bool

Valid returns true if the algorithm is a recognized value.

type AlgorithmOps

type AlgorithmOps interface {
	// GenerateKey creates a new key pair, returning (publicKey, privateSigner, error).
	// The returned crypto.Signer owns the private key material.
	GenerateKey() (crypto.PublicKey, crypto.Signer, error)

	// GenerateKeyFromSeed creates a deterministic key pair from a seed.
	// Seed size requirements are algorithm-specific.
	GenerateKeyFromSeed(seed []byte) (crypto.PublicKey, crypto.Signer, error)

	// SeedSize returns the expected seed size in bytes for this algorithm.
	SeedSize() int

	// Verify checks a signature against a public key and message.
	Verify(pub crypto.PublicKey, message, signature []byte) (bool, error)

	// MarshalPublicKey serializes a public key to bytes for hashing or storage.
	MarshalPublicKey(pub crypto.PublicKey) ([]byte, error)

	// UnmarshalPublicKey deserializes a public key from bytes.
	UnmarshalPublicKey(data []byte) (crypto.PublicKey, error)

	// MatchesPublicKey reports whether the given public key is of this algorithm's type.
	MatchesPublicKey(pub crypto.PublicKey) bool

	// MatchesPrivateKey reports whether the given private key is of this algorithm's type.
	MatchesPrivateKey(key crypto.PrivateKey) bool

	// ZeroizePrivateKey securely zeros private key material.
	ZeroizePrivateKey(key crypto.PrivateKey)
}

AlgorithmOps defines the operations for a signing algorithm. Each algorithm registers an implementation of this interface.

func Get

func Get(alg Algorithm) (AlgorithmOps, error)

Get returns the AlgorithmOps for the given algorithm.

func MustGet

func MustGet(alg Algorithm) AlgorithmOps

MustGet returns the AlgorithmOps for the given algorithm, panicking if not found. Use only in init() or test setup.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL