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/spark-rsi-mv/blob/main/om/p256/p256.om # spark-rsi-mv Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `spark-rsi-mv / om / p256 / p256.om` 682 lines ยท 18073 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note p256.om: ECDSA over NIST P-256 with SHA-256, signing with the RFC 6979 deterministic nonce and 3 note verification, written to match the spark-rsi judge signer byte for byte (RustCrypto p256 0.14.0, 4 note ecdsa 0.17.0, rfc6979 0.6.0): the signature is over SHA-256(message), r||s 64 bytes big endian, 5 note s is NOT normalized to low-S (p256 sets NORMALIZE_S = false) and verification accepts both s 6 note and n - s. Library unit: functions and constants only, no main, no globals. It needs the sibling 7 note unit om/p256/sha256.om (the omega-mv om/sha256/sha256.om bytes) concatenated before this file. 8 note This is a parity-proven library, not a signer deployment: it holds no key and reads no file. 9 note 10 note Numbers: 256-bit values are 8 limbs of 32 bits, least significant first, one limb per ulong cell 11 note of the workspace w (ulong 1024). Arithmetic mod p (field) and mod n (group order) is Montgomery 12 note multiplication (CIOS, R = 2^256): a limb product plus two limbs is at most 2^64 - 1, so no ulong 13 note ever wraps where a carry is meant. Every add, sub and mul result is fully reduced (below the 14 note modulus); inputs must be below the modulus. 15 note 16 note Side channels (see README): field and scalar add, sub, mul and the final subtraction are branch 17 note free and index only by public loop counters; the secret-scalar multiply is double-and-add-always 18 note over all 256 bits with a masked select and complete (Renes-Costello-Batina 2015, Algorithm 4) 19 note additions, so no secret-dependent branch or index occurs in it. Not constant time: the RFC 6979 20 note retry (k >= n, r = 0, s = 0) branches on secret-derived values, as the Rust crate does; the 21 note compiler gives no constant-time guarantee (omc instruction selection is not audited for it); 22 note the key and nonce stay in caller memory with no zeroing. 23 note 24 note Workspace layout (cell offsets into w). A modulus block of 48 cells at PMP (p) and PMN (n): 25 note +0 the modulus, +8 n0' = -m^-1 mod 2^32, +16 R^2 mod m, +24 R mod m (Montgomery one), +32 m - 2. 26 note Points are projective (X : Y : Z), 24 cells, coordinates in the Montgomery domain mod p. 27 note Byte buffer b (uchar 4096): b[0..512) HMAC/SHA staging, PBK K, PBV V, PBX x, PBH h1, 1152 inner 28 note tag, PBT candidate k (RFC 6979 3.2), 2048..3072 message, 3072..3137 public key (0x04 || X || Y), 29 note 3200..3264 signature r||s, 3264..3296 private key d, 3296..3328 SHA-256 of the message. 30 31 const PMP ulong 0 32 const PMN ulong 48 33 const PONE1 ulong 96 34 const PBM ulong 104 35 const PGX ulong 120 36 const PW1 ulong 160 37 const PW2 ulong 168 38 const PXX ulong 176 39 const PYY ulong 184 40 const PZZ ulong 192 41 const PXY ulong 200 42 const PYZ ulong 208 43 const PXZ ulong 216 44 const PU ulong 224 45 const PV ulong 232 46 const PYM ulong 240 47 const PYP ulong 248 48 const PBX3 ulong 256 49 const PXX3 ulong 264 50 const PZZ3 ulong 272 51 const PRX ulong 280 52 const PACC ulong 304 53 const PTMP ulong 328 54 const PBASE ulong 352 55 const PZI ulong 376 56 const PD ulong 512 57 const PK ulong 520 58 const PZ ulong 528 59 const PR ulong 536 60 const PS ulong 544 61 const PU1 ulong 552 62 const PU2 ulong 560 63 const PT1 ulong 576 64 const PT2 ulong 584 65 const PT3 ulong 592 66 const PX ulong 608 67 const PY ulong 616 68 const PQ ulong 640 69 const PP1 ulong 664 70 const PP2 ulong 688 71 const PBK ulong 1024 72 const PBV ulong 1056 73 const PBX ulong 1088 74 const PBH ulong 1120 75 const PBI ulong 1152 76 const PBT ulong 1184 77 const PBMSG ulong 2048 78 const PBPUB ulong 3072 79 const PBSIG ulong 3200 80 const PBKEY ulong 3264 81 const PBDIG ulong 3296 82 83 fn p2cpy w edit ulong 1024 o ulong a ulong to void 84 for i ulong 8 85 put w add o i get w add a i 86 end 87 end 88 89 fn p2pcpy w edit ulong 1024 o ulong a ulong to void 90 for i ulong 24 91 put w add o i get w add a i 92 end 93 end 94 95 note o = the small number v 96 fn p2set w edit ulong 1024 o ulong v ulong to void 97 put w o v 98 for i ulong 7 99 put w add o add i 1 0 100 end 101 end 102 103 note o = t mod m for t = t[0..8) + t[8] 2^256 below 2m: subtract m, keep the difference when t[8] is 104 note set or the subtraction did not borrow; branch free (masked select) 105 fn p2red w edit ulong 1024 o ulong t view ulong 10 m ulong to void 106 u = new ulong 8 0 107 bw = ulong 0 108 x = ulong 0 109 for i ulong 8 110 x = sub sub get t i get w add m i bw 111 put u i and x `4294967295` 112 bw = shr x 63 113 end 114 sel = or get t 8 xor bw 1 115 mk = sub 0 sel 116 for j ulong 8 117 y = and get u j mk 118 put w add o j or y and get t j not mk 119 end 120 end 121 122 note t += a * bi over 8 limbs, carry into t[8], t[9] (one CIOS row) 123 fn p2mrow t edit ulong 10 w edit ulong 1024 a ulong bi ulong to void 124 c = ulong 0 125 x = ulong 0 126 for j ulong 8 127 x = add add get t j mul get w add a j bi c 128 put t j and x `4294967295` 129 c = shr x 32 130 end 131 x = add get t 8 c 132 put t 8 and x `4294967295` 133 put t 9 shr x 32 134 end 135 136 note t = (t + q m) / 2^32 with q = t[0] n0' mod 2^32 (one CIOS reduction step) 137 fn p2mred t edit ulong 10 w edit ulong 1024 m ulong to void 138 q = and mul get t 0 get w add m 8 `4294967295` 139 x = add get t 0 mul q get w m 140 c = shr x 32 141 j1 = ulong 0 142 for k ulong 7 143 j1 = add k 1 144 x = add add get t j1 mul q get w add m j1 c 145 put t k and x `4294967295` 146 c = shr x 32 147 end 148 x = add get t 8 c 149 put t 7 and x `4294967295` 150 put t 8 add get t 9 shr x 32 151 end 152 153 note o = a b R^-1 mod m (Montgomery product); o may be a or b 154 fn p2mul w edit ulong 1024 o ulong a ulong b ulong m ulong to void 155 t = new ulong 10 0 156 for i ulong 8 157 p2mrow t w a get w add b i 158 p2mred t w m 159 end 160 p2red w o t m 161 end 162 163 note o = a + b mod m; o may be a or b 164 fn p2add w edit ulong 1024 o ulong a ulong b ulong m ulong to void 165 t = new ulong 10 0 166 c = ulong 0 167 x = ulong 0 168 for i ulong 8 169 x = add add get w add a i get w add b i c 170 put t i and x `4294967295` 171 c = shr x 32 172 end 173 put t 8 c 174 p2red w o t m 175 end 176 177 note o = a - b mod m; o may be a or b (adds m under a mask when the subtraction borrows) 178 fn p2sub w edit ulong 1024 o ulong a ulong b ulong m ulong to void 179 t = new ulong 8 0 180 bw = ulong 0 181 x = ulong 0 182 for i ulong 8 183 x = sub sub get w add a i get w add b i bw 184 put t i and x `4294967295` 185 bw = shr x 63 186 end 187 mk = sub 0 bw 188 c = ulong 0 189 for j ulong 8 190 x = add add get t j and get w add m j mk c 191 put w add o j and x `4294967295` 192 c = shr x 32 193 end 194 end 195 196 note o = a mod m for a below 2m (one masked subtraction) 197 fn p2modr w edit ulong 1024 o ulong m ulong to void 198 t = new ulong 10 0 199 for i ulong 8 200 put t i get w add o i 201 end 202 p2red w o t m 203 end 204 205 note a < m as 256-bit integers 206 fn p2lt w edit ulong 1024 a ulong m ulong to bool 207 bw = ulong 0 208 x = ulong 0 209 for i ulong 8 210 x = sub sub get w add a i get w add m i bw 211 bw = shr x 63 212 end 213 ret eq bw 1 214 end 215 216 fn p2isz w edit ulong 1024 a ulong to bool 217 v = ulong 0 218 for i ulong 8 219 v = or v get w add a i 220 end 221 ret eq v 0 222 end 223 224 fn p2eqv w edit ulong 1024 a ulong b ulong to bool 225 v = ulong 0 226 for i ulong 8 227 v = or v xor get w add a i get w add b i 228 end 229 ret eq v 0 230 end 231 232 note 1 <= a < n 233 fn p2rng w edit ulong 1024 a ulong to bool 234 ret and not p2isz w a p2lt w a PMN 235 end 236 237 note o = a^e in the Montgomery domain mod m; the exponent e (plain limbs) is public: the branch 238 note follows its bits. o may be a. Uses PW1, PW2. 239 fn p2pow w edit ulong 1024 o ulong a ulong e ulong m ulong to void 240 p2cpy w PW2 a 241 p2cpy w PW1 add m 24 242 i = ulong 0 243 bit = ulong 0 244 for j ulong 256 245 i = sub 255 j 246 p2mul w PW1 PW1 PW1 m 247 bit = and shr get w add e shr i 5 and i 31 1 248 if eq bit 1 249 p2mul w PW1 PW1 PW2 m 250 end 251 end 252 p2cpy w o PW1 253 end 254 255 note o = a^-1 = a^(m-2) (Fermat, m prime; 0 gives 0) 256 fn p2inv w edit ulong 1024 o ulong a ulong m ulong to void 257 p2pow w o a add m 32 m 258 end 259 260 note into the Montgomery domain (a R) and out of it (a R^-1) 261 fn p2tom w edit ulong 1024 o ulong a ulong m ulong to void 262 p2mul w o a add m 16 m 263 end 264 265 fn p2frm w edit ulong 1024 o ulong a ulong m ulong to void 266 p2mul w o a PONE1 m 267 end 268 269 note a modulus block: n0' by Newton steps (x = m0 is right to 3 bits, five steps give 96), 270 note R^2 mod m by 512 modular doublings of 1, R mod m = mont(1, R^2), and m - 2 (m0 >= 2 here) 271 fn p2mod w edit ulong 1024 m ulong to void 272 m0 = get w m 273 x = m0 274 for i ulong 5 275 x = mul x sub 2 mul m0 x 276 end 277 put w add m 8 and sub 0 x `4294967295` 278 p2set w add m 16 1 279 for j ulong 512 280 p2add w add m 16 add m 16 add m 16 m 281 end 282 p2mul w add m 24 PONE1 add m 16 m 283 p2cpy w add m 32 m 284 put w add m 32 sub get w add m 32 2 285 end 286 287 note curve constants (SEC 2 v2 section 2.4.2, FIPS 186-4 D.1.2.3), 32-bit limbs least significant 288 note first: p, n, b, G = (Gx, Gy). b and G are stored in the Montgomery domain, G with Z = R. 289 fn p2init w edit ulong 1024 to void 290 for i ulong 1024 291 put w i 0 292 end 293 put w 0 `4294967295` 294 put w 1 `4294967295` 295 put w 2 `4294967295` 296 put w 6 1 297 put w 7 `4294967295` 298 put w 48 `4234356049` 299 put w 49 `4089039554` 300 put w 50 `2803342980` 301 put w 51 `3169254061` 302 put w 52 `4294967295` 303 put w 53 `4294967295` 304 put w 55 `4294967295` 305 put w PONE1 1 306 p2mod w PMP 307 p2mod w PMN 308 put w PT1 `668098635` 309 put w 577 `1003371582` 310 put w 578 `3428036854` 311 put w 579 `1696401072` 312 put w 580 `1989707452` 313 put w 581 `3018571093` 314 put w 582 `2855965671` 315 put w 583 `1522939352` 316 p2tom w PBM PT1 PMP 317 put w PT1 `3633889942` 318 put w 577 `4104206661` 319 put w 578 `770388896` 320 put w 579 `1996717441` 321 put w 580 `1671708914` 322 put w 581 `4173129445` 323 put w 582 `3777774151` 324 put w 583 `1796723186` 325 p2tom w PGX PT1 PMP 326 put w PT1 `935285237` 327 put w 577 `3417718888` 328 put w 578 `1798397646` 329 put w 579 `734933847` 330 put w 580 `2081398294` 331 put w 581 `2397563722` 332 put w 582 `4263149467` 333 put w 583 `1340293858` 334 p2tom w add PGX 8 PT1 PMP 335 p2cpy w add PGX 16 add PMP 24 336 p2set w PT1 0 337 end 338 339 note o = a + q, complete projective addition for a = -3 (Renes-Costello-Batina 2015, Algorithm 4, 340 note in the grouping of RustCrypto primeorder 0.14 EquationAIsMinusThree::add_assign). Valid for 341 note a = q and for the identity (0 : 1 : 0). o may be a or q; a and q lie outside PXX..PRX+24. 342 fn p2padd w edit ulong 1024 o ulong a ulong q ulong to void 343 p2mul w PXX a q PMP 344 p2mul w PYY add a 8 add q 8 PMP 345 p2mul w PZZ add a 16 add q 16 PMP 346 p2add w PU a add a 8 PMP 347 p2add w PV q add q 8 PMP 348 p2mul w PXY PU PV PMP 349 p2add w PU PXX PYY PMP 350 p2sub w PXY PXY PU PMP 351 p2add w PU add a 8 add a 16 PMP 352 p2add w PV add q 8 add q 16 PMP 353 p2mul w PYZ PU PV PMP 354 p2add w PU PYY PZZ PMP 355 p2sub w PYZ PYZ PU PMP 356 p2add w PU a add a 16 PMP 357 p2add w PV q add q 16 PMP 358 p2mul w PXZ PU PV PMP 359 p2add w PU PXX PZZ PMP 360 p2sub w PXZ PXZ PU PMP 361 note bzz3 = 3 (xz - b zz); ym = yy - bzz3; yp = yy + bzz3 362 p2mul w PU PBM PZZ PMP 363 p2sub w PU PXZ PU PMP 364 p2add w PV PU PU PMP 365 p2add w PU PV PU PMP 366 p2sub w PYM PYY PU PMP 367 p2add w PYP PYY PU PMP 368 note zz3 = 3 zz; bxz3 = 3 (b xz - (zz3 + xx)); xx3 = 3 xx - zz3 369 p2add w PZZ3 PZZ PZZ PMP 370 p2add w PZZ3 PZZ3 PZZ PMP 371 p2mul w PU PBM PXZ PMP 372 p2add w PV PZZ3 PXX PMP 373 p2sub w PU PU PV PMP 374 p2add w PV PU PU PMP 375 p2add w PBX3 PV PU PMP 376 p2add w PV PXX PXX PMP 377 p2add w PV PV PXX PMP 378 p2sub w PXX3 PV PZZ3 PMP 379 note X3 = yp xy - yz bxz3; Y3 = yp ym + xx3 bxz3; Z3 = ym yz + xy xx3 380 p2mul w PU PYP PXY PMP 381 p2mul w PV PYZ PBX3 PMP 382 p2sub w PRX PU PV PMP 383 p2mul w PU PYP PYM PMP 384 p2mul w PV PXX3 PBX3 PMP 385 p2add w add PRX 8 PU PV PMP 386 p2mul w PU PYM PYZ PMP 387 p2mul w PV PXY PXX3 PMP 388 p2add w add PRX 16 PU PV PMP 389 p2pcpy w o PRX 390 end 391 392 note PACC = PTMP when bit is 1, else PACC kept: a masked select over all 24 cells, no branch on bit 393 fn p2psel w edit ulong 1024 bit ulong to void 394 mk = sub 0 bit 395 nm = not mk 396 for c ulong 24 397 y = and get w add PTMP c mk 398 put w add PACC c or y and get w add PACC c nm 399 end 400 end 401 402 note o = k p for the plain scalar k (8 limbs, may be secret) and the point p: double-and-add-always 403 note over all 256 bits, the sum kept or dropped by a masked select (no branch on k). o may be p. 404 fn p2smul w edit ulong 1024 o ulong k ulong p ulong to void 405 p2pcpy w PBASE p 406 p2set w PACC 0 407 p2cpy w add PACC 8 add PMP 24 408 p2set w add PACC 16 0 409 i = ulong 0 410 for j ulong 256 411 i = sub 255 j 412 p2padd w PACC PACC PACC 413 p2padd w PTMP PACC PBASE 414 p2psel w and shr get w add k shr i 5 and i 31 1 415 end 416 p2pcpy w o PACC 417 end 418 419 note affine x, y (plain) of the projective point p; the identity gives (0, 0) 420 fn p2aff w edit ulong 1024 x ulong y ulong p ulong to void 421 p2inv w PZI add p 16 PMP 422 p2mul w x p PZI PMP 423 p2frm w x x PMP 424 p2mul w y add p 8 PZI PMP 425 p2frm w y y PMP 426 end 427 428 note 32 big-endian bytes of b at off into the 8 limbs at o, and back 429 fn p2ld w edit ulong 1024 o ulong b edit uchar 4096 off ulong to void 430 q = ulong 0 431 v = ulong 0 432 for i ulong 8 433 q = add off sub 28 mul i 4 434 v = ulong get b q 435 v = or shl v 8 ulong get b add q 1 436 v = or shl v 8 ulong get b add q 2 437 v = or shl v 8 ulong get b add q 3 438 put w add o i v 439 end 440 end 441 442 fn p2st w edit ulong 1024 o ulong b edit uchar 4096 off ulong to void 443 q = ulong 0 444 v = ulong 0 445 for i ulong 8 446 q = add off sub 28 mul i 4 447 v = get w add o i 448 put b q uchar and shr v 24 255 449 put b add q 1 uchar and shr v 16 255 450 put b add q 2 uchar and shr v 8 255 451 put b add q 3 uchar and v 255 452 end 453 end 454 455 note the digest om/sha256 final leaves packed in s[0..4) (byte k = byte k mod 8 of s[k / 8]) into b[out..out+32) 456 fn p2dig b edit uchar 4096 s edit ulong 64 out ulong to void 457 for i ulong 32 458 put b add out i uchar and shr get s shr i 3 mul and i 7 8 255 459 end 460 end 461 462 note b[out..out+32) = SHA-256(b[off..off+n)); the bytes are first moved down to b[0..n), so off is 0 463 note or off >= n; n <= 1024 464 fn p2sha c edit uchar 104 b edit uchar 4096 s edit ulong 64 off ulong n ulong out ulong to void 465 if gt n 1024 466 trap 3 467 end 468 for i ulong n 469 put b i get b add off i 470 end 471 init c 472 absorb c b n s 473 final c s 474 p2dig b s out 475 end 476 477 note HMAC-SHA-256 (RFC 2104) keyed by the 32 bytes at b[kof..kof+32) over the mlen bytes the caller 478 note staged at b[64..64+mlen); the tag goes to b[oof..oof+32) (oof may be kof). kof, oof >= 1024 479 note and not PBI; mlen <= 448. 480 fn p2hmac c edit uchar 104 b edit uchar 4096 s edit ulong 64 kof ulong mlen ulong oof ulong to void 481 if gt mlen 448 482 trap 3 483 end 484 for i ulong 32 485 put b i xor get b add kof i 54 486 put b add i 32 54 487 end 488 init c 489 absorb c b add 64 mlen s 490 final c s 491 p2dig b s PBI 492 for k ulong 32 493 put b k xor get b add kof k 92 494 put b add k 32 92 495 put b add k 64 get b add PBI k 496 end 497 init c 498 absorb c b 96 s 499 final c s 500 p2dig b s oof 501 end 502 503 note V = HMAC_K(V) 504 fn p2vnext c edit uchar 104 b edit uchar 4096 s edit ulong 64 to void 505 for i ulong 32 506 put b add 64 i get b add PBV i 507 end 508 p2hmac c b s PBK 32 PBV 509 end 510 511 note RFC 6979 3.2 steps d-g: K = HMAC_K(V || sep || x || h1), V = HMAC_K(V) 512 fn p2upd c edit uchar 104 b edit uchar 4096 s edit ulong 64 sep uchar to void 513 for i ulong 32 514 put b add 64 i get b add PBV i 515 put b add 97 i get b add PBX i 516 put b add 129 i get b add PBH i 517 end 518 put b 96 sep 519 p2hmac c b s PBK 97 PBK 520 p2vnext c b s 521 end 522 523 note RFC 6979 3.2 steps b-g: V = 0x01 * 32, K = 0x00 * 32, then the two updates 524 fn p2drbg c edit uchar 104 b edit uchar 4096 s edit ulong 64 to void 525 for i ulong 32 526 put b add PBV i 1 527 put b add PBK i 0 528 end 529 p2upd c b s 0 530 p2upd c b s 1 531 end 532 533 note RFC 6979 3.2 step h: T = V = HMAC_K(V) into PBT (qlen = hlen = 256: one block), then 534 note K = HMAC_K(V || 0x00), V = HMAC_K(V), as rfc6979 0.6 HmacDrbg::fill_bytes does after every draw 535 fn p2gen c edit uchar 104 b edit uchar 4096 s edit ulong 64 to void 536 p2vnext c b s 537 for i ulong 32 538 put b add PBT i get b add PBV i 539 put b add 64 i get b add PBV i 540 end 541 put b 96 0 542 p2hmac c b s PBK 33 PBK 543 p2vnext c b s 544 end 545 546 note one signing attempt with the candidate k at PK (1 <= k < n): 0 and r||s at b[PBSIG..) on success, 547 note 2 when r or s is 0 (the caller draws the next k, as ecdsa 0.17 sign_prehashed_rfc6979 does) 548 fn p2try w edit ulong 1024 b edit uchar 4096 to ulong 549 p2smul w PP1 PK PGX 550 p2aff w PX PY PP1 551 p2cpy w PR PX 552 p2modr w PR PMN 553 if p2isz w PR 554 ret 2 555 end 556 p2tom w PT1 PK PMN 557 p2inv w PT1 PT1 PMN 558 p2tom w PT2 PR PMN 559 p2tom w PT3 PD PMN 560 p2mul w PT2 PT2 PT3 PMN 561 p2tom w PT3 PZ PMN 562 p2add w PT2 PT2 PT3 PMN 563 p2mul w PS PT1 PT2 PMN 564 p2frm w PS PS PMN 565 if p2isz w PS 566 ret 2 567 end 568 p2st w PR b PBSIG 569 p2st w PS b add PBSIG 32 570 ret 0 571 end 572 573 note p2sign: ECDSA P-256 signature of SHA-256(b[PBMSG..PBMSG+mlen)), mlen <= 1024, under the key d 574 note (32 bytes big endian) at b[PBKEY..). 0: r||s at b[PBSIG..PBSIG+64), the accepted k at w[PK..). 575 note 1: d is 0 or >= n (refused, as SigningKey::from_slice refuses it). 576 fn p2sign w edit ulong 1024 c edit uchar 104 b edit uchar 4096 s edit ulong 64 mlen ulong to ulong 577 p2ld w PD b PBKEY 578 if not p2rng w PD 579 ret 1 580 end 581 p2sha c b s PBMSG mlen PBDIG 582 p2ld w PZ b PBDIG 583 p2modr w PZ PMN 584 p2st w PZ b PBH 585 for i ulong 32 586 put b add PBX i get b add PBKEY i 587 end 588 p2drbg c b s 589 st = ulong 2 590 while eq st 2 bound 64 591 p2gen c b s 592 p2ld w PK b PBT 593 if p2rng w PK 594 st = p2try w b 595 end 596 end 597 ret st 598 end 599 600 note p2pub: the public key of d at b[PBKEY..) as uncompressed SEC1 0x04 || X || Y at 601 note b[PBPUB..PBPUB+65). 0 done, 1 d refused (0 or >= n). 602 fn p2pub w edit ulong 1024 b edit uchar 4096 to ulong 603 p2ld w PD b PBKEY 604 if not p2rng w PD 605 ret 1 606 end 607 p2smul w PP1 PD PGX 608 p2aff w PX PY PP1 609 put b PBPUB 4 610 p2st w PX b add PBPUB 1 611 p2st w PY b add PBPUB 33 612 ret 0 613 end 614 615 note the affine point (PX, PY) (plain) is on y^2 = x^3 - 3x + b with x, y < p 616 fn p2oncv w edit ulong 1024 to bool 617 if not p2lt w PX PMP 618 ret false 619 end 620 if not p2lt w PY PMP 621 ret false 622 end 623 p2tom w PT1 PX PMP 624 p2tom w PT2 PY PMP 625 p2mul w PT3 PT2 PT2 PMP 626 p2mul w PT2 PT1 PT1 PMP 627 p2mul w PT2 PT2 PT1 PMP 628 p2sub w PT2 PT2 PT1 PMP 629 p2sub w PT2 PT2 PT1 PMP 630 p2sub w PT2 PT2 PT1 PMP 631 p2add w PT2 PT2 PBM PMP 632 ret p2eqv w PT2 PT3 633 end 634 635 note p2vrfy: 1 when r||s at b[PBSIG..PBSIG+64) is a valid signature of SHA-256(b[PBMSG..PBMSG+mlen)) 636 note under the uncompressed SEC1 key at b[PBPUB..PBPUB+65), else 0: a key that is not 0x04 || X || Y 637 note with X, Y < p on the curve, r or s outside 1..n-1, u1 G + u2 Q the identity, or x mod n != r. 638 note High s is accepted (p256 0.14 does not normalize s). Variable time is fine here: all inputs are public. 639 fn p2vrfy w edit ulong 1024 c edit uchar 104 b edit uchar 4096 s edit ulong 64 mlen ulong to ulong 640 if ne get b PBPUB 4 641 ret 0 642 end 643 p2ld w PX b add PBPUB 1 644 p2ld w PY b add PBPUB 33 645 if not p2oncv w 646 ret 0 647 end 648 p2ld w PR b PBSIG 649 p2ld w PS b add PBSIG 32 650 if not p2rng w PR 651 ret 0 652 end 653 if not p2rng w PS 654 ret 0 655 end 656 p2sha c b s PBMSG mlen PBDIG 657 p2ld w PZ b PBDIG 658 p2modr w PZ PMN 659 p2tom w PT1 PS PMN 660 p2inv w PT1 PT1 PMN 661 p2tom w PT2 PZ PMN 662 p2mul w PU1 PT2 PT1 PMN 663 p2frm w PU1 PU1 PMN 664 p2tom w PT2 PR PMN 665 p2mul w PU2 PT2 PT1 PMN 666 p2frm w PU2 PU2 PMN 667 p2tom w PQ PX PMP 668 p2tom w add PQ 8 PY PMP 669 p2cpy w add PQ 16 add PMP 24 670 p2smul w PP1 PU1 PGX 671 p2smul w PP2 PU2 PQ 672 p2padd w PP1 PP1 PP2 673 if p2isz w add PP1 16 674 ret 0 675 end 676 p2aff w PX PY PP1 677 p2modr w PX PMN 678 if p2eqv w PX PR 679 ret 1 680 end 681 ret 0 682 end