Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions primitives/core/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,21 @@ impl Pair {
let message = secp256k1::Message::parse(message);
secp256k1::sign(&message, &self.secret).into()
}

/// Verify a signature on a pre-hashed message. Return `true` if the signature is valid
pub fn verify_prehashed(&self, sig: &Signature, message: &[u8; 32], public: &Public) -> bool {
let message = secp256k1::Message::parse(message);

let sig: (_, _) = match sig.try_into() {
Ok(x) => x,
_ => return false,
};

match secp256k1::recover(&message, &sig.0, &sig.1) {
Ok(actual) => public.0[..] == actual.serialize_compressed()[..],
_ => false,
}
}
}

impl CryptoType for Public {
Expand Down Expand Up @@ -791,4 +806,18 @@ mod test {

assert_eq!(sig1, sig2);
}

#[test]
fn verify_prehashed_works() {
let (pair, _, _) = Pair::generate_with_phrase(Some("password"));

// `msg` and `sig` match
let msg = keccak_256(b"this should be hashed");
let sig = pair.sign_prehashed(&msg);
assert!(pair.verify_prehashed(&sig, &msg, &pair.public()));

// `msg` and `sig` don't match
let msg = keccak_256(b"this is a different message");
assert!(!pair.verify_prehashed(&sig, &msg, &pair.public()));
}
}