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 / x25519 / driver.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note driver.om: test driver for om/tls/x25519.om (program mode). run.sh writes the line om2, then |
| 3 | note x25519.om, then this file. Reads a vector file on stdin, one case per line: |
| 4 | note X R KHEX UHEX OHEX x25519_scalarmult(k, u) must return R (0 or 1) and write OHEX |
| 5 | note B KHEX OHEX x25519_base(k) must return 0 and write OHEX |
| 6 | note I N OHEX RFC 7748 section 5.2 iteration from k = u = 9, N >= 1 steps |
| 7 | note L length checks: five calls with a 31- or 33-byte o, k or u must return 2 |
| 8 | note T N KHEX UHEX timing: N calls of x25519_scalarmult, prints TIME line L n N ns T |
| 9 | note Lines starting with # and empty lines are skipped. Prints one MISMATCH line per wrong result |
| 10 | note and the summary line: cases C calls V mismatches X ns T (T = ns spent in checked calls). |
| 11 | note Exit 0 only when at least one checked call ran and none mismatched; 1 otherwise; 65 bad input. |
| 12 | |
| 13 | text XCTMIS "MISMATCH line " |
| 14 | text XCTGOT " got rc " |
| 15 | text XCTWANT " want rc " |
| 16 | text XCTCASE "cases " |
| 17 | text XCTCALL " calls " |
| 18 | text XCTMM " mismatches " |
| 19 | text XCTNS " ns " |
| 20 | text XCTTIME "TIME line " |
| 21 | text XCTN " n " |
| 22 | text XCTNL "\n" |
| 23 | text XCTBAD "driver: bad input\n" |
| 24 | |
| 25 | global XCIN uchar 16777216 |
| 26 | global XCLEN ulong 1 |
| 27 | global XCOUT uchar 32 |
| 28 | global XCCNT ulong 8 |
| 29 | |
| 30 | fn x25519t_num v ulong to void |
| 31 | k = ulong 0 |
| 32 | x = v |
| 33 | while true bound 30 |
| 34 | put XCOUT sub 31 k uchar add rem x 10 48 |
| 35 | k = add k 1 |
| 36 | x = div x 10 |
| 37 | if eq x 0 |
| 38 | break |
| 39 | end |
| 40 | end |
| 41 | r = write 1 XCOUT sub 32 k k |
| 42 | end |
| 43 | |
| 44 | fn x25519t_bad to void |
| 45 | r = write 2 XCTBAD 0 len XCTBAD |
| 46 | exit 65 |
| 47 | end |
| 48 | |
| 49 | fn x25519t_nib c ulong to ulong |
| 50 | if and ge c 48 le c 57 |
| 51 | ret sub c 48 |
| 52 | end |
| 53 | if and ge c 97 le c 102 |
| 54 | ret sub c 87 |
| 55 | end |
| 56 | x25519t_bad |
| 57 | ret 0 |
| 58 | end |
| 59 | |
| 60 | note 32 bytes from 64 hex digits at XCIN[o..], followed by a space or the newline |
| 61 | fn x25519t_hex a edit uchar 32 o ulong to void |
| 62 | for i ulong 32 |
| 63 | hi = x25519t_nib ulong get XCIN add o mul i 2 |
| 64 | lo = x25519t_nib ulong get XCIN add add o mul i 2 1 |
| 65 | put a i uchar or shl hi 4 lo |
| 66 | end |
| 67 | c = get XCIN add o 64 |
| 68 | if and ne c 32 ne c 10 |
| 69 | x25519t_bad |
| 70 | end |
| 71 | end |
| 72 | |
| 73 | note decimal number at XCIN[o..] up to a space or newline |
| 74 | fn x25519t_dec o ulong to ulong |
| 75 | v = ulong 0 |
| 76 | p = o |
| 77 | while and ge get XCIN p 48 le get XCIN p 57 bound 20 |
| 78 | v = add mul v 10 sub ulong get XCIN p 48 |
| 79 | p = add p 1 |
| 80 | end |
| 81 | if or eq p o and ne get XCIN p 32 ne get XCIN p 10 |
| 82 | x25519t_bad |
| 83 | end |
| 84 | ret v |
| 85 | end |
| 86 | |
| 87 | note position after the next space at or after o (before e) |
| 88 | fn x25519t_fld o ulong e ulong to ulong |
| 89 | p = o |
| 90 | while ne get XCIN p 32 bound 30 |
| 91 | if ge p e |
| 92 | x25519t_bad |
| 93 | end |
| 94 | p = add p 1 |
| 95 | end |
| 96 | ret add p 1 |
| 97 | end |
| 98 | |
| 99 | fn x25519t_bump i ulong v ulong to void |
| 100 | put XCCNT i add get XCCNT i v |
| 101 | end |
| 102 | |
| 103 | note counters in XCCNT: 0 line, 1 cases, 2 calls, 3 mismatches, 4 ns |
| 104 | fn x25519t_mis got ulong want ulong to void |
| 105 | x25519t_bump 3 1 |
| 106 | r = write 1 XCTMIS 0 len XCTMIS |
| 107 | x25519t_num get XCCNT 0 |
| 108 | r = write 1 XCTGOT 0 len XCTGOT |
| 109 | x25519t_num got |
| 110 | r = write 1 XCTWANT 0 len XCTWANT |
| 111 | x25519t_num want |
| 112 | r = write 1 XCTNL 0 1 |
| 113 | end |
| 114 | |
| 115 | note one checked result: rc got against want, bytes o against x |
| 116 | fn x25519t_check got ulong want ulong o view uchar * x view uchar 32 to void |
| 117 | x25519t_bump 2 1 |
| 118 | d = ulong 0 |
| 119 | for i ulong 32 |
| 120 | d = or d ulong xor get o i get x i |
| 121 | end |
| 122 | if or ne got want ne d 0 |
| 123 | x25519t_mis got want |
| 124 | end |
| 125 | end |
| 126 | |
| 127 | fn x25519t_x p ulong e ulong to void |
| 128 | k = new uchar 32 0 |
| 129 | u = new uchar 32 0 |
| 130 | o = new uchar 32 0 |
| 131 | x = new uchar 32 0 |
| 132 | if ne sub e p 198 |
| 133 | x25519t_bad |
| 134 | end |
| 135 | want = x25519t_nib ulong get XCIN add p 2 |
| 136 | x25519t_hex k add p 4 |
| 137 | x25519t_hex u add p 69 |
| 138 | x25519t_hex x add p 134 |
| 139 | t0 = monons |
| 140 | got = x25519_scalarmult o k u |
| 141 | x25519t_bump 4 ulong sub monons t0 |
| 142 | x25519t_check got want o x |
| 143 | end |
| 144 | |
| 145 | fn x25519t_b p ulong e ulong to void |
| 146 | k = new uchar 32 0 |
| 147 | o = new uchar 32 0 |
| 148 | x = new uchar 32 0 |
| 149 | if ne sub e p 131 |
| 150 | x25519t_bad |
| 151 | end |
| 152 | x25519t_hex k add p 2 |
| 153 | x25519t_hex x add p 67 |
| 154 | t0 = monons |
| 155 | got = x25519_base o k |
| 156 | x25519t_bump 4 ulong sub monons t0 |
| 157 | x25519t_check got 0 o x |
| 158 | end |
| 159 | |
| 160 | note one iteration step: o = X25519(k, u), u = k, k = o; returns the rc |
| 161 | fn x25519t_step k edit uchar 32 u edit uchar 32 o edit uchar 32 to ulong |
| 162 | r = x25519_scalarmult o k u |
| 163 | for i ulong 32 |
| 164 | put u i get k i |
| 165 | put k i get o i |
| 166 | end |
| 167 | ret r |
| 168 | end |
| 169 | |
| 170 | fn x25519t_i p ulong e ulong to void |
| 171 | k = new uchar 32 0 |
| 172 | u = new uchar 32 0 |
| 173 | o = new uchar 32 0 |
| 174 | x = new uchar 32 0 |
| 175 | put k 0 9 |
| 176 | put u 0 9 |
| 177 | n = x25519t_dec add p 2 |
| 178 | q = x25519t_fld add p 2 e |
| 179 | if or eq n 0 ne sub e q 64 |
| 180 | x25519t_bad |
| 181 | end |
| 182 | x25519t_hex x q |
| 183 | bad = ulong 0 |
| 184 | t0 = monons |
| 185 | for a ulong div n 1000 |
| 186 | for b ulong 1000 |
| 187 | bad = or bad x25519t_step k u o |
| 188 | end |
| 189 | end |
| 190 | for b ulong rem n 1000 |
| 191 | bad = or bad x25519t_step k u o |
| 192 | end |
| 193 | x25519t_bump 4 ulong sub monons t0 |
| 194 | x25519t_check bad 0 k x |
| 195 | end |
| 196 | |
| 197 | fn x25519t_l to void |
| 198 | o = new uchar 32 0 |
| 199 | k = new uchar 32 7 |
| 200 | u = new uchar 32 9 |
| 201 | s = new uchar 31 7 |
| 202 | g = new uchar 33 7 |
| 203 | x = new uchar 32 0 |
| 204 | x25519t_check x25519_scalarmult s k u 2 o x |
| 205 | x25519t_check x25519_scalarmult o s u 2 o x |
| 206 | x25519t_check x25519_scalarmult o k g 2 o x |
| 207 | x25519t_check x25519_base g k 2 o x |
| 208 | x25519t_check x25519_base o s 2 o x |
| 209 | end |
| 210 | |
| 211 | fn x25519t_t p ulong e ulong to void |
| 212 | k = new uchar 32 0 |
| 213 | u = new uchar 32 0 |
| 214 | o = new uchar 32 0 |
| 215 | n = x25519t_dec add p 2 |
| 216 | q = x25519t_fld add p 2 e |
| 217 | if ne sub e q 129 |
| 218 | x25519t_bad |
| 219 | end |
| 220 | x25519t_hex k q |
| 221 | x25519t_hex u add q 65 |
| 222 | t0 = monons |
| 223 | for i ulong n |
| 224 | r = x25519_scalarmult o k u |
| 225 | end |
| 226 | dt = ulong sub monons t0 |
| 227 | r = write 1 XCTTIME 0 len XCTTIME |
| 228 | x25519t_num get XCCNT 0 |
| 229 | r = write 1 XCTN 0 len XCTN |
| 230 | x25519t_num n |
| 231 | r = write 1 XCTNS 0 len XCTNS |
| 232 | x25519t_num dt |
| 233 | r = write 1 XCTNL 0 1 |
| 234 | end |
| 235 | |
| 236 | fn x25519t_slurp to void |
| 237 | t = ulong 0 |
| 238 | while true bound 999999 |
| 239 | room = sub len XCIN t |
| 240 | if eq room 0 |
| 241 | x25519t_bad |
| 242 | end |
| 243 | r = read 0 XCIN t room |
| 244 | if lt r 0 |
| 245 | x25519t_bad |
| 246 | end |
| 247 | if eq r 0 |
| 248 | break |
| 249 | end |
| 250 | t = add t ulong r |
| 251 | end |
| 252 | put XCLEN 0 t |
| 253 | end |
| 254 | |
| 255 | note the case on XCIN[p..e), e at its newline |
| 256 | fn x25519t_line p ulong e ulong to void |
| 257 | c = get XCIN p |
| 258 | if or eq e p eq c 35 |
| 259 | ret |
| 260 | end |
| 261 | x25519t_bump 1 1 |
| 262 | if eq c 88 |
| 263 | x25519t_x p e |
| 264 | ret |
| 265 | end |
| 266 | if eq c 66 |
| 267 | x25519t_b p e |
| 268 | ret |
| 269 | end |
| 270 | if eq c 73 |
| 271 | x25519t_i p e |
| 272 | ret |
| 273 | end |
| 274 | if and eq c 76 eq sub e p 1 |
| 275 | x25519t_l |
| 276 | ret |
| 277 | end |
| 278 | if eq c 84 |
| 279 | x25519t_bump 1 0 |
| 280 | x25519t_t p e |
| 281 | ret |
| 282 | end |
| 283 | x25519t_bad |
| 284 | end |
| 285 | |
| 286 | fn main to int |
| 287 | x25519t_slurp |
| 288 | tot = get XCLEN 0 |
| 289 | if or eq tot 0 ne get XCIN sub tot 1 10 |
| 290 | x25519t_bad |
| 291 | end |
| 292 | p = ulong 0 |
| 293 | while lt p tot bound 999999 |
| 294 | x25519t_bump 0 1 |
| 295 | e = p |
| 296 | while ne get XCIN e 10 bound 999999 |
| 297 | e = add e 1 |
| 298 | end |
| 299 | x25519t_line p e |
| 300 | p = add e 1 |
| 301 | end |
| 302 | r = write 1 XCTCASE 0 len XCTCASE |
| 303 | x25519t_num get XCCNT 1 |
| 304 | r = write 1 XCTCALL 0 len XCTCALL |
| 305 | x25519t_num get XCCNT 2 |
| 306 | r = write 1 XCTMM 0 len XCTMM |
| 307 | x25519t_num get XCCNT 3 |
| 308 | r = write 1 XCTNS 0 len XCTNS |
| 309 | x25519t_num get XCCNT 4 |
| 310 | r = write 1 XCTNL 0 1 |
| 311 | if or gt get XCCNT 3 0 eq get XCCNT 2 0 |
| 312 | ret 1 |
| 313 | end |
| 314 | ret 0 |
| 315 | end |