silk.crypto.ed25519.

Ed25519 signatures with 32-byte keys and 64-byte signatures (RFC 8032).

When to use#

Use Ed25519PrivateKey to sign messages, and Ed25519PublicKey to check that a message was signed by the holder of a known Ed25519 key, such as a release signing key.

Details#

Ed25519PrivateKey.fromSeed takes the 32-byte seed that RFC 8032 calls the private key, and Ed25519PrivateKey.generate makes a new one. Signing is deterministic: the same key and message give the same signature. It multiplies by secret scalars in constant time.

Ed25519PublicKey.fromBytes decodes and checks the public key once. verify borrows the message and the signature, requires the scalar half of the signature to be below the group order, and checks the cofactorless equation [S]B = R + [k]A with k = SHA-512(R || A || M) mod L. Arithmetic uses bounded local storage, without an allocator.

Gotchas#

Secrets are not erased. Verification works on public data and does not try to run in constant time. A valid signature proves only that the key holder signed these exact bytes. For prehashed formats, such as minisign's ED algorithm, pass the digest as the message.

Examples#

Reject a signature over a different message#

silk
import silk.crypto.ed25519 {Ed25519PublicKey}
import silk.core.result {Result}

pub fn main() -> i32 {
  // RFC 8032 section 7.1, test 1: the empty message.
  let key: [u8; 32] = [
    215, 90, 152, 1, 130, 177, 10, 183, 213, 75, 254, 211, 201, 100, 7, 58,
    14, 225, 114, 243, 218, 166, 35, 37, 175, 2, 26, 104, 247, 7, 81, 26,
  ]
  let signature: [u8; 64] = [
    229, 86, 67, 0, 195, 96, 172, 114, 144, 134, 226, 204, 128, 110, 130, 138,
    132, 135, 127, 30, 184, 229, 217, 116, 216, 115, 224, 101, 34, 73, 1, 85,
    95, 184, 130, 21, 144, 163, 59, 172, 198, 30, 57, 112, 28, 249, 180, 107,
    210, 91, 245, 240, 89, 91, 190, 36, 101, 81, 65, 67, 142, 122, 16, 11,
  ]
  let empty: [u8; 0] = []
  let other: [u8; 1] = [0]
  return match Ed25519PublicKey.fromBytes(&key) {
    Result.Failure { error } => 1
    Result.Success { value: verifier } => {
      let accepted = match verifier.verify(&empty, &signature) {
        Result.Success { value } => true
        Result.Failure { error } => false
      }
      let forged = match verifier.verify(&other, &signature) {
        Result.Success { value } => true
        Result.Failure { error } => false
      }
      if !accepted || forged {
        return 2
      }
      return 0
    }
  }
}

Sign a message and verify it#

silk
import silk.crypto.ed25519 {Ed25519Error, Ed25519PrivateKey, Ed25519PublicKey}
import silk.core.result {Result}

pub fn main() -> i32 {
  // A fixed seed for the example. Real programs use `Ed25519PrivateKey.generate`.
  let seed: [u8; 32] = [42; 32]
  let key = match Ed25519PrivateKey.fromSeed(&seed) {
    Result<Ed25519PrivateKey, Ed25519Error>.Success { value } => move value
    Result<Ed25519PrivateKey, Ed25519Error>.Failure {..} => {
      return 1
    }
  }
  let signature = key.sign(b"hello")
  return match Ed25519PublicKey.fromBytes(&key.publicKey()) {
    Result<Ed25519PublicKey, Ed25519Error>.Failure {..} => 2
    Result<Ed25519PublicKey, Ed25519Error>.Success { value: verifier } => {
      match verifier.verify(b"hello", &signature) {
        Result<(), Ed25519Error>.Success {..} => 0
        Result<(), Ed25519Error>.Failure {..} => 3
      }
    }
  }
}

Import as Ed25519PublicKey with import silk.crypto.ed25519 { Ed25519PublicKey }.

Public declarations: 3.

Ed25519Error#

silk
pub enum Ed25519Error

A rejected Ed25519 input, without the input bytes in the error.

InvalidKeyLength#

silk
InvalidKeyLength

A public key encoding whose length is not 32 bytes.

InvalidKey#

