spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / om / p256 / tests / run.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note om/p256/tests/run.om: the p256.om known-answer and parity runner. Compiled after its units: |
| 3 | note cat om/p256/sha256.om om/p256/p256.om om/p256/tests/run.om | OMC > run.elf |
| 4 | note ./run.elf om/p256/tests/rfc6979-a25.txt om/p256/tests/parity-rust.txt om/p256/tests/edge-rust.txt |
| 5 | note om/p256/tests/interplane.txt |
| 6 | note Self tests first (G on the curve, n G = identity, (n - 1) G = -G), then every vector file named |
| 7 | note on the command line. A file holds lines (# starts a comment): |
| 8 | note K key pub p2pub(key) must equal the 65-byte uncompressed SEC1 pub |
| 9 | note N key msg k the RFC 6979 nonce p2sign accepts for (key, msg) must equal k |
| 10 | note S key msg sig p2sign(key, msg) must equal sig (r||s), and p2vrfy must accept it under |
| 11 | note p2pub(key) |
| 12 | note V pub msg sig 0|1 p2vrfy(pub, msg, sig) must give the recorded verdict |
| 13 | note all fields lowercase or uppercase hex; msg is 1..1024 bytes. Prints counts and per-call timing |
| 14 | note (monons around each call): p2sign, p2vrfy of the S lines (every one a full verification of a |
| 15 | note valid signature) and p2vrfy of the V lines (early refusals included). Exit 0 iff every check |
| 16 | note passed, 1 on any mismatch or malformed line, 2 when a file cannot be read. |
| 17 | global FB uchar 4194304 |
| 18 | global FP ulong 4 |
| 19 | global CT ulong 32 |
| 20 | global PATH uchar 4096 |
| 21 | text TSELF "self tests: " |
| 22 | text TPASS " pass, " |
| 23 | text TFAIL " fail\n" |
| 24 | text TFILE "file " |
| 25 | text TK ": K " |
| 26 | text TN " N " |
| 27 | text TS " S " |
| 28 | text TV " V " |
| 29 | text TBAD " malformed " |
| 30 | text TNL "\n" |
| 31 | text TSL "/" |
| 32 | text TMIS "MISMATCH line " |
| 33 | text TKIND " kind " |
| 34 | text TTS "timing p2sign: calls " |
| 35 | text TTV "timing p2vrfy (S lines, valid): calls " |
| 36 | text TTW "timing p2vrfy (V lines, mixed): calls " |
| 37 | text TMEAN " mean_us " |
| 38 | text TMIN " min_us " |
| 39 | text TMAX " max_us " |
| 40 | text TOK "p256: PASS\n" |
| 41 | text TNO "p256: FAIL\n" |
| 42 | text TRD "cannot read " |
| 43 | |
| 44 | const CKP ulong 0 |
| 45 | const CNP ulong 2 |
| 46 | const CSP ulong 4 |
| 47 | const CVP ulong 6 |
| 48 | const CBAD ulong 8 |
| 49 | const CTS ulong 10 |
| 50 | const CTV ulong 14 |
| 51 | const CSELF ulong 18 |
| 52 | const CALL ulong 20 |
| 53 | const CTW ulong 22 |
| 54 | |
| 55 | fn onum v ulong to void |
| 56 | d = new uchar 24 0 |
| 57 | n = ulong 0 |
| 58 | x = v |
| 59 | while true bound 24 |
| 60 | put d sub 23 n uchar add rem x 10 48 |
| 61 | n = add n 1 |
| 62 | x = div x 10 |
| 63 | if eq x 0 |
| 64 | break |
| 65 | end |
| 66 | end |
| 67 | r = write 1 d sub 24 n n |
| 68 | end |
| 69 | |
| 70 | fn ochr ch uchar to void |
| 71 | d = new uchar 1 ch |
| 72 | r = write 1 d 0 1 |
| 73 | end |
| 74 | |
| 75 | note count a pass (ok) or a fail at counter base (pass at base, fail at base + 1) |
| 76 | fn tally base ulong ok bool to void |
| 77 | if ok |
| 78 | put CT base add get CT base 1 |
| 79 | ret |
| 80 | end |
| 81 | put CT add base 1 add get CT add base 1 1 |
| 82 | put CT CALL add get CT CALL 1 |
| 83 | end |
| 84 | |
| 85 | note one timed call: count, sum, min, max at base..base+3 |
| 86 | fn tadd base ulong ns ulong to void |
| 87 | n = get CT base |
| 88 | put CT base add n 1 |
| 89 | put CT add base 1 add get CT add base 1 ns |
| 90 | if or eq n 0 lt ns get CT add base 2 |
| 91 | put CT add base 2 ns |
| 92 | end |
| 93 | if gt ns get CT add base 3 |
| 94 | put CT add base 3 ns |
| 95 | end |
| 96 | end |
| 97 | |
| 98 | fn hexv ch uchar to ulong |
| 99 | if and ge ch 48 le ch 57 |
| 100 | ret ulong sub ch 48 |
| 101 | end |
| 102 | if and ge ch 97 le ch 102 |
| 103 | ret ulong sub ch 87 |
| 104 | end |
| 105 | if and ge ch 65 le ch 70 |
| 106 | ret ulong sub ch 55 |
| 107 | end |
| 108 | ret 16 |
| 109 | end |
| 110 | |
| 111 | fn skipsp to void |
| 112 | p = get FP 0 |
| 113 | e = get FP 1 |
| 114 | while lt p e bound 4096 |
| 115 | if ne get FB p 32 |
| 116 | break |
| 117 | end |
| 118 | p = add p 1 |
| 119 | end |
| 120 | put FP 0 p |
| 121 | end |
| 122 | |
| 123 | note the next hex field into b[off..off+cap); returns its byte count, or cap + 1 when the field is |
| 124 | note empty, odd, not hex or longer than cap |
| 125 | fn rhex b edit uchar 4096 off ulong cap ulong to ulong |
| 126 | skipsp |
| 127 | n = ulong 0 |
| 128 | p = get FP 0 |
| 129 | e = get FP 1 |
| 130 | while lt p e bound 4096 |
| 131 | h = hexv get FB p |
| 132 | if eq h 16 |
| 133 | break |
| 134 | end |
| 135 | if or ge add p 1 e ge n cap |
| 136 | ret add cap 1 |
| 137 | end |
| 138 | l = hexv get FB add p 1 |
| 139 | if eq l 16 |
| 140 | ret add cap 1 |
| 141 | end |
| 142 | put b add off n uchar or shl h 4 l |
| 143 | n = add n 1 |
| 144 | p = add p 2 |
| 145 | end |
| 146 | put FP 0 p |
| 147 | if eq n 0 |
| 148 | ret add cap 1 |
| 149 | end |
| 150 | ret n |
| 151 | end |
| 152 | |
| 153 | fn nextln to void |
| 154 | p = get FP 0 |
| 155 | e = get FP 1 |
| 156 | while lt p e bound 999999 |
| 157 | if eq get FB p 10 |
| 158 | put FP 0 add p 1 |
| 159 | put FP 2 add get FP 2 1 |
| 160 | ret |
| 161 | end |
| 162 | p = add p 1 |
| 163 | end |
| 164 | put FP 0 e |
| 165 | end |
| 166 | |
| 167 | fn beq b edit uchar 4096 x ulong y ulong n ulong to bool |
| 168 | v = uchar 0 |
| 169 | for i ulong n |
| 170 | v = or v xor get b add x i get b add y i |
| 171 | end |
| 172 | ret eq v 0 |
| 173 | end |
| 174 | |
| 175 | fn mism kind uchar to void |
| 176 | r = write 1 TMIS 0 len TMIS |
| 177 | onum get FP 2 |
| 178 | r = write 1 TKIND 0 len TKIND |
| 179 | ochr kind |
| 180 | r = write 1 TNL 0 len TNL |
| 181 | end |
| 182 | |
| 183 | fn bad to void |
| 184 | put CT CBAD add get CT CBAD 1 |
| 185 | put CT CALL add get CT CALL 1 |
| 186 | mism 63 |
| 187 | end |
| 188 | |
| 189 | fn dok w edit ulong 1024 c edit uchar 104 b edit uchar 4096 s edit ulong 64 to void |
| 190 | n1 = rhex b PBKEY 32 |
| 191 | n2 = rhex b 3600 65 |
| 192 | if or ne n1 32 ne n2 65 |
| 193 | bad |
| 194 | ret |
| 195 | end |
| 196 | st = p2pub w b |
| 197 | ok = and eq st 0 beq b PBPUB 3600 65 |
| 198 | tally CKP ok |
| 199 | if not ok |
| 200 | mism 75 |
| 201 | end |
| 202 | end |
| 203 | |
| 204 | fn don w edit ulong 1024 c edit uchar 104 b edit uchar 4096 s edit ulong 64 to void |
| 205 | n1 = rhex b PBKEY 32 |
| 206 | ml = rhex b PBMSG 1024 |
| 207 | n3 = rhex b 3700 32 |
| 208 | if or or ne n1 32 gt ml 1024 ne n3 32 |
| 209 | bad |
| 210 | ret |
| 211 | end |
| 212 | st = p2sign w c b s ml |
| 213 | p2st w PK b 3740 |
| 214 | ok = and eq st 0 beq b 3700 3740 32 |
| 215 | tally CNP ok |
| 216 | if not ok |
| 217 | mism 78 |
| 218 | end |
| 219 | end |
| 220 | |
| 221 | fn dos w edit ulong 1024 c edit uchar 104 b edit uchar 4096 s edit ulong 64 to void |
| 222 | n1 = rhex b PBKEY 32 |
| 223 | ml = rhex b PBMSG 1024 |
| 224 | n3 = rhex b 3500 64 |
| 225 | if or or ne n1 32 gt ml 1024 ne n3 64 |
| 226 | bad |
| 227 | ret |
| 228 | end |
| 229 | t0 = monons |
| 230 | st = p2sign w c b s ml |
| 231 | t1 = monons |
| 232 | tadd CTS ulong sub t1 t0 |
| 233 | ok = and eq st 0 beq b PBSIG 3500 64 |
| 234 | st = p2pub w b |
| 235 | t0 = monons |
| 236 | v = p2vrfy w c b s ml |
| 237 | t1 = monons |
| 238 | tadd CTV ulong sub t1 t0 |
| 239 | ok = and ok and eq st 0 eq v 1 |
| 240 | tally CSP ok |
| 241 | if not ok |
| 242 | mism 83 |
| 243 | end |
| 244 | end |
| 245 | |
| 246 | fn dov w edit ulong 1024 c edit uchar 104 b edit uchar 4096 s edit ulong 64 to void |
| 247 | n1 = rhex b PBPUB 65 |
| 248 | ml = rhex b PBMSG 1024 |
| 249 | n3 = rhex b PBSIG 64 |
| 250 | skipsp |
| 251 | p = get FP 0 |
| 252 | if or or ne n1 65 gt ml 1024 ne n3 64 |
| 253 | bad |
| 254 | ret |
| 255 | end |
| 256 | if ge p get FP 1 |
| 257 | bad |
| 258 | ret |
| 259 | end |
| 260 | ex = hexv get FB p |
| 261 | if gt ex 1 |
| 262 | bad |
| 263 | ret |
| 264 | end |
| 265 | put FP 0 add p 1 |
| 266 | t0 = monons |
| 267 | v = p2vrfy w c b s ml |
| 268 | t1 = monons |
| 269 | tadd CTW ulong sub t1 t0 |
| 270 | ok = eq v ex |
| 271 | tally CVP ok |
| 272 | if not ok |
| 273 | mism 86 |
| 274 | end |
| 275 | end |
| 276 | |
| 277 | note self tests: G on the curve; n G = identity; (n - 1) G = (Gx, p - Gy) |
| 278 | fn selft w edit ulong 1024 to void |
| 279 | p2frm w PX PGX PMP |
| 280 | p2frm w PY add PGX 8 PMP |
| 281 | tally CSELF p2oncv w |
| 282 | p2cpy w 900 PMN |
| 283 | p2smul w PP2 900 PGX |
| 284 | tally CSELF p2isz w add PP2 16 |
| 285 | put w 900 sub get w 900 1 |
| 286 | p2smul w PP2 900 PGX |
| 287 | p2aff w PX PY PP2 |
| 288 | p2frm w 910 PGX PMP |
| 289 | p2frm w 920 add PGX 8 PMP |
| 290 | p2add w 920 920 PY PMP |
| 291 | tally CSELF and p2eqv w PX 910 p2isz w 920 |
| 292 | end |
| 293 | |
| 294 | fn report base ulong to void |
| 295 | onum get CT base |
| 296 | r = write 1 TSL 0 len TSL |
| 297 | onum add get CT base get CT add base 1 |
| 298 | end |
| 299 | |
| 300 | fn treport base ulong to void |
| 301 | n = get CT base |
| 302 | onum n |
| 303 | if gt n 0 |
| 304 | r = write 1 TMEAN 0 len TMEAN |
| 305 | onum div div get CT add base 1 n 1000 |
| 306 | r = write 1 TMIN 0 len TMIN |
| 307 | onum div get CT add base 2 1000 |
| 308 | r = write 1 TMAX 0 len TMAX |
| 309 | onum div get CT add base 3 1000 |
| 310 | end |
| 311 | r = write 1 TNL 0 len TNL |
| 312 | end |
| 313 | |
| 314 | note read file argv a into FB; 0 ok, 1 unreadable or too large |
| 315 | fn rdfile a ulong to ulong |
| 316 | pl = argread a PATH 0 4096 |
| 317 | if le pl 0 |
| 318 | ret 1 |
| 319 | end |
| 320 | fd = fopen PATH 0 ulong pl 0 0 |
| 321 | if lt fd 0 |
| 322 | ret 1 |
| 323 | end |
| 324 | got = ulong 0 |
| 325 | while lt got 4194304 bound 100000 |
| 326 | k = read int fd FB got sub 4194304 got |
| 327 | if le k 0 |
| 328 | break |
| 329 | end |
| 330 | got = add got ulong k |
| 331 | end |
| 332 | r = fclose int fd |
| 333 | if or ge got 4194304 ne r 0 |
| 334 | ret 1 |
| 335 | end |
| 336 | put FP 0 0 |
| 337 | put FP 1 got |
| 338 | put FP 2 1 |
| 339 | put FP 3 ulong pl |
| 340 | ret 0 |
| 341 | end |
| 342 | |
| 343 | note after a record only spaces may remain on its line |
| 344 | fn trail to void |
| 345 | skipsp |
| 346 | p = get FP 0 |
| 347 | if lt p get FP 1 |
| 348 | if ne get FB p 10 |
| 349 | bad |
| 350 | end |
| 351 | end |
| 352 | end |
| 353 | |
| 354 | fn dofile w edit ulong 1024 c edit uchar 104 b edit uchar 4096 s edit ulong 64 to void |
| 355 | ch = uchar 0 |
| 356 | while lt get FP 0 get FP 1 bound 999999 |
| 357 | ch = get FB get FP 0 |
| 358 | if or eq ch 35 eq ch 10 |
| 359 | nextln |
| 360 | continue |
| 361 | end |
| 362 | put FP 0 add get FP 0 1 |
| 363 | if eq ch 75 |
| 364 | dok w c b s |
| 365 | else |
| 366 | if eq ch 78 |
| 367 | don w c b s |
| 368 | else |
| 369 | if eq ch 83 |
| 370 | dos w c b s |
| 371 | else |
| 372 | if eq ch 86 |
| 373 | dov w c b s |
| 374 | else |
| 375 | bad |
| 376 | end |
| 377 | end |
| 378 | end |
| 379 | end |
| 380 | trail |
| 381 | nextln |
| 382 | end |
| 383 | end |
| 384 | |
| 385 | fn main to int |
| 386 | w = new ulong 1024 0 |
| 387 | c = new uchar 104 0 |
| 388 | b = new uchar 4096 0 |
| 389 | s = new ulong 64 0 |
| 390 | p2init w |
| 391 | selft w |
| 392 | r = write 1 TSELF 0 len TSELF |
| 393 | onum get CT CSELF |
| 394 | r = write 1 TPASS 0 len TPASS |
| 395 | onum get CT add CSELF 1 |
| 396 | r = write 1 TFAIL 0 len TFAIL |
| 397 | put CT CALL add get CT CALL get CT add CSELF 1 |
| 398 | na = ulong argcnt |
| 399 | for a ulong sub na 1 |
| 400 | if ne rdfile add a 1 0 |
| 401 | r = write 1 TRD 0 len TRD |
| 402 | r = write 1 PATH 0 get FP 3 |
| 403 | r = write 1 TNL 0 len TNL |
| 404 | ret 2 |
| 405 | end |
| 406 | for z ulong 10 |
| 407 | put CT z 0 |
| 408 | end |
| 409 | dofile w c b s |
| 410 | r = write 1 TFILE 0 len TFILE |
| 411 | r = write 1 PATH 0 get FP 3 |
| 412 | r = write 1 TK 0 len TK |
| 413 | report CKP |
| 414 | r = write 1 TN 0 len TN |
| 415 | report CNP |
| 416 | r = write 1 TS 0 len TS |
| 417 | report CSP |
| 418 | r = write 1 TV 0 len TV |
| 419 | report CVP |
| 420 | r = write 1 TBAD 0 len TBAD |
| 421 | onum get CT CBAD |
| 422 | r = write 1 TNL 0 len TNL |
| 423 | end |
| 424 | r = write 1 TTS 0 len TTS |
| 425 | treport CTS |
| 426 | r = write 1 TTV 0 len TTV |
| 427 | treport CTV |
| 428 | r = write 1 TTW 0 len TTW |
| 429 | treport CTW |
| 430 | if eq get CT CALL 0 |
| 431 | r = write 1 TOK 0 len TOK |
| 432 | ret 0 |
| 433 | end |
| 434 | r = write 1 TNO 0 len TNO |
| 435 | ret 1 |
| 436 | end |