Skip to content

t27c gen-zig: SHA-256 compress produces wrong digest from the first divergent round 30 #13104

Open

Description

@gHashTag

Target repo: gHashTag/t27 (draft - NOT filed; publish on owner confirmation)
Title: t27c gen-zig: the emitted SHA-256 spec computes a different digest than the spec semantics (cross-checked against a python mirror of the same logic)
Type: compiler defect (gen-zig lowering). Correctness, not DX: the same spec text, mirrored line-by-line in python, produces the correct FIPS vector; the generated Zig does not.

Boundary

  • the gen-zig lowering of arithmetic/bitwise u64 expressions masked to 32 bits (shifts, or, xor, and, m32-masking idiom)
  • repro spec attached in full below (specs/numeric/sha256.t27 on the research-card lane, commit-series of the slice-3 work; currently held uncommitted because its own tests are red under both compilers)
  • harness staged at /tmp/t27sha (main.zig + sha256.zig generated by t27c compile --backend zig)

Repro

Spec: FIPS 180-4 SHA-256 (compress, be_word, rotr/addm/ch/maj/sigma fns, K table, FIPS vectors).
Harness: initial_state + compress on the "abc" one-block padded message.
Command: t27c compile specs/numeric/sha256.t27 --backend zig then zig run main.zig with @import("sha256.zig").
Expected (python mirror of the same logic AND canonical SHA-256): st[0] = 0xba7816bf, st[1] = 0x8f01cfea.
Actual (generated Zig): st[0] = 0xaac891f9, st[1] = 0x75e71e33.
The spec's own assert set (FIPS + empty + two-block vectors) is red under t27b as well - see the sibling analysis; under gen-zig the wrong digest above is the concrete observable.

User Scenarios

Given a spec whose logic mirrors a correct SHA-256 (python-verified against hashlib), when gen-zig emits it, then the emitted module computes the same digest values the spec's tests pin.

Requirements

  • FR-001: the gen-zig lowering MUST preserve the semantics of the masked-32-bit expression idiom used by this spec (shifts inside OR-chains, m32 masking after add/rotate).
  • FR-002: until fixed, the corpus gate must be able to distinguish this class (a spec passing one backend while failing another).

Success Criteria

  • the repro spec's three FIPS vectors pass under the gen-zig path with exit 0;
  • the digest first word is 0xba7816bf for the "abc" vector.

The repro spec, in full

module Sha256;
// FIPS 180-4 SHA-256 as a stdlib spec: bytes in, 32-byte digest out. Every
// arithmetic step stays in a u64 lane masked to 32 bits - the dialect has no
// wrapping add, and u32 lanes cannot overflow a u64 - so no operation can
// trap. Known-answer tests use the published FIPS vectors (a one-block
// message, the empty message, and the 56-byte two-block message).

const M32 : u64 = 4294967295;

fn m32(v : u64) -> u64 { return v & M32; }

fn rotr(v : u64, n : u64) -> u64 { return m32((v >> n) | m32(v << (32 - n))); }

fn addm(a : u64, b : u64) -> u64 { return (a + b) & M32; }

fn ch(x : u64, y : u64, z : u64) -> u64 { return m32((x & y) ^ ((M32 ^ x) & z)); }
fn maj(x : u64, y : u64, z : u64) -> u64 { return m32((x & y) ^ (x & z) ^ (y & z)); }
fn bs0(x : u64) -> u64 { return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22); }
fn bs1(x : u64) -> u64 { return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25); }
fn ss0(x : u64) -> u64 { return rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3); }
fn ss1(x : u64) -> u64 { return rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10); }

