2022-06-29 04:05:31 +00:00
|
|
|
// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
|
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2021-04-12 11:24:25 +00:00
|
|
|
|
2021-11-05 21:58:18 +00:00
|
|
|
//! Schnorr Bitcoin keys.
|
2021-04-12 11:24:25 +00:00
|
|
|
//!
|
2021-11-05 21:58:18 +00:00
|
|
|
//! This module provides Schnorr keys used in Bitcoin, reexporting Secp256k1
|
|
|
|
//! Schnorr key types.
|
2021-04-12 11:24:25 +00:00
|
|
|
//!
|
|
|
|
|
2021-11-16 00:00:12 +00:00
|
|
|
use core::fmt;
|
2022-06-24 02:01:53 +00:00
|
|
|
|
2022-09-05 02:19:28 +00:00
|
|
|
use bitcoin_internals::write_err;
|
|
|
|
|
2023-01-23 02:57:30 +00:00
|
|
|
pub use secp256k1::{self, constants, Secp256k1, KeyPair, XOnlyPublicKey, Verification, Parity};
|
2022-09-15 02:14:52 +00:00
|
|
|
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::prelude::*;
|
2021-11-16 00:00:12 +00:00
|
|
|
|
2022-09-15 02:59:36 +00:00
|
|
|
use crate::sighash::SchnorrSighashType;
|
2021-11-07 06:11:43 +00:00
|
|
|
|
2021-11-16 00:00:12 +00:00
|
|
|
/// A BIP340-341 serialized schnorr signature with the corresponding hash type.
|
2022-05-06 03:37:24 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2021-10-28 07:43:02 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
2022-05-25 06:41:59 +00:00
|
|
|
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
|
2022-11-08 00:36:52 +00:00
|
|
|
pub struct Signature {
|
2021-11-16 00:00:12 +00:00
|
|
|
/// The underlying schnorr signature
|
2022-01-03 02:52:44 +00:00
|
|
|
pub sig: secp256k1::schnorr::Signature,
|
2021-11-16 00:00:12 +00:00
|
|
|
/// The corresponding hash type
|
2022-03-28 21:52:33 +00:00
|
|
|
pub hash_ty: SchnorrSighashType,
|
2021-11-16 00:00:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 00:36:52 +00:00
|
|
|
impl Signature {
|
2021-11-16 00:00:12 +00:00
|
|
|
/// Deserialize from slice
|
2022-11-08 00:36:52 +00:00
|
|
|
pub fn from_slice(sl: &[u8]) -> Result<Self, Error> {
|
2021-11-16 00:00:12 +00:00
|
|
|
match sl.len() {
|
|
|
|
64 => {
|
|
|
|
// default type
|
2022-01-03 02:52:44 +00:00
|
|
|
let sig = secp256k1::schnorr::Signature::from_slice(sl)
|
2022-11-08 00:36:52 +00:00
|
|
|
.map_err(Error::Secp256k1)?;
|
|
|
|
Ok(Signature { sig, hash_ty: SchnorrSighashType::Default })
|
2021-11-16 00:00:12 +00:00
|
|
|
},
|
|
|
|
65 => {
|
|
|
|
let (hash_ty, sig) = sl.split_last().expect("Slice len checked == 65");
|
2022-06-24 02:01:53 +00:00
|
|
|
let hash_ty = SchnorrSighashType::from_consensus_u8(*hash_ty)
|
2022-11-08 00:36:52 +00:00
|
|
|
.map_err(|_| Error::InvalidSighashType(*hash_ty))?;
|
2022-01-03 02:52:44 +00:00
|
|
|
let sig = secp256k1::schnorr::Signature::from_slice(sig)
|
2022-11-08 00:36:52 +00:00
|
|
|
.map_err(Error::Secp256k1)?;
|
|
|
|
Ok(Signature { sig, hash_ty })
|
2021-11-16 00:00:12 +00:00
|
|
|
}
|
|
|
|
len => {
|
2022-11-08 00:36:52 +00:00
|
|
|
Err(Error::InvalidSignatureSize(len))
|
2021-11-16 00:00:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 00:36:52 +00:00
|
|
|
/// Serialize Signature
|
2022-08-01 22:33:22 +00:00
|
|
|
pub fn to_vec(self) -> Vec<u8> {
|
2021-11-16 00:00:12 +00:00
|
|
|
// TODO: add support to serialize to a writer to SerializedSig
|
|
|
|
let mut ser_sig = self.sig.as_ref().to_vec();
|
2022-03-28 21:52:33 +00:00
|
|
|
if self.hash_ty == SchnorrSighashType::Default {
|
2021-11-16 00:00:12 +00:00
|
|
|
// default sighash type, don't add extra sighash byte
|
|
|
|
} else {
|
|
|
|
ser_sig.push(self.hash_ty as u8);
|
|
|
|
}
|
|
|
|
ser_sig
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A schnorr sig related error.
|
|
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
2022-05-31 04:29:50 +00:00
|
|
|
#[non_exhaustive]
|
2022-11-08 00:36:52 +00:00
|
|
|
pub enum Error {
|
2021-11-16 00:00:12 +00:00
|
|
|
/// Base58 encoding error
|
|
|
|
InvalidSighashType(u8),
|
|
|
|
/// Signature has valid size but does not parse correctly
|
|
|
|
Secp256k1(secp256k1::Error),
|
|
|
|
/// Invalid schnorr signature size
|
2022-11-08 00:36:52 +00:00
|
|
|
InvalidSignatureSize(usize),
|
2021-11-16 00:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-08 00:36:52 +00:00
|
|
|
impl fmt::Display for Error {
|
2021-11-16 00:00:12 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2022-11-08 00:36:52 +00:00
|
|
|
Error::InvalidSighashType(hash_ty) =>
|
2021-11-16 00:00:12 +00:00
|
|
|
write!(f, "Invalid signature hash type {}", hash_ty),
|
2022-11-08 00:36:52 +00:00
|
|
|
Error::Secp256k1(ref e) =>
|
2022-05-25 02:56:51 +00:00
|
|
|
write_err!(f, "Schnorr signature has correct len but is malformed"; e),
|
2022-11-08 00:36:52 +00:00
|
|
|
Error::InvalidSignatureSize(sz) =>
|
2021-11-16 00:00:12 +00:00
|
|
|
write!(f, "Invalid Schnorr signature size: {}", sz),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
2022-11-08 00:36:52 +00:00
|
|
|
impl std::error::Error for Error {
|
2022-05-04 05:56:24 +00:00
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
2022-11-08 00:36:52 +00:00
|
|
|
use self::Error::*;
|
2022-05-04 05:56:24 +00:00
|
|
|
|
|
|
|
match self {
|
|
|
|
Secp256k1(e) => Some(e),
|
2022-11-08 00:36:52 +00:00
|
|
|
InvalidSighashType(_) | InvalidSignatureSize(_) => None,
|
2022-05-04 05:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 00:00:12 +00:00
|
|
|
|
2022-11-08 00:36:52 +00:00
|
|
|
impl From<secp256k1::Error> for Error {
|
2021-11-16 00:00:12 +00:00
|
|
|
|
2022-11-08 00:36:52 +00:00
|
|
|
fn from(e: secp256k1::Error) -> Error {
|
|
|
|
Error::Secp256k1(e)
|
2021-11-16 00:00:12 +00:00
|
|
|
}
|
|
|
|
}
|