Add try_recover_key; handle the recover_key -1 sentinel#59
Merged
Conversation
The host recover_key intrinsic returns the required public-key size on success and a negative code on a contract-observable failure (bad/empty/truncated signature, unactivated or unknown variant, recovery-math failure). The CDT wrappers never accounted for the negative code: - recover_key assigned the int result straight into size_t, so a -1 became SIZE_MAX, fell into the large-buffer branch, and ran malloc(SIZE_MAX) + a datastream over an invalid pointer -- a WASM trap on any unrecoverable signature. Add try_recover_key, returning std::optional<public_key>: std::nullopt when the host signals failure, the key otherwise. Intended for callers that accept untrusted, user-submitted signatures and must not abort the transaction on bad input (e.g. consensus inline-action handlers). Harden the by-value recover_key: a negative host code is now a clean check() abort instead of the SIZE_MAX path. Both functions share one recover_key_impl so the recovery/large-buffer logic has a single implementation. The large-buffer branch now also checks the allocation result and the refill return code instead of trusting them blindly. crypto_tests gains try_recover_key_test, which stubs the host recover_key via set_intrinsic and exercises both wrappers end to end: a non-negative result yields the recovered key; a negative result yields std::nullopt from try_recover_key and a clean abort from recover_key. The recovered-key check is guarded so a regression reports a clean failure rather than dereferencing an empty optional. The host-side non-throwing behavior shipped in wire-sysio (recover_key returns -1 on contract-observable failure); this is the CDT-side half that lets contracts actually consume it without trapping. The host's subjective WebAuthn variable-size guard still rejects the transaction before the contract observes anything and is deliberately not maskable.
capi_checksum256 is declared alignas(16); the byte-array storage from checksum256::extract_as_byte_array() is alignment 1. reinterpret_cast'ing it to capi_checksum256* is a misaligned-pointer access (UB; can trap or miscompile on strict / native targets, and the native unit tests now exercise this path). recover_key_impl and assert_recover_key now memcpy the digest into a properly aligned local and pass that to the host intrinsics.
huangminghuang
approved these changes
May 18, 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.
The host recover_key intrinsic returns the required public-key size on success and a negative code on a contract-observable failure (bad/empty/truncated signature, unactivated or unknown variant, recovery-math failure). The CDT wrappers never accounted for the negative code:
Add try_recover_key, returning std::optional<public_key>: std::nullopt when the host signals failure, the key otherwise. Intended for callers that accept untrusted, user-submitted signatures and must not abort the transaction on bad input (e.g. consensus inline-action handlers).
Harden the by-value recover_key: a negative host code is now a clean check() abort instead of the SIZE_MAX path. Both functions share one recover_key_impl so the recovery/large-buffer logic has a single implementation. The large-buffer branch now also checks the allocation result and the refill return code instead of trusting them blindly.
crypto_tests gains try_recover_key_test, which stubs the host recover_key via set_intrinsic and exercises both wrappers end to end: a non-negative result yields the recovered key; a negative result yields std::nullopt from try_recover_key and a clean abort from recover_key. The recovered-key check is guarded so a regression reports a clean failure rather than dereferencing an empty optional.
The host-side non-throwing behavior shipped in wire-sysio (recover_key returns -1 on contract-observable failure); this is the CDT-side half that lets contracts actually consume it without trapping. The host's subjective WebAuthn variable-size guard still rejects the transaction before the contract observes anything and is deliberately not maskable.
Pairs with wire-sysio's non-throwing recover_key (PR #314). Follow-up on the wire-sysio side: verify_uic_signature switches to try_recover_key and the part3 recover_key_nothrow host intrinsic is dropped, once master lands in the OPP line.