silk
InvalidKey

A public key that does not encode a point on the curve.

InvalidSignatureLength#

silk
InvalidSignatureLength

A signature whose length is not 64 bytes.

InvalidSignature#

silk
InvalidSignature

A signature that does not verify for this key and message.

Implementation Debug for Ed25519Error#

silk
impl Debug for Ed25519Error

Operation debug#

silk
effect fn debug(self: &Self, formatter: &mut Formatter) -> ()
    ! WriterError
    ? &mut Writer

Writes the name of the variant, such as InvalidKeyLength.

Ed25519PrivateKey#

silk
pub struct Ed25519PrivateKey

An Ed25519 private key that signs messages (RFC 8032).

When to use#

Make it from a 32-byte seed with Ed25519PrivateKey.fromSeed, or make a new one with Ed25519PrivateKey.generate. Then call Ed25519PrivateKey.sign.

Details#

The seed is what RFC 8032 calls the private key, and what PKCS #8 stores for Ed25519. The key keeps the seed, the clamped secret scalar, the nonce prefix, and the public key. Signing is deterministic and runs in constant time for the secret values.

Gotchas#

Secrets are not erased.

Implementation Ed25519PrivateKey#

silk
impl Ed25519PrivateKey

Associated function fromSeed#

silk
pub fn fromSeed(seed: &[u8]) -> Result<Ed25519PrivateKey, Ed25519Error>

Returns the key for exactly 32 seed bytes.

Errors#

InvalidKeyLength when seed is not 32 bytes long.

Associated function generate#

silk
pub effect fn generate() -> Ed25519PrivateKey
    ? &mut Random

Returns a new key from 32 bytes of the Random provider.

Gotchas#

Use a secure Random provider.

Method seed#

silk
pub fn seed(self: &Self) -> [u8; 32]

Returns the 32-byte seed.

Method publicKey#

silk
pub fn publicKey(self: &Self) -> [u8; 32]

Returns the 32-byte encoded public key.

Method sign#

silk
pub fn sign(self: &Self, message: &[u8]) -> [u8; 64]

Returns the 64-byte Ed25519 signature of message, R followed by S.

The signature is deterministic: r = SHA-512(prefix || M) mod L, R = [r]B, k = SHA-512(R || A || M) mod L, and S = (r + k a) mod L.

Ed25519PublicKey#

silk
pub struct Ed25519PublicKey

A decoded Ed25519 public key, for signature verification (RFC 8032).

When to use#

Use it to check that a message was signed by the holder of a known Ed25519 key, such as a release signing key. Decode the key once with Ed25519PublicKey.fromBytes, then call Ed25519PublicKey.verify. To sign, use Ed25519PrivateKey.

Gotchas#

Verification works on public data and does not try to run in constant time. A valid signature proves only that the key holder signed these exact bytes. For prehashed formats, such as minisign's ED algorithm, pass the digest as the message.

Implementation Ed25519PublicKey#

silk
impl Ed25519PublicKey

Associated function fromBytes#

silk
pub fn fromBytes(bytes: &[u8]) -> Result<Ed25519PublicKey, Ed25519Error>

Decodes exactly 32 borrowed public key bytes.

Errors#

InvalidKeyLength when bytes is not 32 bytes long, and InvalidKey when it does not encode a curve point.

Method bytes#

silk
pub fn bytes(self: &Ed25519PublicKey) -> [u8; 32]

Returns the 32-byte encoding this key was decoded from.

Method verify#

silk
pub fn verify(self: &Ed25519PublicKey, message: &[u8], signature: &[u8]) -> Result<
    (),
    Ed25519Error
  >

Checks that signature is this key's Ed25519 signature of message.

It borrows the message and the signature, requires the scalar half of the signature to be below the group order, and checks the cofactorless equation [S]B = R + [k]A with k = SHA-512(R || A || M) mod L. It does not allocate.

Gotchas#

It does not try to run in constant time. A valid signature proves only that the key holder signed these exact bytes. For prehashed formats, such as minisign's ED algorithm, pass the digest as the message.

Errors#

InvalidSignatureLength when signature is not 64 bytes long, and InvalidSignature when its scalar is not below the group order or the verification equation does not hold.

Category: Cryptography.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page