Repository navigation
Conversation
The folding branch starts with gamma*P[n-1], folds P[n-2]..P[1] in the loop and adds P[0] at the end. With one point the loop doesn't run and the result is P[0] + gamma*P[0] instead of P[0]. Return P[0] directly in that case, in the four implementations that support folding.
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.
MultiScalarMulwithalgopts.WithFoldingScalarMul()returnsP0 + [gamma]P0instead ofP0when it is called with a single point:The folding branch computes
sum gamma^i * P[i]Horner-style: it starts with[gamma]P[n-1], foldsP[n-2]..P[1]in the loop, then addsP[0]. Forn == 1the loop body never runs, soP[0]is both the starting point and the final addend. Forn >= 2the result is correct.The same code is in four places, all fixed the same way (early return of the single point):
std/algebra/emulated/sw_emulated(Curve.MultiScalarMul)std/algebra/emulated/sw_bls12381(G2.MultiScalarMul)std/algebra/native/sw_bls12377(Curve.MultiScalarMul)std/algebra/native/sw_grumpkin(Curve.MultiScalarMul)Tests:
TestMultiScalarMulFoldedSinglePointis added in each package and fails on master in all four; existing MSM tests and stats are unchanged.Note
Medium Risk
Correctness fix in folded multi-scalar multiplication used in ZK circuits; wrong results could affect proofs that call MSM with one point, but the change is a small edge-case guard with per-package regression tests.
Overview
Fixes
MultiScalarMulwithalgopts.WithFoldingScalarMul()when there is only one point: the Horner-style folding path used to returnP₀ + [γ]P₀instead ofγ⁰·P₀ = P₀, because the same point was both the initial[γ]Paccumulator and the final addend.The folding branch now early-returns
p[0]whenlen(p) == 1in all four curve wrappers: emulatedsw_emulatedandsw_bls12381G2, and nativesw_bls12377andsw_grumpkin.TestMultiScalarMulFoldedSinglePointis added in each package to lock in the single-point behavior viatest.IsSolved.Reviewed by Cursor Bugbot for commit be283bf. Bugbot is set up for automated code reviews on this repo. Configure here.