interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / ed25519 / README.md

253 lines · 15438 bytes Raw Blame History Rendered
1# om/ed25519: Ed25519 signature verification in Omega
2
3`ed25519.om` verifies Ed25519 signatures (RFC 8032 section 5.1.7). It does not sign and holds no
4private keys. It has two rule sets because Tailscale Tailnet Lock (TKA) uses both:
5
6| mode | name | behaves as |
7|---|---|---|
8| 0 | strict | Go `crypto/ed25519.Verify` (go1.26.0) |
9| 1 | consensus | ZIP 215, as `github.com/hdevalence/ed25519consensus.Verify` v0.2.0 |
10
11It is a library unit: functions only, no `fn main`, no globals. Every function name starts with
12`ed`. SHA-512 comes from the sibling unit `om/sha512/sha512.om` (`s512pre`), which must be
13concatenated before this file:
14
15```sh
16cat om/sha512/sha512.om om/ed25519/ed25519.om <your program> | "$OMC" > prog
17```
18
19## API
20
21```
22fn edverify pk view uchar 32 m view uchar 65536 n ulong sg view uchar 64 mode ulong to ulong
23```
24
25- `pk`: the 32-byte public key A.
26- `m[0..n)`: the message. `n` is at most 65536. A larger `n` traps 3 inside `s512pre`.
27- `sg`: the 64-byte signature R || S.
28- `mode`: 0 for strict, 1 for consensus. Any other value traps 11.
29- Returns 1 when the signature is valid and 0 when it is not.
30
31A signature that is not exactly 64 bytes long cannot be passed in. The caller rejects it first,
32as Go does (`verifyWithDom` checks `len(sig) != signatureSize`).
33
34The other functions are internal: field arithmetic `edf*`, points `edp*`, and scalars `edsclt`,
35`edmodl`, `edhram`. Their contracts are in the `note` lines of the source.
36
37## Rules (pinned to source)
38
39Both modes compute k = SHA-512(R || A || M) mod L over the bytes exactly as received. They
40reject S >= L and decode A the same way, because Go's strict path and ed25519consensus both call
41the same permissive `Point.SetBytes`. The modes differ only in how R is checked.
42
43| step | strict (Go crypto/ed25519) | consensus (ed25519consensus) |
44|---|---|---|
45| S | reject `sig[63]&224 != 0` and S >= L | same |
46| A | `Point.SetBytes`: y >= p accepted (reduced mod p), x = 0 with the sign bit set accepted, not on the curve rejected; small-order and mixed-order A accepted | same |
47| R | not decoded; the canonical encoding of [S]B - [k]A must equal sig[0..32) byte for byte (so a non-canonical R always fails) | decoded with `Point.SetBytes` (same leniency as A); accept iff [8]([S]B - [k]A - R) is the identity |
48
49The task description asked strict mode to reject non-canonical A. Go does not do that, and this
50unit follows Go. In `zip215.txt`, Go strict accepts 12 of the 196 cases, and 4 of those 12 have a
51non-canonical A (y >= p). This unit gives the same 12 results.
52
53Line citations (raw files fetched 2026-10-25):
54
55- Go go1.26.0, `src/crypto/ed25519/ed25519.go`: `Verify` at 221-223, `VerifyWithOptions` at
56 236-257, `ed25519.NewPublicKey(publicKey)` at 240.
57- Go go1.26.0, `src/crypto/internal/fips140/ed25519/ed25519.go`:
58 - `newPublicKey` at 141-151, which calls `pub.a.SetBytes` at 146.
59 - `verifyWithDom` at 290-327: the `sig[63]&224` check at 295, `SetUniformBytes(hramDigest)` at
60 310, `SetCanonicalBytes(sig[32:])` at 315, `Negate` at 321,
61 `VarTimeDoubleScalarBaseMult` at 322, and `bytes.Equal(sig[:32], R.Bytes())` at 324.
62- Go go1.26.0, `src/crypto/internal/fips140/edwards25519/edwards25519.go`: `Point.SetBytes` at
63 145-187. Its comment at 146-149 lists the non-canonical encodings it accepts. `SqrtRatio` is
64 called at 172, the `wasSquare == 0` reject is at 173, and the sign select is at 179.
65- Go go1.26.0, `.../edwards25519/field/fe.go`: `Element.SetBytes` at 187-218. Its doc at 191-194
66 says the top bit is ignored and values 2^255-19 .. 2^255-1 are accepted. `SqrtRatio` is at
67 399-421.
68- Go go1.26.0, `.../edwards25519/scalar.go`: `SetCanonicalBytes` at 159-171 and `isReduced` at
69 178.
70- `github.com/hdevalence/ed25519consensus` `ed25519.go` at commit
71 15731de461e4d21d6a79d5b7aaf473454f9d2009 (tag v0.2.0): `Verify` at 19-67.
72 - Length and `sig[63]&224` checks at 20-26.
73 - `SetBytes(publicKey)` at 29 and the hash at 35-40.
74 - `SetUniformBytes` at 42, `SetBytes(sig[:32])` at 48, and `SetCanonicalBytes` at 56.
75 - `VarTimeDoubleScalarBaseMult` at 61, `Subtract` at 64, `MultByCofactor` at 65, and the
76 identity `Equal` at 66.
77 - It requires `filippo.io/edwards25519` v1.0.0, whose `Point.SetBytes` (edwards25519.go 142-187)
78 and `Element.SetBytes` (field/fe.go 195-198) carry the same accept-non-canonical rule. The test
79 oracle builds with v1.1.1, which fixes an unrelated `MultiScalarMult` bug (Dependabot alert 1).
80 In v1.1.1, `Point.SetBytes` differs from v1.0.0 only in a comment, and oracle builds on v1.0.0
81 and v1.1.1 give identical results on all 818 static cases.
82
83Point decoding follows Go's `SqrtRatio`:
84
851. r = u v^3 (u v^7)^((p-5)/8), with u = y^2 - 1 and v = d y^2 + 1.
862. Accept if v r^2 = u. If v r^2 = -u, multiply r by sqrt(-1) and accept. Otherwise reject.
873. Take the non-negative r. Negate it when bit 255 is set.
88
89This is RFC 8032 section 5.1.3 without the y < p check and without the rejection of x = 0 when
90the sign bit is set.
91
92## Implementation
93
94- **Field GF(2^255-19).** A field element is 16 limbs of radix 2^16, stored in `ulong` cells (the
95 TweetNaCl layout, made unsigned).
96 - Omega has no 64x64->128 multiply, and signed overflow traps. So every product is at most
97 21 x 21 bits, and every limb operation is unsigned.
98 - Bounds: a multiply output has limbs <= 65535, except limb 0, which is <= 65573. Subtraction
99 computes a + 8p - b limb by limb. Each 8p limb is >= 262136, so the result cannot wrap while
100 the subtrahend is a multiply output, a canonical value, or a sum of two of those.
101 - Multiply inputs stay below 2^21 per limb, so a column sum is below 16 * 2^42 and the folded
102 sum (columns 16..30 times 38) is below 2^52.
103 - Two carry passes fold the carry out of limb 15 back into limb 0 with weight 38, because
104 2^256 = 38 mod p.
105 - The bounds are also checked at run time. `edfsub` traps 9 when a subtrahend limb is above the
106 8p limb, and `edfmul` traps 10 when an input limb is >= 2^21. A bound broken by a future edit
107 therefore stops the program instead of wrapping silently. Neither trap fired in any run below.
108 - Canonical form (`edfcan`): carry until every limb is <= 65535 (value < 2^256 = 2p + 38), then
109 subtract p twice, each time only when no borrow results.
110- **Workspace.** Every field element and point lives in one `ulong 1024` array at fixed offsets.
111 Only one array is passed, which satisfies Omega's rule that the arrays of one call are distinct
112 names, and the 6-parameter limit.
113- **Inversion and the (p-5)/8 power.** These use the ref10 addition chain: 250 squarings and 11
114 multiplications to reach z^(2^250-1), then 5 or 2 more squarings.
115- **Points.**
116 - Coordinates are extended (X:Y:Z:T).
117 - Addition is add-2008-hwcd-3, the unified formula TweetNaCl uses. It is complete for
118 Ed25519 and handles the small-order points.
119 - Doubling is dbl-2008-hwcd with a = -1, with every output coordinate negated (the same
120 projective point). This keeps each subtraction within the bound above.
121- **Scalars.**
122 - S < L is a byte comparison from the top byte down.
123 - k mod L is TweetNaCl `modL`, in signed `long` limbs: any overflow would trap rather than
124 wrap.
125 - [S]B + [k](-A) is computed by joint double-and-add from bit 252 (S, k < L < 2^253), with
126 B + (-A) precomputed.
127- **Timing.** Variable time. Every input of verification is public (key, message, signature).
128 The code is NOT constant-time-audited, and Omega gives no constant-time guarantee.
129
130## Tests
131
132```sh
133sh om/ed25519/test/run.sh "$(tools/omc.sh)"
134```
135
136`run.sh` builds `sha512.om + ed25519.om + test/driver.om` into `build/test/ed25519/` and does the
137following:
138
1391. Runs every static vector file in both modes.
1402. Runs three mutants, each of which must be detected:
141 - an expected result flipped;
142 - one limb of the constant 2d changed;
143 - the consensus cofactor multiplication deleted.
144
145 A mutant counts as detected only when the driver exits with status exactly 1 and prints at
146 least one `MISMATCH` line. Any other outcome (exit 0, 65 for bad input, 100+N for a trap, or a
147 signal) fails the run, so a mutant that crashes is not mistaken for one that was caught. This
148 was checked by temporarily swapping in a mutant that traps 11 on every call: `run.sh` reported
149 `FAIL mutant-d2-rfc8032: exit 111, 0 MISMATCH lines` and ended with `ed25519: FAIL`.
1503. When python3 has pynacl and cryptography, generates and runs the differential corpus.
151 Otherwise it prints a NOTE and skips it.
1524. Times one series.
153
154It exits 0 only when every check passes. Environment knobs: `ED_DIFF_COUNT` (2000), `ED_DIFF_SEED`
155(1), `ED_BENCH_N` (1000).
156
157Driver line format: `ES EC PKHEX SIGHEX MSGHEX`. ES and EC are the expected strict and consensus
158results (`0`, `1`, or `-` to skip that mode). Lines starting with `#` are comments.
159
160### Vector sources (fetched 2026-10-25)
161
162| file | cases | source | expected |
163|---|---|---|---|
164| `vectors/rfc8032.txt` | 24 | RFC 8032 section 7.1 (https://www.rfc-editor.org/rfc/rfc8032.txt, sha256 `ed63657f…c4c3`): TEST 1, 2, 3, 1024, SHA(abc), plus one flipped bit in R, in S, in pk, and in the message (not TEST 1: empty message) | 1 for the 5 vectors, 0 for the 19 flips, in both modes |
165| `vectors/zip215.txt` | 196 | `zip215_test.go` of ed25519consensus at 15731de4 (https://github.com/hdevalence/ed25519consensus/blob/15731de461e4d21d6a79d5b7aaf473454f9d2009/zip215_test.go, sha256 `7008831b…34bd`), message "Zcash" | consensus 1 for all 196 (from the source); strict = Go 1.26.0 run as referee (12 accept) |
166| `vectors/wycheproof.txt` | 198 | Wycheproof `testvectors_v1/ed25519_test.json` at a3d5d142 (https://github.com/C2SP/wycheproof/blob/a3d5d1423f2592d6646836a45a21a1b209da9f4d/testvectors_v1/ed25519_test.json, sha256 `ea80ffeb…74b3`) | strict = Wycheproof result for valid/invalid; Go agrees on every one |
167| `vectors/edge.txt` | 400 | generated by `gen/vectors.py` (seed 20261025) | Go 1.26.0 strict and ed25519consensus v0.2.0, run as referees |
168
169Notes on the Wycheproof and edge vectors:
170
171- **Wycheproof, 210 tests in total.** 117 valid, 64 invalid, 29 acceptable.
172 - 12 invalid tests (tcId 30-41) have signatures that are not 64 bytes. They cannot be passed to
173 `edverify` and are listed in the file header.
174 - The 29 acceptable tests (flags MixedOrderPublicKey and SmallOrderPublicKey, tcId 152-180) are
175 all accepted by Go strict and by consensus. This unit accepts them too.
176 - Consensus expectations for Wycheproof come from ed25519consensus run as referee.
177- **Edge vectors.** These are RFC 8032 signatures made by the pure-Python signer in
178 `gen/vectors.py`, pushed into the corners where strict and consensus differ:
179 - S + L;
180 - R and/or A plus a point of order 8, hashed as sent;
181 - small-order A and R with S = 0;
182 - random A or R bytes;
183 - high bits of S set.
184
185 Go strict accepts 22 of them and consensus accepts 200.
186
187`gen/vectors.py SRC ORACLE OUT` regenerates the four files and the third-party notices from the
188pinned upstream files and the referee in `gen/oracle/`. The referee is a 44-line Go program that
189calls `crypto/ed25519.Verify` and `ed25519consensus.Verify`, built with Go 1.26.0, module
190`ed25519consensus` v0.2.0. Go and Python are test referees and generators only. Neither builds
191nor implements anything in `ed25519.om`, and `run.sh` needs neither for the static vectors.
192
193`gen/diff.py OUT COUNT SEED` writes the differential corpus. Each keypair has a random message of
1940..4096 bytes, signed by pynacl 1.5.0 (even index) or cryptography 41.0.7 (odd index). The
195signature is expected valid in both modes. A copy with one random bit of pk || sig || msg flipped
196is expected invalid in both modes.
197
198### Third-party material and notices
199
200| copied material | owner and license | notice file |
201|---|---|---|
202| key/signature hex in `vectors/zip215.txt`, from ed25519consensus `zip215_test.go` | Copyright 2016 Henry de Valence (file header); upstream LICENSE: Copyright (c) 2009 The Go Authors, Copyright (c) 2020 Henry de Valence; BSD 3-clause | `vectors/zip215.LICENSE`, the upstream LICENSE at 15731de4, verbatim (sha256 `789be8b2…6bc7`) |
203| test data in `vectors/wycheproof.txt`, converted from `ed25519_test.json` (modified: JSON to line format, tests with signatures that are not 64 bytes left out) | Copyright 2016-2026 The Wycheproof Authors; Apache License 2.0 (no NOTICE file upstream) | `vectors/wycheproof.LICENSE`, the upstream LICENSE at a3d5d142, verbatim (sha256 `58d1e17f…d8bd`) |
204| keys, messages and signatures in `vectors/rfc8032.txt`, from RFC 8032 section 7.1 | Copyright (c) 2017 IETF Trust and the document authors; IRTF stream, under BCP 78 and the IETF Trust Legal Provisions | `vectors/rfc8032.NOTICE`, the RFC's Copyright Notice section, verbatim |
205
206Each vector file's header names its notice file. `vectors.py` copies the two LICENSE files from
207SRC and cuts the RFC notice from the RFC text, so regeneration keeps them.
208
209The RFC 8032 boilerplate has no Code Components clause (it is an IRTF-stream document). I have not
210determined whether test vectors in it count as code components under the Trust Legal Provisions.
211The notice is included as attribution; whether it is sufficient is not checked.
212
213Not copied: `edge.txt` and the differential corpus come from this unit's own generators. In
214`ed25519.om`, the field layout, the `modL` reduction and the add/double formulas follow the
215published algorithms of TweetNaCl, and the inversion addition chain follows ref10. Both are
216public-domain releases. The Omega code was written for this unit and contains no copied source
217text. Go's source is cited by line numbers only.
218
219### Evidence (2026-10-25, Linux arm64 host)
220
221- omc sha256: `734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61` (omega-mv
222 acb3f15c, `tools/omc.sh`)
223- `ed25519.om` sha256: `7ca5ffb8f9cbf753d6d5a6008ba70270e51c94cbcc43303e29ffbdbb3bb4c167`
224- `sha512.om` sha256 at test time: `069a54b75484221bc7dbacfbea744ca9002c295f4f34f2c3905b0776450e3ce2`
225- driver binary sha256: `4d939305f7958ff133baec2fcce1b028550d86f5080ff97d2902db4891f77a3f`
226
227Output of `sh om/ed25519/test/run.sh "$(tools/omc.sh)"` (exit 0):
228
229```
230PASS rfc8032: cases 24 verifies 48 mismatches 0
231PASS zip215: cases 196 verifies 392 mismatches 0
232PASS wycheproof: cases 198 verifies 396 mismatches 0
233PASS edge: cases 400 verifies 800 mismatches 0
234PASS mutant-expectation detected: 2 mismatches
235PASS mutant-d2-rfc8032 detected: 10 mismatches
236PASS mutant-cofactor-zip215 detected: 157 mismatches
237diff: 2000 keypairs, 4000 cases, 5 files, seed 1
238PASS diff-00 .. diff-04: 800 cases, 1600 verifies, 0 mismatches each (8000 verifies)
239TIME strict: 1000 verifies, 1063530420 ns total, 1063530 ns per verify
240TIME consensus: 1000 verifies, 1073996973 ns total, 1073996 ns per verify
241```
242
243The timing figure is one series of N = 1000 per mode on RFC 8032 TEST 1 (empty message), measured
244with `monons` around each `edverify` call. That is about 1.07 ms per verification on this host.
245
246## Limits
247
248- The message is at most 65536 bytes, because `s512pre` takes a 65536-byte view. A shorter
249 message is passed in a 65536-byte array together with its length.
250- Verification only: no signing, key generation, batch verification, Ed25519ph, or Ed25519ctx.
251- Not constant-time-audited (see above).
252- Single-signature speed is about 1.07 ms. No windowed or precomputed-table scalar
253 multiplication is used.