Skip to content

[crypto] i31_from_monty silently wrong for moduli >= 590 bits via #1049 ([u32;80] temp only 20 words zeroed) #1051

Closed

Description

@eas4ai

Summary

Blast radius of #1049 in lib/std/crypto/bigint.w: i31_from_monty silently returns wrong results for every modulus with mlen >= 20 (bit length >= 590), because its one temp relies on [0u32; 80] zeroing limbs 2..mlen, of which only 0..19 are actually zeroed.

Mechanism

lib/std/crypto/bigint.w:215-219:

var one: [u32; 80] = [0u32; 80]
...
*(op + 0u64) = *(m + 0u64)
*(op + 1u64) = 1u32

Only one[0] and one[1] are set explicitly; one[2..mlen] must be zero (the value 1). Under #1049 only one[0..19] are zeroed, so for mlen >= 20, i31_montmul reads stack garbage as y[20..mlen]. Source is correct-if-sized; the wrongness comes from the codegen defect, but the observable crypto impact (silent wrong modpow for RSA-1024/2048-sized moduli) belongs here until #1049 lands, plus a regression test at this level.

Evidence (all executed at 450733e; oracles are python3 pow, never self-derived)

Self-contained monty probes (inline the module's i31 code verbatim since the symbols are private):

probe modulus mlen oracle==want result
s64/s256/s512/s576 <=576 bits <=19 — PASS
s610 610 bits 20 True FAIL
s640/s768/m610 610-768 bits 20-25 — FAIL
1024/2048 1024/2048 bits 34/67 — FAIL
b2449 2449 bits 79 True FAIL

The pass/fail boundary is exactly mlen = 20 <=> bitlen >= 590 ("above ~576 bits"), matching #1049's 20-word zero prefix for [u32; 80] to the word. Stable across runs.

Notes

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced

    Verdict. Established. i31_from_monty builds its Montgomery one in a var one: [u32; 80] = [0u32; 80] temp, sets only limbs 0 and 1, and relies on limbs 2..mlen being zero. Because of the #1049/#1050 codegen defect, [0u32; 80] zeroes only the first 20 words (bytes 0..79), so limbs 20..79 keep stack garbage. i31_montmul then reads those garbage limbs, so i31_from_monty and every i31_modpow above ~590-bit moduli (mlen >= 20) is silently wrong. The pass/fail boundary is exactly mlen = 20, matching #1049's 20-word zero prefix for [u32; 80]. I reproduced the mechanism directly and traced the consuming read; I relied on the report's oracle-checked modpow table for the end-to-end RSA numbers (see blockers).

    Tested.

    Source main @ 5809ac1 (2026-09-14); lib/std/crypto/bigint.w 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 result
    Platform macOS Darwin 25.6.0 arm64

    Mechanism reproduction (mirrors the one temp setup verbatim, then scans limbs 2..79):

    var one: [u32; 80] = [0u32; 80]
    let op = &raw mut one[0] as *mut u32
    unsafe:
        *(op + 0u64) = 999u32
        *(op + 1u64) = 1u32
    // firstbad = first index >= 2 with one[i] != 0
    

    Output on both compilers: firstbad = 20, and 49 of limbs 20..79 nonzero this run. So limbs 0..19 are zeroed and limb 20 onward is uninitialized — precisely the value i31_from_monty depends on being zero for mlen >= 20.

    Causal chain (all in lib/std/crypto/bigint.w, 5809ac1).

    1. Under-zeroed temp: bigint.w:215-216 (var one: [u32; 80] = [0u32; 80]), limbs 0/1 set at :217-218.
    2. Consumed: i31_montmul(dp, x, op, m, m0i) at :221, where op is the y argument.
    3. Garbage read: i31_montmul reads let yj = *(y + j as u64) for j in 2..=mlen (:184-185). For mlen >= 20 this reads the uninitialized limbs, corrupting the product, hence i31_from_monty and i31_modpow (:227 calls i31_from_monty at :257).

    mlen = i31_word_count(modulus) is the number of 31-bit limbs; ceil(590/31) = 20, so the boundary is 590 bits, matching the report's oracle table (610-bit and up fail; <=576-bit pass). RSA-1024 (mlen 34) and RSA-2048 (mlen 67) are in the failing range.

    Root cause. The source is correct if [u32; 80] = [0u32; 80] actually zeroed all 80 limbs. The wrongness is entirely the #1049/#1050 codegen defect (src/CodegenDispatch.w:5227, memcpy length = element count instead of byte size). Fixing that line fixes this for mlen <= 79.

    Additional finding worth its own issue. i31_from_monty (and the sibling temps in i31_modpow) size these buffers at [u32; 80], capping usable mlen at 79 (~2449 bits) even with #1049 fixed — yet rsa_pkcs1_sha256_verify allocates [u32; 140] and permits n_len up to 512 (4096-bit, mlen ~133). So a 4096-bit RSA verify would overflow the 80-word temp regardless of #1049. That is a separate source-level buffer-sizing defect; the report flags it and I confirm the [u32; 80] vs [u32; 140] mismatch.

    Validator gap. No crypto self-test exercises modpow above 576 bits, so CI is green while RSA-1024/2048 verification is broken. That is the missing coverage this issue should add.

    Related. #1049 (repeat-init) and #1050 (array copy) are the codegen root cause. No open PR.

    Recommendation — champion, at two levels.

    Acceptance criteria: the 610-bit modpow vector matches its oracle; the mechanism probe above reports firstbad = -1; a 4096-bit path either works or is explicitly bounded with a loud error rather than a silent overflow.

    Blockers / not tested. The i31 routines and rsa_pkcs1_sha256_verify are private unsafe fn, so I could not call the full RSA path from an external program without inlining the module; the report did that and computed oracles with python3 pow, which I did not independently re-run. My independent evidence is the temp-truncation mechanism (executed) plus the source trace of the consuming read; the end-to-end modpow numbers rest on the report's oracle table. Linux/Windows not exercised.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): does not reproduce; the cause, #1049 (repeat-init of [v; N>64]), is fixed and closed. An [u32; 80] zero fill now zeroes every word, even over a dirtied stack:

    $ cat zero.w   # dirty(): [7u32; 80] summed; zeroed(): [0u32; 80] summed
    $ with run zero.w
    560
    0
    

    The fixed-size temps above 2449 bits remain, tracked in #1052.

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