Repository navigation
feat(crypto): Grumpkin affine point arithmetic - #729
Conversation
Brings in CAP-80 BN254 scalar field arithmetic (Bn254Fr Add/Sub/Mul, pow, inv), CAP-82 checked U256/I256 arithmetic, CAP-78 bounded TTL extensions, and CAP-79 muxed-address strkey conversions. Changes are additive — workspace builds, clippy passes, and the full test suite remains green.
Adds Grumpkin (y² = x³ - 17 over BN254's scalar field) affine point add/sub/neg and on-curve / non-identity validators, implemented on top of the BN254 Fr host calls exposed by CAP-80. Coordinates use the 64-byte (be(x) || be(y)) encoding with (0, 0) as the identity. The five addition cases (left/right identity, inverse, doubling, generic) are each branched explicitly; the inverse case is detected before the slope formula to avoid division by zero. Tested with 16 unit tests plus 8 proptests covering commutativity, associativity, identity, inversion, Pedersen homomorphism, and on-curve validation. Ground-truth point values come from ark-grumpkin (dev-dep) to keep the tests independent of the implementation.
Bn254Fr::from_bytes silently reduces mod r, so be(x+r) || be(y) and be(x) || be(y) decode to the same logical point but compare unequal as BytesN<64>. A downstream contract keyed on raw Point bytes (the idiomatic Soroban pattern for uniqueness / nullifier / commitment-log checks) would accept both encodings and double-record the same commitment. is_on_curve now rejects any 32-byte half that is >= r, mirroring the SDK's own Bn254Fp validation. Coordinates that already passed the validator are now guaranteed canonical, making BytesN<64> equality sound for uniqueness on validated points.
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
WalkthroughThis PR implements Grumpkin elliptic curve point arithmetic for the Soroban contract wrapper by adding a new ChangesGrumpkin point arithmetic implementation
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #729 +/- ##
==========================================
+ Coverage 96.44% 96.49% +0.04%
==========================================
Files 60 61 +1
Lines 6356 6440 +84
==========================================
+ Hits 6130 6214 +84
Misses 226 226 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/contract-utils/src/crypto/test/grumpkin.rs (1)
364-382: 💤 Low valueMinor: Comment overstates overflow likelihood.
For canonical coordinates (< r), the sum
coord + ris always < 2r ≈ 2^255 < 2^256, so overflow cannot occur. TheNonebranch is purely defensive and will never trigger for valid test inputs fromto_point. The current comment suggests overflow is "unlikely" when it's actually impossible for canonical inputs.This is cosmetic and doesn't affect correctness.
📝 Suggested documentation fix
/// Adds the BN254 scalar field modulus `r` to a canonical 32-byte big-endian /// coordinate, producing a non-canonical encoding that reduces to the same /// field element under `Bn254Fr::from_bytes`. Returns `None` if the sum -/// overflows 32 bytes (only happens for `coord` very close to `r`, which is -/// extremely unlikely for randomly generated points). +/// overflows 32 bytes (cannot happen for canonical coordinates < r, since +/// coord + r < 2r < 2^256; the check is purely defensive).🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/contract-utils/src/crypto/test/grumpkin.rs` around lines 364 - 382, The comment on add_modulus_be overstates overflow likelihood; update the docstring to state that for canonical coordinates (coord < r) coord + r < 2r < 2^256 so byte overflow cannot occur and the None branch is purely defensive, while keeping the implementation intact; reference add_modulus_be and FR_MODULUS_BE in the comment to clarify that None is only reachable for non-canonical/invalid inputs.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/contract-utils/src/crypto/test/grumpkin.rs`:
- Around line 364-382: The comment on add_modulus_be overstates overflow
likelihood; update the docstring to state that for canonical coordinates (coord
< r) coord + r < 2r < 2^256 so byte overflow cannot occur and the None branch is
purely defensive, while keeping the implementation intact; reference
add_modulus_be and FR_MODULUS_BE in the comment to clarify that None is only
reachable for non-canonical/invalid inputs.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 98adeeee-a9f0-4e7f-9299-7b34a8dd30c5
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (6)
Cargo.tomlpackages/contract-utils/Cargo.tomlpackages/contract-utils/src/crypto/grumpkin.rspackages/contract-utils/src/crypto/mod.rspackages/contract-utils/src/crypto/test/grumpkin.rspackages/contract-utils/src/crypto/test/mod.rs
Summary
Adds Grumpkin (y² = x³ − 17 over BN254's scalar field F_r) affine point arithmetic to
packages/contract-utils/src/crypto/, intended as a building block for on-chain Pedersen commitments, ECDH, and any other Grumpkin-based primitive built on top of CAP-80.Resolves #700.
Grumpkin::{add, sub, neg}— affine add/sub/neg with full identity handling (left/right identity, inverse, doubling, generic), implemented on top of the BN254 Fr host calls (bn254_fr_{add, sub, mul, inv}).Grumpkin::{is_on_curve, is_identity, is_not_identity}— validation helpers for points entering the system without a soundness proof.is_on_curveenforces both the curve equation and the canonical 32-byte representative of each coordinate (mirroring the SDK's ownBn254Fpvalidation), so byte equality on validated points is sound for uniqueness / nullifier / commitment-log keys.Pointis a 64-byteBytesN<64>laid out asbe(x) || be(y); identity is the all-zero encoding.Out of scope (per the issue)
multi_scalar_mul.multi_add— sequentialaddis fine for v0.8; revisit if benchmarks justify.Commits
bump soroban-sdk to 26.0.0— additive: CAP-80 BN254 Fr arithmetic + CAP-78/79/82.feat(crypto): on-chain Grumpkin affine point arithmetic— the module + 24 tests.fix(grumpkin): reject non-canonical coordinate encodings in is_on_curve— a self-review caught thatBn254Fr::from_bytessilently reduces mod r, which would have let two distinct byte encodings of the same logical commitment slip past the validator. Fix + 3 additional tests.Test plan
cargo +nightly fmt --all -- --checkcargo clippy --release --locked --all-targets -- -D warningscargo test --workspaceark-grumpkin(dev-dep).Notes for reviewers
Bn254FrAdd/Sub/Mul/inv(no equivalent surface in 25). Bump is additive per the SDK migration notes; the full workspace builds and tests pass unchanged. Worth a glance at theCargo.lockdiff in the first commit.add/sub/neg: documented precondition, matches the design's "validate at the trust boundary" stance. The validator is the line of defence; arithmetic is hot-path.🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Dependencies
soroban-sdkto version 26.0.0.ark-*cryptographic libraries for curve operations.