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 / src / omega_blackwell_encoder.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "omega_blackwell_encoder.h" |
| 18 | #include "omega_native.h" |
| 19 | #include "sha256.h" |
| 20 | #include <string.h> |
| 21 | |
| 22 | int omega_blackwell_encode_vecadd(uint8_t *code_buf, size_t max_len, size_t *out_len) { |
| 23 | if (!code_buf || max_len < OMEGA_BW_VECADD_CODE_SIZE) return -1; |
| 24 | uint64_t written = 0; |
| 25 | if (omega2_bw_encode_vecadd(&written, code_buf, OMEGA_BW_VECADD_CODE_SIZE) != OMEGA_OK || |
| 26 | written != OMEGA_BW_VECADD_CODE_SIZE) { |
| 27 | return -1; |
| 28 | } |
| 29 | if (out_len) *out_len = OMEGA_BW_VECADD_CODE_SIZE; |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | int omega_blackwell_compute_code_digest(const uint8_t *code_buf, size_t len, uint8_t *out_digest) { |
| 34 | if (!code_buf || len == 0 || !out_digest) return -1; |
| 35 | sha256_ctx ctx; |
| 36 | sha256_init(&ctx); |
| 37 | sha256_update(&ctx, code_buf, len); |
| 38 | sha256_final(&ctx, out_digest); |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | static uint32_t read_le32(const uint8_t *p) { |
| 43 | return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); |
| 44 | } |
| 45 | |
| 46 | int omega_blackwell_verify_encoder_fixtures(void) { |
| 47 | uint8_t code_buf[OMEGA_BW_VECADD_CODE_SIZE]; |
| 48 | size_t out_len = 0; |
| 49 | if (omega_blackwell_encode_vecadd(code_buf, sizeof(code_buf), &out_len) != 0) { |
| 50 | return -1; |
| 51 | } |
| 52 | if (out_len != OMEGA_BW_VECADD_CODE_SIZE) { |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | uint8_t digest[32]; |
| 57 | if (omega_blackwell_compute_code_digest(code_buf, out_len, digest) != 0) { |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | static const char HEXDIGITS[] = "0123456789abcdef"; |
| 62 | char hex[65]; |
| 63 | for (int i = 0; i < 32; i++) { /* no sprintf: this file is part of the freestanding link target */ |
| 64 | hex[i * 2] = HEXDIGITS[digest[i] >> 4]; |
| 65 | hex[i * 2 + 1] = HEXDIGITS[digest[i] & 0x0f]; |
| 66 | } |
| 67 | hex[64] = 0; |
| 68 | |
| 69 | if (strcmp(hex, OMEGA_BW_VECADD_EXPECTED_SHA256) != 0) { |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | /* Verify critical instruction patterns: */ |
| 74 | /* 1. S2R R9, SR_CTAID.X */ |
| 75 | if (read_le32(code_buf + 1 * 16) != 0x00097919) return -1; |
| 76 | /* 2. S2R R0, SR_TID.X */ |
| 77 | if (read_le32(code_buf + 3 * 16) != 0x00007919) return -1; |
| 78 | /* 3. IADD3 R9, PT, PT, R2, R5, RZ */ |
| 79 | if (read_le32(code_buf + 17 * 16) != 0x02097210) return -1; |
| 80 | /* 4. STG.E desc[UR4][R6.64], R9 */ |
| 81 | if (read_le32(code_buf + 18 * 16) != 0x06007986) return -1; |
| 82 | |
| 83 | return 0; |
| 84 | } |