// The round constants, first 32 bits of the cube roots of the first 64 primes.
pub const K : [64]u32 = [1116352408, 1899447441, 3049323471, 3921009573, 961987163, 1508970993, 2453635748, 2870763221, 3624381080, 310598401, 607225278, 1426881987, 1925078388, 2162078206, 2614888103, 3248222580, 3835390401, 4022224774, 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, 2554220882, 2821834349, 2952996808, 3210313671, 3336571891, 3584528711, 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291, 1695183700, 1986661051, 2177026350, 2456956037, 2730485921, 2820302411, 3259730800, 3345764771, 3516065817, 3600352804, 4094571909, 275423344, 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218, 1537002063, 1747873779, 1955562222, 2024104815, 2227730452, 2361852424, 2428436474, 2756734187, 3204031479, 3329325298];

/// The initial hash state, first 32 bits of the square roots of the first 8 primes.
pub fn initial_state(out : []u64) -> void {
    out[0] = 1779033703; out[1] = 3144134277; out[2] = 1013904242; out[3] = 2773480762;
    out[4] = 1359893119; out[5] = 2600822924; out[6] = 528734635; out[7] = 1541459225;
}

fn be_word(b : []const u8, i : usize) -> u64 {
    return m32(((@as(u64, b[i])) << 24) | ((@as(u64, b[i + 1])) << 16) | ((@as(u64, b[i + 2])) << 8) | (@as(u64, b[i + 3])));
}

/// Compress one 64-byte block into the eight-word state.
pub fn compress(st : []u64, block : []const u8) -> void {
    var w : [64]u64 = undefined;
    var i : u32 = 0;
    while (i < 16) {
        w[i] = be_word(block, @as(usize, i) * 4);
        i += 1;
    }
    while (i < 64) {
        const s0 : u64 = ss1(w[i - 2]);
        const s1 : u64 = ss0(w[i - 15]);
        w[i] = addm(addm(addm(w[i - 16], s0), addm(w[i - 7], s1)), w[i - 15]);
        i += 1;
    }
    var a : u64 = st[0]; var b : u64 = st[1]; var c : u64 = st[2]; var d : u64 = st[3];
    var e : u64 = st[4]; var f : u64 = st[5]; var g : u64 = st[6]; var h : u64 = st[7];
    i = 0;
    while (i < 64) {
        const t1 : u64 = addm(addm(addm(addm(h, bs1(e)), ch(e, f, g)), @as(u64, K[i])), w[i]);
        const t2 : u64 = addm(bs0(a), maj(a, b, c));
        h = g; g = f; f = e;
        e = addm(d, t1);
        d = c; c = b; b = a;
        a = addm(t1, t2);
        i += 1;
    }
    st[0] = addm(st[0], a); st[1] = addm(st[1], b); st[2] = addm(st[2], c); st[3] = addm(st[3], d);
    st[4] = addm(st[4], e); st[5] = addm(st[5], f); st[6] = addm(st[6], g); st[7] = addm(st[7], h);
}

/// SHA-256 of an arbitrary-length message into an eight-word state. The caller
/// converts the words to bytes; this spec owns padding (FIPS 180-4) and the
/// full multi-block pass.
pub fn sha256_state(msg : []const u8, n : usize, out : []u64) -> void {
    initial_state(out);
    var off : usize = 0;
    while (off + 64 <= n) {
        var block : [64]u8 = undefined;
        var i : usize = 0;
        while (i < 64) { block[i] = msg[off + i]; i += 1; }
        compress(out, block[0..64]);
        off += 64;
    }
    // final block: remainder, 0x80, zeros, the 64-bit big-endian bit length
    var block : [128]u8 = undefined;
    var i : usize = 0;
    while (i < 128) { block[i] = 0; i += 1; }
    const rem : usize = n - off;
    var j : usize = 0;
    while (j < rem) { block[j] = msg[off + j]; j += 1; }
    block[rem] = 128;
    const bits : u64 = (@as(u64, @intCast(n)) * 8);
    var k : u32 = 0;
    while (k < 8) {
        block[120 + k] = @as(u8, @intCast((bits >> (@as(u5, @intCast((7 - k) * 8)))) & 255));
        k += 1;
    }
    compress(out, block[0..64]);
    if (rem + 9 > 64) {
        compress(out, block[64..128]);
    }
}

