Skip to content

Commit 1979ab3

Browse files
committed
fix: correct usage of verify method
Update the function signature to avoid the potentially confusing usage.
1 parent bea9713 commit 1979ab3

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

stacks-common/src/util/secp256r1.rs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ pub enum Secp256r1Error {
4444
InvalidRecoveryId,
4545
#[error("Signing failed")]
4646
SigningFailed,
47-
#[error("Recovery failed")]
48-
RecoveryFailed,
4947
}
5048

5149
/// A Secp256r1 public key
@@ -174,24 +172,24 @@ impl Secp256r1PublicKey {
174172
}
175173

176174
/// Verify a signature against a message hash.
175+
/// Returns Ok(()) if the signature is valid, or an error otherwise.
177176
pub fn verify_digest(
178177
&self,
179178
msg_hash: &[u8],
180179
sig: &MessageSignature,
181-
) -> Result<bool, &'static str> {
180+
) -> Result<(), Secp256r1Error> {
182181
if msg_hash.len() != 32 {
183-
return Err("Invalid message: must be a 32-byte hash");
182+
return Err(Secp256r1Error::InvalidMessage);
184183
}
185184

186185
let p256_sig = sig
187186
.to_p256_signature()
188-
.map_err(|_| "Invalid signature: failed to decode signature")?;
187+
.map_err(|_| Secp256r1Error::InvalidSignature)?;
189188

190189
// Verify the signature
191-
match self.key.verify(msg_hash, &p256_sig) {
192-
Ok(()) => Ok(true),
193-
Err(_) => Ok(false),
194-
}
190+
self.key
191+
.verify(msg_hash, &p256_sig)
192+
.map_err(|_| Secp256r1Error::InvalidSignature)
195193
}
196194
}
197195

@@ -319,20 +317,16 @@ pub fn secp256r1_verify(
319317
signature_arr: &[u8],
320318
pubkey_arr: &[u8],
321319
) -> Result<(), Secp256r1Error> {
322-
if message_arr.len() != 32 {
323-
return Err(Secp256r1Error::InvalidMessage);
324-
}
325-
326-
if signature_arr.len() != 64 {
327-
return Err(Secp256r1Error::InvalidSignature);
328-
}
320+
let msg: &[u8; 32] = message_arr
321+
.try_into()
322+
.map_err(|_| Secp256r1Error::InvalidMessage)?;
323+
let sig_bytes: &[u8; 64] = signature_arr
324+
.try_into()
325+
.map_err(|_| Secp256r1Error::InvalidSignature)?;
329326

330327
let pk = Secp256r1PublicKey::from_slice(pubkey_arr).map_err(|_| Secp256r1Error::InvalidKey)?;
331-
let sig =
332-
MessageSignature::from_bytes(signature_arr).ok_or(Secp256r1Error::InvalidSignature)?;
333-
pk.verify_digest(message_arr, &sig)
334-
.map_err(|_| Secp256r1Error::InvalidSignature)?;
335-
Ok(())
328+
let sig = MessageSignature::from_bytes(sig_bytes).ok_or(Secp256r1Error::InvalidSignature)?;
329+
pk.verify_digest(msg, &sig)
336330
}
337331

338332
#[cfg(test)]
@@ -391,9 +385,8 @@ mod tests {
391385
let msg_hash = Sha256Sum::from_data(msg).as_bytes().to_vec();
392386

393387
let sig = privk.sign(&msg_hash).unwrap();
394-
let valid = pubk.verify_digest(&msg_hash, &sig).unwrap();
395-
396-
assert!(valid);
388+
pubk.verify_digest(&msg_hash, &sig)
389+
.expect("invalid signature");
397390
}
398391

399392
#[test]
@@ -406,9 +399,10 @@ mod tests {
406399
let msg_hash = Sha256Sum::from_data(msg).as_bytes().to_vec();
407400

408401
let sig = privk1.sign(&msg_hash).unwrap();
409-
let valid = pubk2.verify_digest(&msg_hash, &sig).unwrap();
410-
411-
assert!(!valid);
402+
let e = pubk2
403+
.verify_digest(&msg_hash, &sig)
404+
.expect_err("expected an error");
405+
assert_eq!(e, Secp256r1Error::InvalidSignature);
412406
}
413407

414408
#[test]

0 commit comments

Comments
 (0)