@@ -276,6 +276,7 @@ pub enum HpkeError {
276276 InvalidKeyLength ,
277277 PayloadTooLarge { actual : usize , max : usize } ,
278278 PayloadTooShort ,
279+ UnexpectedSecp256k1Error ,
279280}
280281
281282impl From < hpke:: HpkeError > for HpkeError {
@@ -285,10 +286,11 @@ impl From<hpke::HpkeError> for HpkeError {
285286impl From < secp256k1:: Error > for HpkeError {
286287 fn from ( value : secp256k1:: Error ) -> Self {
287288 match value {
288- // As of writing, this is the only relevant variant that could arise here.
289- // This may need to be updated if relevant variants are added to secp256k1
289+ // As of writing, this is the only relevant variant that could arise here. The other variant has
290+ // been added due to new secp256k1::Error variants that may be added in the future. update this
291+ // match statement if relevant error variants that are needed are added to secp256k1
290292 secp256k1:: Error :: InvalidPublicKey => Self :: InvalidPublicKey ,
291- _ => panic ! ( "Unsupported variant of secp256k1::Error" ) ,
293+ _other => Self :: UnexpectedSecp256k1Error ,
292294 }
293295 }
294296}
@@ -308,6 +310,7 @@ impl fmt::Display for HpkeError {
308310 }
309311 PayloadTooShort => write ! ( f, "Payload too small" ) ,
310312 InvalidPublicKey => write ! ( f, "Invalid public key" ) ,
313+ UnexpectedSecp256k1Error => write ! ( f, "Unexpected secp256k1 error" ) ,
311314 }
312315 }
313316}
@@ -321,6 +324,7 @@ impl error::Error for HpkeError {
321324 PayloadTooLarge { .. } => None ,
322325 InvalidKeyLength | PayloadTooShort => None ,
323326 InvalidPublicKey => None ,
327+ UnexpectedSecp256k1Error => None ,
324328 }
325329 }
326330}
@@ -329,6 +333,25 @@ impl error::Error for HpkeError {
329333mod test {
330334 use super :: * ;
331335
336+ #[ test]
337+ fn secp256k1_error_conversion_no_panic ( ) {
338+ // Test the known variant that maps to InvalidPublicKey(update if new variants are added)
339+ let err = secp256k1:: Error :: InvalidPublicKey ;
340+ let hpke_err: HpkeError = err. into ( ) ;
341+ assert_eq ! ( hpke_err, HpkeError :: InvalidPublicKey ) ;
342+ // Test other variants that may arise
343+ let other_variants = [
344+ secp256k1:: Error :: InvalidSecretKey ,
345+ secp256k1:: Error :: InvalidRecoveryId ,
346+ secp256k1:: Error :: InvalidTweak ,
347+ secp256k1:: Error :: NotEnoughMemory ,
348+ ] ;
349+ for err in other_variants {
350+ let hpke_err: HpkeError = err. into ( ) ;
351+ assert_eq ! ( hpke_err, HpkeError :: UnexpectedSecp256k1Error ) ;
352+ }
353+ }
354+
332355 #[ test]
333356 fn message_a_round_trip ( ) {
334357 let mut plaintext = "foo" . as_bytes ( ) . to_vec ( ) ;
0 commit comments