Skip to content

Add try_recover_key; handle the recover_key -1 sentinel#59

Merged
heifner merged 2 commits into
masterfrom
feature/try-recover-key
May 18, 2026
Merged

Add try_recover_key; handle the recover_key -1 sentinel#59
heifner merged 2 commits into
masterfrom
feature/try-recover-key

Conversation

@heifner
Copy link
Copy Markdown
Contributor

@heifner heifner commented May 18, 2026

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.

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.

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.
@heifner heifner changed the title crypto: add try_recover_key; handle the recover_key -1 sentinel Add try_recover_key; handle the recover_key -1 sentinel May 18, 2026
@heifner heifner requested a review from huangminghuang May 18, 2026 21:04
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.
@heifner heifner merged commit 3bf3c05 into master May 18, 2026
4 checks passed
@heifner heifner deleted the feature/try-recover-key branch May 18, 2026 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants