Fix panics on out-of-range CurveID and oversized hash-to-curve domains - #52
Merged
Conversation
UnmarshalJSON for Zr/G1/G2/Gt indexed the Curves slice with an attacker-controlled CurveID from the JSON payload with no bounds check, causing an unrecovered index-out-of-range panic on malformed input. Curve.HashToG1WithDomain/HashToG2WithDomain likewise panicked on the gnark-backed curves when given a domain longer than 255 bytes. Also clarifies FP256BN's HashToG2 panic message, and corrects doc comments on Mul2/JointScalarMultiplication and BLS12-381's Zr.rawBigInt that overstated performance characteristics or omitted a documented invariant. Fixes #51. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
ale-linux
approved these changes
Jul 27, 2026
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.
Summary
Fixes the panics described in #51:
Zr/G1/G2/GtUnmarshalJSONindexed theCurvesslice with an unbounded, attacker-controlledCurveIDfrom the JSON payload, causing an unrecoveredindex out of rangepanic on malformed input (e.g.{"curve": 9999, ...}). Added avalidCurveIDbounds check that returns an error instead.Curve.HashToG1WithDomain/HashToG2WithDomainpanicked on the gnark-backed curves when given a domain longer than 255 bytes. Wrapped both inrecover(), consistent with the existing panic-to-nil pattern used elsewhere inmath.go, returningnilinstead.HashToG2/HashToG2WithDomainpanic message (AMCL has no G2 hash map) in bothdriver/amcl/fp256bn.goandfp256bn_miracl.go.Mul2/JointScalarMultiplicationdoc comments that implied a wall-clock speedup — verified against gnark-crypto source that neither implementation uses the GLV endomorphism, so the joint scalar-mult is an allocation optimization only, not a wall-clock one.rawBigIntdual-representation invariant on BLS12-381'sZrtype (driver/gurvy/bls12381/bls12-381.go) so a future change doesn't silently dropGroupOrder's raw value viaPlus/Minus/Mul.Two AMCL-side performance findings (naive
MultiScalarMul,Neg()via full scalar-mult instead of native negation) are intentionally out of scope for this PR — tracked as follow-up perf work, not correctness fixes.Test plan
TestJSONUnmarshalerInvalidCurveID— regression for the CurveID panic (rejects out-of-range/negative IDs with an error, still accepts all valid IDs).TestHashToGWithDomainOverlongDomain— regression for the oversized-domain panic across all curves, including AMCL's differing behavior (HMAC-basedHashToG1WithDomainnever panics;HashToG2WithDomainpanics unconditionally, now caught byrecover()and returned asnil).FuzzUnmarshalJSONCurveIDandFuzzHashToGWithDomainnative Go fuzz targets — ran locally for 20s each (~2.2M and ~209k executions) with zero crashes.go build ./...,go vet ./...,go test ./...all pass.Closes #51.