Fix DoS on negative scalar input to mulPointEscalar + EdDSA bounds hardening#38
Open
simonmasson wants to merge 2 commits into
Open
Fix DoS on negative scalar input to mulPointEscalar + EdDSA bounds hardening#38simonmasson wants to merge 2 commits into
simonmasson wants to merge 2 commits into
Conversation
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.
Problem
BabyJub.mulPointEscalar(P, e)assumede >= 0. Passing a negativeBigIntmade thewhile (!Scalar.isZero(rem))loop spin forever -- right-shifting a negative scalar never reaches zero.This was reachable through EdDSA verification: the
verify*functions checkedS >= subOrderbut did not checkS >= 0, so a malformed signature with a raw negativeSmadeverifyPoseidon/verifyMiMC/verifyMiMCSponge/verifyPedersenhang inside scalar mult instead of returningfalse. Verifier DoS, not a forgery.Fix
Two layers:
1. Make the primitive total over
ℤ(src/babyjub.js).If
e < 0, normalize to[-e]·(-P)before the double-and-add loop. On twisted Edwards,-(x, y) = (-x, y), so the rewrite is one field negation and is universally correct (works for any point, in or out of subgroup -- important becauseinSubgroupitself callsmulPointEscalaron potentially-cofactor points, where reducinge mod Lwould silently give the wrong answer).2. Reject malformed signatures at the API boundary (src/eddsa.js).
Symmetric lower-bound check
if (Scalar.lt(sig.S, Scalar.e(0))) return false;added to all fourverify*functions, paired with the existingS >= subOrderupper-bound check. NegativeSis rejected before any scalar mult runs.Layer 1 closes the foot-gun for every caller of
mulPointEscalar(current and future). Layer 2 is defense-in-depth at the EdDSA boundary -- faster rejection and clearer intent.Test
Added "Reject a signature with a negative S" in test/eddsa.js, covering both:
S' = subOrder - S-- stays in[0, subOrder)and passes the bounds check, so it goes through scalar mult; verification correctly returnsfalsebecause the equation doesn't hold.S' = -S-- must be rejected by the new bounds check beforemulPointEscalaris called. The test enforces this by monkey-patchingeddsa.babyJub.mulPointEscalarto throw; if the bounds check ever regresses and verification falls through, the test fails loudly.All existing tests still pass.
Notes
Scalar.geq(Pedersen) and native>=(others). The new lower-bound check usesScalar.ltconsistently. Happy to also normalize the upper-bound style if preferred.