Skip to main content

Crate elligator2

Crate elligator2 

Source
Expand description

Elligator2 for Curve25519: encode X25519 public keys as uniform bit strings.

A raw X25519 public key is not a random string. It is the u-coordinate of a curve point, and u³ + A·u² + u is a square for every one of them — a property a censor can test with a single Legendre symbol. Any protocol that puts a public key at a fixed offset in its first packet is separable from random traffic on that basis alone. Elligator2 removes the tell: it maps about half of all curve points to a uniformly distributed 32-byte string, and maps every 32-byte string back to a point.

use elligator2::{from_representative, generate};

let key = generate(&mut rand::rng()).expect("a working RNG");

// Send this. It is indistinguishable from 32 random bytes.
let on_the_wire: &[u8; 32] = key.representative();

// The peer recovers the public point, and uses *that* for Diffie-Hellman
// and for anything the protocol hashes.
assert_eq!(from_representative(on_the_wire), *key.point());

§The rule

The representative goes on the wire. The point goes into the protocol.

This crate converts between the two and nothing else. It performs no handshake, defines no wire format, and takes no opinion about what you hash. Slotting it into an existing protocol should leave that protocol’s transcript byte-identical, which is what keeps interoperability testing meaningful.

§Three traps, all of which produce working-but-broken code

  1. Rejection sampling. Only about half of all curve points have an Elligator2 preimage, so key generation is a loop. Straightforward, and the only one of the three that fails loudly if you get it wrong.

  2. Dirty keys. This is the one that silently produces a “uniform” encoding that is trivially distinguishable. A clamped X25519 public key always lies in the prime-order subgroup. If you only ever encode such points, an adversary decodes an observed string with the public map and tests whether the result has prime order: true ~1/8 of the time for genuinely random strings, and always for yours.

    The fix is to publish a point drawn from the whole group: E_pub = clamp(e)·B + T, with T a uniformly chosen point of order dividing 8. The Diffie-Hellman is unaffected, and the reason is the whole trick: every clamped X25519 scalar is a multiple of 8, and 8·T = O, so the peer’s scalar annihilates the torsion component.

    generate does this for you. If you build your own points, do it yourself — to_representative cannot tell, and will happily encode a clean point into a fingerprint.

    Static, long-term keys are a different matter: they are not usually transmitted in the clear, so they have no fingerprint to fix, and dirtying one changes what the identity is.

  3. High-bit randomisation. A representative is a field element below (p−1)/2, so bits 254 and 255 of the encoding are always zero. Two always-zero bits at a fixed offset is a fingerprint in its own right, and free to remove: fill them with random bits on send, mask them off on receive. generate fills them; from_representative masks them, so no caller can forget.

§What this crate does not hide

Packet lengths, timing, volume, and endpoint addresses. Elligator2 removes one specific, decisive tell. A protocol whose packets are a constant 192 bytes and arrive in a fixed pattern is still trivially fingerprinted, uniform keys or not.

§Verification

The map is differentially tested against curve25519-elligator2, the Tor Project’s fork of curve25519-dalek used in the Rust obfs4/lyrebird stack, in four tiers plus a test for the dirty-key property above. That crate is a dev-dependency only. See tests/differential.rs, and the README for what each tier pins.

§Constant time

The field arithmetic comes from fiat-crypto, machine-generated from a formal specification with proofs of functional correctness and constant-time behaviour. The composition on top is branch-free with respect to field values.

One deliberate exception: to_representative returns None for non-representable points, and generate loops until it gets a Some. That branch is on the discarded candidates. The accepted key is uniform over the representable set, so the retry count leaks nothing about the key finally used.

§Features

FeatureDefaultBrings inGives you
——fiat-crypto, subtleto_representative, from_representative, is_montgomery_u
keygenyescurve25519-dalek, rand_core, zeroizegenerate, HiddenKey
x25519nox25519-dalekHiddenKey::diffie_hellman

no_std throughout; no allocation anywhere.

Structs§

HiddenKey
A keypair whose public key can be sent as a uniform bit string.

Constants§

LEN
Length of a public key, a Montgomery u-coordinate, and a representative. All three are 32 bytes.
MAX_ATTEMPTS
Cap on generate-and-retry attempts.

Functions§

from_representative
Decode a representative back to a Montgomery u-coordinate.
generate
Generate a keypair whose public point is torsion-dirty and Elligator2-representable.
is_montgomery_u
Whether bytes is a valid Curve25519 u-coordinate, i.e. whether u³ + A·u² + u is a square.
to_representative
Encode a Montgomery u-coordinate as a uniform 32-byte string.