# Asymmetric Ed25519 Signing
Asymmetric Ed25519 signatures allow public verification of origin-generated HTML fragments without exposing private signing keys to CDNs or clients.
## 1. How Ed25519 Origin Signing Works
1. The Rust origin backend generates an Ed25519 keypair or loads a private signing key.
2. When rendering initial HTML pages, the origin publishes the public key in a `<meta name="anza-key">` tag or serves it at `/.well-known/anza.json`.
3. The origin signs dynamic fragment payloads with its private key.
4. The client browser verifies signatures using the native Web Crypto API (`crypto.subtle`).
```
┌────────────────────────┐
│ Rust Origin Server │
│ (Private Key Holder) │
└───────────┬────────────┘
│ 1. Signs Envelope: ed25519::sign(priv_key, "ts:slot:html")
▼
┌────────────────────────┐
│ Cloudflare / Edge CDN │ ◄── Intermediary proxy terminates TLS & caches
└───────────┬────────────┘
│ 2. Forwards untouched signed envelope
▼
┌────────────────────────┐
│ Client Web Browser │
│ (Public Key Verifier) │ ◄── crypto.subtle.verify("Ed25519", pub_key, sig, msg)
└────────────────────────┘
```
## 2. Rust API Usage
Located in `src/crypto/ed25519/sign.rs` and `src/crypto/ed25519/verify.rs`:
```rust
use anza::crypto::ed25519::{keypair, sign, verify};
// 1. Generate or load 32-byte keypair
let (priv_key, pub_key) = keypair()?;
// 2. Sign canonical message
let message = b"1724771200:feed:<ui-card>Live</ui-card>";
let signature_hex = sign(&priv_key, message)?;
// 3. Verify signature
let is_valid = verify(&pub_key, message, &signature_hex)?;
assert!(is_valid);
```
## 3. Client-Side Browser Verification
In standard modern browsers:
```javascript
async function verifyEnvelope(envelope, originPublicKeyBase64) {
const binaryKey = Uint8Array.from(atob(originPublicKeyBase64), c => c.charCodeAt(0));
const cryptoKey = await crypto.subtle.importKey(
"raw",
binaryKey,
{ name: "Ed25519", namedCurve: "Ed25519" },
false,
["verify"]
);
const message = new TextEncoder().encode(`${envelope.ts}:${envelope.slot}:${envelope.html}`);
const sigBytes = Uint8Array.from(envelope.sig.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
return await crypto.subtle.verify(
{ name: "Ed25519" },
cryptoKey,
sigBytes,
message
);
}
```