Repository navigation
perf(sapling-crypto)!: hold value commitments as bytes, not points - #638
Conversation
Parsing a Sapling bundle decompressed every spend's and output's cv into a Jubjub point - a field square root each, plus a small-order check - and every consumer short of proof and binding-signature verification then compressed it straight back: the txid digest (cv().to_bytes() is the only field the digest re-serialises from a parsed point), the outgoing cipher key (prf_ock), the sighash, serialisation. Profiled on a wallet's memo backfill, that round trip was 26% of all CPU. ValueCommitment now keeps its 32 bytes. to_bytes() returns them; try_point() decompresses and checks small order on demand, by value; derive() computes the point once and stores its encoding; from_bytes_unvalidated() is the parser's constructor and from_bytes_not_small_order() still validates eagerly for callers that want that. The verifier calls try_point() and fails closed on an invalid encoding, and CommitmentSum takes the point rather than the commitment, so the callers that need arithmetic validate first. The consensus checks move from parse time to verification time, where a node checks the description anyway. Microbenchmark over 30,752 testnet transactions (110 MB; 3,339 Sapling spends, 11,462 outputs): Transaction::read 1.930 s to 0.993 s, 1.94x; 130 to 67 us per Sapling description. The wallet crates read cv as bytes only (prf_ock, serialisation), so they need no change; as_inner()'s only caller in this repository is the verifier. BREAKING CHANGE: ValueCommitment::as_inner() is removed (use try_point()); CommitmentSum's AddAssign/SubAssign take &jubjub::ExtendedPoint; a parsed cv is no longer validated at parse time.
|
@v12sec pls review |
|
1 finding ·
|
Review: node safety, and the rollout planVerdict: the change is consensus-equivalent. The node ( There is one blocking issue, plus some follow-ups. The plan below covers three PRs: this one, then wallet-libraries and the node after the 5.0.0 release. PR 1: revise this PR (
|
| Layer | Today | After bump |
|---|---|---|
check::sapling_point_encodings_are_valid (zakura-consensus/src/transaction.rs:508) |
rejects | rejects |
to_librustzcash → Transaction::read |
rejects | accepts |
sapling_crypto::BatchValidator |
never sees one | rejects |
- Bump
sapling-cryptoandzcash_primitives(rootCargo.tomllines 55 and 58), plus the otherzakura-*crates fromcommon, to=5.0.0. - Rewrite
sapling_small_order_cv_epk_deferred_but_caught_by_librustzcash(crates/zakura-chain/src/transaction/tests/vectors.rs, around line 2425). It asserts thatto_librustzcashrejects a small-ordercv, which will now fail. Don't just flip the assertion: rename the test, restate its doc, and assert that:tx.sapling_point_encodings_are_valid()isfalse;to_librustzcashnow succeeds;BatchValidator::check_bundlerejects the converted bundle (in azakura-consensustest ifzakura-chaincan't reach the verifier).
- In
sapling_point_checks_match_librustzcash_predicates(around line 3085), compare againstfrom_bytes_unvalidated(b).try_point().is_some(), and rewrite the "read_value_commitmentaccepts acviff …" wording. Update the matching comment onis_valid_not_small_orderincrates/zakura-chain/src/sapling/commitment.rs(around lines 55–62). - At
check::sapling_point_encodings_are_valid(&tx)?, add a comment saying it is now the only check before verification for Saplingcv. Keep the check. - Coinbase:
decrypts_successfully(zakura-chain/src/primitives/zcash_note_encryption.rs:15) used to returnfalsefor a coinbase with an invalidcv, because the conversion failed. It can now returntrue. Add or confirm a test that such a block is rejected on the semantic verification path. - Switch
binding_verification_key(zakura-chain/src/sapling/shielded_data.rs:334, used only in tests) totry_sum. This removes its second decode of each point. - Add the changelog fragment and run
./scripts/changelog.py check.
Optional, separate PR: the node serializes and re-parses each transaction through to_librustzcash several times per verification (sighash setup, auth_digest, txid fallback, coinbase decryption check). Converting once and sharing the result would save more for the node than this PR does, and it doesn't depend on the 5.0.0 release.
Line numbers are as of e9ce583 and the current main of each repository; search for the named symbol if they have shifted.
Generated by Claude Code
…um::try_sum The Add, Sub and Sum impls over ValueCommitment decoded each point with an expect. Transaction::read now yields unvalidated commitments, so summing the cvs of a parsed transaction could panic. Replace them with CommitmentSum::try_sum, which returns None on an invalid commitment; the builder expects on its own commitments and the PCZT IO finalizer returns ValueCommitMismatch. Correct the ValueCommitment doc on network data, and test that try_point accepts exactly the encodings parsing used to accept.
…cation Corrupt a spend cv and an output cv of a valid testnet transaction, first to the identity and then to bytes off the curve. The transaction still reads and round-trips, and the batch validator, the single verifier and CommitmentSum::try_sum all reject it.
|
Re: v12 F-297 (Info): Deferred checks amplify invalid transaction work The finding's description is accurate, and we accept it as Info. With this change, a transaction whose first Sapling This doesn't make a DoS vector, for two reasons:
Wallets receive transactions from their own lightwalletd/indexer and only trial-decrypt them. For a wallet, parsing without validating Moving the check back into parsing would undo the purpose of this PR. A cheaper early exit in Generated by Claude Code |
Demo only. Patch every Common crate to zakura-core/common@0f4793b, the merge of zakura-core/common#638, which keeps Sapling value commitments as bytes. That landed after 5.0.0-rc.2; replace the git pin with the next Common release before merging. librustzcash's parser now keeps `cv` as bytes, so `to_librustzcash` no longer rejects an invalid Sapling `cv`. The node's early semantic check, `check::sapling_point_encodings_are_valid`, becomes the first rejection and the library verifier's `try_point` the second. - Move `binding_verification_key` to `CommitmentSum::try_sum`; the `Sum` impl it used is removed, and this drops a second decode of each point. - Comment that the early check is the only one before proof work. - Rewrite the chain tests that asserted `to_librustzcash` rejects an invalid `cv`: they now assert the semantic check rejects it, conversion keeps the bytes, and `try_point` rejects the converted commitment. - Pin the parity test to the verifier's `try_point`. - Add a test that `BatchValidator::check_bundle` rejects real mainnet bundles with a small-order or off-curve spend or output `cv`. - Add a test that a coinbase with an invalid Sapling `cv` is rejected with `SmallOrder`, since the coinbase decryption check can no longer be relied on to reject it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YAZUf8ugteNYiQRAkZfZUV
…es) (#1907) * build(deps): pin Common to the merge of zakura-core/common#638 Demo only. Patch every Common crate to zakura-core/common@0f4793b, the merge of zakura-core/common#638, which keeps Sapling value commitments as bytes. That landed after 5.0.0-rc.2; replace the git pin with the next Common release before merging. librustzcash's parser now keeps `cv` as bytes, so `to_librustzcash` no longer rejects an invalid Sapling `cv`. The node's early semantic check, `check::sapling_point_encodings_are_valid`, becomes the first rejection and the library verifier's `try_point` the second. - Move `binding_verification_key` to `CommitmentSum::try_sum`; the `Sum` impl it used is removed, and this drops a second decode of each point. - Comment that the early check is the only one before proof work. - Rewrite the chain tests that asserted `to_librustzcash` rejects an invalid `cv`: they now assert the semantic check rejects it, conversion keeps the bytes, and `try_point` rejects the converted commitment. - Pin the parity test to the verifier's `try_point`. - Add a test that `BatchValidator::check_bundle` rejects real mainnet bundles with a small-order or off-curve spend or output `cv`. - Add a test that a coinbase with an invalid Sapling `cv` is rejected with `SmallOrder`, since the coinbase decryption check can no longer be relied on to reject it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YAZUf8ugteNYiQRAkZfZUV * docs(changelog): fragment for #1907 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01YAZUf8ugteNYiQRAkZfZUV * build(deps): use published Common 5.0.0-rc.3 * build(deps): carry Common audit backlog to rc.3 --------- Co-authored-by: Claude <[email protected]> Co-authored-by: roman <[email protected]>
Problem
Parsing a Sapling bundle decompresses every spend's and output's
cvinto a Jubjub point - a field square root each, plus a small-order check - and every consumer short of proof and binding-signature verification compresses it straight back: the txid digest (cv().to_bytes()is the only field the digest re-serialises from a parsed point; everything else hashes the original bytes),prf_ockin note encryption, the v4 sighash, serialisation.Profiled on a wallet's memo backfill (the drain that fetches and stores full transactions), that round trip was 26% of all CPU, inside a
Transaction::readthat was 37%.Solution
ValueCommitmentkeeps its 32-byte encoding.to_bytes()returns the stored bytes.try_point() -> CtOption<jubjub::ExtendedPoint>decompresses and checks small order on demand.derive()computes the point once and stores its encoding.from_bytes_unvalidated()is what parsing uses;from_bytes_not_small_order()still validates eagerly for callers that want that.CommitmentSum'sAddAssign/SubAssigntake&jubjub::ExtendedPointinstead of&ValueCommitment. The Sapling verifier (the onlyas_innercaller in this repository) callstry_point()and fails closed on an invalid encoding before summing;zcash_proofsreaches it through the same path.The consensus checks on
cvmove from parse time to verification time, where a node checks the description anyway. Chain data a node has validated never fails them.Behavior change (breaking)
ValueCommitment::as_inner()is removed; usetry_point().CommitmentSum: AddAssign<&ValueCommitment>/SubAssign<&ValueCommitment>becomeAddAssign<&jubjub::ExtendedPoint>/SubAssign<&jubjub::ExtendedPoint>.Transaction::readaccepts a bundle whosecvis not a valid point; verification rejects it instead. A wallet that stores such a transaction stores its bytes, as it stores every other field it does not verify.The wallet-libraries workspace reads Sapling
cvas bytes only (prf_ock, serialisation); itsValueCommitmentuses are Orchard's type, so it needs no change.workspace.package.versionis already 5.0.0 onmainwithrefactor!commits, so this fits the open breaking cycle; the semver job should report the removal as covered by that bump. Downstream consumers of the verifier (the node) should be grepped foras_innerbefore this is merged upstream.Verification
cargo test -p zakura-sapling-crypto --all-features: 78 passed (3 ignored, as onmain), 1 doc-test;-p zakura-primitives: 72 + 1;-p zakura-proofs: passed.cargo +1.97.1 clippy --no-deps --all-features --all-targets -- -D warningson the three crates clean;cargo doc --no-depsclean;cargo fmt --checkclean.Transaction::read1.930 s to 0.993 s, 1.94x; 130 us to 67 us per Sapling description.