Skip to content

Commit 28947bd

Browse files
committed
Prevent Word16 overflow using a partial function
While this is less defensive, it avoids an overflow going unnoticed.
1 parent d71c586 commit 28947bd

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

  • cardano-crypto-leios/src/Cardano/Crypto

cardano-crypto-leios/src/Cardano/Crypto/Leios.hs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,24 @@ resolveLeiosVoter committee voterId =
194194
-- If the committee carries duplicate verification keys, returns the smallest
195195
-- index matching @vk@ (committee selection is expected to deduplicate, but
196196
-- this module does not enforce it).
197+
--
198+
-- Errors if the matching index does not fit in 'Word16'. The wire format of
199+
-- 'LeiosCert' indexes voters by a 16-bit field, so a committee with more than
200+
-- @2^16@ seats is already malformed. NOTE: this partiality could later be
201+
-- avoided by introducing a smart constructor for 'LeiosCommittee' (or for the
202+
-- committee-selection step in consensus) that rejects oversized committees
203+
-- up front.
197204
getLeiosVoterId :: LeiosVerificationKey -> LeiosCommittee -> Maybe LeiosVoterId
198205
getLeiosVoterId vk committee =
199-
LeiosVoterId . fromIntegral @Int @Word16
200-
<$> V.findIndex ((== vk) . voterVKey) committee.committeeVoters
206+
toVoterId <$> V.findIndex ((== vk) . voterVKey) committee.committeeVoters
207+
where
208+
toVoterId i
209+
| i > fromIntegral @Word16 @Int maxBound =
210+
error $
211+
"Cardano.Crypto.Leios.getVoterId: committee index "
212+
<> show i
213+
<> " does not fit in Word16"
214+
| otherwise = LeiosVoterId (fromIntegral @Int @Word16 i)
201215

202216
-- | A Leios certificate over an endorser block, as specified in CIP-164:
203217
--

0 commit comments

Comments
 (0)