Skip to main content

dpp/util/ecdsa/
mod.rs

1//! ECDSA secp256k1 verification as every released Platform does it: a signature whose S is in
2//! the upper half of the curve order is refused. `dashcore` verifies such a signature like its
3//! low-S twin, the same signature with S negated, so Platform checks S itself, at every protocol
4//! version. Accepting it would fork from nodes that refuse it and change the replay of chain
5//! history.
6//!
7//! A signature checked by recovering its public key ([`verify_hash_signature`]) is the
8//! exception: every released Platform accepts a high-S one there, and one whose header names an
9//! uncompressed key, so refusing them is a rule of protocol version 15, selected by
10//! `verify_hash_signature`.
11
12use dashcore::crypto::ecdsa::{EcdsaError, EcdsaPublicKey, EcdsaSignature};
13use dashcore::{sign_message, signer};
14
15mod verify_hash_signature;
16
17pub use verify_hash_signature::verify_hash_signature;
18
19/// [`signer::verify_data_signature`], refusing a high-S signature with the error of a signature
20/// that does not verify. Every other input gets the result and the error `dashcore` gives.
21pub fn verify_data_signature(
22    data: &[u8],
23    signature: &[u8],
24    public_key: &[u8],
25) -> Result<(), anyhow::Error> {
26    signer::verify_data_signature(data, signature, public_key)?;
27    // `dashcore` parsed the signature to verify it, so parsing it again succeeds.
28    let signature = sign_message::from_slice(signature)?.signature()?;
29    if signature.is_low_s() {
30        Ok(())
31    } else {
32        Err(anyhow::Error::msg(EcdsaError::VerifyFailed))
33    }
34}
35
36/// [`EcdsaPublicKey::verify`], refusing a high-S signature with
37/// [`EcdsaError::VerifyFailed`].
38pub fn verify_digest(
39    public_key: &EcdsaPublicKey,
40    digest: &[u8; 32],
41    signature: EcdsaSignature,
42) -> Result<(), EcdsaError> {
43    if !signature.is_low_s() {
44        return Err(EcdsaError::VerifyFailed);
45    }
46    public_key.verify(digest, signature)
47}