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/omnet/blob/main/om/blake2b/test/driver.om # omnet aien-dev/omnet 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 `omnet / om / blake2b / test / driver.om` 337 lines ยท 5757 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note driver.om: program-mode test driver for om/blake2b/blake2b.om (concatenated after it). 3 note 4 note driver reads case lines from stdin, one digest line per case on stdout: 5 note OUTN KEY MSG\n 6 note OUTN decimal 1..64; KEY and MSG are hex (either case, even digit count) or 7 note "-" for empty; KEY at most 64 bytes, MSG at most 65536 bytes. Output is 8 note 2*OUTN lowercase hex digits and a newline. Exit 0 at end of input, 9 note 2 on a malformed line (the digests already written stay on stdout). 10 note driver selftest runs the RFC 7693 Appendix E BLAKE2b self-test and prints its grand hash 11 note (64 hex digits); exit 0 when it equals the RFC value, 1 otherwise. 12 13 text tself "selftest" 14 15 global ib uchar 65536 16 global ist ulong 2 17 18 note next input byte, -1 at end of input 19 fn getc to int 20 if eq get ist 0 get ist 1 21 r = read 0 ib 0 65536 22 if le r 0 23 ret -1 24 end 25 put ist 0 0 26 put ist 1 ulong r 27 end 28 p = get ist 0 29 put ist 0 add p 1 30 ret int get ib p 31 end 32 33 fn hexv c int to int 34 if and ge c 48 le c 57 35 ret sub c 48 36 end 37 if and ge c 97 le c 102 38 ret sub c 87 39 end 40 if and ge c 65 le c 70 41 ret sub c 55 42 end 43 ret -1 44 end 45 46 fn hexd v ulong to uchar 47 if lt v 10 48 ret uchar add v 48 49 end 50 ret uchar add v 87 51 end 52 53 note one byte from two hex digits, the first already read as c; -1 when not hex 54 fn hexbyte c int to int 55 hi = hexv c 56 lo = hexv getc 57 if or lt hi 0 lt lo 0 58 ret -1 59 end 60 ret add mul hi 16 lo 61 end 62 63 note a hex field into a (at most cap bytes) ended by the byte sep; the length, or -1 64 fn rdmsg a edit uchar 65536 cap ulong sep int to long 65 c = getc 66 if eq c 45 67 if eq getc sep 68 ret 0 69 end 70 ret -1 71 end 72 t = ulong 0 73 while ne c sep bound 999999 74 if ge t cap 75 ret -1 76 end 77 v = hexbyte c 78 if lt v 0 79 ret -1 80 end 81 put a t uchar v 82 t = add t 1 83 c = getc 84 end 85 if eq t 0 86 ret -1 87 end 88 ret long t 89 end 90 91 fn rdkey a edit uchar 64 sep int to long 92 c = getc 93 if eq c 45 94 if eq getc sep 95 ret 0 96 end 97 ret -1 98 end 99 t = ulong 0 100 while ne c sep bound 999999 101 if ge t 64 102 ret -1 103 end 104 v = hexbyte c 105 if lt v 0 106 ret -1 107 end 108 put a t uchar v 109 t = add t 1 110 c = getc 111 end 112 if eq t 0 113 ret -1 114 end 115 ret long t 116 end 117 118 note decimal OUTN ended by a space; -2 at end of input before any byte, -1 when malformed 119 fn rdoutn to long 120 c = getc 121 if eq c -1 122 ret -2 123 end 124 t = long 0 125 k = ulong 0 126 while ne c 32 bound 16 127 d = sub c 48 128 if or lt d 0 gt d 9 129 ret -1 130 end 131 if ge k 2 132 ret -1 133 end 134 t = add mul t 10 long d 135 k = add k 1 136 c = getc 137 end 138 if or lt t 1 gt t 64 139 ret -1 140 end 141 ret t 142 end 143 144 note o[0..outn) as lowercase hex and a newline 145 fn emit o view uchar 64 outn ulong to void 146 h = new uchar 129 0 147 for i ulong outn 148 v = ulong get o i 149 put h mul i 2 hexd shr v 4 150 put h add mul i 2 1 hexd and v 15 151 end 152 put h mul outn 2 10 153 r = write 1 h 0 add mul outn 2 1 154 if ne r long add mul outn 2 1 155 trap 1 156 end 157 end 158 159 fn cases to int 160 m = new uchar 65536 0 161 k = new uchar 64 0 162 o = new uchar 64 0 163 while true bound 999999 164 on = rdoutn 165 if eq on -2 166 ret 0 167 end 168 if lt on 0 169 ret 2 170 end 171 kn = rdkey edit k 32 172 if lt kn 0 173 ret 2 174 end 175 n = rdmsg edit m 65536 10 176 if lt n 0 177 ret 2 178 end 179 b2bhash view m ulong n view k ulong kn ulong on edit o 180 emit view o ulong on 181 end 182 ret 2 183 end 184 185 note seqm, seqk, mdlen, inlen, wantres and selftst translate the RFC 7693 Appendix E self-test code; 186 note the RFC's Copyright Notice is in vectors/rfc7693.NOTICE (attribution). 187 note RFC 7693 Appendix E selftest_seq: a = 0xDEAD4BAD * seed, b = 1, out[i] = (a+b) >> 24 (Fibonacci) 188 fn seqm a edit uchar 65536 n ulong seed ulong to void 189 x = and mul `3735899053` seed `4294967295` 190 y = ulong 1 191 for i ulong n 192 t = and add x y `4294967295` 193 x = y 194 y = t 195 put a i uchar shr t 24 196 end 197 end 198 199 fn seqk a edit uchar 64 n ulong seed ulong to void 200 x = and mul `3735899053` seed `4294967295` 201 y = ulong 1 202 for i ulong n 203 t = and add x y `4294967295` 204 x = y 205 y = t 206 put a i uchar shr t 24 207 end 208 end 209 210 note RFC 7693 Appendix E b2b_md_len and b2b_in_len tables 211 fn mdlen i ulong to ulong 212 if eq i 0 213 ret 20 214 end 215 if eq i 1 216 ret 32 217 end 218 if eq i 2 219 ret 48 220 end 221 ret 64 222 end 223 224 fn inlen j ulong to ulong 225 if eq j 0 226 ret 0 227 end 228 if eq j 1 229 ret 3 230 end 231 if eq j 2 232 ret 128 233 end 234 if eq j 3 235 ret 129 236 end 237 if eq j 4 238 ret 255 239 end 240 ret 1024 241 end 242 243 note append md[0..n) to g at offset at 244 fn append g edit uchar 65536 at ulong md view uchar 64 n ulong to void 245 for i ulong n 246 put g add at i get md i 247 end 248 end 249 250 note RFC 7693 Appendix E blake2b_res bytes C2 3A 78 00 D9 81 23 BD 10 F5 06 C6 1E 29 DA 56 251 note 03 D7 63 B8 BB AD 2E 73 7F 5E 76 5A 7B CC D4 75 252 fn wantres r edit uchar 32 to void 253 put r 0 194 254 put r 1 58 255 put r 2 120 256 put r 3 0 257 put r 4 217 258 put r 5 129 259 put r 6 35 260 put r 7 189 261 put r 8 16 262 put r 9 245 263 put r 10 6 264 put r 11 198 265 put r 12 30 266 put r 13 41 267 put r 14 218 268 put r 15 86 269 put r 16 3 270 put r 17 215 271 put r 18 99 272 put r 19 184 273 put r 20 187 274 put r 21 173 275 put r 22 46 276 put r 23 115 277 put r 24 127 278 put r 25 94 279 put r 26 118 280 put r 27 90 281 put r 28 123 282 put r 29 204 283 put r 30 212 284 put r 31 117 285 end 286 287 note the grand hash is BLAKE2b-256 (outn 32) of all 48 digests in order; feeding them one update at a time 288 note (as Appendix E does) and hashing their concatenation once give the same digest 289 fn selftst to int 290 m = new uchar 65536 0 291 g = new uchar 65536 0 292 k = new uchar 64 0 293 md = new uchar 64 0 294 r = new uchar 32 0 295 at = ulong 0 296 for i ulong 4 297 ol = mdlen i 298 for j ulong 6 299 il = inlen j 300 seqm edit m il il 301 b2bhash view m il view k 0 ol edit md 302 append edit g at view md ol 303 at = add at ol 304 seqk edit k ol ol 305 b2bhash view m il view k ol ol edit md 306 append edit g at view md ol 307 at = add at ol 308 end 309 end 310 b2bhash view g at view k 0 32 edit md 311 emit view md 32 312 wantres edit r 313 bad = ulong 0 314 for i ulong 32 315 if ne get md i get r i 316 bad = 1 317 end 318 end 319 ret int bad 320 end 321 322 fn main to int 323 if eq argcnt 1 324 ret cases 325 end 326 a = new uchar 16 0 327 l = argread 1 a 0 16 328 if ne l 8 329 ret 2 330 end 331 for i ulong 8 332 if ne get a i get tself i 333 ret 2 334 end 335 end 336 ret selftst 337 end