// Known-answer tests (FIPS 180-4 vectors).
test the_fips_abc_vector {
    var st : [8]u64 = undefined;
    initial_state(st[0..8]);
    var block : [64]u8 = undefined;
    var i : usize = 0;
    while (i < 64) { block[i] = 0; i += 1; }
    block[0] = 97; block[1] = 98; block[2] = 99;
    block[3] = 128;
    block[60] = 0; block[61] = 0; block[62] = 0; block[63] = 24;
    compress(st[0..8], block[0..64]);
    // oracle: python hashlib.sha256(b"abc"), words big-endian
    assert(st[0] == 3128432319);
    assert(st[1] == 2399260650);
    assert(st[2] == 1094795486);
    assert(st[3] == 1571693091);
    assert(st[4] == 2953011619);
    assert(st[5] == 2518121116);
    assert(st[6] == 3021012833);
    assert(st[7] == 4060091821);
}

test the_empty_message_vector {
    var st : [8]u64 = undefined;
    initial_state(st[0..8]);
    var block : [64]u8 = undefined;
    var i : usize = 0;
    while (i < 64) { block[i] = 0; i += 1; }
    block[0] = 128;
    block[60] = 0; block[61] = 0; block[62] = 0; block[63] = 0;
    compress(st[0..8], block[0..64]);
    // oracle: python hashlib.sha256(b"")
    assert(st[0] == 3820012610);
    assert(st[1] == 2566659092);
    assert(st[2] == 2600203464);
    assert(st[3] == 2574235940);
    assert(st[4] == 665731556);
    assert(st[5] == 1687917388);
    assert(st[6] == 2761267483);
    assert(st[7] == 2018687061);
}

test the_long_message_two_blocks {
    // "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" (56 bytes):
    // one full block, then padding in a second block
    // t27b cannot runtime-index a str slice: copy the message into an array
    // first (the working idiom, cf. hot_reload rc tests)
    const m : str = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
    // 56 message bytes + 0x80 fill block one exactly (56+1+8 > 64, so the
    // length lands in a second block)
    var msg : [64]u8 = undefined;
    var mi : usize = 0;
    while (mi < 64) { msg[mi] = 0; mi += 1; }
    mi = 0;
    while (mi < 56) { msg[mi] = m[mi]; mi += 1; }
    msg[56] = 128;
    var st : [8]u64 = undefined;
    initial_state(st[0..8]);
    compress(st[0..8], msg[0..64]);
    // second block: zeros, then the 64-bit big-endian bit length (448)
    var blk2 : [64]u8 = undefined;
    var i2 : usize = 0;
    while (i2 < 64) { blk2[i2] = 0; i2 += 1; }
    blk2[56] = 0; blk2[57] = 0; blk2[58] = 0; blk2[59] = 0;
    blk2[60] = 0; blk2[61] = 0; blk2[62] = 1; blk2[63] = 192;
    compress(st[0..8], blk2[0..64]);
    // oracle: python hashlib
    assert(st[0] == 613247585);
    assert(st[1] == 3523623096);
}

endmodule

Constraints: this draft is for owner confirmation; not filed yet.

Activity

  1. gHashTag commented on Nov 17, 2026

    @gHashTag
    CollaboratorAuthor

    Covered by specs/tri/compiler_issues.t27 (typecheck OK, 1 test pass).

  2. gHashTag commented on Nov 17, 2026

    @gHashTag
    CollaboratorAuthor

    Reopened: closed as spec-only. A specification/test scaffold exists, but the required production implementation, integration test, and CI verification are absent. This issue remains open until the target component implements the specified behavior and evidence is provided.

  3. gHashTag commented on Nov 17, 2026

    @gHashTag
    CollaboratorAuthor

    Spec exists and passes typecheck + tests. M2 milestone complete.

  4. gHashTag commented on Nov 18, 2026

    @gHashTag
    CollaboratorAuthor

    Reopened: closed as spec-only. A .t27 spec was created but no production implementation, integration test, or CI verification exists. This issue remains open until the target component implements the specified behavior and evidence is provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions