Skip to content

Separate dynamic circuit integers and streamline integer kernels - #211

Draft
wangxiao1254 wants to merge 4 commits into
audit/ir-foundationsfrom
audit/circuits-foundations
Draft

wangxiao1254 wants to merge 4 commits into
audit/ir-foundationsfrom
audit/circuits-foundations

Conversation

@wangxiao1254

Copy link
Copy Markdown
Member

Summary

Separate fixed-width and dynamic circuit integers, reduce integer circuit costs, and remove pointer-sort staging copies.

Changes

Integer representations

  • require N > 0 for fixed UInt_T<Ctx,N> and Int_T<Ctx,N>;
  • add explicit DynamicUInt_T<Ctx> and DynamicInt_T<Ctx> types with private positive-width vector storage;
  • retain unbound default construction as an assignment target;
  • validate runtime codecs and session boundaries;
  • add to_dynamic() / to_fixed<M>() conversions and signed/unsigned reinterpretation;
  • remove UInt_T::mod_exp;
  • preserve internal BitVec_T<Ctx,0> use while excluding zero-width frontend/session bundles.

Circuit kernels

  • reduce equality from N to N-1 AND gates;
  • remove the discarded difference vector from unsigned comparison;
  • seed multiplication from its first partial row;
  • avoid materializing a quotient for remainder-only calls and remove the per-bit quotient branch;
  • seed fixed and dynamic popcount from the first input bit;
  • preserve existing aliasing and division-by-zero semantics.

Sorting and ownership checks

  • run pointer-backed Batcher sorting directly over caller storage;
  • check signed secret-shift operands belong to the same context before building the barrel shifter.

Source migration

Replace both old runtime-width spellings:

UInt_T<Ctx, 0>              -> DynamicUInt_T<Ctx>
Int_T<Ctx, 0>               -> DynamicInt_T<Ctx>
UInt_T<Ctx, runtime_width>  -> DynamicUInt_T<Ctx>
Int_T<Ctx, runtime_width>   -> DynamicInt_T<Ctx>

UInt_T::mod_exp has no replacement in this PR. A future modular exponentiation API should use a full-width modular multiplication rather than an N-bit wrapping product.

Tests

  • clean Apple Clang 17 Release/Ninja build: 100/100 build steps;
  • full CTest suite: 77/77 passed;
  • focused dynamic/fixed integer, sorting, and foreign-context shift suite: 6/6 passed;
  • fresh Debug ASan+UBSan focused build: 16/16 build steps and 6/6 tests passed.

Fixed/dynamic parity is checked at widths 1, 2, 7, 8, 31, 32, 63, 64, and 65, including values, gate counts, output wires, and structural digests. Additional tests cover exhaustive small-width arithmetic, exact gate counts, codecs, unbound/moved states, conversions, pointer sorting, empty ranges, and foreign-context secret shifts.

Stack

This is PR 6 of 6 and targets audit/ir-foundations.

Seed equality and multiplication from their first useful row, compute comparison borrows without a discarded difference vector, and reuse division scratch for quotient bits.

Preserve aliasing and division-by-zero semantics while removing dead gates, heap allocations, and a per-bit quotient branch.
Run the Batcher network directly over pointer-backed circuit values instead of copying through temporary vectors. Cover ascending and descending pointer forms, keyed sorting, exact aliasing, and empty ranges.
Require positive compile-time widths for UInt_T and Int_T, and move runtime widths into DynamicUInt_T and DynamicInt_T with private vector storage and validated codecs.

Add fixed-to-dynamic and dynamic-to-fixed conversions, preserve zero-gate reinterpretation between signed and unsigned forms, and remove the invalid mod_exp API. Migrate concepts, sessions, documentation, and fixed/dynamic parity tests.
Check signed secret-shift operands before building the barrel shifter and reject foreign-context values at the operator boundary.

Exercise fixed and dynamic values at widths 1, 2, 7, 8, 31, 32, 63, 64, and 65. Compare values, gate counts, output wires, and structural digests; cover unbound assignment targets, move state, conversions, and runtime codec boundaries.
@wangxiao1254
wangxiao1254 force-pushed the audit/circuits-foundations branch from f5d8044 to f6d68ce Compare August 22, 2026 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant