2023-05-03 01:37:07 +00:00
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
|
|
|
|
2021-09-09 09:25:06 +00:00
|
|
|
//! Structs and functionality related to the ECDSA signature algorithm.
|
2022-11-24 00:13:45 +00:00
|
|
|
//!
|
2021-09-09 09:25:06 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "recovery")]
|
|
|
|
mod recovery;
|
2022-11-03 00:09:32 +00:00
|
|
|
pub mod serialized_signature;
|
|
|
|
|
2022-11-16 00:06:12 +00:00
|
|
|
use core::{fmt, ptr, str};
|
2021-09-09 09:25:06 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "recovery")]
|
2022-11-03 00:09:32 +00:00
|
|
|
pub use self::recovery::{RecoverableSignature, RecoveryId};
|
|
|
|
pub use self::serialized_signature::SerializedSignature;
|
|
|
|
use crate::ffi::CPtr;
|
2022-02-08 06:11:05 +00:00
|
|
|
#[cfg(feature = "global-context")]
|
2021-09-14 09:18:22 +00:00
|
|
|
use crate::SECP256K1;
|
2022-11-03 00:09:32 +00:00
|
|
|
use crate::{
|
|
|
|
ffi, from_hex, Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification,
|
|
|
|
};
|
2022-02-08 06:11:05 +00:00
|
|
|
|
2021-09-09 09:25:06 +00:00
|
|
|
/// An ECDSA signature
|
Implement stable comparison functionality
Currently we rely on the inner bytes with types that are passed across
the FFI boundry when implementing comparison functions (e.g. `Ord`,
`PartialEq`), this is incorrect because the bytes are opaque, meaning
the byte layout is not guaranteed across versions of `libsecp26k1`.
Implement stable comparison functionality by doing:
- Implement `core::cmp` traits by first coercing the data into a stable
form e.g., by serializing it.
- Add fast comparison methods to `secp256k1-sys` types that wrap types
from libsecp, add similar methods to types in `secp256k1` that wrap
`secp256k1-sys` types (just call through to inner type).
- In `secp256k1-sys` feature gate the new `core::cmp` impls on
`not(fuzzing)`, when fuzzing just derive the impls instead.
Any additional methods added to `secp256k1-sys` types are private,
justified by the fact the -sys is meant to be just a thin wrapper around
libsecp256k1, we don't want to commit to supporting additional API
functions.
Please note, the solution presented in this patch is already present for
`secp256k1::PublicKey`, this PR removes that code in favour of deriving
traits that then call down to the same logic in `secp256k1-sys`.
2022-11-17 04:56:35 +00:00
|
|
|
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
2021-09-09 09:25:06 +00:00
|
|
|
pub struct Signature(pub(crate) ffi::Signature);
|
Implement stable comparison functionality
Currently we rely on the inner bytes with types that are passed across
the FFI boundry when implementing comparison functions (e.g. `Ord`,
`PartialEq`), this is incorrect because the bytes are opaque, meaning
the byte layout is not guaranteed across versions of `libsecp26k1`.
Implement stable comparison functionality by doing:
- Implement `core::cmp` traits by first coercing the data into a stable
form e.g., by serializing it.
- Add fast comparison methods to `secp256k1-sys` types that wrap types
from libsecp, add similar methods to types in `secp256k1` that wrap
`secp256k1-sys` types (just call through to inner type).
- In `secp256k1-sys` feature gate the new `core::cmp` impls on
`not(fuzzing)`, when fuzzing just derive the impls instead.
Any additional methods added to `secp256k1-sys` types are private,
justified by the fact the -sys is meant to be just a thin wrapper around
libsecp256k1, we don't want to commit to supporting additional API
functions.
Please note, the solution presented in this patch is already present for
`secp256k1::PublicKey`, this PR removes that code in favour of deriving
traits that then call down to the same logic in `secp256k1-sys`.
2022-11-17 04:56:35 +00:00
|
|
|
impl_fast_comparisons!(Signature);
|
2021-09-09 09:25:06 +00:00
|
|
|
|
|
|
|
impl fmt::Debug for Signature {
|
2022-11-16 00:06:12 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(self, f) }
|
2021-09-09 09:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Signature {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let sig = self.serialize_der();
|
2022-01-06 23:31:15 +00:00
|
|
|
sig.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 09:25:06 +00:00
|
|
|
impl str::FromStr for Signature {
|
|
|
|
type Err = Error;
|
|
|
|
fn from_str(s: &str) -> Result<Signature, Error> {
|
|
|
|
let mut res = [0u8; 72];
|
|
|
|
match from_hex(s, &mut res) {
|
|
|
|
Ok(x) => Signature::from_der(&res[0..x]),
|
|
|
|
_ => Err(Error::InvalidSignature),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Signature {
|
|
|
|
#[inline]
|
|
|
|
/// Converts a DER-encoded byte slice to a signature
|
|
|
|
pub fn from_der(data: &[u8]) -> Result<Signature, Error> {
|
2022-11-16 00:06:12 +00:00
|
|
|
if data.is_empty() {
|
|
|
|
return Err(Error::InvalidSignature);
|
|
|
|
}
|
2021-09-09 09:25:06 +00:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut ret = ffi::Signature::new();
|
|
|
|
if ffi::secp256k1_ecdsa_signature_parse_der(
|
|
|
|
ffi::secp256k1_context_no_precomp,
|
|
|
|
&mut ret,
|
|
|
|
data.as_c_ptr(),
|
2022-12-16 00:02:02 +00:00
|
|
|
data.len(),
|
2021-09-09 09:25:06 +00:00
|
|
|
) == 1
|
|
|
|
{
|
|
|
|
Ok(Signature(ret))
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidSignature)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a 64-byte compact-encoded byte slice to a signature
|
|
|
|
pub fn from_compact(data: &[u8]) -> Result<Signature, Error> {
|
|
|
|
if data.len() != 64 {
|
2022-11-16 00:06:12 +00:00
|
|
|
return Err(Error::InvalidSignature);
|
2021-09-09 09:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut ret = ffi::Signature::new();
|
|
|
|
if ffi::secp256k1_ecdsa_signature_parse_compact(
|
|
|
|
ffi::secp256k1_context_no_precomp,
|
|
|
|
&mut ret,
|
|
|
|
data.as_c_ptr(),
|
|
|
|
) == 1
|
|
|
|
{
|
|
|
|
Ok(Signature(ret))
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidSignature)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a "lax DER"-encoded byte slice to a signature. This is basically
|
|
|
|
/// only useful for validating signatures in the Bitcoin blockchain from before
|
|
|
|
/// 2016. It should never be used in new applications. This library does not
|
|
|
|
/// support serializing to this "format"
|
|
|
|
pub fn from_der_lax(data: &[u8]) -> Result<Signature, Error> {
|
2022-11-16 00:06:12 +00:00
|
|
|
if data.is_empty() {
|
|
|
|
return Err(Error::InvalidSignature);
|
|
|
|
}
|
2021-09-09 09:25:06 +00:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut ret = ffi::Signature::new();
|
|
|
|
if ffi::ecdsa_signature_parse_der_lax(
|
|
|
|
ffi::secp256k1_context_no_precomp,
|
|
|
|
&mut ret,
|
|
|
|
data.as_c_ptr(),
|
2022-12-16 00:02:02 +00:00
|
|
|
data.len(),
|
2021-09-09 09:25:06 +00:00
|
|
|
) == 1
|
|
|
|
{
|
|
|
|
Ok(Signature(ret))
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidSignature)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Normalizes a signature to a "low S" form. In ECDSA, signatures are
|
|
|
|
/// of the form (r, s) where r and s are numbers lying in some finite
|
|
|
|
/// field. The verification equation will pass for (r, s) iff it passes
|
|
|
|
/// for (r, -s), so it is possible to ``modify'' signatures in transit
|
|
|
|
/// by flipping the sign of s. This does not constitute a forgery since
|
|
|
|
/// the signed message still cannot be changed, but for some applications,
|
|
|
|
/// changing even the signature itself can be a problem. Such applications
|
|
|
|
/// require a "strong signature". It is believed that ECDSA is a strong
|
|
|
|
/// signature except for this ambiguity in the sign of s, so to accommodate
|
2023-04-20 02:46:18 +00:00
|
|
|
/// these applications libsecp256k1 considers signatures for which s is in
|
|
|
|
/// the upper half of the field range invalid. This eliminates the
|
2021-09-09 09:25:06 +00:00
|
|
|
/// ambiguity.
|
|
|
|
///
|
|
|
|
/// However, for some systems, signatures with high s-values are considered
|
|
|
|
/// valid. (For example, parsing the historic Bitcoin blockchain requires
|
|
|
|
/// this.) For these applications we provide this normalization function,
|
|
|
|
/// which ensures that the s value lies in the lower half of its range.
|
|
|
|
pub fn normalize_s(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
// Ignore return value, which indicates whether the sig
|
|
|
|
// was already normalized. We don't care.
|
|
|
|
ffi::secp256k1_ecdsa_signature_normalize(
|
|
|
|
ffi::secp256k1_context_no_precomp,
|
|
|
|
self.as_mut_c_ptr(),
|
|
|
|
self.as_c_ptr(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Obtains a raw pointer suitable for use with FFI functions
|
|
|
|
#[inline]
|
2022-11-07 01:09:57 +00:00
|
|
|
#[deprecated(since = "0.25.0", note = "Use Self::as_c_ptr if you need to access the FFI layer")]
|
2022-11-16 00:06:12 +00:00
|
|
|
pub fn as_ptr(&self) -> *const ffi::Signature { self.as_c_ptr() }
|
2021-09-09 09:25:06 +00:00
|
|
|
|
|
|
|
/// Obtains a raw mutable pointer suitable for use with FFI functions
|
|
|
|
#[inline]
|
2022-11-16 00:06:12 +00:00
|
|
|
#[deprecated(
|
|
|
|
since = "0.25.0",
|
|
|
|
note = "Use Self::as_mut_c_ptr if you need to access the FFI layer"
|
|
|
|
)]
|
|
|
|
pub fn as_mut_ptr(&mut self) -> *mut ffi::Signature { self.as_mut_c_ptr() }
|
2021-09-09 09:25:06 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Serializes the signature in DER format
|
|
|
|
pub fn serialize_der(&self) -> SerializedSignature {
|
2022-06-21 22:29:57 +00:00
|
|
|
let mut data = [0u8; serialized_signature::MAX_LEN];
|
|
|
|
let mut len: usize = serialized_signature::MAX_LEN;
|
2021-09-09 09:25:06 +00:00
|
|
|
unsafe {
|
|
|
|
let err = ffi::secp256k1_ecdsa_signature_serialize_der(
|
|
|
|
ffi::secp256k1_context_no_precomp,
|
2022-06-21 22:29:57 +00:00
|
|
|
data.as_mut_ptr(),
|
2021-09-09 09:25:06 +00:00
|
|
|
&mut len,
|
|
|
|
self.as_c_ptr(),
|
|
|
|
);
|
|
|
|
debug_assert!(err == 1);
|
2022-06-21 22:29:57 +00:00
|
|
|
SerializedSignature::from_raw_parts(data, len)
|
2021-09-09 09:25:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Serializes the signature in compact format
|
|
|
|
pub fn serialize_compact(&self) -> [u8; 64] {
|
|
|
|
let mut ret = [0u8; 64];
|
|
|
|
unsafe {
|
|
|
|
let err = ffi::secp256k1_ecdsa_signature_serialize_compact(
|
|
|
|
ffi::secp256k1_context_no_precomp,
|
|
|
|
ret.as_mut_c_ptr(),
|
|
|
|
self.as_c_ptr(),
|
|
|
|
);
|
|
|
|
debug_assert!(err == 1);
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
2022-02-08 06:11:05 +00:00
|
|
|
|
|
|
|
/// Verifies an ECDSA signature for `msg` using `pk` and the global [`SECP256K1`] context.
|
2023-03-29 19:14:58 +00:00
|
|
|
/// The signature must be normalized or verification will fail (see [`Signature::normalize_s`]).
|
2022-02-08 06:11:05 +00:00
|
|
|
#[inline]
|
|
|
|
#[cfg(feature = "global-context")]
|
|
|
|
pub fn verify(&self, msg: &Message, pk: &PublicKey) -> Result<(), Error> {
|
|
|
|
SECP256K1.verify_ecdsa(msg, self, pk)
|
|
|
|
}
|
2021-09-09 09:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CPtr for Signature {
|
|
|
|
type Target = ffi::Signature;
|
|
|
|
|
2022-11-16 00:06:12 +00:00
|
|
|
fn as_c_ptr(&self) -> *const Self::Target { &self.0 }
|
2021-09-09 09:25:06 +00:00
|
|
|
|
2022-11-16 00:06:12 +00:00
|
|
|
fn as_mut_c_ptr(&mut self) -> *mut Self::Target { &mut self.0 }
|
2021-09-09 09:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new signature from a FFI signature
|
|
|
|
impl From<ffi::Signature> for Signature {
|
|
|
|
#[inline]
|
2022-11-16 00:06:12 +00:00
|
|
|
fn from(sig: ffi::Signature) -> Signature { Signature(sig) }
|
2021-09-09 09:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
2021-09-14 09:18:22 +00:00
|
|
|
impl serde::Serialize for Signature {
|
|
|
|
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
2021-09-09 09:25:06 +00:00
|
|
|
if s.is_human_readable() {
|
|
|
|
s.collect_str(self)
|
|
|
|
} else {
|
|
|
|
s.serialize_bytes(&self.serialize_der())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde")]
|
2021-09-14 09:18:22 +00:00
|
|
|
impl<'de> serde::Deserialize<'de> for Signature {
|
|
|
|
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
2021-09-09 09:25:06 +00:00
|
|
|
if d.is_human_readable() {
|
2021-09-14 09:18:22 +00:00
|
|
|
d.deserialize_str(crate::serde_util::FromStrVisitor::new(
|
2022-11-16 00:06:12 +00:00
|
|
|
"a hex string representing a DER encoded Signature",
|
2021-09-09 09:25:06 +00:00
|
|
|
))
|
|
|
|
} else {
|
2021-09-14 09:18:22 +00:00
|
|
|
d.deserialize_bytes(crate::serde_util::BytesVisitor::new(
|
2021-09-09 09:25:06 +00:00
|
|
|
"raw byte stream, that represents a DER encoded Signature",
|
2022-11-16 00:06:12 +00:00
|
|
|
Signature::from_der,
|
2021-09-09 09:25:06 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-09 09:58:59 +00:00
|
|
|
|
|
|
|
impl<C: Signing> Secp256k1<C> {
|
2022-03-20 02:54:31 +00:00
|
|
|
fn sign_ecdsa_with_noncedata_pointer(
|
|
|
|
&self,
|
|
|
|
msg: &Message,
|
|
|
|
sk: &SecretKey,
|
2022-08-11 18:57:51 +00:00
|
|
|
noncedata: Option<&[u8; 32]>,
|
2022-03-20 02:54:31 +00:00
|
|
|
) -> Signature {
|
2021-09-09 09:58:59 +00:00
|
|
|
unsafe {
|
|
|
|
let mut ret = ffi::Signature::new();
|
2022-08-11 18:57:51 +00:00
|
|
|
let noncedata_ptr = match noncedata {
|
|
|
|
Some(arr) => arr.as_c_ptr() as *const _,
|
|
|
|
None => ptr::null(),
|
|
|
|
};
|
2021-09-09 09:58:59 +00:00
|
|
|
// We can assume the return value because it's not possible to construct
|
|
|
|
// an invalid signature from a valid `Message` and `SecretKey`
|
2022-11-16 00:06:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
ffi::secp256k1_ecdsa_sign(
|
2022-11-29 03:58:06 +00:00
|
|
|
self.ctx.as_ptr(),
|
2022-11-16 00:06:12 +00:00
|
|
|
&mut ret,
|
|
|
|
msg.as_c_ptr(),
|
|
|
|
sk.as_c_ptr(),
|
|
|
|
ffi::secp256k1_nonce_function_rfc6979,
|
|
|
|
noncedata_ptr
|
|
|
|
),
|
|
|
|
1
|
|
|
|
);
|
2021-09-09 09:58:59 +00:00
|
|
|
Signature::from(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 02:54:31 +00:00
|
|
|
/// Constructs a signature for `msg` using the secret key `sk` and RFC6979 nonce
|
|
|
|
/// Requires a signing-capable context.
|
|
|
|
pub fn sign_ecdsa(&self, msg: &Message, sk: &SecretKey) -> Signature {
|
2022-08-11 18:57:51 +00:00
|
|
|
self.sign_ecdsa_with_noncedata_pointer(msg, sk, None)
|
2022-03-20 02:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs a signature for `msg` using the secret key `sk` and RFC6979 nonce
|
|
|
|
/// and includes 32 bytes of noncedata in the nonce generation via inclusion in
|
|
|
|
/// one of the hash operations during nonce generation. This is useful when multiple
|
|
|
|
/// signatures are needed for the same Message and SecretKey while still using RFC6979.
|
|
|
|
/// Requires a signing-capable context.
|
|
|
|
pub fn sign_ecdsa_with_noncedata(
|
|
|
|
&self,
|
|
|
|
msg: &Message,
|
|
|
|
sk: &SecretKey,
|
|
|
|
noncedata: &[u8; 32],
|
|
|
|
) -> Signature {
|
2022-08-11 18:57:51 +00:00
|
|
|
self.sign_ecdsa_with_noncedata_pointer(msg, sk, Some(noncedata))
|
2022-03-20 02:54:31 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 09:58:59 +00:00
|
|
|
fn sign_grind_with_check(
|
2022-11-16 00:06:12 +00:00
|
|
|
&self,
|
|
|
|
msg: &Message,
|
2021-09-09 09:58:59 +00:00
|
|
|
sk: &SecretKey,
|
2022-11-16 00:06:12 +00:00
|
|
|
check: impl Fn(&ffi::Signature) -> bool,
|
|
|
|
) -> Signature {
|
|
|
|
let mut entropy_p: *const ffi::types::c_void = ptr::null();
|
|
|
|
let mut counter: u32 = 0;
|
|
|
|
let mut extra_entropy = [0u8; 32];
|
|
|
|
loop {
|
|
|
|
unsafe {
|
|
|
|
let mut ret = ffi::Signature::new();
|
|
|
|
// We can assume the return value because it's not possible to construct
|
|
|
|
// an invalid signature from a valid `Message` and `SecretKey`
|
|
|
|
assert_eq!(
|
|
|
|
ffi::secp256k1_ecdsa_sign(
|
2022-11-29 03:58:06 +00:00
|
|
|
self.ctx.as_ptr(),
|
2022-11-16 00:06:12 +00:00
|
|
|
&mut ret,
|
|
|
|
msg.as_c_ptr(),
|
|
|
|
sk.as_c_ptr(),
|
|
|
|
ffi::secp256k1_nonce_function_rfc6979,
|
|
|
|
entropy_p
|
|
|
|
),
|
|
|
|
1
|
|
|
|
);
|
|
|
|
if check(&ret) {
|
2021-09-09 09:58:59 +00:00
|
|
|
return Signature::from(ret);
|
|
|
|
}
|
2022-11-16 00:06:12 +00:00
|
|
|
|
|
|
|
counter += 1;
|
|
|
|
extra_entropy[..4].copy_from_slice(&counter.to_le_bytes());
|
|
|
|
entropy_p = extra_entropy.as_c_ptr().cast::<ffi::types::c_void>();
|
|
|
|
|
|
|
|
// When fuzzing, these checks will usually spinloop forever, so just short-circuit them.
|
2023-04-26 15:16:26 +00:00
|
|
|
#[cfg(secp256k1_fuzz)]
|
2022-11-16 00:06:12 +00:00
|
|
|
return Signature::from(ret);
|
2021-09-09 09:58:59 +00:00
|
|
|
}
|
2022-11-16 00:06:12 +00:00
|
|
|
}
|
2021-09-09 09:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs a signature for `msg` using the secret key `sk`, RFC6979 nonce
|
|
|
|
/// and "grinds" the nonce by passing extra entropy if necessary to produce
|
2022-01-19 04:22:37 +00:00
|
|
|
/// a signature that is less than 71 - `bytes_to_grind` bytes. The number
|
2021-09-09 09:58:59 +00:00
|
|
|
/// of signing operation performed by this function is exponential in the
|
|
|
|
/// number of bytes grinded.
|
|
|
|
/// Requires a signing capable context.
|
2022-11-16 00:06:12 +00:00
|
|
|
pub fn sign_ecdsa_grind_r(
|
|
|
|
&self,
|
|
|
|
msg: &Message,
|
|
|
|
sk: &SecretKey,
|
|
|
|
bytes_to_grind: usize,
|
|
|
|
) -> Signature {
|
|
|
|
let len_check = |s: &ffi::Signature| der_length_check(s, 71 - bytes_to_grind);
|
2022-01-06 21:58:25 +00:00
|
|
|
self.sign_grind_with_check(msg, sk, len_check)
|
2021-09-09 09:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs a signature for `msg` using the secret key `sk`, RFC6979 nonce
|
|
|
|
/// and "grinds" the nonce by passing extra entropy if necessary to produce
|
|
|
|
/// a signature that is less than 71 bytes and compatible with the low r
|
|
|
|
/// signature implementation of bitcoin core. In average, this function
|
|
|
|
/// will perform two signing operations.
|
|
|
|
/// Requires a signing capable context.
|
|
|
|
pub fn sign_ecdsa_low_r(&self, msg: &Message, sk: &SecretKey) -> Signature {
|
2022-01-06 21:58:25 +00:00
|
|
|
self.sign_grind_with_check(msg, sk, compact_sig_has_zero_first_bit)
|
2021-09-09 09:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C: Verification> Secp256k1<C> {
|
|
|
|
/// Checks that `sig` is a valid ECDSA signature for `msg` using the public
|
|
|
|
/// key `pubkey`. Returns `Ok(())` on success. Note that this function cannot
|
|
|
|
/// be used for Bitcoin consensus checking since there may exist signatures
|
|
|
|
/// which OpenSSL would verify but not libsecp256k1, or vice-versa. Requires a
|
|
|
|
/// verify-capable context.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2022-11-17 23:00:23 +00:00
|
|
|
/// # #[cfg(feature = "rand-std")] {
|
|
|
|
/// # use secp256k1::{rand, Secp256k1, Message, Error};
|
2021-09-09 09:58:59 +00:00
|
|
|
/// #
|
|
|
|
/// # let secp = Secp256k1::new();
|
2022-11-17 23:00:23 +00:00
|
|
|
/// # let (secret_key, public_key) = secp.generate_keypair(&mut rand::thread_rng());
|
2021-09-09 09:58:59 +00:00
|
|
|
/// #
|
2023-08-09 22:03:10 +00:00
|
|
|
/// let message = Message::from_digest_slice(&[0xab; 32]).expect("32 bytes");
|
2021-09-09 09:58:59 +00:00
|
|
|
/// let sig = secp.sign_ecdsa(&message, &secret_key);
|
|
|
|
/// assert_eq!(secp.verify_ecdsa(&message, &sig, &public_key), Ok(()));
|
|
|
|
///
|
2023-08-09 22:03:10 +00:00
|
|
|
/// let message = Message::from_digest_slice(&[0xcd; 32]).expect("32 bytes");
|
2021-09-09 09:58:59 +00:00
|
|
|
/// assert_eq!(secp.verify_ecdsa(&message, &sig, &public_key), Err(Error::IncorrectSignature));
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2022-11-16 00:06:12 +00:00
|
|
|
pub fn verify_ecdsa(
|
|
|
|
&self,
|
|
|
|
msg: &Message,
|
|
|
|
sig: &Signature,
|
|
|
|
pk: &PublicKey,
|
|
|
|
) -> Result<(), Error> {
|
2021-09-09 09:58:59 +00:00
|
|
|
unsafe {
|
2022-11-29 03:58:06 +00:00
|
|
|
if ffi::secp256k1_ecdsa_verify(
|
|
|
|
self.ctx.as_ptr(),
|
|
|
|
sig.as_c_ptr(),
|
|
|
|
msg.as_c_ptr(),
|
|
|
|
pk.as_c_ptr(),
|
|
|
|
) == 0
|
2022-11-16 00:06:12 +00:00
|
|
|
{
|
2021-09-09 09:58:59 +00:00
|
|
|
Err(Error::IncorrectSignature)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn compact_sig_has_zero_first_bit(sig: &ffi::Signature) -> bool {
|
|
|
|
let mut compact = [0u8; 64];
|
|
|
|
unsafe {
|
|
|
|
let err = ffi::secp256k1_ecdsa_signature_serialize_compact(
|
|
|
|
ffi::secp256k1_context_no_precomp,
|
|
|
|
compact.as_mut_c_ptr(),
|
|
|
|
sig,
|
|
|
|
);
|
|
|
|
debug_assert!(err == 1);
|
|
|
|
}
|
|
|
|
compact[0] < 0x80
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn der_length_check(sig: &ffi::Signature, max_len: usize) -> bool {
|
|
|
|
let mut ser_ret = [0u8; 72];
|
|
|
|
let mut len: usize = ser_ret.len();
|
|
|
|
unsafe {
|
|
|
|
let err = ffi::secp256k1_ecdsa_signature_serialize_der(
|
|
|
|
ffi::secp256k1_context_no_precomp,
|
|
|
|
ser_ret.as_mut_c_ptr(),
|
|
|
|
&mut len,
|
|
|
|
sig,
|
|
|
|
);
|
|
|
|
debug_assert!(err == 1);
|
|
|
|
}
|
|
|
|
len <= max_len
|
|
|
|
}
|