1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! 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.
//!
//! ```
//! # #[cfg(feature = "keygen")] {
//! 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
//!
//! | Feature | Default | Brings in | Gives you |
//! |---|---|---|---|
//! | — | — | `fiat-crypto`, `subtle` | [`to_representative`], [`from_representative`], [`is_montgomery_u`] |
//! | `keygen` | yes | `curve25519-dalek`, `rand_core`, `zeroize` | [`generate`], [`HiddenKey`] |
//! | `x25519` | no | `x25519-dalek` | [`HiddenKey::diffie_hellman`] |
//!
//! `no_std` throughout; no allocation anywhere.
extern crate std;
use FieldElement;
use ;
pub use ;
/// Length of a public key, a Montgomery u-coordinate, and a representative.
/// All three are 32 bytes.
pub const LEN: usize = 32;
/// Bits 254–255 of a representative, which the map never sets.
const HIGH_BITS: u8 = 0xc0;
/// Encode a Montgomery u-coordinate as a uniform 32-byte string.
///
/// Returns `None` when the point has no preimage — roughly half the time. Callers
/// generating keys should retry with a fresh key rather than perturbing this one;
/// [`generate`] does exactly that.
///
/// `tweak` supplies the two high bits of the output, and its low bit
/// additionally selects which of the map's two branches is inverted. Both choices
/// are free parameters: any value produces a valid representative of the same
/// point. Draw it from your RNG.
///
/// # Warning
///
/// This function cannot tell a torsion-dirty point from a clean one, and encoding
/// clean points produces output that is *distinguishable from random* however
/// uniform each individual string looks. See trap 2 in the crate documentation.
/// Decode a representative back to a Montgomery u-coordinate.
///
/// Total: every 32-byte string maps to some point, which is what makes a
/// representative safe to send in the clear — an observer cannot filter on
/// "decodes to nothing", because nothing does. The two high bits are masked here
/// so no caller can forget them.
/// Whether `bytes` is a valid Curve25519 u-coordinate, i.e. whether
/// `u³ + A·u² + u` is a square.
///
/// **This is the distinguisher this crate exists to defeat**, made executable. A
/// raw X25519 public key passes every time; a uniformly random 32-byte string
/// passes about half the time. Anything emitting raw public keys at a fixed
/// offset is therefore separable from random traffic by one Legendre symbol per
/// packet.
///
/// Exposed so that a protocol's test suite can *measure* its own uniformity
/// rather than assume it. A uniformity claim with no adversary implemented
/// against it is not evidence.
/// The Elligator2 forward map on the Montgomery curve, `r ↦ u`.
///
/// ```text
/// d = -A / (1 + 2r²)
/// gx = d³ + A·d² + d
/// u = d if gx is square
/// u = -d - A otherwise
/// ```
/// Whether `d³ + A·d² + d` is a square, i.e. whether `d` is itself the
/// u-coordinate of a curve point.