frost_enrollment: fix API contract issues found in review

Five review findings, all non-blocking, all in the contract between the
module and its callers rather than in the cryptography. Each fix comes
with a regression test that fails without it.

1. shares_gen zeroed shares32_out before validating n_ids.

   shares32_out is the only output in this module whose size is
   caller-supplied. A caller that takes the helper count from a
   negotiated protocol message, passes a fixed buffer, and relies on
   this API's "invalid ranges return 0" convention would have memory
   past that buffer zeroed before the call reported failure -- turning a
   recoverable length-confusion bug into memory corruption. The frost
   module validates counts first for exactly this reason
   (trusted_dealer_keygen, keygen_impl.h:228).

   Validation now happens before the memset. The early return still
   wipes session_secrand32, because "a failed call cannot be retried on
   the same randomness" is a security property and an exception to it
   would be worse than the tidier control flow. The header's zeroing
   promise is scoped accordingly: the buffer is zeroed on failure except
   when n_ids itself is out of range, where it is not written at all.

2. mismatch_id had an undocumented second cause.

   The header said mismatch_id names the helper whose PARAMETERS HASH
   disagrees and is UINT32_MAX "when the failure has another cause", but
   share_agg also sets it when a helper's share is not a valid scalar.
   The example baked the wrong reading in, printing "Helper %u disagrees
   about the enrollment parameters" for what may be a corrupted
   transmission.

   Documented rather than removed: the attribution is genuinely useful
   for both causes, and this is API- and vector-compatible. The header
   now names both, says they are not distinguished so a caller must not
   report one specifically, and calls out that the second can name the
   CALLER'S OWN identifier, since the kept share is summed with the
   rest. The example's message is corrected in a following commit.

3. params_hash's doc claimed it returns 0 on an "unparseable thresh_pk".

   It does not, and cannot: secp256k1_pubkey_load (secp256k1.c:280) only
   ARG_CHECKs that x is nonzero, so a zeroed pubkey fires the
   illegal-argument callback and any other 64-byte content is accepted
   without curve validation. A caller writing input screening around the
   documented return 0 would abort on the first malformed input. The doc
   now states that an unusable pubkey object is API misuse, matching the
   pointer/value split the impl already follows.

4. params_hash's doc listed three of its ten validity conditions.

   It is the natural pre-validation entry point -- it enforces exactly
   what the other four enforce -- but the doc mentioned only duplicate
   ids and the two n_ids bounds, so the threshold >= 2 divergence and
   the mode-specific n bounds were discoverable only from the .md or the
   source. The parameter list now carries the same constraint lines as
   shares_gen.

5. secshare_gen required a signing context even when it would not sign.

   The ecmult_gen check was unconditional, but ecmult_gen is used only
   inside the expected_pubshare != NULL branch. A caller on a
   verification-only context passing NULL -- explicitly permitted -- hit
   the illegal-argument callback for a generator multiplication that
   would never happen.

   The check is now conditional on expected_pubshare being non-NULL, and
   stays at the top of the function rather than moving into the branch:
   ARG_CHECK returns directly, and from inside the branch that would
   skip the cleanup that wipes secshare and term. Documented in the
   header.

Also in this commit, three comment/dead-code fixes the review noted:
the redundant set_int of `term` in both aggregation loops (always
written by set_b32 before it is read), the Lagrange denominator comment
crediting new_id for something only id distinctness provides, and the
comment that described the memset-before-validation ordering rather than
justifying it -- now moot.

The new run_frost_enrollment_contract_test also closes review coverage
gaps 1, 2 and 8, which overlap these findings: malformed wire scalars
into share_agg and secshare_gen, sigmas summing to zero mod the order,
an invalid secshare32 into shares_gen (the only path exercising its
declassify branch), mismatch_id asserted on a NON-CONTIGUOUS helper set
{0, 2} so an implementation returning the array index would now be
caught, mismatch_id at the caller's own slot, and successful runs with
each optional secshare_gen check skipped and with both skipped.

Both fixes were verified to be load-bearing by reverting them
individually: the F1 test fails on `guarded[i] == 0xa5` and the F5 test
fires the illegal-argument callback. ./tests, ./noverify_tests and
ctime_tests pass; the module is clean under valgrind (0 errors from 0
contexts).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso Ngako
2026-09-04 10:15:26 +02:00
parent 782fede2c5
commit 266c6a7c4f
3 changed files with 222 additions and 24 deletions

View File

@@ -106,19 +106,31 @@ extern "C" {
*
* This function operates on public data only.
*
* Returns: 0 if the arguments are invalid (duplicate ids, n_ids == 0, n_ids
* greater than SECP256K1_FROST_MAX_PARTICIPANTS, unparseable
* thresh_pk), 1 otherwise
* This is also the natural place to pre-validate a parameter tuple: it
* enforces exactly the same constraints as the four functions below, and
* nothing else. Note that an unusable `thresh_pk` object is API MISUSE, not
* an invalid parameter: like every other entry point in the library, this
* function reports it through the illegal-argument callback rather than by
* returning 0.
*
* Returns: 0 if the parameters are invalid, 1 otherwise
* Args: ctx: pointer to a context object
* Out: out32: pointer to a 32-byte array for the hash. Set to zero
* if this function returns 0.
* In: thresh_pk: pointer to the threshold public key of the group
* ids: array of the u helper identifiers. Every id must be
* unique; the order is irrelevant.
* n_ids: number of helpers u
* new_id: identifier of the participant receiving the share
* n_participants: total number of participants n
* threshold: threshold t
* unique, smaller than n_participants and different
* from new_id; the order is irrelevant.
* n_ids: number of helpers u. Must be between threshold and
* n_participants.
* new_id: identifier of the participant receiving the share.
* Must equal n_participants (enrollment) or be smaller
* than it (repair).
* n_participants: total number of participants n. Must be at most
* SECP256K1_FROST_MAX_PARTICIPANTS, and strictly
* smaller in enrollment mode.
* threshold: threshold t. Must be at least 2 (see
* frost_enrollment.md) and at most n_participants.
*/
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_frost_enrollment_params_hash(
const secp256k1_context *ctx,
@@ -160,8 +172,10 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_frost_enrollment_params
* Returns: 0 if the arguments are invalid, 1 otherwise
* Args: ctx: pointer to a context object
* Out: shares32_out: pointer to an array of u*32 bytes for the enrollment
* shares, aligned with `ids`. Set to zero if this
* function returns 0.
* shares, aligned with `ids`. Zeroed if this function
* returns 0 -- except when n_ids itself is out of
* range, in which case the buffer is not written at
* all, since its size is not known to be u*32.
* params_hash32_out: pointer to a 32-byte array for the parameters hash,
* identical to what
* `secp256k1_frost_enrollment_params_hash` returns for
@@ -208,11 +222,27 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_frost_enrollment_shares
*
* The function recomputes its own parameters hash from `thresh_pk` and the
* parameter tuple it is given, and compares every entry of
* `received_params_hashes32` against it. On the first disagreement it returns
* 0 and, if `mismatch_id` is not NULL, stores the IDENTIFIER of the
* disagreeing helper there (not an array index, which would be ambiguous
* because identifiers need not be 0..u-1). `mismatch_id` is set to
* UINT32_MAX when the failure has another cause.
* `received_params_hashes32` against it. It then sums the shares.
*
* `mismatch_id` reports fault attribution for BOTH ways a specific helper's
* contribution can be at fault:
*
* - its parameters hash disagrees with the recomputed one, meaning that
* helper ran round 1.1 on a different parameter tuple or in a different
* group;
* - its entry in `all_shares32` is not a valid scalar (it is not smaller
* than the group order), meaning the value was corrupted in transit or
* fabricated.
*
* In both cases the function returns 0 and, if `mismatch_id` is not NULL,
* stores the IDENTIFIER of the responsible helper there -- not an array
* index, which would be ambiguous because identifiers need not be 0..u-1.
* The two causes are not distinguished, so a caller should not report one of
* them specifically. Note that the second cause can name the CALLER'S OWN
* identifier, since the share kept locally is summed along with the rest.
*
* `mismatch_id` is set to UINT32_MAX when the failure has neither cause,
* which covers every invalid-parameter and API-misuse case.
*
* Note the deliberately OPPOSITE own-slot conventions of the two u*32 input
* buffers, both of which are aligned with `ids`:
@@ -349,7 +379,10 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_frost_enrollment_pubsha
* from the helpers, or NULL to skip the comparison
* expected_pubshare: pointer to the expected public share, from
* `secp256k1_frost_enrollment_pubshare_derive`, or NULL
* to skip the verification (not recommended)
* to skip the verification (not recommended). When it
* is non-NULL, ctx must have been initialized for
* signing; when it is NULL, no context capability
* beyond the default is required.
*/
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_frost_enrollment_secshare_gen(
const secp256k1_context *ctx,