fix: preserve Sapling proving state on failure and report ZIP32 decoding errors

FE
5d ago
+231-87

A failed Sapling Spend or Output proof could leave commitment randomness in the proving context. A caller that reused the context after a failure could then fail to create a binding signature for an otherwise valid transaction. The Spend path could also write the randomized key (rk) before returning false.

Sapling proof generation

  • Prepare proof outputs locally. Update the context and copy the outputs only after proof creation, Spend proof verification where applicable, and serialization succeed. If either entry point returns false, the context and all output buffers retain their previous values.
  • Reject small-order Spend authorization keys (ak). Return false for an incorrect witness depth prefix or sibling-node length prefix, replacing the two assertions on those fields.
  • Return false when required proving parameters or the Spend verification key are unavailable, when OsRng::new() fails, or when proof creation returns an error.

ZIP32 native interfaces

  • Add librustzcash_zip32_xsk_derive_checked, which returns false if the parent spending key cannot be decoded or its depth is already 255. Keep the existing void entry point as a wrapper. Callers that need the failure status must use the checked entry point.
  • Return false on viewing-key decoding errors in librustzcash_zip32_xfvk_derive and librustzcash_zip32_xfvk_address. Viewing-key child derivation also rejects a parent at depth 255.

Validation

  • All 10 Rust library tests passed on macOS x86_64 with cargo test --offline --locked --package librustzcash --lib. The release-mode run at a57f7b2 also passed all 10 tests.
  • In the companion SDK, rebuilding the macOS x86_64 JNI library against a57f7b2 and running mvn -o clean package passed all 41 tests, including proof failure followed by another proof on the same context and the complete transaction flow. Three SDK regression tests failed against the pre-fix library and passed after rebuilding.

Downstream adoption

The four-platform builds in SDK #17 and SDK #19 use Rust revision 45f8049. To ship these fixes, the SDK needs to pin the merged Rust revision and rebuild its four bundled libraries. To propagate XSK derivation failures to Java, the JNI wrapper also needs to call the checked entry point and handle false before copying the output. After the SDK release, java-tron can verify proof-failure recovery using the native libraries in its packaged JAR.