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 / chacha / bench.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note bench.om: 1 MiB AEAD_CHACHA20_POLY1305 round trip, built as `om2`, chacha.om, bench.om. |
| 3 | note key = bytes 0..31, nonce = bytes 32..43 (kn[i] = i), aad = 12 bytes 160..171, plaintext byte i = |
| 4 | note (31 i + 7) mod 256. Writes the sealed buffer (1 MiB ciphertext || tag) to stdout once, then |
| 5 | note opens and reseals it ROUNDS times, checking every open returns 0 with the plaintext restored |
| 6 | note and the resealed buffer equal to the first. Exit 0 when all checks hold, 1 otherwise. run.sh |
| 7 | note compares the stdout bytes with an independent oracle and times the whole run: |
| 8 | note (1 + 2 ROUNDS) MiB pass through the AEAD. |
| 9 | |
| 10 | const BROUNDS ulong 16 |
| 11 | |
| 12 | note fill b[0..1 MiB) with the pattern |
| 13 | fn bfill b edit uchar * to void |
| 14 | for h ulong 16 |
| 15 | for l ulong 65536 |
| 16 | i = add shl h 16 l |
| 17 | put b i uchar and add mul i 31 7 255 |
| 18 | end |
| 19 | end |
| 20 | end |
| 21 | |
| 22 | note number of bytes of b[0..1 MiB) that differ from the pattern |
| 23 | fn bcheck b view uchar * to ulong |
| 24 | d = ulong 0 |
| 25 | for h ulong 16 |
| 26 | for l ulong 65536 |
| 27 | i = add shl h 16 l |
| 28 | if ne get b i uchar and add mul i 31 7 255 |
| 29 | d = add d 1 |
| 30 | end |
| 31 | end |
| 32 | end |
| 33 | ret d |
| 34 | end |
| 35 | |
| 36 | note number of bytes where a and b differ (same length) |
| 37 | fn bdiff a view uchar * b view uchar * to ulong |
| 38 | d = ulong 0 |
| 39 | for h ulong 17 |
| 40 | for l ulong 65536 |
| 41 | i = add shl h 16 l |
| 42 | if lt i len a |
| 43 | if ne get a i get b i |
| 44 | d = add d 1 |
| 45 | end |
| 46 | end |
| 47 | end |
| 48 | end |
| 49 | ret d |
| 50 | end |
| 51 | |
| 52 | fn main to int |
| 53 | b = new uchar 1048592 0 |
| 54 | c = new uchar 1048592 0 |
| 55 | kn = new uchar 44 0 |
| 56 | ad = new uchar 12 0 |
| 57 | for i ulong 44 |
| 58 | put kn i uchar i |
| 59 | end |
| 60 | for i ulong 12 |
| 61 | put ad i uchar add i 160 |
| 62 | end |
| 63 | bfill b |
| 64 | bad = aead_chacha_seal kn ad b |
| 65 | for h ulong 17 |
| 66 | for l ulong 65536 |
| 67 | i = add shl h 16 l |
| 68 | if lt i len b |
| 69 | put c i get b i |
| 70 | end |
| 71 | end |
| 72 | end |
| 73 | t = ulong 0 |
| 74 | while lt t len b bound 999999 |
| 75 | w = write 1 b t sub len b t |
| 76 | if le w 0 |
| 77 | ret 1 |
| 78 | end |
| 79 | t = add t ulong w |
| 80 | end |
| 81 | for j ulong BROUNDS |
| 82 | bad = or bad aead_chacha_open kn ad b |
| 83 | bad = or bad bcheck part b 0 1048576 |
| 84 | bad = or bad aead_chacha_seal kn ad b |
| 85 | bad = or bad bdiff b c |
| 86 | end |
| 87 | if ne bad 0 |
| 88 | ret 1 |
| 89 | end |
| 90 | ret 0 |
| 91 | end |