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
omega-mv / om / tls / test / ec / p256_driver.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note p256_driver.om: test driver for the P-256 p2_* section of om/tls/ec.om (program mode). run.sh |
| 3 | note writes the line om2, then ec.om, then this file. Reads a vector file on stdin, one case per line; H is 64 lowercase hex |
| 4 | note digits (a 256-bit number, big-endian), fields separated by one space: |
| 5 | note M A B R p2_mul: A * B mod p must be R |
| 6 | note S A R p2_sqr: A^2 mod p must be R |
| 7 | note A A B R p2_add: A + B mod p must be R (A, B < p) |
| 8 | note U A B R p2_sub: A - B mod p must be R (A, B < p) |
| 9 | note I A R p2_inv: A^(p-2) mod p must be R |
| 10 | note K k X Y RX RY F p2_smul: k * (X, Y) must be (RX, RY) with finite flag F (0 or 1) |
| 11 | note D u v X Y RX RY F p2_smul2: u * G + v * (X, Y) must be (RX, RY) with flag F |
| 12 | note T N k X Y timing: N calls of p2_smul, prints TIME smul n N ns T |
| 13 | note W N u v X Y timing: N calls of p2_smul2, prints TIME smul2 n N ns T |
| 14 | note F N A B timing: N calls of p2_mul, prints TIME mul n N ns T |
| 15 | note Lines starting with # and empty lines are skipped. Prints one MISMATCH line per wrong result |
| 16 | note and the summary line: cases C calls V mismatches X. Exit 0 only when at least one checked call |
| 17 | note ran and none mismatched; 1 otherwise; 65 bad input. |
| 18 | |
| 19 | text P2TMIS "MISMATCH line " |
| 20 | text P2TCASE "cases " |
| 21 | text P2TCALL " calls " |
| 22 | text P2TMM " mismatches " |
| 23 | text P2TTIME "TIME " |
| 24 | text P2TSM "smul" |
| 25 | text P2TSM2 "smul2" |
| 26 | text P2TMUL "mul" |
| 27 | text P2TN " n " |
| 28 | text P2TNS " ns " |
| 29 | text P2TNL "\n" |
| 30 | text P2TBAD "driver: bad input\n" |
| 31 | |
| 32 | global P2IN uchar 16777216 |
| 33 | global P2LEN ulong 1 |
| 34 | global P2OUT uchar 32 |
| 35 | global P2CNT ulong 8 |
| 36 | |
| 37 | fn p2t_num v ulong to void |
| 38 | k = ulong 0 |
| 39 | x = v |
| 40 | while true bound 30 |
| 41 | put P2OUT sub 31 k uchar add rem x 10 48 |
| 42 | k = add k 1 |
| 43 | x = div x 10 |
| 44 | if eq x 0 |
| 45 | break |
| 46 | end |
| 47 | end |
| 48 | r = write 1 P2OUT sub 32 k k |
| 49 | end |
| 50 | |
| 51 | fn p2t_bad to void |
| 52 | r = write 2 P2TBAD 0 len P2TBAD |
| 53 | exit 65 |
| 54 | end |
| 55 | |
| 56 | fn p2t_nib c ulong to ulong |
| 57 | if and ge c 48 le c 57 |
| 58 | ret sub c 48 |
| 59 | end |
| 60 | if and ge c 97 le c 102 |
| 61 | ret sub c 87 |
| 62 | end |
| 63 | p2t_bad |
| 64 | ret 0 |
| 65 | end |
| 66 | |
| 67 | note field f (0-based) of the line at p: 64 hex digits at p + 2 + 65 f, then a space or newline, |
| 68 | note into 8 little-endian 32-bit limbs at a[o..] |
| 69 | fn p2t_hex a edit ulong * o ulong p ulong f ulong to void |
| 70 | s = add add p 2 mul f 65 |
| 71 | for j ulong 8 |
| 72 | v = ulong 0 |
| 73 | q = add s mul sub 7 j 8 |
| 74 | for d ulong 8 |
| 75 | c = ulong get P2IN add q d |
| 76 | v = or shl v 4 p2t_nib c |
| 77 | end |
| 78 | put a add o j v |
| 79 | end |
| 80 | c = get P2IN add s 64 |
| 81 | if and ne c 32 ne c 10 |
| 82 | p2t_bad |
| 83 | end |
| 84 | end |
| 85 | |
| 86 | note decimal number at P2IN[o..] up to a space; returns it, position after the space in P2CNT 7 |
| 87 | fn p2t_dec o ulong to ulong |
| 88 | v = ulong 0 |
| 89 | p = o |
| 90 | while and ge get P2IN p 48 le get P2IN p 57 bound 20 |
| 91 | v = add mul v 10 sub ulong get P2IN p 48 |
| 92 | p = add p 1 |
| 93 | end |
| 94 | if or eq p o ne get P2IN p 32 |
| 95 | p2t_bad |
| 96 | end |
| 97 | put P2CNT 7 add p 1 |
| 98 | ret v |
| 99 | end |
| 100 | |
| 101 | fn p2t_bump i ulong v ulong to void |
| 102 | put P2CNT i add get P2CNT i v |
| 103 | end |
| 104 | |
| 105 | note compare n limbs of the workspace at o with x[0..n) and the flags got, want |
| 106 | fn p2t_check w view ulong P2WN x view ulong 24 o ulong n ulong got ulong want ulong to void |
| 107 | p2t_bump 2 1 |
| 108 | d = xor got want |
| 109 | for j ulong n |
| 110 | d = or d xor get w add o j get x j |
| 111 | end |
| 112 | if ne d 0 |
| 113 | p2t_bump 3 1 |
| 114 | r = write 1 P2TMIS 0 len P2TMIS |
| 115 | p2t_num get P2CNT 0 |
| 116 | r = write 1 P2TNL 0 1 |
| 117 | end |
| 118 | end |
| 119 | |
| 120 | fn p2t_time what view uchar * n ulong t ulong to void |
| 121 | r = write 1 P2TTIME 0 len P2TTIME |
| 122 | r = write 1 what 0 len what |
| 123 | r = write 1 P2TN 0 len P2TN |
| 124 | p2t_num n |
| 125 | r = write 1 P2TNS 0 len P2TNS |
| 126 | p2t_num t |
| 127 | r = write 1 P2TNL 0 1 |
| 128 | end |
| 129 | |
| 130 | note expected width of a line with k hex fields after the op letter |
| 131 | fn p2t_need p ulong e ulong k ulong to void |
| 132 | if ne sub e p add 1 mul k 65 |
| 133 | p2t_bad |
| 134 | end |
| 135 | end |
| 136 | |
| 137 | fn p2t_line p ulong e ulong w edit ulong P2WN x edit ulong 24 to void |
| 138 | c = get P2IN p |
| 139 | if or eq e p eq c 35 |
| 140 | ret |
| 141 | end |
| 142 | p2t_bump 1 1 |
| 143 | if eq c 77 |
| 144 | p2t_need p e 3 |
| 145 | p2t_hex w 0 p 0 |
| 146 | p2t_hex w 8 p 1 |
| 147 | p2t_hex x 0 p 2 |
| 148 | p2_mul w 16 0 8 |
| 149 | p2t_check w x 16 8 0 0 |
| 150 | ret |
| 151 | end |
| 152 | if eq c 83 |
| 153 | p2t_need p e 2 |
| 154 | p2t_hex w 0 p 0 |
| 155 | p2t_hex x 0 p 1 |
| 156 | p2_sqr w 16 0 |
| 157 | p2t_check w x 16 8 0 0 |
| 158 | ret |
| 159 | end |
| 160 | if eq c 65 |
| 161 | p2t_need p e 3 |
| 162 | p2t_hex w 0 p 0 |
| 163 | p2t_hex w 8 p 1 |
| 164 | p2t_hex x 0 p 2 |
| 165 | p2_add w 16 0 8 |
| 166 | p2t_check w x 16 8 0 0 |
| 167 | ret |
| 168 | end |
| 169 | if eq c 85 |
| 170 | p2t_need p e 3 |
| 171 | p2t_hex w 0 p 0 |
| 172 | p2t_hex w 8 p 1 |
| 173 | p2t_hex x 0 p 2 |
| 174 | p2_sub w 16 0 8 |
| 175 | p2t_check w x 16 8 0 0 |
| 176 | ret |
| 177 | end |
| 178 | if eq c 73 |
| 179 | p2t_need p e 2 |
| 180 | p2t_hex w 0 p 0 |
| 181 | p2t_hex x 0 p 1 |
| 182 | p2_inv w 16 0 |
| 183 | p2t_check w x 16 8 0 0 |
| 184 | ret |
| 185 | end |
| 186 | if eq c 75 |
| 187 | if ne sub e p 328 |
| 188 | p2t_bad |
| 189 | end |
| 190 | p2t_hex w P2K p 0 |
| 191 | p2t_hex w P2P p 1 |
| 192 | p2t_hex w add P2P 8 p 2 |
| 193 | p2t_hex x 0 p 3 |
| 194 | p2t_hex x 8 p 4 |
| 195 | want = p2t_nib ulong get P2IN add p 327 |
| 196 | got = p2_smul w |
| 197 | p2t_check w x P2O 16 got want |
| 198 | ret |
| 199 | end |
| 200 | if eq c 68 |
| 201 | if ne sub e p 393 |
| 202 | p2t_bad |
| 203 | end |
| 204 | p2t_hex w P2K p 0 |
| 205 | p2t_hex w P2U p 1 |
| 206 | p2t_hex w P2Q p 2 |
| 207 | p2t_hex w add P2Q 8 p 3 |
| 208 | p2t_hex x 0 p 4 |
| 209 | p2t_hex x 8 p 5 |
| 210 | want = p2t_nib ulong get P2IN add p 392 |
| 211 | got = p2_smul2 w |
| 212 | p2t_check w x P2O 16 got want |
| 213 | ret |
| 214 | end |
| 215 | if or or eq c 84 eq c 87 eq c 70 |
| 216 | p2t_bump 1 0 |
| 217 | n = p2t_dec add p 2 |
| 218 | q = sub sub get P2CNT 7 p 2 |
| 219 | if eq c 84 |
| 220 | p2t_need add p q e 3 |
| 221 | p2t_hex w P2K add p q 0 |
| 222 | p2t_hex w P2P add p q 1 |
| 223 | p2t_hex w add P2P 8 add p q 2 |
| 224 | t0 = monons |
| 225 | for i ulong n |
| 226 | r = p2_smul w |
| 227 | end |
| 228 | p2t_time P2TSM n ulong sub monons t0 |
| 229 | ret |
| 230 | end |
| 231 | if eq c 87 |
| 232 | p2t_need add p q e 4 |
| 233 | p2t_hex w P2K add p q 0 |
| 234 | p2t_hex w P2U add p q 1 |
| 235 | p2t_hex w P2Q add p q 2 |
| 236 | p2t_hex w add P2Q 8 add p q 3 |
| 237 | t0 = monons |
| 238 | for i ulong n |
| 239 | r = p2_smul2 w |
| 240 | end |
| 241 | p2t_time P2TSM2 n ulong sub monons t0 |
| 242 | ret |
| 243 | end |
| 244 | p2t_need add p q e 2 |
| 245 | p2t_hex w 0 add p q 0 |
| 246 | p2t_hex w 8 add p q 1 |
| 247 | t0 = monons |
| 248 | for i ulong n |
| 249 | p2_mul w 16 0 8 |
| 250 | end |
| 251 | p2t_time P2TMUL n ulong sub monons t0 |
| 252 | ret |
| 253 | end |
| 254 | p2t_bad |
| 255 | end |
| 256 | |
| 257 | fn p2t_slurp to void |
| 258 | t = ulong 0 |
| 259 | while true bound 999999 |
| 260 | room = sub len P2IN t |
| 261 | if eq room 0 |
| 262 | p2t_bad |
| 263 | end |
| 264 | r = read 0 P2IN t room |
| 265 | if lt r 0 |
| 266 | p2t_bad |
| 267 | end |
| 268 | if eq r 0 |
| 269 | break |
| 270 | end |
| 271 | t = add t ulong r |
| 272 | end |
| 273 | put P2LEN 0 t |
| 274 | end |
| 275 | |
| 276 | fn main to int |
| 277 | w = new ulong P2WN 0 |
| 278 | x = new ulong 24 0 |
| 279 | p2_init w |
| 280 | p2t_slurp |
| 281 | tot = get P2LEN 0 |
| 282 | if or eq tot 0 ne get P2IN sub tot 1 10 |
| 283 | p2t_bad |
| 284 | end |
| 285 | p = ulong 0 |
| 286 | while lt p tot bound 999999 |
| 287 | p2t_bump 0 1 |
| 288 | e = p |
| 289 | while ne get P2IN e 10 bound 999999 |
| 290 | e = add e 1 |
| 291 | end |
| 292 | p2t_line p e w x |
| 293 | p = add e 1 |
| 294 | end |
| 295 | r = write 1 P2TCASE 0 len P2TCASE |
| 296 | p2t_num get P2CNT 1 |
| 297 | r = write 1 P2TCALL 0 len P2TCALL |
| 298 | p2t_num get P2CNT 2 |
| 299 | r = write 1 P2TMM 0 len P2TMM |
| 300 | p2t_num get P2CNT 3 |
| 301 | r = write 1 P2TNL 0 1 |
| 302 | if or gt get P2CNT 3 0 eq get P2CNT 2 0 |
| 303 | ret 1 |
| 304 | end |
| 305 | ret 0 |
| 306 | end |