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/open-humanity/blob/main/om/ed25519.om # open-humanity Privacy-first peer assistance network for sovereign autonomous agents 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 `open-humanity / om / ed25519.om` 637 lines ยท 14332 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note `ed25519`.om: Ed25519 signature verification (RFC 8032 section 5.1.7), verification only. 3 note Two rule sets, chosen by mode: 0 strict = Go crypto/`ed25519`.Verify, 1 consensus = ZIP 215 as in 4 note github.com/hdevalence/ed25519consensus. Needs the sibling unit om/sha512/sha512.om (s512pre) 5 note concatenated before this file. Library unit: functions only, no main, no globals. 6 note Variable time: every input is public (key, message, signature). NOT constant-time-audited. 7 note 8 note Field GF(2^255-19): 16 limbs of radix 2^16 held in ulong cells (TweetNaCl layout, but unsigned). 9 note Every field element lives in one workspace w (ulong 1024) at a slot offset; functions take 10 note offsets so one array is passed and no two names alias. Limb bounds (see README): 11 note mul output: limbs <= 65535 except limb 0 <= 65573; 12 note sub adds 8p limbwise before subtracting, so it never wraps while the subtrahend is a mul output, 13 note a canonical value, or a sum of two of those; edfsub traps 9 if a subtrahend limb exceeds the 14 note 8p limb; mul inputs must be below 2^21 per limb (edfmul traps 10 otherwise), so a column sum 15 note is below 16 * 2^42 and the folded sum (times 39) below 2^52: no ulong ever wraps. 16 note Signed arithmetic (scalar reduction mod L, edmodl) traps on overflow instead of wrapping. 17 18 note workspace slots (ulong offsets into w) 19 const edzero ulong 0 20 const edone ulong 16 21 const edd ulong 32 22 const edd2 ulong 48 23 const edsqm1 ulong 64 24 const edbp ulong 80 25 const edna ulong 144 26 const edba ulong 208 27 const edq ulong 272 28 const edrr ulong 336 29 const edta ulong 400 30 const edtb ulong 416 31 const edtc ulong 432 32 const edtd ulong 448 33 const edte ulong 464 34 const edtf ulong 480 35 const edtg ulong 496 36 const edth ulong 512 37 const edtt ulong 528 38 const edp0 ulong 544 39 const edp1 ulong 560 40 const edp2 ulong 576 41 const edp3 ulong 592 42 const edp4 ulong 608 43 const edu ulong 624 44 const edv ulong 640 45 const edv3 ulong 656 46 const edr ulong 672 47 const edck ulong 688 48 const edx ulong 704 49 const edlimb ulong `2097152` 50 51 note constants: 1, d, 2d, sqrt(-1) (RFC 8032 section 5.1), base point B (RFC 8032 section 5.1) as 52 note extended coordinates X Y Z=1 T=XY; decimal radix-2^16 limbs, least significant first 53 fn edinit w edit ulong 1024 to void 54 put w 16 1 55 put w 32 30883 56 put w 33 4953 57 put w 34 19914 58 put w 35 30187 59 put w 36 55467 60 put w 37 16705 61 put w 38 2637 62 put w 39 112 63 put w 40 59544 64 put w 41 30585 65 put w 42 16505 66 put w 43 36039 67 put w 44 65139 68 put w 45 11119 69 put w 46 27886 70 put w 47 20995 71 put w 48 61785 72 put w 49 9906 73 put w 50 39828 74 put w 51 60374 75 put w 52 45398 76 put w 53 33411 77 put w 54 5274 78 put w 55 224 79 put w 56 53552 80 put w 57 61171 81 put w 58 33010 82 put w 59 6542 83 put w 60 64743 84 put w 61 22239 85 put w 62 55772 86 put w 63 9222 87 put w 64 41136 88 put w 65 18958 89 put w 66 6951 90 put w 67 50414 91 put w 68 58488 92 put w 69 44335 93 put w 70 6150 94 put w 71 12099 95 put w 72 55207 96 put w 73 15867 97 put w 74 153 98 put w 75 11085 99 put w 76 57099 100 put w 77 20417 101 put w 78 9344 102 put w 79 11139 103 put w 80 54554 104 put w 81 36645 105 put w 82 11616 106 put w 83 51542 107 put w 84 42930 108 put w 85 38181 109 put w 86 51040 110 put w 87 26924 111 put w 88 56412 112 put w 89 64982 113 put w 90 57905 114 put w 91 49316 115 put w 92 21502 116 put w 93 52590 117 put w 94 14035 118 put w 95 8553 119 put w 96 26200 120 put w 97 26214 121 put w 98 26214 122 put w 99 26214 123 put w 100 26214 124 put w 101 26214 125 put w 102 26214 126 put w 103 26214 127 put w 104 26214 128 put w 105 26214 129 put w 106 26214 130 put w 107 26214 131 put w 108 26214 132 put w 109 26214 133 put w 110 26214 134 put w 111 26214 135 put w 128 56739 136 put w 129 42423 137 put w 130 35507 138 put w 131 28126 139 put w 132 21237 140 put w 133 30545 141 put w 134 40832 142 put w 135 8432 143 put w 136 58237 144 put w 137 25771 145 put w 138 20110 146 put w 139 26346 147 put w 140 30309 148 put w 141 55179 149 put w 142 24335 150 put w 143 26503 151 put w 112 1 152 end 153 154 note copy n cells from offset a to offset o 155 fn edfcpy w edit ulong 1024 o ulong a ulong n ulong to void 156 for i ulong n 157 put w add o i get w add a i 158 end 159 end 160 161 note o = a + b limbwise, no carry 162 fn edfadd w edit ulong 1024 o ulong a ulong b ulong to void 163 for i ulong 16 164 put w add o i add get w add a i get w add b i 165 end 166 end 167 168 note limb i of 8p = 8 * (2^255 - 19) in radix 2^16: 8*65517, 8*65535 (x14), 8*32767 169 fn ed8p i ulong to ulong 170 if eq i 0 171 ret 524136 172 end 173 if eq i 15 174 ret 262136 175 end 176 ret 524280 177 end 178 179 note o = a + 8p - b limbwise; trap 9 if a subtrahend limb is above the 8p limb (cannot happen for 180 note the call sites below; the check makes a broken bound stop instead of wrapping) 181 fn edfsub w edit ulong 1024 o ulong a ulong b ulong to void 182 for i ulong 16 183 k = ed8p i 184 y = get w add b i 185 if gt y k 186 trap 9 187 end 188 put w add o i sub add get w add a i k y 189 end 190 end 191 192 note one carry pass over 16 limbs; the carry out of limb 15 (weight 2^256 = 38 mod p) folds into limb 0 193 fn edfcar t edit ulong 16 to void 194 for i ulong 15 195 put t add i 1 add get t add i 1 shr get t i 16 196 put t i and get t i 65535 197 end 198 c = shr get t 15 16 199 put t 15 and get t 15 65535 200 put t 0 add get t 0 mul c 38 201 end 202 203 note o = a * b mod p (o may equal a or b); trap 10 if an input limb is 2^21 or more 204 fn edfmul w edit ulong 1024 o ulong a ulong b ulong to void 205 t = new ulong 32 0 206 c = new ulong 16 0 207 for j ulong 16 208 y = get w add b j 209 if ge y edlimb 210 trap 10 211 end 212 put c j y 213 end 214 for i ulong 16 215 x = get w add a i 216 if ge x edlimb 217 trap 10 218 end 219 for j ulong 16 220 q = add i j 221 put t q add get t q mul x get c j 222 end 223 end 224 r = new ulong 16 0 225 for i ulong 15 226 put r i add get t i mul 38 get t add i 16 227 end 228 put r 15 get t 15 229 edfcar r 230 edfcar r 231 for i ulong 16 232 put w add o i get r i 233 end 234 end 235 236 note o = a^(2^n), n >= 1 237 fn edfsqn w edit ulong 1024 o ulong a ulong n ulong to void 238 edfmul w o a a 239 for i ulong sub n 1 240 edfmul w o o o 241 end 242 end 243 244 note canonical limbs of slot a into t: full carries, then subtract p while the value is >= p 245 fn edfcan w edit ulong 1024 a ulong t edit ulong 16 to void 246 for i ulong 16 247 put t i get w add a i 248 end 249 edfcar t 250 edfcar t 251 while gt get t 0 65535 bound 8 252 edfcar t 253 end 254 m = new ulong 16 0 255 for r ulong 2 256 bw = ulong 0 257 for i ulong 16 258 pl = div ed8p i 8 259 v = sub sub add get t i 65536 pl bw 260 put m i and v 65535 261 bw = sub 1 shr v 16 262 end 263 if eq bw 0 264 for i ulong 16 265 put t i get m i 266 end 267 end 268 end 269 end 270 271 note o = canonical form of a 272 fn edfnorm w edit ulong 1024 o ulong a ulong to void 273 t = new ulong 16 0 274 edfcan w a t 275 for i ulong 16 276 put w add o i get t i 277 end 278 end 279 280 note 1 when a = b mod p 281 fn edfeq w edit ulong 1024 a ulong b ulong to ulong 282 s = new ulong 16 0 283 t = new ulong 16 0 284 edfcan w a s 285 edfcan w b t 286 for i ulong 16 287 if ne get s i get t i 288 ret 0 289 end 290 end 291 ret 1 292 end 293 294 note o = z^(2^250 - 1), e = z^11 (addition chain of ref10 fe_pow22523 / fe_invert); uses edp0..edp2 295 fn edf250 w edit ulong 1024 o ulong e ulong z ulong to void 296 edfmul w edp0 z z 297 edfmul w edp1 edp0 edp0 298 edfmul w edp1 edp1 edp1 299 edfmul w edp1 z edp1 300 edfmul w e edp0 edp1 301 edfmul w edp0 e e 302 edfmul w edp0 edp1 edp0 303 edfsqn w edp1 edp0 5 304 edfmul w edp0 edp1 edp0 305 edfsqn w edp1 edp0 10 306 edfmul w edp1 edp1 edp0 307 edfsqn w edp2 edp1 20 308 edfmul w edp1 edp2 edp1 309 edfsqn w edp1 edp1 10 310 edfmul w edp0 edp1 edp0 311 edfsqn w edp1 edp0 50 312 edfmul w edp1 edp1 edp0 313 edfsqn w edp2 edp1 100 314 edfmul w edp1 edp2 edp1 315 edfsqn w edp1 edp1 50 316 edfmul w o edp1 edp0 317 end 318 319 note o = a^(p-2) = 1/a (0 for a = 0) 320 fn edfinv w edit ulong 1024 o ulong a ulong to void 321 edf250 w edp4 edp3 a 322 edfsqn w edp4 edp4 5 323 edfmul w o edp4 edp3 324 end 325 326 note o = a^((p-5)/8) = a^(2^252 - 3) 327 fn edfp58 w edit ulong 1024 o ulong a ulong to void 328 edf250 w edp4 edp3 a 329 edfsqn w edp4 edp4 2 330 edfmul w o edp4 a 331 end 332 333 note slot o = the 32 little-endian bytes of b with the top bit cleared; values p..2^255-1 are kept 334 note (they are reduced mod p by later arithmetic), as Go field.Element.SetBytes does 335 fn edfbytes w edit ulong 1024 o ulong b view uchar 32 to void 336 for i ulong 16 337 lo = ulong get b mul i 2 338 hi = ulong get b add mul i 2 1 339 put w add o i or lo shl hi 8 340 end 341 put w add o 15 and get w add o 15 32767 342 end 343 344 note decode 32 bytes into the extended point at slot pt; 1 ok, 0 not a point. Go edwards25519 345 note Point.SetBytes: y = bytes mod p (non-canonical y accepted), x = +sqrt((y^2-1)/(d y^2+1)) via 346 note field.Element.SqrtRatio, negated when bit 255 is set (x = 0 with bit 255 set is accepted). 347 fn edpdec w edit ulong 1024 pt ulong b view uchar 32 to ulong 348 y = add pt 16 349 edfbytes w y b 350 edfmul w edu y y 351 edfmul w edv edu edd 352 edfadd w edv edv edone 353 edfsub w edu edu edone 354 edfnorm w edu edu 355 edfmul w edv3 edv edv 356 edfmul w edv3 edv3 edv 357 edfmul w edr edv3 edv3 358 edfmul w edr edr edv 359 edfmul w edr edr edu 360 edfp58 w edr edr 361 edfmul w edr edr edv3 362 edfmul w edr edr edu 363 edfmul w edck edr edr 364 edfmul w edck edck edv 365 if eq edfeq w edck edu 0 366 edfsub w edx edzero edu 367 if eq edfeq w edck edx 0 368 ret 0 369 end 370 edfmul w edr edr edsqm1 371 end 372 edfnorm w edx edr 373 sign = shr ulong get b 31 7 374 if ne and get w edx 1 sign 375 edfsub w edx edzero edx 376 edfnorm w edx edx 377 end 378 edfcpy w pt edx 16 379 edfcpy w add pt 32 edone 16 380 edfmul w add pt 48 edx y 381 ret 1 382 end 383 384 note p = -p (negate X and T); the results are brought back to canonical limbs so that every stored 385 note coordinate stays a valid subtrahend for edfsub 386 fn edpneg w edit ulong 1024 p ulong to void 387 edfsub w p edzero p 388 edfnorm w p p 389 t = add p 48 390 edfsub w t edzero t 391 edfnorm w t t 392 end 393 394 note p = p + q, extended coordinates, add-2008-hwcd-3 (TweetNaCl add), complete for Ed25519 395 fn edpadd w edit ulong 1024 p ulong q ulong to void 396 py = add p 16 397 qy = add q 16 398 edfsub w edta py p 399 edfsub w edtt qy q 400 edfmul w edta edta edtt 401 edfadd w edtb p py 402 edfadd w edtt q qy 403 edfmul w edtb edtb edtt 404 edfmul w edtc add p 48 add q 48 405 edfmul w edtc edtc edd2 406 edfmul w edtd add p 32 add q 32 407 edfadd w edtd edtd edtd 408 edfsub w edte edtb edta 409 edfsub w edtf edtd edtc 410 edfadd w edtg edtd edtc 411 edfadd w edth edtb edta 412 edfmul w p edte edtf 413 edfmul w py edth edtg 414 edfmul w add p 32 edtg edtf 415 edfmul w add p 48 edte edth 416 end 417 418 note p = 2p, dbl-2008-hwcd with a = -1, every output coordinate negated (same projective point): 419 note A = X^2, B = Y^2, C = 2Z^2, H = A + B, E = (X+Y)^2 - H, G = B - A, F = C + A - B, 420 note X = E F, Y = G H, T = E H, Z = F G 421 fn edpdbl w edit ulong 1024 p ulong to void 422 py = add p 16 423 edfmul w edta p p 424 edfmul w edtb py py 425 edfmul w edtc add p 32 add p 32 426 edfadd w edtc edtc edtc 427 edfadd w edth edta edtb 428 edfadd w edte p py 429 edfmul w edte edte edte 430 edfsub w edte edte edth 431 edfsub w edtg edtb edta 432 edfadd w edtf edtc edta 433 edfsub w edtf edtf edtb 434 edfmul w p edte edtf 435 edfmul w py edtg edth 436 edfmul w add p 48 edte edth 437 edfmul w add p 32 edtf edtg 438 end 439 440 note canonical encoding of point p (RFC 8032 section 5.1.2): y, with bit 255 = x mod 2 441 fn edpenc w edit ulong 1024 p ulong o edit uchar 32 to void 442 edfinv w edu add p 32 443 edfmul w edv p edu 444 edfmul w edv3 add p 16 edu 445 edfnorm w edv edv 446 edfnorm w edv3 edv3 447 for i ulong 16 448 v = get w add edv3 i 449 put o mul i 2 uchar and v 255 450 put o add mul i 2 1 uchar shr v 8 451 end 452 put o 31 or get o 31 uchar shl and get w edv 1 7 453 end 454 455 note 1 when p is the identity (X = 0 and Y = Z), as Go Point.Equal(NewIdentityPoint()) 456 fn edpid w edit ulong 1024 p ulong to ulong 457 if eq edfeq w p edzero 0 458 ret 0 459 end 460 ret edfeq w add p 16 add p 32 461 end 462 463 note bit i of the 32-byte little-endian scalar b 464 fn edbit b view uchar 32 i ulong to ulong 465 ret and shr ulong get b shr i 3 and i 7 1 466 end 467 468 note q = [s]B + [k]P where P is the point at edna; joint double-and-add from bit 252 469 note (s < L and k < L are below 2^253), with B + P precomputed at edba 470 fn edsmul w edit ulong 1024 s view uchar 32 k view uchar 32 to void 471 edfcpy w edba edbp 64 472 edpadd w edba edna 473 edfcpy w edq edzero 64 474 edfcpy w add edq 16 edone 16 475 edfcpy w add edq 32 edone 16 476 for j ulong 253 477 i = sub 252 j 478 edpdbl w edq 479 sb = edbit s i 480 kb = edbit k i 481 if eq sb 1 482 if eq kb 1 483 edpadd w edq edba 484 else 485 edpadd w edq edbp 486 end 487 else 488 if eq kb 1 489 edpadd w edq edna 490 end 491 end 492 end 493 end 494 495 note the group order L = 2^252 + `27742317777372353535851937790883648493` as 32 little-endian bytes 496 fn edlset l edit long 32 to void 497 put l 0 237 498 put l 1 211 499 put l 2 245 500 put l 3 92 501 put l 4 26 502 put l 5 99 503 put l 6 18 504 put l 7 88 505 put l 8 214 506 put l 9 156 507 put l 10 247 508 put l 11 162 509 put l 12 222 510 put l 13 249 511 put l 14 222 512 put l 15 20 513 put l 31 16 514 end 515 516 note 1 when the 32-byte little-endian scalar s is below L (Go Scalar.SetCanonicalBytes / isReduced) 517 fn edsclt s view uchar 32 to ulong 518 l = new long 32 0 519 edlset l 520 i = ulong 32 521 while gt i 0 bound 33 522 i = sub i 1 523 a = long get s i 524 b = get l i 525 if lt a b 526 ret 1 527 end 528 if gt a b 529 ret 0 530 end 531 end 532 ret 0 533 end 534 535 note x[0..32) = x mod L as bytes, for the 64 bytes x[0..64) (TweetNaCl modL, signed limbs; any 536 note overflow traps) 537 fn edmodl x edit long 64 to void 538 l = new long 32 0 539 edlset l 540 i = long 63 541 while ge i 32 bound 33 542 carry = long 0 543 b = sub i 32 544 q = mul 16 get x i 545 j = b 546 while lt j sub i 12 bound 21 547 lj = get l sub j b 548 v = sub add get x j carry mul q lj 549 carry = shr add v 128 8 550 put x j sub v mul carry 256 551 j = add j 1 552 end 553 put x j add get x j carry 554 put x i 0 555 i = sub i 1 556 end 557 carry = long 0 558 for j ulong 32 559 v = add get x j sub carry mul shr get x 31 4 get l j 560 carry = shr v 8 561 put x j and v 255 562 end 563 for j ulong 32 564 put x j sub get x j mul carry get l j 565 end 566 for j ulong 32 567 put x add j 1 add get x add j 1 shr get x j 8 568 put x j and get x j 255 569 end 570 end 571 572 note k = SHA-512(R || A || M) mod L (RFC 8032 section 5.1.7 step 2) 573 fn edhram sg view uchar 64 pk view uchar 32 m view uchar 65536 n ulong k edit uchar 32 to void 574 pre = new uchar 64 0 575 for i ulong 32 576 put pre i get sg i 577 put pre add i 32 get pk i 578 end 579 hd = new uchar 64 0 580 s512pre pre m n hd 581 x = new long 64 0 582 for i ulong 64 583 put x i long get hd i 584 end 585 edmodl x 586 for i ulong 32 587 put k i uchar get x i 588 end 589 end 590 591 note Ed25519 verification. pk: 32-byte public key A; m[0..n): message, n <= 65536 (else trap 3 in 592 note s512pre); sg: 64-byte signature R || S; mode 0 strict (Go crypto/`ed25519`.Verify), mode 1 593 note consensus (ZIP 215, ed25519consensus.Verify); any other mode traps 11. Returns 1 valid, 0 invalid. 594 fn edverify pk view uchar 32 m view uchar 65536 n ulong sg view uchar 64 mode ulong to ulong 595 if gt mode 1 596 trap 11 597 end 598 s = new uchar 32 0 599 encoded_r = new uchar 32 0 600 k = new uchar 32 0 601 e = new uchar 32 0 602 w = new ulong 1024 0 603 for i ulong 32 604 put encoded_r i get sg i 605 put s i get sg add i 32 606 end 607 if eq edsclt s 0 608 ret 0 609 end 610 edinit w 611 if eq edpdec w edna pk 0 612 ret 0 613 end 614 edpneg w edna 615 if eq mode 1 616 if eq edpdec w edrr encoded_r 0 617 ret 0 618 end 619 end 620 edhram sg pk m n k 621 edsmul w s k 622 if eq mode 0 623 edpenc w edq e 624 for i ulong 32 625 if ne get e i get encoded_r i 626 ret 0 627 end 628 end 629 ret 1 630 end 631 edpneg w edrr 632 edpadd w edq edrr 633 edpdbl w edq 634 edpdbl w edq 635 edpdbl w edq 636 ret edpid w edq 637 end