Skip to content

AEAD entry point chacha20_poly1305_encrypt is private (the module compiles now) #1054

Closed

Description

@eas4ai

Restated 2026-10-25. The module now compiles (importing std.crypto.chacha20poly1305 builds and runs). What is left: the AEAD entry point and its chain are private, so no program can call them, and a public surface needs a safe signature, not the raw-pointer unsafe fn it has.


Original report

Summary

The ChaCha20-Poly1305 AEAD path (lib/std/crypto/chacha20poly1305.w) is dead code at 450733e: the entry point is unreachable from outside the module, and the Poly1305 MAC it depends on does not compile under the current checker. (Stale-test half already filed as #1053.)

Evidence (executed at 450733e)

  1. chacha20_poly1305_encrypt (chacha20poly1305.w:7) is not pub, nor is anything else in the chain (chacha20.w:23,58, poly1305.w:12,89,105; contrast sha256.w:142 which shows the pub convention). An external probe fails at sema with symbol 'chacha20_poly1305_encrypt' is private. Zero in-repo callers (only generated embedded copies). tests/test_crypto.w:10 imports the module for external testing and reaches it no other way, so this reads as unintended sealing, not encapsulation.
  2. Any import of the MAC path fails to compile: poly1305_finish (poly1305.w:112-126,143-149) binds var hN = ctx.h[N], which is reference-typed under the current checker, so all carry/mask writes are rejected, e.g.:
    error: cannot assign an owned value to a reference-typed binding ... poly1305.w:113:5 'h1 = h1 & 0x03FFFFFF as u32'
    Reproduced by with-stage1 check on a file that only does use std.crypto.poly1305. The intra-stdlib caller (:46) fails identically, so nothing routes around it. Minimal repair direction (not applied; audit is read-only): annotate owned copies (var h0: u32 = ...).

Notes

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced

    Verdict. Both claims established: the ChaCha20-Poly1305 AEAD path is dead two ways. (1) chacha20_poly1305_encrypt and its whole chain are non-pub, so unreachable from outside the module. (2) Importing the path fails to compile because poly1305_finish binds var h0 = ctx.h[0] (an element view under D27) and then assigns to it, which the checker rejects. One clarification: the compile failure (2) actually masks the privacy error (1) — an external import dies inside poly1305.w before the privacy check on the AEAD entry is even reached. And (2) is not a compiler bug: the checker is correctly enforcing the D27 element-view rule; the stdlib code is non-conforming.

    Tested.

    Source main @ 5809ac1 (2026-09-14); the AEAD chain and poly1305_finish are unchanged since the reported revision 450733e
    Compilers with v0.15.2.1 built from 5809ac1 (staging worktree); a compiler built from 450733e with the nightly-20260903 seed. Same errors
    Platform macOS Darwin 25.6.0 arm64

    Repro 1 — compile failure (with check on use std.crypto.poly1305 and empty main), both compilers:

    error: cannot assign an owned value to a reference-typed binding; annotate the binding's declared type to take an owned copy (e.g. `var x: T = ...`)
     --> <embedded-std>/std/crypto/poly1305.w:113:5
    113 |     h1 = h1 & 0x03FFFFFF as u32
    

    Repro 2 — importing the AEAD entry (use std.crypto.chacha20poly1305) fails with the same poly1305.w:113 error, because it pulls in poly1305 transitively; the module never finishes compiling.

    Root cause.

    • Visibility (claim 1): none of the chain is pub — chacha20_poly1305_encrypt (chacha20poly1305.w:7), chacha20_block/chacha20_crypt (chacha20.w:23,58), and the poly1305_* functions (poly1305.w:35,89,105). Contrast sha256_hash at sha256.w:142, which is pub. Spec §18.3 makes cross-module access to a non-pub symbol an error, so the entry is sealed. This is source-established (execution can't reach it past defect 2), the same class as std.time Duration is private but appears in pub signatures; its constructors are uncallable #1018.
    • Compile failure (claim 2): poly1305_finish does var h0 = ctx.h[0] (etc.) where Poly1305.h is [u32; 5] (poly1305.w:8). Per D27 (docs/decisions.md, spec §3.6/§13.3), var x = place binds the element place as a &u32 view, not an owned copy; the subsequent h1 = h1 & ... then assigns to a reference-typed binding, which is the exact error. The checker is behaving per the ruling; the stdlib code predates or ignores it. Note the sibling poly1305_block at :41-45 does var h0 = ctx.h[0] +% (...), an owned arithmetic result, so it compiles — which is why only poly1305_finish trips.

    Why the module can ship broken. Modules are compiled on demand; nothing in the compiler or the embedded stdlib imports poly1305/chacha20poly1305, so the non-conforming poly1305_finish is never type-checked during a normal build and CI stays green. This is the same "no executable coverage" situation as #1053 (the AEAD test that would import it does not compile either).

    AGENTS.md / no-ceremony note. The fix — var h0: u32 = ctx.h[0] — is exactly the D27-sanctioned "annotate to demand an owned copy" spelling; it is not adding ceremony, it is stating the owned-value intent the arithmetic needs. The pub additions likewise just expose an intended API surface.

    Related. #1053 (crypto tests don't compile / not wired in) is the coverage side; #1018 is the same non-pub-surface class in std.time. No open PR touches these files.

    Recommendation — champion. The AEAD construction traces correctly against RFC 8439 §2.8 by static review (block-0 key derivation, counter-1 payload), so it only needs to be made reachable and compilable:

    • Annotate the owned copies in poly1305_finish (var h0: u32 = ctx.h[0], and the four siblings) so the mask/carry writes are owned-value assignments.
    • Mark the intended entry points pub: chacha20_poly1305_encrypt (and a decrypt/verify counterpart), and whatever chacha20/poly1305 surface the public API needs.
    • Add an RFC 8439 §2.8.2 test vector once [tests] crypto suites (bigint/rsa/crypto) not executable: stale unsafe:/-> void syntax #1053's suite compiles and is wired into test/.

    Acceptance criteria: use std.crypto.chacha20poly1305 compiles; a pub AEAD encrypt/decrypt round-trips and matches the RFC §2.8.2 vector; a regression test lives under test/ and runs in with build :test.

    Not tested. I could not execute the AEAD end to end (it does not compile), nor confirm the privacy error at runtime (defect 2 fires first); claim 1 rests on source + §18.3. Linux/Windows not exercised.

  2. changed the title [-][crypto] AEAD path dead: chacha20_poly1305 entry not pub and poly1305_finish rejected by checker[/-] [+]AEAD entry point chacha20_poly1305_encrypt is private (the module compiles now)[/+] on Oct 25, 2026
  3. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced for visibility.

    $ with run import.w   # use std.crypto.chacha20poly1305; print
    imported
    $ with run call.w     # calls chacha20poly1305.chacha20_poly1305_encrypt(...)
    error: 'chacha20_poly1305_encrypt' is private to its package (declared in '<embedded-std>/std/crypto/chacha20poly1305.w', §18.3)
    

    post-release: exposing it means designing a safe std.crypto AEAD API (spans instead of raw pointers), a std design task.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions