interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / sha512 / README.md

142 lines · 10231 bytes Raw Blame History Source

om/sha512: SHA-512 in Omega

SHA-512 as defined in FIPS 180-4, section 6.4, written in Omega (.om). The library takes one message of up to 65536 bytes and writes its 64-byte digest. It exists for Ed25519, which needs SHA-512(R || A || M) (RFC 8032, section 5.1.7).

sha512.om is a library unit: it holds only functions, with no fn main and no globals. Every function name starts with s512. To use it, concatenate it before the program that calls it.

API

fn s512hash m view uchar 65536 n ulong o edit uchar 64 to void
fn s512pre p view uchar 64 m view uchar 65536 n ulong o edit uchar 64 to void

The other functions are internal: compression, padding, and big-endian loads. The constants come straight from FIPS 180-4: K (section 4.2.3), H(0) (section 5.3.5), Ch, Maj, Σ0, Σ1, σ0 and σ1 (section 4.1.3, equations 4.8-4.13), and padding (section 5.1.2). Words are ulong, and unsigned arithmetic wraps mod 2^64. Omega has no rotate word, so ROTR^r(x) is written (x >> r) | (x << (64 - r)) (section 3.2).

In the source, K and H(0) are decimal literals, because Omega has no hex literals. They were checked against the hex tables in FIPS 180-4 sections 4.2.3 and 5.3.5: all 80 K words matched the extracted table, and so did the cube-root and square-root derivation. Changing one bit of K[0] makes every NIST example fail (see "mutant-constant" below).

Tests

sh om/sha512/test/run.sh "$(tools/omc.sh)"

run.sh concatenates sha512.om with test/driver.om, builds build/test/sha512/driver, and feeds it vector records on stdin. The driver's record format is described in its header. The script exits 0 only if every vector passes and every negative check is rejected.

CheckRecordsSource
nist-examples10vectors/nist-examples.txt: 'abc' and the 896-bit two-block message from NIST SHA512.pdf, and SHA-512 items #1-#8 from NIST SHA2_Additional.pdf (0 bytes; 111, 112, 113 and 122 zero bytes; 1000 zero bytes; 1000 'A'; 1005 'U'). Items #9-#12 are 10^6 bytes or more, beyond the capacity.
cavp-short129vectors/SHA512ShortMsg.rsp, unmodified (0 to 1024 bits)
cavp-long128vectors/SHA512LongMsg.rsp, unmodified (1816 to 102400 bits, all within the capacity)
cavp-monte100vectors/SHA512Monte.rsp, unmodified: the SHAVS Monte Carlo chain, 100 checkpoints of 1000 hashes each
oracle-static52vectors/oracle.txt, generated by gen_vectors.py static with python3 hashlib.sha512 as the oracle. It covers the 448-bit "abcdbcdec…nopq" message, lengths 0, 1, 55, 56, 63, 64, 110-113, 119, 120, 126-129, 239-241, 255-257 and 1023-1025, all-0x00 and all-0xff messages of 111/112/127/128 bytes, n = 65536, and 17 s512pre records
hashlib-diff1125Generated at test time: every length 0..1100, plus 65536, 65535, 65519, 65408, 65407, 65520, 32768, 32767 and 16 random lengths up to 65536. Random bytes from random.Random(seed), oracle python3 hashlib.sha512
hashlib-pre313Generated at test time: s512pre with every length 0..300, plus 65536, 65535, 65472, 65471 and 8 random lengths

The seed defaults to 1 and can be changed with S512_SEED. Without python3 the two hashlib checks are skipped and a NOTE line is printed. The committed vectors still run.

Negative checks (each must be detected):

The first three count as detected only when the driver finished normally and reported the difference. That means exit status exactly 1, which is the driver's own "a check failed" return, and a complete last line sha512 driver: C checks, F failed with the expected C (all records) and F > 0. A trap (exit 100+N), a signal (128+N), a launch failure (126/127), exit 0 or a truncated summary all fail the run. A passing check likewise needs exit 0 and a complete ... 0 failed line. Exit statuses are taken directly from the driver, never through a pipeline. This was checked on 2026-10-25 by temporarily replacing the K[0] mutant with one that traps 3 on every call (put k 0 get k add 80 get k 1). run.sh reported FAIL mutant-constant: expected exit 1 and "sha512 driver: 10 checks, 10 failed", got exit 103, ended with sha512: FAIL and exited 1. The real mutant was then restored.

Evidence (2026-10-25)

Vector sources (fetched 2026-10-25)

Third-party material and notices

copied materialowner and termsnotice file
vectors/SHA512ShortMsg.rsp, SHA512LongMsg.rsp, SHA512Monte.rsp, byte-for-byte copies from the CAVP zip aboveNIST, a US Government agency. Under 17 U.S.C. §105, works of NIST employees are not subject to copyright protection in the United States and may be subject to foreign copyright. NIST grants a worldwide royalty-free right to copy, modify and distribute, asks that NIST be acknowledged, and provides the data "AS IS" (https://www.nist.gov/open/license, "Fair Use of Other NIST Data/Works")vectors/NIST.NOTICE
digests and message descriptions in vectors/nist-examples.txt, transcribed from NIST SHA512.pdf and SHA2_Additional.pdf (modified on 2026-10-25: values copied into the record format, the PDF layout and intermediate values dropped)NIST, same termsvectors/NIST.NOTICE, which also records the change, as NIST's terms ask

vectors/NIST.NOTICE quotes NIST's recommended statement for non-SRD data/works verbatim from https://www.nist.gov/open/license (fetched 2026-10-25), names the source URLs, and states what was changed. The header of nist-examples.txt points to it. The .rsp files carry no notice of their own and are kept unmodified.

FIPS 180-4 (also a NIST work) is cited by section only. Its K and H(0) values appear in sha512.om as decimal constants, and the standard defines them as cube- and square-root fractions. The 448-bit message in oracle.txt is the standard's sample text. No other third-party material is included: oracle.txt and the differential corpus are this unit's own output, with python3 hashlib used only as the reference.

Limits