Skip to content

Store exact circuit products in contiguous width-segmented buffers #99

Done

Description

@wu-s-john

The witness backend records exact A/B/C products as individual StoredInteger values. Each nonzero owns a boxed limb slice; repeated circuit rows therefore create many small allocations before field projection.

This issue owns a contiguous product/coefficient owner grouped by public declared limb width, with stable logical row order and borrowed projection views.

Sources

The target repository's existing relation, field encoding, coordinate order, and transcript contracts are authoritative. The current target implementation determines compatibility; the PoC implementation is reference-only and must be re-derived against those contracts.

In scope

  • Retain signed two's-complement semantics and gadget-declared widths; store row-to-segment locations with checked index conversions.
  • Replace per-product boxes and expose row iteration/indexing without reconstructing temporary integers. Resolve segment dispatch once before bulk projection.
  • Keep public storage metadata separate from values; prepare one reusable view for sequential and parallel consumers.

Out of scope: changing witness arithmetic, narrowing P-256 hints, replacing circuit accumulation structures, or changing proof messages.

Potential impact

The allocation count can fall from one per nonzero product toward one growable buffer per width. Locality improves, but total bytes need not decrease: the old owner trims sign limbs and represents zero cheaply. Measure zeros and mixed-width rows separately.

Done when

  • Compare exact products and projected residues with the existing backend for SHA, P-256 and mixed widths; include negative values, zero, sign extension and maximum declared magnitudes.
  • Reject overflowing row/segment indices and malformed lengths before indexing; no private magnitude may select the storage width.
  • Existing proof/transcript golden bytes and tamper rejection remain unchanged for identical inputs.
  • Record matched allocation count, allocated bytes, peak live memory, projection time and witness time, including cold and reused owners.

Activity

  1. added
    enhancement
    difficulty: medium
    memory
    optimization-port
    urgency: med
    witness-generation
    on Sep 24, 2026
  2. zkfriendly commented on Nov 24, 2026

    @zkfriendly
    Collaborator

    Status update (main 34addac): partially implemented by #149. StoredInteger (crates/circuit/src/matrix_products.rs:297) now holds single-word (<= 64-bit) values inline (Words::Inline(u64)), eliminating per-product heap allocations for SHA-256 (witness ~25→10 ms, Free witness ~25→1 ms on 600 blocks). Remaining scope is multi-limb (>64-bit) circuits such as P-256 (Words::Heap(Box<[u64]>)).

  3. zkfriendly commented on Nov 24, 2026

    @zkfriendly
    Collaborator

    Closing as substantially addressed by #149: StoredInteger (crates/circuit/src/matrix_products.rs:297) now stores single-word (<= 64-bit) values inline (Words::Inline(u64)), eliminating per-product heap allocations for SHA-256 (witness ~10 ms, Free witness ~1 ms on 600 blocks). P-256 has only ~7k rows (~1.75 ms total witgen) and is not wired E2E, so dedicated width-segmented buffers are not worth tracking separately.

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

    difficulty: mediumenhancementmemoryoptimization-porturgency: medwitness-generation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions