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
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note om/tls/ec.om: ECDSA signature verification on NIST P-256 and P-384 (FIPS 186-4 section 6.4.2). |
| 3 | note Library unit (no om2 line, no main). Module code EC: functions ec_* and ecdsa_*, consts EC*. |
| 4 | note |
| 5 | note API (all inputs are public; this module is variable time by design, see "Timing" below): |
| 6 | note ecdsa_verify crv pub hs hlen crv 256 or 384; pub = 0x04 || X || Y (SEC1 uncompressed); |
| 7 | note hs = digest (hlen bytes) || DER Ecdsa-Sig-Value |
| 8 | note ecdsa_verify_raw crv pub hs hlen same, signature r || s, each 32 (P-256) or 48 (P-384) bytes |
| 9 | note Return codes: 0 valid, 1 signature mismatch (computed point at infinity included), |
| 10 | note 2 bad DER or encoding (also hlen > len hs), 3 bad public key (format, coordinate >= p, |
| 11 | note not on the curve), 4 r or s not in [1, n-1], 5 unsupported crv. |
| 12 | note The digest may have any length: e = its leftmost min(hlen, 32|48) bytes (bits2int; the bit |
| 13 | note length of n is 256 or 384, byte aligned), reduced mod n. |
| 14 | note |
| 15 | note Arithmetic: 32-bit limbs held in ulong cells, little-endian, L = 8 (P-256) or 12 (P-384). |
| 16 | note Generic Montgomery multiplication (CIOS) for p and for n; n0' = -m^-1 mod 2^32 (Newton |
| 17 | note iteration), R mod m and R^2 mod m (R = 2^(32L), by repeated doubling) are computed at run |
| 18 | note time. Points in Jacobian coordinates (a = -3), Z = 0 is the point at infinity. Shamir's |
| 19 | note joint double-scalar multiplication u1*G + u2*Q with the table G, Q, G+Q. |
| 20 | note |
| 21 | note Timing: signature verification handles only public values (key, digest, signature), so this |
| 22 | note module is NOT constant time and does not try to be: DER parsing, range checks, the Shamir |
| 23 | note loop (branches on the bits of u1 and u2), point addition (branches on Z = 0, H = 0, r = 0), |
| 24 | note ec_csub (branch on the borrow), ec_cmp (early exit) and the Fermat inversions (branch on the |
| 25 | note exponent bits of the public moduli) all take data-dependent time. Do not reuse these helpers |
| 26 | note on secret data (e.g. ECDH or signing) without a constant-time rewrite. Compiler caveat: omc |
| 27 | note emits plain AArch64 and no DIT bit is set. No secret is held, so nothing is wiped. |
| 28 | note Untrusted input never traps: every length and offset is checked before indexing (fuzzed by |
| 29 | note test/ec/gen_fuzz.py). |
| 30 | note |
| 31 | note Workspace w (ECWN ulong cells) layout, slot stride 12 cells: |
| 32 | note 0 L; 2..15 Montgomery accumulator T (L+2 cells) |
| 33 | note ECCP 16 / ECCN 56: modulus context: +0 m, +12 n0', +16 R mod m, +28 R^2 mod m |
| 34 | note ECZ zero, ECON plain 1, ECB curve b (Montgomery), points ECG ECQ ECGQ ECR (X,Y,Z), |
| 35 | note scalars ECSR r, ECSS s, ECE e, ECU1, ECU2, ECSI s^-1, temporaries ECA0..ECA9, ECAC. |
| 36 | |
| 37 | const ECWN ulong 480 |
| 38 | const ECCP ulong 16 |
| 39 | const ECCN ulong 56 |
| 40 | const ECZ ulong 96 |
| 41 | const ECON ulong 108 |
| 42 | const ECB ulong 120 |
| 43 | const ECG ulong 132 |
| 44 | const ECQ ulong 168 |
| 45 | const ECGQ ulong 204 |
| 46 | const ECR ulong 240 |
| 47 | const ECSR ulong 276 |
| 48 | const ECSS ulong 288 |
| 49 | const ECE ulong 300 |
| 50 | const ECU1 ulong 312 |
| 51 | const ECU2 ulong 324 |
| 52 | const ECSI ulong 336 |
| 53 | const ECA0 ulong 348 |
| 54 | const ECA1 ulong 360 |
| 55 | const ECA2 ulong 372 |
| 56 | const ECA3 ulong 384 |
| 57 | const ECA4 ulong 396 |
| 58 | const ECA5 ulong 408 |
| 59 | const ECA6 ulong 420 |
| 60 | const ECA7 ulong 432 |
| 61 | const ECA8 ulong 444 |
| 62 | const ECA9 ulong 456 |
| 63 | const ECAC ulong 468 |
| 64 | |
| 65 | note Curve constants, verbatim from FIPS 186-4 Appendix D.1.2.3 (P-256) and D.1.2.4 (P-384) |
| 66 | note (same values as SEC 2 v2 sections 2.4.2 secp256r1 and 2.5.1 secp384r1). a = -3 for both. |
| 67 | note Checked with Wolfram (WolframLanguageEvaluator, 2026-10-26; exact query and output in |
| 68 | note test/ec/wolfram.txt): PrimeQ[p], PrimeQ[n], Mod[Gy^2-(Gx^3-3Gx+b),p]==0, n*G == O (affine |
| 69 | note double-and-add mod p), BitLength[p]==BitLength[n] (256, 384), n<p<2n: all True for both curves. |
| 70 | fn ec_p256 w edit ulong ECWN to void |
| 71 | put w 0 8 |
| 72 | note p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff (limbs little-endian) |
| 73 | put w 16 0xffffffff |
| 74 | put w 17 0xffffffff |
| 75 | put w 18 0xffffffff |
| 76 | put w 19 0x0 |
| 77 | put w 20 0x0 |
| 78 | put w 21 0x0 |
| 79 | put w 22 0x1 |
| 80 | put w 23 0xffffffff |
| 81 | note n = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551 (limbs little-endian) |
| 82 | put w 56 0xfc632551 |
| 83 | put w 57 0xf3b9cac2 |
| 84 | put w 58 0xa7179e84 |
| 85 | put w 59 0xbce6faad |
| 86 | put w 60 0xffffffff |
| 87 | put w 61 0xffffffff |
| 88 | put w 62 0x0 |
| 89 | put w 63 0xffffffff |
| 90 | note b = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b (limbs little-endian) |
| 91 | put w 120 0x27d2604b |
| 92 | put w 121 0x3bce3c3e |
| 93 | put w 122 0xcc53b0f6 |
| 94 | put w 123 0x651d06b0 |
| 95 | put w 124 0x769886bc |
| 96 | put w 125 0xb3ebbd55 |
| 97 | put w 126 0xaa3a93e7 |
| 98 | put w 127 0x5ac635d8 |
| 99 | note gx = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296 (limbs little-endian) |
| 100 | put w 132 0xd898c296 |
| 101 | put w 133 0xf4a13945 |
| 102 | put w 134 0x2deb33a0 |
| 103 | put w 135 0x77037d81 |
| 104 | put w 136 0x63a440f2 |
| 105 | put w 137 0xf8bce6e5 |
| 106 | put w 138 0xe12c4247 |
| 107 | put w 139 0x6b17d1f2 |
| 108 | note gy = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5 (limbs little-endian) |
| 109 | put w 144 0x37bf51f5 |
| 110 | put w 145 0xcbb64068 |
| 111 | put w 146 0x6b315ece |
| 112 | put w 147 0x2bce3357 |
| 113 | put w 148 0x7c0f9e16 |
| 114 | put w 149 0x8ee7eb4a |
| 115 | put w 150 0xfe1a7f9b |
| 116 | put w 151 0x4fe342e2 |
| 117 | end |
| 118 | |
| 119 | fn ec_p384 w edit ulong ECWN to void |
| 120 | put w 0 12 |
| 121 | note p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff (limbs little-endian) |
| 122 | put w 16 0xffffffff |
| 123 | put w 17 0x0 |
| 124 | put w 18 0x0 |
| 125 | put w 19 0xffffffff |
| 126 | put w 20 0xfffffffe |
| 127 | put w 21 0xffffffff |
| 128 | put w 22 0xffffffff |
| 129 | put w 23 0xffffffff |
| 130 | put w 24 0xffffffff |
| 131 | put w 25 0xffffffff |
| 132 | put w 26 0xffffffff |
| 133 | put w 27 0xffffffff |
| 134 | note n = 0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973 (limbs little-endian) |
| 135 | put w 56 0xccc52973 |
| 136 | put w 57 0xecec196a |
| 137 | put w 58 0x48b0a77a |
| 138 | put w 59 0x581a0db2 |
| 139 | put w 60 0xf4372ddf |
| 140 | put w 61 0xc7634d81 |
| 141 | put w 62 0xffffffff |
| 142 | put w 63 0xffffffff |
| 143 | put w 64 0xffffffff |
| 144 | put w 65 0xffffffff |
| 145 | put w 66 0xffffffff |
| 146 | put w 67 0xffffffff |
| 147 | note b = 0xb3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef (limbs little-endian) |
| 148 | put w 120 0xd3ec2aef |
| 149 | put w 121 0x2a85c8ed |
| 150 | put w 122 0x8a2ed19d |
| 151 | put w 123 0xc656398d |
| 152 | put w 124 0x5013875a |
| 153 | put w 125 0x314088f |
| 154 | put w 126 0xfe814112 |
| 155 | put w 127 0x181d9c6e |
| 156 | put w 128 0xe3f82d19 |
| 157 | put w 129 0x988e056b |
| 158 | put w 130 0xe23ee7e4 |
| 159 | put w 131 0xb3312fa7 |
| 160 | note gx = 0xaa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7 (limbs little-endian) |
| 161 | put w 132 0x72760ab7 |
| 162 | put w 133 0x3a545e38 |
| 163 | put w 134 0xbf55296c |
| 164 | put w 135 0x5502f25d |
| 165 | put w 136 0x82542a38 |
| 166 | put w 137 0x59f741e0 |
| 167 | put w 138 0x8ba79b98 |
| 168 | put w 139 0x6e1d3b62 |
| 169 | put w 140 0xf320ad74 |
| 170 | put w 141 0x8eb1c71e |
| 171 | put w 142 0xbe8b0537 |
| 172 | put w 143 0xaa87ca22 |
| 173 | note gy = 0x3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f (limbs little-endian) |
| 174 | put w 144 0x90ea0e5f |
| 175 | put w 145 0x7a431d7c |
| 176 | put w 146 0x1d7e819d |
| 177 | put w 147 0xa60b1ce |
| 178 | put w 148 0xb5f0b8c0 |
| 179 | put w 149 0xe9da3113 |
| 180 | put w 150 0x289a147c |
| 181 | put w 151 0xf8f41dbd |
| 182 | put w 152 0x9292dc29 |
| 183 | put w 153 0x5d9e98bf |
| 184 | put w 154 0x96262c6f |
| 185 | put w 155 0x3617de4a |
| 186 | end |
| 187 | |
| 188 | note T[0..L+1] += a[0..L-1] * y (a at w offset a, y < 2^32); each step fits: (2^32-1) + |
| 189 | note (2^32-1)^2 + (2^32-1) = 2^64 - 1 |
| 190 | fn ec_row w edit ulong ECWN a ulong y ulong to void |
| 191 | n = get w 0 |
| 192 | c = ulong 0 |
| 193 | x = ulong 0 |
| 194 | for j ulong n |
| 195 | x = add add get w add 2 j mul get w add a j y c |
| 196 | put w add 2 j and x 0xffffffff |
| 197 | c = shr x 32 |
| 198 | end |
| 199 | x = add get w add 2 n c |
| 200 | put w add 2 n and x 0xffffffff |
| 201 | put w add 3 n add get w add 3 n shr x 32 |
| 202 | end |
| 203 | |
| 204 | note o = T - m if T >= m else T (T has L+1 limbs, T < 2m); modulus m at offset c |
| 205 | fn ec_csub w edit ulong ECWN c ulong o ulong to void |
| 206 | n = get w 0 |
| 207 | br = ulong 0 |
| 208 | x = ulong 0 |
| 209 | for j ulong n |
| 210 | x = sub sub get w add 2 j get w add c j br |
| 211 | br = shr x 63 |
| 212 | end |
| 213 | x = sub get w add 2 n br |
| 214 | if eq shr x 63 0 |
| 215 | br = 0 |
| 216 | for j ulong n |
| 217 | x = sub sub get w add 2 j get w add c j br |
| 218 | put w add o j and x 0xffffffff |
| 219 | br = shr x 63 |
| 220 | end |
| 221 | ret |
| 222 | end |
| 223 | for j ulong n |
| 224 | put w add o j get w add 2 j |
| 225 | end |
| 226 | end |
| 227 | |
| 228 | note T = a + b as L+1 limbs |
| 229 | fn ec_sum w edit ulong ECWN a ulong b ulong to void |
| 230 | n = get w 0 |
| 231 | cy = ulong 0 |
| 232 | x = ulong 0 |
| 233 | for j ulong n |
| 234 | x = add add get w add a j get w add b j cy |
| 235 | put w add 2 j and x 0xffffffff |
| 236 | cy = shr x 32 |
| 237 | end |
| 238 | put w add 2 n cy |
| 239 | end |
| 240 | |
| 241 | note o = a + b mod m (a, b < m; also a < 2m with b = 0 reduces a) |
| 242 | fn ec_add w edit ulong ECWN c ulong o ulong a ulong b ulong to void |
| 243 | ec_sum w a b |
| 244 | ec_csub w c o |
| 245 | end |
| 246 | |
| 247 | note o = a - b mod m (a, b < m): T = a + m - b, in (0, 2m), then one conditional subtraction |
| 248 | fn ec_sub w edit ulong ECWN c ulong o ulong a ulong b ulong to void |
| 249 | ec_sum w a c |
| 250 | n = get w 0 |
| 251 | br = ulong 0 |
| 252 | x = ulong 0 |
| 253 | for j ulong n |
| 254 | x = sub sub get w add 2 j get w add b j br |
| 255 | put w add 2 j and x 0xffffffff |
| 256 | br = shr x 63 |
| 257 | end |
| 258 | put w add 2 n sub get w add 2 n br |
| 259 | ec_csub w c o |
| 260 | end |
| 261 | |
| 262 | note one CIOS step: T = (T + a*y + q*m) / 2^32 with q = (T + a*y)[0] * n0' mod 2^32 |
| 263 | fn ec_step w edit ulong ECWN c ulong a ulong y ulong to void |
| 264 | ec_row w a y |
| 265 | q = and mul get w 2 get w add c 12 0xffffffff |
| 266 | ec_row w c q |
| 267 | n = get w 0 |
| 268 | for j ulong add n 1 |
| 269 | put w add 2 j get w add 3 j |
| 270 | end |
| 271 | put w add 3 n 0 |
| 272 | end |
| 273 | |
| 274 | note o = a * b * R^-1 mod m (Montgomery, CIOS; a, b < m). o may alias a or b. |
| 275 | fn ec_mul w edit ulong ECWN c ulong o ulong a ulong b ulong to void |
| 276 | n = get w 0 |
| 277 | for j ulong add n 2 |
| 278 | put w add 2 j 0 |
| 279 | end |
| 280 | for i ulong n |
| 281 | ec_step w c a get w add b i |
| 282 | end |
| 283 | ec_csub w c o |
| 284 | end |
| 285 | |
| 286 | note compare L-limb numbers: 0 equal, 1 a < b, 2 a > b |
| 287 | fn ec_cmp w edit ulong ECWN a ulong b ulong to ulong |
| 288 | n = get w 0 |
| 289 | for k ulong n |
| 290 | j = sub sub n 1 k |
| 291 | x = get w add a j |
| 292 | y = get w add b j |
| 293 | if lt x y |
| 294 | ret 1 |
| 295 | end |
| 296 | if gt x y |
| 297 | ret 2 |
| 298 | end |
| 299 | end |
| 300 | ret 0 |
| 301 | end |
| 302 | |
| 303 | fn ec_copy w edit ulong ECWN o ulong a ulong to void |
| 304 | for j ulong get w 0 |
| 305 | put w add o j get w add a j |
| 306 | end |
| 307 | end |
| 308 | |
| 309 | note copy a point (three slots) |
| 310 | fn ec_pcopy w edit ulong ECWN o ulong a ulong to void |
| 311 | for j ulong 36 |
| 312 | put w add o j get w add a j |
| 313 | end |
| 314 | end |
| 315 | |
| 316 | note bit i of the L-limb number at a |
| 317 | fn ec_bit w edit ulong ECWN a ulong i ulong to ulong |
| 318 | ret and shr get w add a shr i 5 and i 31 1 |
| 319 | end |
| 320 | |
| 321 | note modulus context at c (m already stored): n0' = -m^-1 mod 2^32 by Newton iteration |
| 322 | note (x = m0 is right to 3 bits; each step doubles the bits: 3, 6, 12, 24, 48 >= 32), |
| 323 | note R mod m = 2^(32L) mod m and R^2 mod m by repeated modular doubling from 1. |
| 324 | fn ec_ctx w edit ulong ECWN c ulong to void |
| 325 | n = get w 0 |
| 326 | m0 = get w c |
| 327 | x = m0 |
| 328 | for k ulong 5 |
| 329 | x = and mul x sub 2 mul m0 x 0xffffffff |
| 330 | end |
| 331 | put w add c 12 and sub 0 x 0xffffffff |
| 332 | for j ulong n |
| 333 | put w add add c 16 j 0 |
| 334 | end |
| 335 | put w add c 16 1 |
| 336 | for k ulong mul n 32 |
| 337 | ec_add w c add c 16 add c 16 add c 16 |
| 338 | end |
| 339 | ec_copy w add c 28 add c 16 |
| 340 | for k ulong mul n 32 |
| 341 | ec_add w c add c 28 add c 28 add c 28 |
| 342 | end |
| 343 | end |
| 344 | |
| 345 | note o = a^(m-2) (Montgomery domain; m prime, so a^-1 for a != 0). m - 2: m0 >= 2 for all four |
| 346 | note moduli, so only limb 0 changes. |
| 347 | fn ec_inv w edit ulong ECWN c ulong o ulong a ulong to void |
| 348 | ec_copy w ECAC add c 16 |
| 349 | nb = mul get w 0 32 |
| 350 | for k ulong nb |
| 351 | i = sub sub nb 1 k |
| 352 | ec_mul w c ECAC ECAC ECAC |
| 353 | v = get w add c shr i 5 |
| 354 | if eq shr i 5 0 |
| 355 | v = sub v 2 |
| 356 | end |
| 357 | if eq and shr v and i 31 1 1 |
| 358 | ec_mul w c ECAC ECAC a |
| 359 | end |
| 360 | end |
| 361 | ec_copy w o ECAC |
| 362 | end |
| 363 | |
| 364 | note load the big-endian bytes of b (len b <= 4L) as an L-limb number at o |
| 365 | fn ec_load w edit ulong ECWN o ulong b view uchar * to void |
| 366 | for j ulong get w 0 |
| 367 | put w add o j 0 |
| 368 | end |
| 369 | k = len b |
| 370 | for i ulong k |
| 371 | bv = shl ulong get b sub sub k i 1 mul and i 3 8 |
| 372 | li = add o shr i 2 |
| 373 | put w li or get w li bv |
| 374 | end |
| 375 | end |
| 376 | |
| 377 | note P = 2P in place, a = -3 (dbl-2001-b). Infinity (Z = 0) stays infinity. |
| 378 | fn ec_dbl w edit ulong ECWN p ulong to void |
| 379 | y = add p 12 |
| 380 | z = add p 24 |
| 381 | ec_mul w ECCP ECA0 z z |
| 382 | ec_mul w ECCP ECA1 y y |
| 383 | ec_mul w ECCP ECA2 p ECA1 |
| 384 | ec_sub w ECCP ECA4 p ECA0 |
| 385 | ec_add w ECCP ECA5 p ECA0 |
| 386 | ec_mul w ECCP ECA3 ECA4 ECA5 |
| 387 | ec_add w ECCP ECA4 ECA3 ECA3 |
| 388 | ec_add w ECCP ECA3 ECA4 ECA3 |
| 389 | ec_add w ECCP ECA4 y z |
| 390 | ec_mul w ECCP ECA4 ECA4 ECA4 |
| 391 | ec_sub w ECCP ECA4 ECA4 ECA1 |
| 392 | ec_sub w ECCP z ECA4 ECA0 |
| 393 | ec_add w ECCP ECA2 ECA2 ECA2 |
| 394 | ec_add w ECCP ECA2 ECA2 ECA2 |
| 395 | ec_add w ECCP ECA5 ECA2 ECA2 |
| 396 | ec_mul w ECCP p ECA3 ECA3 |
| 397 | ec_sub w ECCP p p ECA5 |
| 398 | ec_sub w ECCP ECA2 ECA2 p |
| 399 | ec_mul w ECCP ECA2 ECA3 ECA2 |
| 400 | ec_mul w ECCP ECA1 ECA1 ECA1 |
| 401 | ec_add w ECCP ECA1 ECA1 ECA1 |
| 402 | ec_add w ECCP ECA1 ECA1 ECA1 |
| 403 | ec_add w ECCP ECA1 ECA1 ECA1 |
| 404 | ec_sub w ECCP y ECA2 ECA1 |
| 405 | end |
| 406 | |
| 407 | note P = P + Q (add-1998-cmo-2). Q infinity: P unchanged; P infinity: P = Q; H = 0 and R = 0 |
| 408 | note (P == Q): doubling; H = 0, R != 0 (P == -Q): infinity. |
| 409 | fn ec_padd w edit ulong ECWN p ulong q ulong to void |
| 410 | if eq ec_cmp w add q 24 ECZ 0 |
| 411 | ret |
| 412 | end |
| 413 | if eq ec_cmp w add p 24 ECZ 0 |
| 414 | ec_pcopy w p q |
| 415 | ret |
| 416 | end |
| 417 | ec_mul w ECCP ECA0 add p 24 add p 24 |
| 418 | ec_mul w ECCP ECA1 add q 24 add q 24 |
| 419 | ec_mul w ECCP ECA2 p ECA1 |
| 420 | ec_mul w ECCP ECA3 q ECA0 |
| 421 | ec_mul w ECCP ECA4 add p 12 add q 24 |
| 422 | ec_mul w ECCP ECA4 ECA4 ECA1 |
| 423 | ec_mul w ECCP ECA5 add q 12 add p 24 |
| 424 | ec_mul w ECCP ECA5 ECA5 ECA0 |
| 425 | ec_sub w ECCP ECA3 ECA3 ECA2 |
| 426 | ec_sub w ECCP ECA5 ECA5 ECA4 |
| 427 | if eq ec_cmp w ECA3 ECZ 0 |
| 428 | if eq ec_cmp w ECA5 ECZ 0 |
| 429 | ec_dbl w p |
| 430 | ret |
| 431 | end |
| 432 | ec_copy w add p 24 ECZ |
| 433 | ret |
| 434 | end |
| 435 | ec_mul w ECCP ECA6 ECA3 ECA3 |
| 436 | ec_mul w ECCP ECA7 ECA3 ECA6 |
| 437 | ec_mul w ECCP ECA2 ECA2 ECA6 |
| 438 | ec_mul w ECCP p ECA5 ECA5 |
| 439 | ec_sub w ECCP p p ECA7 |
| 440 | ec_sub w ECCP p p ECA2 |
| 441 | ec_sub w ECCP p p ECA2 |
| 442 | ec_sub w ECCP ECA2 ECA2 p |
| 443 | ec_mul w ECCP ECA2 ECA5 ECA2 |
| 444 | ec_mul w ECCP ECA4 ECA4 ECA7 |
| 445 | ec_sub w ECCP add p 12 ECA2 ECA4 |
| 446 | ec_mul w ECCP add p 24 add p 24 add q 24 |
| 447 | ec_mul w ECCP add p 24 add p 24 ECA3 |
| 448 | end |
| 449 | |
| 450 | note ECR = u1*G + u2*Q (Shamir, table G, Q, G+Q; variable time) |
| 451 | fn ec_smul w edit ulong ECWN to void |
| 452 | ec_pcopy w ECGQ ECG |
| 453 | ec_padd w ECGQ ECQ |
| 454 | ec_pcopy w ECR ECG |
| 455 | ec_copy w add ECR 24 ECZ |
| 456 | nb = mul get w 0 32 |
| 457 | for k ulong nb |
| 458 | i = sub sub nb 1 k |
| 459 | ec_dbl w ECR |
| 460 | t = or ec_bit w ECU1 i shl ec_bit w ECU2 i 1 |
| 461 | if eq t 1 |
| 462 | ec_padd w ECR ECG |
| 463 | end |
| 464 | if eq t 2 |
| 465 | ec_padd w ECR ECQ |
| 466 | end |
| 467 | if eq t 3 |
| 468 | ec_padd w ECR ECGQ |
| 469 | end |
| 470 | end |
| 471 | end |
| 472 | |
| 473 | note curve setup: constants, both Montgomery contexts, b and G into the Montgomery domain |
| 474 | fn ec_setup w edit ulong ECWN crv ulong to ulong |
| 475 | if eq crv 256 |
| 476 | ec_p256 w |
| 477 | else |
| 478 | if eq crv 384 |
| 479 | ec_p384 w |
| 480 | else |
| 481 | ret 5 |
| 482 | end |
| 483 | end |
| 484 | ec_ctx w ECCP |
| 485 | ec_ctx w ECCN |
| 486 | put w ECON 1 |
| 487 | ec_mul w ECCP ECB ECB add ECCP 28 |
| 488 | ec_mul w ECCP ECG ECG add ECCP 28 |
| 489 | ec_mul w ECCP add ECG 12 add ECG 12 add ECCP 28 |
| 490 | ec_copy w add ECG 24 add ECCP 16 |
| 491 | ret 0 |
| 492 | end |
| 493 | |
| 494 | note public key: exactly 0x04 || X || Y, X < p, Y < p, Y^2 = X^3 - 3X + b; Q in Montgomery form |
| 495 | fn ec_pub w edit ulong ECWN pub view uchar * to ulong |
| 496 | nb = mul get w 0 4 |
| 497 | if ne len pub add 1 mul nb 2 |
| 498 | ret 3 |
| 499 | end |
| 500 | if ne get pub 0 4 |
| 501 | ret 3 |
| 502 | end |
| 503 | ec_load w ECQ part pub 1 nb |
| 504 | ec_load w add ECQ 12 part pub add 1 nb nb |
| 505 | if ne ec_cmp w ECQ ECCP 1 |
| 506 | ret 3 |
| 507 | end |
| 508 | if ne ec_cmp w add ECQ 12 ECCP 1 |
| 509 | ret 3 |
| 510 | end |
| 511 | ec_mul w ECCP ECQ ECQ add ECCP 28 |
| 512 | ec_mul w ECCP add ECQ 12 add ECQ 12 add ECCP 28 |
| 513 | ec_copy w add ECQ 24 add ECCP 16 |
| 514 | ec_mul w ECCP ECA0 add ECQ 12 add ECQ 12 |
| 515 | ec_mul w ECCP ECA1 ECQ ECQ |
| 516 | ec_add w ECCP ECA2 add ECCP 16 add ECCP 16 |
| 517 | ec_add w ECCP ECA2 ECA2 add ECCP 16 |
| 518 | ec_sub w ECCP ECA1 ECA1 ECA2 |
| 519 | ec_mul w ECCP ECA1 ECA1 ECQ |
| 520 | ec_add w ECCP ECA1 ECA1 ECB |
| 521 | if ne ec_cmp w ECA0 ECA1 0 |
| 522 | ret 3 |
| 523 | end |
| 524 | ret 0 |
| 525 | end |
| 526 | |
| 527 | note one DER INTEGER at hs[p..e): tag 2, short-form length 1..127 inside e, non-negative, |
| 528 | note minimal (a leading 0x00 only before a byte >= 0x80). Writes the value to o; a value longer |
| 529 | note than 4L bytes is replaced by n so the range check rejects it. Returns the bytes used, 0 bad. |
| 530 | fn ec_int w edit ulong ECWN o ulong hs view uchar * p ulong e ulong to ulong |
| 531 | if lt sub e p 3 |
| 532 | ret 0 |
| 533 | end |
| 534 | if ne get hs p 2 |
| 535 | ret 0 |
| 536 | end |
| 537 | il = ulong get hs add p 1 |
| 538 | if or ge il 128 eq il 0 |
| 539 | ret 0 |
| 540 | end |
| 541 | if gt il sub e add p 2 |
| 542 | ret 0 |
| 543 | end |
| 544 | v = add p 2 |
| 545 | if ge get hs v 128 |
| 546 | ret 0 |
| 547 | end |
| 548 | k = il |
| 549 | if and eq get hs v 0 gt il 1 |
| 550 | if lt get hs add v 1 128 |
| 551 | ret 0 |
| 552 | end |
| 553 | v = add v 1 |
| 554 | k = sub k 1 |
| 555 | end |
| 556 | if gt k mul get w 0 4 |
| 557 | ec_copy w o ECCN |
| 558 | else |
| 559 | ec_load w o part hs v k |
| 560 | end |
| 561 | ret add il 2 |
| 562 | end |
| 563 | |
| 564 | note strict DER Ecdsa-Sig-Value at hs[h..]: SEQUENCE (short-form length; every valid signature |
| 565 | note on these curves is < 128 bytes, so a long form is never minimal) of two INTEGERs, and no |
| 566 | note byte after it. |
| 567 | fn ec_der w edit ulong ECWN hs view uchar * h ulong to ulong |
| 568 | k = len hs |
| 569 | if lt sub k h 2 |
| 570 | ret 2 |
| 571 | end |
| 572 | if ne get hs h 0x30 |
| 573 | ret 2 |
| 574 | end |
| 575 | sl = ulong get hs add h 1 |
| 576 | if ge sl 128 |
| 577 | ret 2 |
| 578 | end |
| 579 | if ne add add h 2 sl k |
| 580 | ret 2 |
| 581 | end |
| 582 | c = ec_int w ECSR hs add h 2 k |
| 583 | if eq c 0 |
| 584 | ret 2 |
| 585 | end |
| 586 | d = ec_int w ECSS hs add add h 2 c k |
| 587 | if eq d 0 |
| 588 | ret 2 |
| 589 | end |
| 590 | if ne add add add h 2 c d k |
| 591 | ret 2 |
| 592 | end |
| 593 | ret 0 |
| 594 | end |
| 595 | |
| 596 | note raw r || s at hs[h..], each 4L bytes, nothing after |
| 597 | fn ec_raw w edit ulong ECWN hs view uchar * h ulong to ulong |
| 598 | nb = mul get w 0 4 |
| 599 | if ne sub len hs h mul nb 2 |
| 600 | ret 2 |
| 601 | end |
| 602 | ec_load w ECSR part hs h nb |
| 603 | ec_load w ECSS part hs add h nb nb |
| 604 | ret 0 |
| 605 | end |
| 606 | |
| 607 | note ---- P-256 specialised field and group arithmetic (module code P2: functions p2_*, consts P2*), |
| 608 | note used by ec_core for P-256 verification and by ecdh.om for P-256 ECDH. Pure: no I/O, no randomness. |
| 609 | note |
| 610 | note API (all values in the workspace w, P2WN ulong cells, 32-bit limbs little-endian, 8 per element): |
| 611 | note p2_init w load p, b and G; call once per fresh workspace |
| 612 | note p2_smul w P2O = k*P for the secret scalar k at P2K (k < 2^256) and the affine point |
| 613 | note P at P2P (X, Y); constant time; returns 1 when the result is finite, else 0 |
| 614 | note p2_smul2 w P2O = u1*G + u2*Q for the public scalars u1 at P2K, u2 at P2U and the |
| 615 | note affine point Q at P2Q; not constant time (public data only); returns 1 when |
| 616 | note the result is finite, else 0 |
| 617 | note p2_wipe w overwrite the whole workspace with 0 |
| 618 | note The output P2O is the affine (x, y), plain (not Montgomery) and fully reduced, both 0 when the |
| 619 | note result is the point at infinity. Inputs must be on the curve with coordinates < p (the callers |
| 620 | note validate public keys in ec.om, ec_pub); scalars are not reduced here. |
| 621 | note |
| 622 | note Field: p = 2^256 - 2^224 + 2^192 + 2^96 - 1. Elements are 8 limbs < 2^32, value in [0, p). |
| 623 | note Multiplication: the 16-limb product by operand scanning, each 64-bit partial product split into |
| 624 | note its low and high 32-bit halves accumulated in their own columns (a column takes at most 16 halves, |
| 625 | note < 2^36), one carry pass to 32-bit words c0..c15, then the fast reduction of FIPS 186-4 Appendix |
| 626 | note D.2.3 (Solinas): with c = (c15, ..., c0), |
| 627 | note r = T + 2 S1 + 2 S2 + S3 + S4 - D1 - D2 - D3 - D4 (mod p), per 32-bit word j: |
| 628 | note w0 = c0 + c8 + c9 - c11 - c12 - c13 - c14 w4 = c4 + 2 c12 + 2 c13 + c14 - c9 - c10 |
| 629 | note w1 = c1 + c9 + c10 - c12 - c13 - c14 - c15 w5 = c5 + 2 c13 + 2 c14 + c15 - c10 - c11 |
| 630 | note w2 = c2 + c10 + c11 - c13 - c14 - c15 w6 = c6 + c13 + 3 c14 + 2 c15 - c8 - c9 |
| 631 | note w3 = c3 + 2 c11 + 2 c12 + c13 - c8 - c9 - c15 w7 = c7 + c8 + 3 c15 - c10 - c11 - c12 - c13 |
| 632 | note The words are formed in wrapping ulong arithmetic (two's complement); |w_j| < 2^35, so a signed |
| 633 | note carry (arithmetic shift right by 32 through bitcast long) is exact. The carry f out of word 7 is |
| 634 | note in [-4, 6] and folds back as f 2^256 = f (2^224 - 2^192 - 2^96 + 1) mod p (+f at word 0, -f at |
| 635 | note words 3 and 6, +f at word 7). Two carry-and-fold rounds leave a value in [0, 2^256) (the second |
| 636 | note carry is -1, 0 or 1 and its fold cannot carry again); 2^256 < 2p, so one masked subtraction of p |
| 637 | note gives [0, p). Addition and subtraction: carry or borrow chains with a masked correction by p. |
| 638 | note Inversion: Fermat a^(p-2) by a fixed addition chain (255 squarings, 12 multiplications). |
| 639 | note Checked with the Wolfram Language evaluator (2026-10-26, queries in test/ec/wolfram.txt): the |
| 640 | note Solinas word identity, the fold identity, 2^256 < 2p, the inversion chain exponent. |
| 641 | note |
| 642 | note Group: projective (X : Y : Z), the point at infinity is (0 : 1 : 0). Addition and doubling use |
| 643 | note the complete formula for a = -3 of Renes, Costello and Batina, "Complete addition formulas for |
| 644 | note prime order elliptic curves" (EUROCRYPT 2016), Algorithm 4 (also used by ecdh.om): no exceptional |
| 645 | note case, so no branch. Scalar multiplication: fixed 4-bit window, the table 0P .. 15P (0P the point |
| 646 | note at infinity), 63 rounds of 4 doublings and one addition after the top window; every table read |
| 647 | note for a secret scalar goes through p2_sel, which reads all 16 entries and keeps one by an AND mask |
| 648 | note computed without a branch ((j xor v) - 1) >> 63. Window digits come from fixed bit positions. |
| 649 | note |
| 650 | note Constant time (p2_smul): no branch, loop count or memory index depends on the scalar, the point |
| 651 | note or any intermediate value; all carries are shifts. The verification path p2_smul2 picks table |
| 652 | note entries by direct index (public scalars only). Caveats as in ecdh.om: omc emits plain AArch64 |
| 653 | note (MUL, ADD, SUB, EOR, AND, ORR, LSL/LSR/ASR); PSTATE.DIT is not set by this unit; register and |
| 654 | note spill contents are not scrubbed. Local scratch arrays holding limbs are zeroed before return and |
| 655 | note p2_wipe clears the workspace. |
| 656 | |
| 657 | note workspace layout (ulong offsets): two 16-entry point tables, points, field temporaries, constants |
| 658 | const P2WN ulong 1072 |
| 659 | const P2TA ulong 0 |
| 660 | const P2TB ulong 384 |
| 661 | const P2R ulong 768 |
| 662 | const P2S ulong 792 |
| 663 | const P2P ulong 816 |
| 664 | const P2Q ulong 840 |
| 665 | const P2T0 ulong 864 |
| 666 | const P2T1 ulong 872 |
| 667 | const P2T2 ulong 880 |
| 668 | const P2T3 ulong 888 |
| 669 | const P2T4 ulong 896 |
| 670 | const P2X3 ulong 904 |
| 671 | const P2Y3 ulong 912 |
| 672 | const P2Z3 ulong 920 |
| 673 | const P2I0 ulong 928 |
| 674 | const P2I1 ulong 936 |
| 675 | const P2I2 ulong 944 |
| 676 | const P2I3 ulong 952 |
| 677 | const P2I4 ulong 960 |
| 678 | const P2I5 ulong 968 |
| 679 | const P2K ulong 976 |
| 680 | const P2U ulong 984 |
| 681 | const P2PM ulong 992 |
| 682 | const P2B ulong 1000 |
| 683 | const P2GX ulong 1008 |
| 684 | const P2GY ulong 1016 |
| 685 | const P2O ulong 1024 |
| 686 | const P2ZI ulong 1040 |
| 687 | const P2M32 ulong 0xffffffff |
| 688 | |
| 689 | note p, b, G (FIPS 186-4 D.1.2.3, the values ec_p256 in ec.om loads), limbs little-endian |
| 690 | fn p2_init w edit ulong P2WN to void |
| 691 | note p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff |
| 692 | put w 992 0xffffffff |
| 693 | put w 993 0xffffffff |
| 694 | put w 994 0xffffffff |
| 695 | put w 995 0x0 |
| 696 | put w 996 0x0 |
| 697 | put w 997 0x0 |
| 698 | put w 998 0x1 |
| 699 | put w 999 0xffffffff |
| 700 | note b = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b |
| 701 | put w 1000 0x27d2604b |
| 702 | put w 1001 0x3bce3c3e |
| 703 | put w 1002 0xcc53b0f6 |
| 704 | put w 1003 0x651d06b0 |
| 705 | put w 1004 0x769886bc |
| 706 | put w 1005 0xb3ebbd55 |
| 707 | put w 1006 0xaa3a93e7 |
| 708 | put w 1007 0x5ac635d8 |
| 709 | note gx = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296 |
| 710 | put w 1008 0xd898c296 |
| 711 | put w 1009 0xf4a13945 |
| 712 | put w 1010 0x2deb33a0 |
| 713 | put w 1011 0x77037d81 |
| 714 | put w 1012 0x63a440f2 |
| 715 | put w 1013 0xf8bce6e5 |
| 716 | put w 1014 0xe12c4247 |
| 717 | put w 1015 0x6b17d1f2 |
| 718 | note gy = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5 |
| 719 | put w 1016 0x37bf51f5 |
| 720 | put w 1017 0xcbb64068 |
| 721 | put w 1018 0x6b315ece |
| 722 | put w 1019 0x2bce3357 |
| 723 | put w 1020 0x7c0f9e16 |
| 724 | put w 1021 0x8ee7eb4a |
| 725 | put w 1022 0xfe1a7f9b |
| 726 | put w 1023 0x4fe342e2 |
| 727 | end |
| 728 | |
| 729 | note r (8 words, each < 2^32 + 7 as wrapping two's complement; value in (-2^259, 2^259)) to the |
| 730 | note words in [0, 2^32) and the signed carry out of word 7 (arithmetic shift, no branch) |
| 731 | fn p2_carry r edit ulong 8 to ulong inline |
| 732 | cr = ulong 0 |
| 733 | for k ulong 8 unroll |
| 734 | z = add get r k cr |
| 735 | put r k and z P2M32 |
| 736 | cr = bitcast ulong shr bitcast long z 32 |
| 737 | end |
| 738 | ret cr |
| 739 | end |
| 740 | |
| 741 | note fold the signed carry f: f 2^256 = f (2^224 - 2^192 - 2^96 + 1) mod p |
| 742 | fn p2_fold r edit ulong 8 f ulong to void inline |
| 743 | put r 0 add get r 0 f |
| 744 | put r 3 sub get r 3 f |
| 745 | put r 6 sub get r 6 f |
| 746 | put r 7 add get r 7 f |
| 747 | end |
| 748 | |
| 749 | note w[o] = r mod p for r in [0, 2^256) (2^256 < 2p): one masked subtraction of p, no branch; |
| 750 | note r and the scratch are zeroed |
| 751 | fn p2_csub w edit ulong P2WN o ulong r edit ulong 8 to void |
| 752 | d = new ulong 8 0 |
| 753 | br = ulong 0 |
| 754 | for k ulong 8 unroll |
| 755 | z = sub sub get r k get w add P2PM k br |
| 756 | put d k and z P2M32 |
| 757 | br = shr z 63 |
| 758 | end |
| 759 | mk = sub br 1 |
| 760 | for k ulong 8 unroll |
| 761 | x = get r k |
| 762 | put w add o k xor x and mk xor x get d k |
| 763 | put d k 0 |
| 764 | put r k 0 |
| 765 | end |
| 766 | end |
| 767 | |
| 768 | note w[o] = the 16 product columns t (t[k] < 2^37) mod p: carry pass, Solinas words, two |
| 769 | note carry-and-fold rounds, the masked subtraction. t and the scratch are zeroed. |
| 770 | fn p2_red w edit ulong P2WN o ulong t edit ulong 16 to void |
| 771 | cr = ulong 0 |
| 772 | for k ulong 16 unroll |
| 773 | z = add get t k cr |
| 774 | put t k and z P2M32 |
| 775 | cr = shr z 32 |
| 776 | end |
| 777 | r = new ulong 8 0 |
| 778 | s = add add get t 0 get t 8 get t 9 |
| 779 | put r 0 sub sub sub sub s get t 11 get t 12 get t 13 get t 14 |
| 780 | s = add add get t 1 get t 9 get t 10 |
| 781 | put r 1 sub sub sub sub s get t 12 get t 13 get t 14 get t 15 |
| 782 | s = add add get t 2 get t 10 get t 11 |
| 783 | put r 2 sub sub sub s get t 13 get t 14 get t 15 |
| 784 | s = add add get t 3 shl add get t 11 get t 12 1 get t 13 |
| 785 | put r 3 sub sub sub s get t 8 get t 9 get t 15 |
| 786 | s = add add get t 4 shl add get t 12 get t 13 1 get t 14 |
| 787 | put r 4 sub sub s get t 9 get t 10 |
| 788 | s = add add get t 5 shl add get t 13 get t 14 1 get t 15 |
| 789 | put r 5 sub sub s get t 10 get t 11 |
| 790 | s = add add add get t 6 get t 13 mul get t 14 3 shl get t 15 1 |
| 791 | put r 6 sub sub s get t 8 get t 9 |
| 792 | s = add add get t 7 get t 8 mul get t 15 3 |
| 793 | put r 7 sub sub sub sub s get t 10 get t 11 get t 12 get t 13 |
| 794 | for k ulong 16 unroll |
| 795 | put t k 0 |
| 796 | end |
| 797 | f = p2_carry r |
| 798 | p2_fold r f |
| 799 | f = p2_carry r |
| 800 | p2_fold r f |
| 801 | f = p2_carry r |
| 802 | p2_csub w o r |
| 803 | end |
| 804 | |
| 805 | note w[o] = w[a] * w[b] mod p (o may equal a or b) |
| 806 | fn p2_mul w edit ulong P2WN o ulong a ulong b ulong to void |
| 807 | x = new ulong 8 0 |
| 808 | y = new ulong 8 0 |
| 809 | t = new ulong 16 0 |
| 810 | for i ulong 8 unroll |
| 811 | put x i get w add a i |
| 812 | put y i get w add b i |
| 813 | end |
| 814 | for i ulong 8 unroll |
| 815 | xi = get x i |
| 816 | for j ulong 8 unroll |
| 817 | p = mul xi get y j |
| 818 | put t add i j add get t add i j and p P2M32 |
| 819 | put t add add i j 1 add get t add add i j 1 shr p 32 |
| 820 | end |
| 821 | end |
| 822 | for i ulong 8 unroll |
| 823 | put x i 0 |
| 824 | put y i 0 |
| 825 | end |
| 826 | p2_red w o t |
| 827 | end |
| 828 | |
| 829 | note w[o] = w[a]^2 mod p: the 28 cross products once and doubled, the 8 squares (o may equal a) |
| 830 | fn p2_sqr w edit ulong P2WN o ulong a ulong to void |
| 831 | x = new ulong 8 0 |
| 832 | t = new ulong 16 0 |
| 833 | for i ulong 8 unroll |
| 834 | put x i get w add a i |
| 835 | end |
| 836 | for i ulong 8 unroll |
| 837 | xi = get x i |
| 838 | for j ulong 8 unroll |
| 839 | if lt i j |
| 840 | p = mul xi get x j |
| 841 | put t add i j add get t add i j and p P2M32 |
| 842 | put t add add i j 1 add get t add add i j 1 shr p 32 |
| 843 | end |
| 844 | end |
| 845 | end |
| 846 | for k ulong 16 unroll |
| 847 | put t k shl get t k 1 |
| 848 | end |
| 849 | for i ulong 8 unroll |
| 850 | xi = get x i |
| 851 | p = mul xi xi |
| 852 | put t add i i add get t add i i and p P2M32 |
| 853 | put t add add i i 1 add get t add add i i 1 shr p 32 |
| 854 | put x i 0 |
| 855 | end |
| 856 | p2_red w o t |
| 857 | end |
| 858 | |
| 859 | note w[o] = w[a] + w[b] mod p (a, b < p; o may equal a or b) |
| 860 | fn p2_add w edit ulong P2WN o ulong a ulong b ulong to void |
| 861 | s = new ulong 8 0 |
| 862 | c = ulong 0 |
| 863 | for k ulong 8 unroll |
| 864 | z = add add get w add a k get w add b k c |
| 865 | put s k and z P2M32 |
| 866 | c = shr z 32 |
| 867 | end |
| 868 | br = ulong 0 |
| 869 | d = new ulong 8 0 |
| 870 | for k ulong 8 unroll |
| 871 | z = sub sub get s k get w add P2PM k br |
| 872 | put d k and z P2M32 |
| 873 | br = shr z 63 |
| 874 | end |
| 875 | z = sub c br |
| 876 | br = shr z 63 |
| 877 | mk = sub br 1 |
| 878 | for k ulong 8 unroll |
| 879 | x = get s k |
| 880 | put w add o k xor x and mk xor x get d k |
| 881 | put s k 0 |
| 882 | put d k 0 |
| 883 | end |
| 884 | end |
| 885 | |
| 886 | note w[o] = w[a] - w[b] mod p (a, b < p; o may equal a or b): borrow chain, then + (p AND mask) |
| 887 | fn p2_sub w edit ulong P2WN o ulong a ulong b ulong to void |
| 888 | d = new ulong 8 0 |
| 889 | br = ulong 0 |
| 890 | for k ulong 8 unroll |
| 891 | z = sub sub get w add a k get w add b k br |
| 892 | put d k and z P2M32 |
| 893 | br = shr z 63 |
| 894 | end |
| 895 | mk = sub 0 br |
| 896 | c = ulong 0 |
| 897 | for k ulong 8 unroll |
| 898 | z = add add get d k and mk get w add P2PM k c |
| 899 | put w add o k and z P2M32 |
| 900 | c = shr z 32 |
| 901 | put d k 0 |
| 902 | end |
| 903 | end |
| 904 | |
| 905 | fn p2_copy w edit ulong P2WN o ulong a ulong to void |
| 906 | for k ulong 8 unroll |
| 907 | put w add o k get w add a k |
| 908 | end |
| 909 | end |
| 910 | |
| 911 | note w[o] = w[a]^(2^n), n >= 1 (n public) |
| 912 | fn p2_sqn w edit ulong P2WN o ulong a ulong n ulong to void |
| 913 | p2_sqr w o a |
| 914 | for i ulong sub n 1 |
| 915 | p2_sqr w o o |
| 916 | end |
| 917 | end |
| 918 | |
| 919 | note w[o] = w[a]^(p - 2) = 1 / w[a] (0 for 0). p - 2 = 0xffffffff 00000001 0^96 ffffffff ffffffff |
| 920 | note fffffffd: x2 = a^(2^2-1), x3, x6, x12, x15, x30, x32 (xk = a^(2^k - 1)), then |
| 921 | note (((x32 2^32 + 1) 2^128 + x32) 2^32 + x32) 2^30 + x30) 2^2 + 1 in the exponent. Uses P2I0..P2I5; |
| 922 | note o and a must be other slots. |
| 923 | fn p2_inv w edit ulong P2WN o ulong a ulong to void |
| 924 | p2_sqr w P2I0 a |
| 925 | p2_mul w P2I0 P2I0 a |
| 926 | p2_sqr w P2I1 P2I0 |
| 927 | p2_mul w P2I1 P2I1 a |
| 928 | p2_sqn w P2I2 P2I1 3 |
| 929 | p2_mul w P2I2 P2I2 P2I1 |
| 930 | p2_sqn w P2I3 P2I2 6 |
| 931 | p2_mul w P2I3 P2I3 P2I2 |
| 932 | p2_sqn w P2I3 P2I3 3 |
| 933 | p2_mul w P2I3 P2I3 P2I1 |
| 934 | p2_sqn w P2I4 P2I3 15 |
| 935 | p2_mul w P2I4 P2I4 P2I3 |
| 936 | p2_sqn w P2I5 P2I4 2 |
| 937 | p2_mul w P2I5 P2I5 P2I0 |
| 938 | p2_sqn w P2I3 P2I5 32 |
| 939 | p2_mul w P2I3 P2I3 a |
| 940 | p2_sqn w P2I3 P2I3 128 |
| 941 | p2_mul w P2I3 P2I3 P2I5 |
| 942 | p2_sqn w P2I3 P2I3 32 |
| 943 | p2_mul w P2I3 P2I3 P2I5 |
| 944 | p2_sqn w P2I3 P2I3 30 |
| 945 | p2_mul w P2I3 P2I3 P2I4 |
| 946 | p2_sqn w P2I3 P2I3 2 |
| 947 | p2_mul w o P2I3 a |
| 948 | end |
| 949 | |
| 950 | note o = a + b, complete projective addition for a = -3 (RCB 2016 Algorithm 4, 12M + 2 mb + 29a, |
| 951 | note the sequence of ecdh.om ecd_padd). a == b gives the doubling; o may alias a or b (the result is |
| 952 | note formed in P2X3, P2Y3, P2Z3 after the last read of the inputs). |
| 953 | fn p2_padd w edit ulong P2WN o ulong a ulong b ulong to void |
| 954 | p2_mul w P2T0 a b |
| 955 | p2_mul w P2T1 add a 8 add b 8 |
| 956 | p2_mul w P2T2 add a 16 add b 16 |
| 957 | p2_add w P2T3 a add a 8 |
| 958 | p2_add w P2T4 b add b 8 |
| 959 | p2_mul w P2T3 P2T3 P2T4 |
| 960 | p2_add w P2T4 P2T0 P2T1 |
| 961 | p2_sub w P2T3 P2T3 P2T4 |
| 962 | p2_add w P2T4 add a 8 add a 16 |
| 963 | p2_add w P2X3 add b 8 add b 16 |
| 964 | p2_mul w P2T4 P2T4 P2X3 |
| 965 | p2_add w P2X3 P2T1 P2T2 |
| 966 | p2_sub w P2T4 P2T4 P2X3 |
| 967 | p2_add w P2X3 a add a 16 |
| 968 | p2_add w P2Y3 b add b 16 |
| 969 | p2_mul w P2X3 P2X3 P2Y3 |
| 970 | p2_add w P2Y3 P2T0 P2T2 |
| 971 | p2_sub w P2Y3 P2X3 P2Y3 |
| 972 | p2_mul w P2Z3 P2B P2T2 |
| 973 | p2_sub w P2X3 P2Y3 P2Z3 |
| 974 | p2_add w P2Z3 P2X3 P2X3 |
| 975 | p2_add w P2X3 P2X3 P2Z3 |
| 976 | p2_sub w P2Z3 P2T1 P2X3 |
| 977 | p2_add w P2X3 P2T1 P2X3 |
| 978 | p2_mul w P2Y3 P2B P2Y3 |
| 979 | p2_add w P2T1 P2T2 P2T2 |
| 980 | p2_add w P2T2 P2T1 P2T2 |
| 981 | p2_sub w P2Y3 P2Y3 P2T2 |
| 982 | p2_sub w P2Y3 P2Y3 P2T0 |
| 983 | p2_add w P2T1 P2Y3 P2Y3 |
| 984 | p2_add w P2Y3 P2T1 P2Y3 |
| 985 | p2_add w P2T1 P2T0 P2T0 |
| 986 | p2_add w P2T0 P2T1 P2T0 |
| 987 | p2_sub w P2T0 P2T0 P2T2 |
| 988 | p2_mul w P2T1 P2T4 P2Y3 |
| 989 | p2_mul w P2T2 P2T0 P2Y3 |
| 990 | p2_mul w P2Y3 P2X3 P2Z3 |
| 991 | p2_add w P2Y3 P2Y3 P2T2 |
| 992 | p2_mul w P2X3 P2T3 P2X3 |
| 993 | p2_sub w P2X3 P2X3 P2T1 |
| 994 | p2_mul w P2Z3 P2T4 P2Z3 |
| 995 | p2_mul w P2T1 P2T3 P2T0 |
| 996 | p2_add w P2Z3 P2Z3 P2T1 |
| 997 | p2_copy w o P2X3 |
| 998 | p2_copy w add o 8 P2Y3 |
| 999 | p2_copy w add o 16 P2Z3 |
| 1000 | end |
| 1001 | |
| 1002 | note copy the 24-cell point at a to o |
| 1003 | fn p2_pcopy w edit ulong P2WN o ulong a ulong to void |
| 1004 | for k ulong 24 unroll |
| 1005 | put w add o k get w add a k |
| 1006 | end |
| 1007 | end |
| 1008 | |
| 1009 | note the table t[j] = j P (j = 0..15) for the projective point at p; t[0] = (0 : 1 : 0) |
| 1010 | fn p2_tab w edit ulong P2WN t ulong p ulong to void |
| 1011 | for k ulong 24 unroll |
| 1012 | put w add t k 0 |
| 1013 | end |
| 1014 | put w add t 8 1 |
| 1015 | p2_pcopy w add t 24 p |
| 1016 | for j ulong 14 |
| 1017 | p2_padd w add t mul add j 2 24 add t mul add j 1 24 p |
| 1018 | end |
| 1019 | end |
| 1020 | |
| 1021 | note o = t[v] for the secret digit v in 0..15: all 16 entries are read, each AND a mask that is all |
| 1022 | note ones only for j = v (m = 0 - (((j xor v) - 1) >> 63)), and ORed together; no branch, no index |
| 1023 | note from v |
| 1024 | fn p2_sel w edit ulong P2WN o ulong t ulong v ulong to void |
| 1025 | s = new ulong 24 0 |
| 1026 | for j ulong 16 |
| 1027 | m = sub 0 shr sub xor j v 1 63 |
| 1028 | e = add t mul j 24 |
| 1029 | for k ulong 24 unroll |
| 1030 | put s k or get s k and m get w add e k |
| 1031 | end |
| 1032 | end |
| 1033 | for k ulong 24 unroll |
| 1034 | put w add o k get s k |
| 1035 | put s k 0 |
| 1036 | end |
| 1037 | end |
| 1038 | |
| 1039 | note digit i (0..63, public) of the 4-bit windows of the scalar at k: bits 4i .. 4i + 3 |
| 1040 | fn p2_dig w edit ulong P2WN k ulong i ulong to ulong |
| 1041 | ret and shr get w add k shr i 3 mul and i 7 4 15 |
| 1042 | end |
| 1043 | |
| 1044 | note the affine point at a (X, Y; 16 cells) to projective (X : Y : 1) at o |
| 1045 | fn p2_proj w edit ulong P2WN o ulong a ulong to void |
| 1046 | for k ulong 16 unroll |
| 1047 | put w add o k get w add a k |
| 1048 | end |
| 1049 | for k ulong 8 unroll |
| 1050 | put w add add o 16 k 0 |
| 1051 | end |
| 1052 | put w add o 16 1 |
| 1053 | end |
| 1054 | |
| 1055 | note P2O = P2R in affine coordinates; returns 1 when Z != 0, else 0 (and P2O = (0, 0) since |
| 1056 | note Z^(p-2) = 0); the Z test is an OR of the limbs and ((z | -z) >> 63), no branch |
| 1057 | fn p2_aff w edit ulong P2WN to ulong |
| 1058 | z = ulong 0 |
| 1059 | for k ulong 8 unroll |
| 1060 | z = or z get w add add P2R 16 k |
| 1061 | end |
| 1062 | p2_inv w P2ZI add P2R 16 |
| 1063 | p2_mul w P2O P2R P2ZI |
| 1064 | p2_mul w add P2O 8 add P2R 8 P2ZI |
| 1065 | ret shr or z sub 0 z 63 |
| 1066 | end |
| 1067 | |
| 1068 | fn p2_smul w edit ulong P2WN to ulong |
| 1069 | p2_proj w P2Q P2P |
| 1070 | p2_tab w P2TA P2Q |
| 1071 | p2_sel w P2R P2TA p2_dig w P2K 63 |
| 1072 | for n ulong 63 |
| 1073 | i = sub 62 n |
| 1074 | p2_padd w P2R P2R P2R |
| 1075 | p2_padd w P2R P2R P2R |
| 1076 | p2_padd w P2R P2R P2R |
| 1077 | p2_padd w P2R P2R P2R |
| 1078 | p2_sel w P2S P2TA p2_dig w P2K i |
| 1079 | p2_padd w P2R P2R P2S |
| 1080 | end |
| 1081 | ret p2_aff w |
| 1082 | end |
| 1083 | |
| 1084 | fn p2_smul2 w edit ulong P2WN to ulong |
| 1085 | p2_proj w P2S P2GX |
| 1086 | p2_tab w P2TA P2S |
| 1087 | p2_proj w P2S P2Q |
| 1088 | p2_tab w P2TB P2S |
| 1089 | p2_pcopy w P2R add P2TA mul p2_dig w P2K 63 24 |
| 1090 | p2_padd w P2R P2R add P2TB mul p2_dig w P2U 63 24 |
| 1091 | for n ulong 63 |
| 1092 | i = sub 62 n |
| 1093 | p2_padd w P2R P2R P2R |
| 1094 | p2_padd w P2R P2R P2R |
| 1095 | p2_padd w P2R P2R P2R |
| 1096 | p2_padd w P2R P2R P2R |
| 1097 | p2_padd w P2R P2R add P2TA mul p2_dig w P2K i 24 |
| 1098 | p2_padd w P2R P2R add P2TB mul p2_dig w P2U i 24 |
| 1099 | end |
| 1100 | ret p2_aff w |
| 1101 | end |
| 1102 | |
| 1103 | fn p2_wipe w edit ulong P2WN to void |
| 1104 | for j ulong P2WN |
| 1105 | put w j 0 |
| 1106 | end |
| 1107 | end |
| 1108 | |
| 1109 | note P-256: ECA9 = x(u1 G + u2 Q) plain, by p2_smul2 in its own workspace; returns 1 when the |
| 1110 | note point is finite, else 0. Q is taken out of the Montgomery domain (times plain 1). |
| 1111 | fn ec_smul256 w edit ulong ECWN to ulong |
| 1112 | v = new ulong P2WN 0 |
| 1113 | p2_init v |
| 1114 | ec_mul w ECCP ECA0 ECQ ECON |
| 1115 | ec_mul w ECCP ECA1 add ECQ 12 ECON |
| 1116 | for j ulong 8 unroll |
| 1117 | put v add P2K j get w add ECU1 j |
| 1118 | put v add P2U j get w add ECU2 j |
| 1119 | put v add P2Q j get w add ECA0 j |
| 1120 | put v add add P2Q 8 j get w add ECA1 j |
| 1121 | end |
| 1122 | f = p2_smul2 v |
| 1123 | for j ulong 8 unroll |
| 1124 | put w add ECA9 j get v add P2O j |
| 1125 | end |
| 1126 | ret f |
| 1127 | end |
| 1128 | |
| 1129 | note r, s in [1, n-1], then u1 = e s^-1, u2 = r s^-1 mod n, R = u1 G + u2 Q, accept iff R is |
| 1130 | note finite and x(R) mod n == r. digest = hs[0..hlen). |
| 1131 | fn ec_core w edit ulong ECWN hs view uchar * hlen ulong to ulong |
| 1132 | if eq ec_cmp w ECSR ECZ 0 |
| 1133 | ret 4 |
| 1134 | end |
| 1135 | if ne ec_cmp w ECSR ECCN 1 |
| 1136 | ret 4 |
| 1137 | end |
| 1138 | if eq ec_cmp w ECSS ECZ 0 |
| 1139 | ret 4 |
| 1140 | end |
| 1141 | if ne ec_cmp w ECSS ECCN 1 |
| 1142 | ret 4 |
| 1143 | end |
| 1144 | k = mul get w 0 4 |
| 1145 | if lt hlen k |
| 1146 | k = hlen |
| 1147 | end |
| 1148 | ec_load w ECE part hs 0 k |
| 1149 | ec_add w ECCN ECE ECE ECZ |
| 1150 | ec_mul w ECCN ECSI ECSS add ECCN 28 |
| 1151 | ec_inv w ECCN ECSI ECSI |
| 1152 | ec_mul w ECCN ECU1 ECE ECSI |
| 1153 | ec_mul w ECCN ECU2 ECSR ECSI |
| 1154 | if eq get w 0 8 |
| 1155 | if eq ec_smul256 w 0 |
| 1156 | ret 1 |
| 1157 | end |
| 1158 | else |
| 1159 | ec_smul w |
| 1160 | if eq ec_cmp w add ECR 24 ECZ 0 |
| 1161 | ret 1 |
| 1162 | end |
| 1163 | ec_inv w ECCP ECA8 add ECR 24 |
| 1164 | ec_mul w ECCP ECA9 ECA8 ECA8 |
| 1165 | ec_mul w ECCP ECA9 ECA9 ECR |
| 1166 | ec_mul w ECCP ECA9 ECA9 ECON |
| 1167 | end |
| 1168 | ec_add w ECCN ECA9 ECA9 ECZ |
| 1169 | if eq ec_cmp w ECA9 ECSR 0 |
| 1170 | ret 0 |
| 1171 | end |
| 1172 | ret 1 |
| 1173 | end |
| 1174 | |
| 1175 | fn ecdsa_verify crv ulong pub view uchar * hs view uchar * hlen ulong to ulong |
| 1176 | w = new ulong ECWN 0 |
| 1177 | rc = ec_setup w crv |
| 1178 | if ne rc 0 |
| 1179 | ret rc |
| 1180 | end |
| 1181 | rc = ec_pub w pub |
| 1182 | if ne rc 0 |
| 1183 | ret rc |
| 1184 | end |
| 1185 | if gt hlen len hs |
| 1186 | ret 2 |
| 1187 | end |
| 1188 | rc = ec_der w hs hlen |
| 1189 | if ne rc 0 |
| 1190 | ret rc |
| 1191 | end |
| 1192 | ret ec_core w hs hlen |
| 1193 | end |
| 1194 | |
| 1195 | fn ecdsa_verify_raw crv ulong pub view uchar * hs view uchar * hlen ulong to ulong |
| 1196 | w = new ulong ECWN 0 |
| 1197 | rc = ec_setup w crv |
| 1198 | if ne rc 0 |
| 1199 | ret rc |
| 1200 | end |
| 1201 | rc = ec_pub w pub |
| 1202 | if ne rc 0 |
| 1203 | ret rc |
| 1204 | end |
| 1205 | if gt hlen len hs |
| 1206 | ret 2 |
| 1207 | end |
| 1208 | rc = ec_raw w hs hlen |
| 1209 | if ne rc 0 |
| 1210 | ret rc |
| 1211 | end |
| 1212 | ret ec_core w hs hlen |
| 1213 | end |