Repository navigation
Conversation
Contributor
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Comment |
This was referenced Oct 15, 2026
This was referenced Oct 15, 2026
pepebndc
force-pushed
the
refactor/math-11-native-div
branch
from
October 15, 2026 06:22
2fd75a9 to
999f24f
Compare
The Uint<128> forms of div, rem, divRem and isMultiple call wit_divUint128(a, b) once and check its result with native Uint arithmetic: r < b, q_h * b <= 2^64 - 1, q * b <= 2^128 - 1 and q * b + r == a. They no longer split a and b with toU128 and no longer call wit_divU128 or _mul. The zero-divisor check stays before the witness call. Rows (compiler 0.34.0, v2 / v3): div 3356 / 1017 to 845 / 287, rem 3356 / 1017 to 843 / 285, divRem 3269 / 1026 to 886 / 328, isMultiple 3233 / 992 to 850 / 290. Every valid input keeps its result and its failure message. For each witness quotient and remainder, the first failing check has the message of the U128 form, also for the plan review MM-04 witness (10 / 2^65 with quotient 2^64 - 1). Under D13, a wrong split can no longer make these circuits fail with "Uint128: conversion invalid", and each makes one witness call (10 before: 9 of wit_divUint128 and 1 of wit_divU128). Tests: the unit and malicious-witness tests forge the division witness of the new form; a dry spy shows one wit_divUint128(a, b) call; a property test compares the failure message with the U128 form for forged results; the relation tests forge every limb of the division witness, and a constraint-removal test lists the five constraints that other constraints imply. MathRelationCounterexamples.test.ts keeps the _div preimages for the U128 forms. For the Uint<128> forms it has field preimages of their own constraints: the range of a, the four limbs of the witness result, the remainder check and the final equation (the audit A2 case 10 / 3 with quotient 4 and remainder 1). The coordinated split forgeries and the replay fallback tests of PR 5 apply to the U128 forms (divU128 and the others), where wit_divU128 runs before the splits of _mul.
The Uint<128> forms of div, rem, divRem and isMultiple now make one wit_divUint128 call and no wit_divU128 call. Update the witness table, the rows table and the migration notes, and add the CHANGELOG line.
pepebndc
force-pushed
the
refactor/math-11-native-div
branch
from
October 15, 2026 09:25
999f24f to
ba8b17e
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part 11 of 13 of the math library migration from midnight-apps
contracts/src/math. Refs #213.Stack: #1125 → #1126 → #1127 → #1128 → #1140 → #1141 → #1142 → #1143 → #1144 → #1145 → #1146 → #1147 → #1148. Merge in order.
Summary
The
Uint<128>forms ofdiv,rem,divRemandisMultiplecheck the result of onewit_divUint128(a, b)call with native arithmetic. Every valid input keeps its result and its failure message, and the circuits are about 4 times smaller.Changes
U128form (73f3901d). Rows (v2 / v3):div3356 / 1017 to 845 / 287,divRem3269 / 1026 to 886 / 328. The forms no longer callwit_divU128.wit_divUint128with the divisorb, so a consumer witness must implement a general division, as the reference does; a witness that only splits at 2^64 makes them fail (ba8b17e1).aandb, so a wrong split no longer fails them withUint128: conversion invalid; they return the honest result. (2) They no longer callwit_divU128, so a replacedwit_divU128result has no effect; they return the honest result. (3) The firstwit_divUint128call is now the division, so a replaced result of that call is a forged quotient and remainder, and it fails with the first failing message of theU128form, for exampleUint128: remainder error.Validation
b > 0,r < banda = q * b + ras integers. Only one pair(q, r)satisfies these conditions, so an accepted result is the true quotient and remainder. A machine-checked proof confirms this.wit_divU128result, which these forms no longer request, and now return the honest result. Two replace the firstwit_divUint128result, which is now the division, and now fail withUint128: remainder errorinstead ofUint128: conversion invalid.Types of changes
PR Checklist