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 / src / omega_blake3.c

54 lines · 2151 bytes Raw Blame History
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/* BLAKE3 is implemented in Omega: om/blake3/b3unit.om, compiled by the seeded compiler and bound
18 * through the omega-bridge native call ABI v1 as omega_omb3 (generated into src/omega_om_units.inc,
19 * which src/sha256.c compiles; regenerate with om/units.sh). This file only cuts the input into
20 * 1024-byte chunks, because ABI v1 arrays have a fixed length. No hashing logic lives here. */
21#include "omega_blake3.h"
22
23#include <stdlib.h>
24#include <string.h>
25
26#define OMB3_CHUNK 1024u
27#define OMB3_STATE 448u
28
29uint32_t omega_omb3(uint64_t *result, void *st, uint64_t st_len, const void *b, uint64_t b_len, uint64_t n,
30 uint64_t fin, void *o, uint64_t o_len);
31
32/* A trap or refused argument can only mean a broken invariant (chunk sizes, stack depth), never a
33 * recoverable condition, so it aborts like a failed assert. */
34static void omb3_call(uint64_t *st, const uint8_t *chunk, size_t n, unsigned fin, uint8_t out[32])
35{
36 uint64_t unused;
37 if (omega_omb3(&unused, st, OMB3_STATE, chunk, OMB3_CHUNK, n, fin, out, 32) != 0)
38 abort();
39}
40
41void omega_blake3_hash(const uint8_t *data, size_t len, uint8_t out[32])
42{
43 uint64_t st[OMB3_STATE];
44 memset(st, 0, sizeof st);
45 while (len > OMB3_CHUNK) {
46 omb3_call(st, data, OMB3_CHUNK, 0, out);
47 data += OMB3_CHUNK;
48 len -= OMB3_CHUNK;
49 }
50 uint8_t last[OMB3_CHUNK]; /* only last[0..len) is read */
51 if (len > 0)
52 memcpy(last, data, len);
53 omb3_call(st, last, len, 1, out);
54}