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