interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / ng / front / kernels / x25519.om

350 lines · 12195 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0
3note x25519.om: X25519 (RFC 7748 section 5) for the om/tls TLS 1.3 client (RFC 8446 section 7.4.2).
4note Library unit (module code XC): functions and consts only, no om2 line, no main, no globals.
5note
6note API
7note x25519_scalarmult o edit uchar * k view uchar * u view uchar * to ulong
8note o = X25519(k, u). o, k, u must each be exactly 32 bytes. Returns 0 ok, 1 when the output is
9note all zero (RFC 7748 section 6.1, RFC 8446 section 7.4.2: the caller must abort), 2 when a
10note length is not 32 (nothing is written then). The zero test is computed without branches.
11note x25519_base o edit uchar * k view uchar * to ulong
12note o = X25519(k, 9), same return codes.
13note
14note Field GF(p), p = 2^255 - 19: ten limbs h0..h9 in ulong cells, radix 2^25.5 (ref10 layout):
15note limb i has weight 2^e(i), e(i) = ceil(51 i / 2) = 0 26 51 77 102 128 153 179 204 230, and width
16note w(i) = 26 for even i, 25 for odd i (e(10) = 255). A product h_i g_j lands at weight
17note e(i) + e(j) = e(i + j) + [i and j odd], and weight 2^255 folds as 19, so column k of a product
18note collects coef(i, j) h_i g_j over i + j = k (mod 10) with coef = (1 + [i, j odd]) (19 if i + j >= 10).
19note
20note Limb bounds (no ulong ever wraps). "Carried" means every limb < 2^26 and limbs 2..9 within their
21note width; only limb 1 may exceed 2^25, by at most 91314 (below).
22note decode (fload): limbs within width (top bit of u masked), so carried.
23note mul / mul24 / canonical outputs: carried (fcarry, below).
24note fadd of two carried values: limbs < 2^27.
25note fsub a b = a + 2p - b limbwise, b carried: 2p limbs are 134217690 (i = 0), 67108862 (odd),
26note 134217726 (even) and every carried b limb is at or below them (limb 1 <= 33645746 <= 67108862),
27note so no limb wraps; result limbs < 2^26 + 2^27 = 3 * 2^26 = L.
28note fmul inputs: every call site passes carried, fadd or fsub values, so all limbs < L. The largest
29note column coefficient sum is 267 (column 0), so a column is < 267 L^2 < 2^63.24; a carry into the
30note next column is < 2^38.24, so the sum stays below 2^64.
31note fcarry: limbs 0..8 carry into the next limb (shift by the width), limb 9 carries (times 19) into
32note limb 0, then limb 0 carries once more into limb 1: the folded carry is < 19 * 2^38.24, so the
33note second carry out of limb 0 is <= 91315 and limb 1 ends <= 2^25 - 1 + 91315 = 33645746 < 2^26.
34note fmul24: limbs < L times 121665 < 2^44.48, then fcarry.
35note canonical (fstore): the value h < 2^255 - 1 + 91315 * 2^26 < 2p, so q = floor((h + 19) / 2^255)
36note is 0 or 1 and h - q p is the canonical residue (computed with masks only).
37note Checked with the Wolfram Language evaluator (2026-10-26):
38note e[i_]:=Ceiling[51 i/2]; coef[i_,j_]:=(1+BitAnd[i,j,1]) If[i+j>=10,19,1];
39note colsum=Table[Total[Flatten@Table[If[Mod[i+j,10]==k,coef[i,j],0],{i,0,9},{j,0,9}]],{k,0,9}];
40note L=3*2^26; col=Max[colsum] L^2; cin=Floor[(col+2^40)/2^25]; c9=Floor[(col+cin)/2^25];
41note c0=Floor[(2^26-1+19 c9)/2^26]; ... ->
42note e[10]=255; widths {26,25,...}; weight identity e[i]+e[j]==e[Mod[i+j,10]]+255[i+j>=10]+[i,j odd]
43note True for all i,j; colsum={267,154,213,118,159,82,105,46,51,10}; Log2[col]=63.2306;
44note col+cin<2^64 True; Log2[c9]=38.2306; c0=91315; t1=33645746 <= 2(2^25-1) True;
45note Sum[pl_i 2^e[i]]==2^255-19 True; 2p limbs {134217690,67108862,134217726,...};
46note hmax<2p True; Log2[3 2^26 121665]=44.4775; (486662-2)/4=121665; PrimeQ[2^255-19] True.
47note
48note Constant time: no branch, loop count or array index depends on k, u or any intermediate value.
49note Every loop has a fixed count; indices depend only on loop counters and slot offsets. The ladder
50note swap is an xor with a mask m = 0 - bit; the final reduction subtracts q p with q from a carry
51note chain; the all-zero test is ((a | -a) >> 63) ^ 1. Secret arrays (clamped scalar, workspace,
52note multiplication scratch) are overwritten with 0 before return. Caveats: omc emits plain AArch64
53note and we rely on MUL, ADD, SUB, EOR, AND, ORR, LSL/LSR with register amounts being data-independent
54note (no DIT bit is set); register and spill contents are not scrubbed; the length check (return 2)
55note branches on public lengths; bounds checks on indices branch on public offsets only. Conversions
56note of secret values (ulong of a scalar byte in x25519_ladder, uchar of an output byte masked to
57note 0..255 in x25519_fenc) can never fail; any range check omc emits for them is a never-taken trap
58note branch whose direction does not depend on data.
59
60note workspace slots (ulong offsets into w, 10 limbs each); the inversion reuses the step temporaries
61const XCWN ulong 140
62const XCX1 ulong 0
63const XCX2 ulong 10
64const XCZ2 ulong 20
65const XCX3 ulong 30
66const XCZ3 ulong 40
67const XCTA ulong 50
68const XCAA ulong 60
69const XCTB ulong 70
70const XCBB ulong 80
71const XCTE ulong 90
72const XCTC ulong 100
73const XCTD ulong 110
74const XCDA ulong 120
75const XCCB ulong 130
76note a24 = (486662 - 2) / 4 (RFC 7748 section 5)
77const XCA24 ulong 121665
78
79note one carry pass: limbs 0..8 into the next limb, limb 9 (times 19) into limb 0, limb 0 into limb 1
80fn x25519_fcarry t edit ulong 10 to void
81for i ulong 9 unroll
82put t add i 1 add get t add i 1 shr get t i sub 26 and i 1
83put t i and get t i sub 67108863 mul 33554432 and i 1
84end
85c = shr get t 9 25
86put t 9 and get t 9 33554431
87put t 0 add get t 0 mul c 19
88c = shr get t 0 26
89put t 0 and get t 0 67108863
90put t 1 add get t 1 c
91end
92
93note slot o = slot a * slot b mod p, carried (o may equal a or b)
94fn x25519_fmul w edit ulong XCWN o ulong a ulong b ulong to void
95x = new ulong 10 0
96y = new ulong 10 0
97t = new ulong 10 0
98for i ulong 10 unroll
99put x i get w add a i
100put y i get w add b i
101end
102for i ulong 10 unroll
103for j ulong 10 unroll
104put t rem add i j 10 add get t rem add i j 10 mul mul get x i get y j mul add 1 and and i j 1 add 1 mul 18 div add i j 10
105end
106end
107x25519_fcarry t
108for i ulong 10 unroll
109put w add o i get t i
110put x i 0
111put y i 0
112put t i 0
113end
114end
115
116note slot o = slot a + slot b limbwise, no carry
117fn x25519_fadd w edit ulong XCWN o ulong a ulong b ulong to void
118for i ulong 10 unroll
119put w add o i add get w add a i get w add b i
120end
121end
122
123note slot o = slot a + 2p - slot b limbwise, no carry (b must be carried)
124fn x25519_fsub w edit ulong XCWN o ulong a ulong b ulong to void
125for i ulong 10 unroll
126put w add o i sub add get w add a i sub sub 134217726 mul 67108864 and i 1 mul 36 div sub 10 i 10 get w add b i
127end
128end
129
130note slot o = a24 * slot a, carried
131fn x25519_fmul24 w edit ulong XCWN o ulong a ulong to void
132t = new ulong 10 0
133for i ulong 10 unroll
134put t i mul get w add a i XCA24
135end
136x25519_fcarry t
137for i ulong 10 unroll
138put w add o i get t i
139put t i 0
140end
141end
142
143note slot o = slot a ^ (2^n), n >= 1 (n public)
144fn x25519_fsqn w edit ulong XCWN o ulong a ulong n ulong to void
145x25519_fmul w o a a
146for i ulong sub n 1
147x25519_fmul w o o o
148end
149end
150
151note slot o = slot a ^ (p - 2) = 1 / a (0 for a = 0), p - 2 = 2^255 - 21: the fixed ref10 chain
152note z^11, z^(2^250 - 1), then (2^250 - 1) * 2^5 + 11 = 2^255 - 21. Uses XCTA XCAA XCTB XCBB XCTE;
153note o and a must be other slots.
154fn x25519_finv w edit ulong XCWN o ulong a ulong to void
155x25519_fmul w XCTA a a
156x25519_fmul w XCAA XCTA XCTA
157x25519_fmul w XCAA XCAA XCAA
158x25519_fmul w XCAA a XCAA
159x25519_fmul w XCBB XCTA XCAA
160x25519_fmul w XCTA XCBB XCBB
161x25519_fmul w XCTA XCAA XCTA
162x25519_fsqn w XCAA XCTA 5
163x25519_fmul w XCTA XCAA XCTA
164x25519_fsqn w XCAA XCTA 10
165x25519_fmul w XCAA XCAA XCTA
166x25519_fsqn w XCTB XCAA 20
167x25519_fmul w XCAA XCTB XCAA
168x25519_fsqn w XCAA XCAA 10
169x25519_fmul w XCTA XCAA XCTA
170x25519_fsqn w XCAA XCTA 50
171x25519_fmul w XCAA XCAA XCTA
172x25519_fsqn w XCTB XCAA 100
173x25519_fmul w XCAA XCTB XCAA
174x25519_fsqn w XCAA XCAA 50
175x25519_fmul w XCTE XCAA XCTA
176x25519_fsqn w XCTE XCTE 5
177x25519_fmul w o XCTE XCBB
178end
179
180note swap slots a and b when m is all ones, keep them when m is 0 (m = 0 - bit, no branch)
181fn x25519_fcswap w edit ulong XCWN a ulong b ulong m ulong to void
182for i ulong 10 unroll
183x = get w add a i
184y = get w add b i
185d = and m xor x y
186put w add a i xor x d
187put w add b i xor y d
188end
189end
190
191note slot o = the 32 little-endian bytes of u with bit 255 masked (RFC 7748 decodeUCoordinate);
192note values p..2^255-1 are kept and reduced by the arithmetic. len u = 32 is checked by the caller.
193fn x25519_fload w edit ulong XCWN o ulong u view uchar * to void
194for i ulong 10
195e = div add mul i 51 1 2
196b = shr e 3
197v = ulong get u b
198v = or v shl ulong get u add b 1 8
199v = or v shl ulong get u add b 2 16
200v = or v shl ulong get u add b 3 24
201m = sub shl ulong 1 sub 26 and i 1 1
202put w add o i and shr v and e 7 m
203end
204end
205
206note o = the 32-byte little-endian encoding of the canonical limbs t; returns 1 when o is all zero,
207note else 0 (no branch on the value: z = ((a | -a) >> 63) ^ 1 over the OR of the four words)
208fn x25519_fenc t view ulong 10 o edit uchar * to ulong
209q = new ulong 4 0
210for i ulong 10
211e = div add mul i 51 1 2
212d = shr e 6
213s = and e 63
214v = get t i
215put q d or get q d shl v s
216if gt add s sub 26 and i 1 64
217d = add d 1
218put q d or get q d shr v sub 64 s
219end
220end
221for j ulong 32
222v = shr get q shr j 3 mul and j 7 8
223put o j uchar and v 255
224end
225z = or or get q 0 get q 1 or get q 2 get q 3
226z = xor shr or z sub 0 z 63 1
227for i ulong 4 unroll
228put q i 0
229end
230ret z
231end
232
233note o = the canonical 32-byte encoding of slot a (carried input): q = floor((h + 19) / 2^255) by a
234note carry chain, then h + 19 q with the carry out of bit 255 dropped = h - q p. Returns x25519_fenc.
235fn x25519_fstore w edit ulong XCWN a ulong o edit uchar * to ulong
236t = new ulong 10 0
237for i ulong 10 unroll
238put t i get w add a i
239end
240c = ulong 19
241for i ulong 10 unroll
242c = shr add get t i c sub 26 and i 1
243end
244put t 0 add get t 0 mul c 19
245for i ulong 9 unroll
246put t add i 1 add get t add i 1 shr get t i sub 26 and i 1
247put t i and get t i sub 67108863 mul 33554432 and i 1
248end
249put t 9 and get t 9 33554431
250z = x25519_fenc t o
251for i ulong 10 unroll
252put t i 0
253end
254ret z
255end
256
257note one Montgomery ladder step (RFC 7748 section 5):
258note A = x2 + z2, AA = A^2, B = x2 - z2, BB = B^2, E = AA - BB, C = x3 + z3, D = x3 - z3,
259note DA = D A, CB = C B, x3 = (DA + CB)^2, z3 = x1 (DA - CB)^2, x2 = AA BB, z2 = E (AA + a24 E)
260fn x25519_step w edit ulong XCWN to void
261x25519_fadd w XCTA XCX2 XCZ2
262x25519_fmul w XCAA XCTA XCTA
263x25519_fsub w XCTB XCX2 XCZ2
264x25519_fmul w XCBB XCTB XCTB
265x25519_fsub w XCTE XCAA XCBB
266x25519_fadd w XCTC XCX3 XCZ3
267x25519_fsub w XCTD XCX3 XCZ3
268x25519_fmul w XCDA XCTD XCTA
269x25519_fmul w XCCB XCTC XCTB
270x25519_fadd w XCTA XCDA XCCB
271x25519_fmul w XCX3 XCTA XCTA
272x25519_fsub w XCTB XCDA XCCB
273x25519_fmul w XCTB XCTB XCTB
274x25519_fmul w XCZ3 XCX1 XCTB
275x25519_fmul w XCX2 XCAA XCBB
276x25519_fmul24 w XCTC XCTE
277x25519_fadd w XCTC XCAA XCTC
278x25519_fmul w XCZ2 XCTE XCTC
279end
280
281note the ladder over bits 254..0 of the clamped scalar e, conditional swaps by masks
282fn x25519_ladder w edit ulong XCWN e view uchar 32 to void
283sw = ulong 0
284for i ulong 255
285t = sub 254 i
286b = and shr ulong get e shr t 3 and t 7 1
287sw = xor sw b
288m = sub 0 sw
289x25519_fcswap w XCX2 XCX3 m
290x25519_fcswap w XCZ2 XCZ3 m
291sw = b
292x25519_step w
293end
294m = sub 0 sw
295x25519_fcswap w XCX2 XCX3 m
296x25519_fcswap w XCZ2 XCZ3 m
297end
298
299fn x25519_scalarmult o edit uchar * k view uchar * u view uchar * to ulong
300if or or ne len o 32 ne len k 32 ne len u 32
301ret 2
302end
303w = new ulong XCWN 0
304e = new uchar 32 0
305for i ulong 32
306put e i get k i
307end
308put e 0 and get e 0 248
309put e 31 or and get e 31 127 64
310x25519_fload w XCX1 u
311for i ulong 10 unroll
312put w add XCX3 i get w add XCX1 i
313end
314put w XCX2 1
315put w XCZ3 1
316x25519_ladder w e
317x25519_finv w XCTC XCZ2
318x25519_fmul w XCX2 XCX2 XCTC
319r = x25519_fstore w XCX2 o
320for i ulong XCWN
321put w i 0
322end
323for i ulong 32
324put e i 0
325end
326ret r
327end
328
329fn x25519_base o edit uchar * k view uchar * to ulong
330b = new uchar 32 0
331put b 0 9
332ret x25519_scalarmult o k b
333end
334note bench: 2000 chained X25519 base-point multiplications (om/tls/x25519.om), A/B only
335fn main to int
336k = new uchar 32 0
337o = new uchar 32 0
338for i ulong 32
339put k i uchar add i 1
340end
341bad = ulong 0
342for r ulong 2000
343bad = or bad x25519_base o k
344for i ulong 32
345put k i get o i
346end
347end
348w = write 1 o 0 32
349ret int bad
350end