kryptos/eddsa

Edwards-curve Digital Signature Algorithm (EdDSA).

EdDSA provides digital signatures using Edwards curves Ed25519 and Ed448. Unlike ECDSA, EdDSA has built-in hashing (SHA-512 for Ed25519, SHAKE256 for Ed448) and produces deterministic signatures.

Example

import kryptos/eddsa

let #(private_key, public_key) = eddsa.generate_key_pair(eddsa.Ed25519)
let message = <<"hello world":utf8>>
let signature = eddsa.sign(private_key, message)
let valid = eddsa.verify(public_key, message, signature)
// valid == True

Types

Supported curves for EdDSA signatures.

pub type Curve {
  Ed25519
  Ed448
}

Constructors

  • Ed25519

    Ed25519 curve. 32-byte keys, 64-byte signatures.

  • Ed448

    Ed448 curve. 57-byte keys, 114-byte signatures.

An EdDSA private key.

pub type PrivateKey

An EdDSA public key.

pub type PublicKey

Values

pub fn curve(key: PrivateKey) -> Curve

Returns the curve for an EdDSA private key.

Parameters

  • key: The private key

Returns

The curve used by this key.

pub fn from_bytes(
  curve: Curve,
  private_bytes: BitArray,
) -> Result(#(PrivateKey, PublicKey), Nil)

Imports a private key from raw bytes.

The bytes should be the raw private key seed:

  • Ed25519: 32 bytes
  • Ed448: 57 bytes

Returns the private key and its corresponding public key, or Error(Nil) if the bytes are invalid.

pub fn from_der(
  der: BitArray,
) -> Result(#(PrivateKey, PublicKey), Nil)

Imports an EdDSA private key from DER-encoded data.

The key must be in PKCS#8 format.

Parameters

  • der: DER-encoded key data

Returns

Ok(#(private_key, public_key)) on success, Error(Nil) on failure.

pub fn from_pem(
  pem: String,
) -> Result(#(PrivateKey, PublicKey), Nil)

Imports an EdDSA private key from PEM-encoded data.

The key must be in PKCS#8 format.

Parameters

  • pem: PEM-encoded key string

Returns

Ok(#(private_key, public_key)) on success, Error(Nil) on failure.

pub fn generate_key_pair(
  curve: Curve,
) -> #(PrivateKey, PublicKey)

Generates a new EdDSA key pair.

Parameters

  • curve: The curve to use for key generation (Ed25519 or Ed448)

Returns

A tuple of #(private_key, public_key).

pub fn key_size(curve: Curve) -> Int

Returns the key size in bytes for the given curve.

  • Ed25519: 32 bytes
  • Ed448: 57 bytes
pub fn public_key_curve(key: PublicKey) -> Curve

Returns the curve for an EdDSA public key.

Parameters

  • key: The public key

Returns

The curve used by this key.

pub fn public_key_from_bytes(
  curve: Curve,
  public_bytes: BitArray,
) -> Result(PublicKey, Nil)

Imports a public key from raw bytes.

The bytes should be the raw public key point:

  • Ed25519: 32 bytes
  • Ed448: 57 bytes

Returns the public key or Error(Nil) if the bytes are invalid.

pub fn public_key_from_der(
  der: BitArray,
) -> Result(PublicKey, Nil)

Imports an EdDSA public key from DER-encoded data.

The key must be in SPKI format.

Parameters

  • der: DER-encoded key data

Returns

Ok(public_key) on success, Error(Nil) on failure.

pub fn public_key_from_pem(pem: String) -> Result(PublicKey, Nil)

Imports an EdDSA public key from PEM-encoded data.

The key must be in SPKI format.

Parameters

  • pem: PEM-encoded key string

Returns

Ok(public_key) on success, Error(Nil) on failure.

pub fn public_key_from_private_key(key: PrivateKey) -> PublicKey

Derives the public key from an EdDSA private key.

Parameters

  • key: The private key

Returns

The corresponding public key.

pub fn public_key_to_bytes(key: PublicKey) -> BitArray

Exports a public key to raw bytes.

Returns the raw public key point:

  • Ed25519: 32 bytes
  • Ed448: 57 bytes
pub fn public_key_to_der(key: PublicKey) -> Result(BitArray, Nil)

Exports an EdDSA public key to DER format.

The key is exported in SPKI format.

Parameters

  • key: The public key to export

Returns

Ok(der_data) on success, Error(Nil) on failure.

pub fn public_key_to_pem(key: PublicKey) -> Result(String, Nil)

Exports an EdDSA public key to PEM format.

The key is exported in SPKI format.

Parameters

  • key: The public key to export

Returns

Ok(pem_string) on success, Error(Nil) on failure.

pub fn sign(
  private_key: PrivateKey,
  message: BitArray,
) -> BitArray

Signs a message using EdDSA.

The message is hashed internally using the curve’s built-in hash function (SHA-512 for Ed25519, SHAKE256 for Ed448). Signatures are deterministic: signing the same message with the same key always produces the same signature.

Parameters

  • private_key: An EdDSA private key from generate_key_pair
  • message: The message to sign (any length)

Returns

A signature (64 bytes for Ed25519, 114 bytes for Ed448).

pub fn to_bytes(key: PrivateKey) -> BitArray

Exports a private key to raw bytes.

Returns the raw private key seed:

  • Ed25519: 32 bytes
  • Ed448: 57 bytes
pub fn to_der(key: PrivateKey) -> Result(BitArray, Nil)

Exports an EdDSA private key to DER format.

The key is exported in PKCS#8 format.

Parameters

  • key: The private key to export

Returns

Ok(der_data) on success, Error(Nil) on failure.

pub fn to_pem(key: PrivateKey) -> Result(String, Nil)

Exports an EdDSA private key to PEM format.

The key is exported in PKCS#8 format.

Parameters

  • key: The private key to export

Returns

Ok(pem_string) on success, Error(Nil) on failure.

pub fn verify(
  public_key: PublicKey,
  message: BitArray,
  signature signature: BitArray,
) -> Bool

Verifies an EdDSA signature against a message.

Parameters

  • public_key: The EdDSA public key corresponding to the signing key
  • message: The original message that was signed
  • signature: The signature to verify

Returns

True if the signature is valid, False otherwise.

Search Document