Skip to content

feat(math): add Pack, Bytes8 and Uint64 (2/13) - #1126

Draft
pepebndc wants to merge 7 commits into
build/math-01-scaffoldfrom
feat/math-02-uint64
Draft

pepebndc wants to merge 7 commits into
build/math-01-scaffoldfrom
feat/math-02-uint64

Conversation

@pepebndc

@pepebndc pepebndc commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Part 2 of 13 of the math library migration from midnight-apps contracts/src/math. Closes #214.

Stack: #1125 → #1126 → #1127 → #1128 → #1140 → #1141 → #1142 → #1143 → #1144 → #1145 → #1146 → #1147 → #1148. Merge in order.

Summary

Adds the Pack, Bytes8 and Uint64 modules. The code is a copy of the audited source, with the repository conventions and two fixes. Names, signatures and failure messages do not change.

Changes

  • Port the three modules, with their mocks, async simulators, test witnesses and tests (ea7e9803).
  • Fix: a bad Uint64 division witness now fails with Uint64: division invalid. Before this fix, it failed with a compiler cast error (82a1c291). This changes only the error message. The checks that reject the witness do not change.
  • Fix: correct the Bytes8 circuit title. This is the Bytes8 part of audit finding N-01 in the May 2026 audit. The audit marks it resolved, but midnight-apps#312 is not merged (3693d805).
  • Correct the Uint64.sqrt description (b727a7c2).
  • Use the repository doc style. This changes comments only (833b8419).
  • Add tsconfig.math-test.json, a type check of all math TypeScript. yarn types runs it, so CI runs it too (5530cce4). Open PR chore(contracts): type-check specs and simulators #897 also changes type checking, so the two PRs need a merge check.
  • Remove the rows of the exported wrapper from the doc comment of the internal Bytes8._toUint64 (fd2952d7).

Tests that replace a witness run only on the simulator backend, because the live backend fixes witnesses at deploy.

Validation

  • All 139 source tests are ported, plus 1 new test for the fix: 140 pass.
  • The full suite passes: 2,336 passed, 48 skipped.
  • Differential tests (28 circuits): 6,395 equal results and 549 equal failures. The only difference is the intended message change of the fix.
  • Each fix has a test that fails before the fix and passes after it.
  • Every module and mock also compiles with ZKIR v3.

Types of changes

  • Bugfix
  • New feature
  • Breaking change
  • Documentation update

PR Checklist

  • I have read the Contributing Guide
  • I have added tests that prove my fix is effective or that my feature works
  • I have added documentation for new methods or changes to existing method behavior
  • CI Workflows Are Passing

Port the Pack, Bytes8 and Uint64 modules from midnight-apps
contracts/src/math (7de45ab, identical to the audited commit 7e09763).
The circuit code is unchanged. Only the header, the pragma
(>= 0.26.0) and the module imports (relative paths) change.

Add the test material in the compact-contracts layout:
- mocks MockPack, MockBytes8 and MockUint64 with the source circuit names
  and disclose() calls, a test-only header, and a Counter write that keeps
  every circuit impure;
- async simulators on @openzeppelin/compact-simulator;
- test-only witnesses (PackWitnesses, Uint64Witnesses, sqrtBigint);
- the source tests (139), converted to await every circuit call and to
  assert failures with expectRejection. Tests that replace a witness run
  on the dry backend only, because a live node binds the witnesses at
  deploy. The live backend deploys each mock once per file.

The sqrt range test matches the compact-runtime 0.19.0 type error text.
Uint64._div checked the witness with (quotient * b + remainder) as
Uint<64> == a. A witness with quotient * b + remainder above 2^64 - 1
failed in the implicit cast with a compiler cast error, not with the
module message. Compare the sum as a Uint<128> without the cast, so every
bad division witness fails with a "Uint64:" message.

The check stays sound: quotient * b + remainder is at most 2^128 - 2^64,
so the Uint<128> sum cannot wrap.

Test: div(10, 4) with a witness that returns quotient 2^63 and remainder 2
fails with "Uint64: division invalid".
The doc comment of the internal circuit _toUint64 used the old name
_vector8ToUint64. Use the real name. This is the Bytes8 part of audit
finding N-01 (midnight-apps #312).
The sqrt doc comment described special cases for common inputs and a
sqrtLocally function. Neither exists. The circuit takes the root from the
wit_sqrtUint64 witness and verifies it. Describe that.
…int64

Convert the doc comments of the three modules to the repository style:
@module and @description instead of @title, @dev for implementation
notes, a Requirements list for every assert (with its failure message)
instead of @throws, and @Constraints for the circuit size.

The @Constraints values replace the old @circuitInfo values. They are the
rows of the matching mock circuit, measured with compiler 0.34.0 for ZKIR
v2 and v3 at this commit. The Pack values are per instance size.

Doc comments only. The code is unchanged, and every mock circuit compiles
to the same ZKIR as before this commit.
The base tsconfig.json includes only the witness files, so a type error in
a math simulator, spec or test helper reached CI unreported. The new
tsconfig.math-test.json includes every TypeScript file under src/math and
emits nothing. The package build keeps its own include in
tsconfig.build.json. yarn types runs both checks.
@coderabbitai

coderabbitai Bot commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

The rows of _toUint64 were the rows of its exported wrapper, which
documents them.

This branch has not been deployed

No deployments
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