interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / sha256 / parity / receipts.c

96 lines · 5448 bytes Raw Blame History
1/* Section 7 / erratum E3 receipts for om/sha256/sha256-om2.om through the generated API (sha256.h ->
2 * omega_api_decl.h -> omega2_sha256_*), against the historical C (src/sha256.c @ a647b6a9, renamed orc_*).
3 * receipts receipts one line per check, exit 1 on any FAIL (run.sh)
4 * receipts vec stdin -> hex digest of sha256_hash; exit 4 if init/update(random splits)/final differ
5 * receipts big one sha256_update of 2^32 + 4094 bytes and sha256_hash of 2^32 + 4097, against
6 * the C (4 GiB, about 50 s on GB10; not in run.sh) */
7#include "sha256.h"
8#include <stdio.h>
9#include <string.h>
10#include <sys/mman.h>
11typedef struct { unsigned char b[104]; } orc_ctx;
12void orc_sha256_init(orc_ctx *);
13void orc_sha256_update(orc_ctx *, const uint8_t *, size_t);
14void orc_sha256_final(orc_ctx *, uint8_t *);
15void orc_sha256_hash(const uint8_t *, size_t, uint8_t *);
16static uint64_t rs = 838;
17static uint64_t rnd(void) { rs += 0x9E3779B97F4A7C15ull; uint64_t z = rs; z = (z ^ z >> 30) * 0xBF58476D1CE4E5B9ull; z = (z ^ z >> 27) * 0x94D049BB133111EBull; return z ^ z >> 31; }
18static int fails;
19static void check(int ok, const char *what) { printf("%s %s\n", ok ? "PASS" : "FAIL", what); if (!ok) fails++; }
20static void split(const uint8_t *m, size_t n, uint8_t *out)
21{
22 static const size_t ch[] = {0, 1, 3, 55, 56, 63, 64, 65, 127, 128, 200, 4096};
23 sha256_ctx c; sha256_init(&c);
24 for (size_t p = 0; p < n;) { size_t k = ch[rnd() % 12]; if (k > n - p) k = n - p; sha256_update(&c, m + p, k); p += k; }
25 sha256_final(&c, out);
26}
27int main(int argc, char **argv)
28{
29 uint8_t a[32], b[32], c[32];
30 if (argc > 1 && !strcmp(argv[1], "vec")) {
31 static uint8_t m[1 << 21]; size_t n = fread(m, 1, sizeof m, stdin);
32 sha256_hash(m, n, a); split(m, n, b);
33 for (int i = 0; i < 32; i++) printf("%02x", a[i]);
34 printf("\n");
35 return memcmp(a, b, 32) ? 4 : 0;
36 }
37 if (argc > 1 && !strcmp(argv[1], "big")) {
38 size_t n = (1ull << 32) + 4097;
39 uint8_t *m = mmap(0, n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
40 if (m == MAP_FAILED) { puts("NOT_RUN mmap"); return 3; }
41 for (size_t i = 0; i < n; i++) m[i] = (uint8_t)(i % 251);
42 sha256_ctx x; sha256_init(&x); sha256_update(&x, m, 3); sha256_update(&x, m + 3, n - 3); sha256_final(&x, a);
43 sha256_hash(m, n, b); orc_sha256_hash(m, n, c);
44 check(!memcmp(a, c, 32), "update of 2^32+4094 bytes (after 3) equals C");
45 check(!memcmp(b, c, 32), "hash of 2^32+4097 bytes equals C");
46 return fails != 0;
47 }
48 static uint8_t m[20000], d[64];
49 for (size_t i = 0; i < sizeof m; i++) m[i] = (uint8_t)rnd();
50 int bad = 0;
51 for (size_t n = 0; n <= 9000; n++) {
52 orc_sha256_hash(m, n, c); sha256_hash(m, n, a); split(m, n, b);
53 if (memcmp(a, c, 32) || memcmp(b, c, 32)) bad++;
54 }
55 check(!bad, "lengths 0..9000: hash and split update equal the C");
56 bad = 0;
57 for (int t = 0; t < 2000; t++) {
58 size_t n = rnd() % sizeof m; orc_sha256_hash(m, n, c); sha256_hash(m, n, a); split(m, n, b);
59 if (memcmp(a, c, 32) || memcmp(b, c, 32)) bad++;
60 }
61 check(!bad, "2000 random lengths < 20000: hash and split update equal the C");
62 /* E3 (2): hash(d, 32, d), iterated as tests/runtime/rx_sem_incremental.c:183 */
63 memcpy(d, m, 32); memcpy(c, m, 32);
64 for (int i = 0; i < 100; i++) { sha256_hash(d, 32, d); orc_sha256_hash(c, 32, c); }
65 check(!memcmp(d, c, 32), "hash(d,32,d) x100 equals the C");
66 /* src/omega_program.c:777 shape: digest inside the hashed range at offset 0 */
67 memcpy(d, m, 64); orc_sha256_hash(d, 64, c); sha256_hash(d, 64, d);
68 check(!memcmp(d, c, 32), "hash(d,64,d) (digest overlaps the first 32 bytes) equals the C");
69 /* E3 (1): byte copy of a live context after k updates, both continued */
70 bad = 0;
71 for (size_t k = 0; k < 300; k++) {
72 sha256_ctx x, y; sha256_init(&x);
73 for (size_t i = 0; i < k; i++) sha256_update(&x, m + i * 7, 7);
74 memcpy(&y, &x, sizeof x);
75 sha256_update(&x, m + k * 7, 1000); sha256_update(&y, m + k * 7, 1000);
76 sha256_final(&x, a); sha256_final(&y, b); orc_sha256_hash(m, k * 7 + 1000, c);
77 if (memcmp(a, c, 32) || memcmp(b, c, 32)) bad++;
78 }
79 check(!bad, "copy of a live context after k = 0..299 updates of 7 bytes, both continued, equals the C");
80 /* section 7: init/update/final after a final equals a fresh context */
81 { sha256_ctx x; sha256_init(&x); sha256_update(&x, m, 777); sha256_final(&x, a);
82 sha256_init(&x); sha256_update(&x, m + 5, 1234); sha256_final(&x, a);
83 orc_sha256_hash(m + 5, 1234, c); check(!memcmp(a, c, 32), "init/update/final after a final equals a fresh context"); }
84 /* section 7: hash(n) equals init/update(n)/final; empty updates */
85 bad = 0;
86 for (size_t n = 0; n < 300; n++) {
87 sha256_ctx x; sha256_init(&x); sha256_update(&x, NULL, 0); sha256_update(&x, m, n); sha256_update(&x, m + n, 0);
88 sha256_final(&x, a); sha256_hash(m, n, b); if (memcmp(a, b, 32)) bad++;
89 }
90 check(!bad, "hash(n) equals init/update(0, NULL)/update(n)/update(0)/final, n = 0..299");
91 /* identity */
92 const char *id = omega2_ident_sha256_om2();
93 printf("ident %s\n", id);
94 check(strstr(id, "profile=OM-2") && strstr(id, "unit=sha256_om2"), "omega2_ident_sha256_om2 names the unit and profile");
95 return fails != 0;
96}