Skip to content

Commit

Permalink
Merge rust-bitcoin#607: Add serialize function for schnorr::Signature
Browse files Browse the repository at this point in the history
8af2cf1 add .serialize() function to schnorr signature (isaac-asdf)

Pull request description:

  convert from Signature to a byte_array

ACKs for top commit:
  Kixunil:
    ACK 8af2cf1
  tcharding:
    ACK 8af2cf1
  apoelstra:
    ACK 8af2cf1

Tree-SHA512: b69d58646cdba4d83a79189f18628590970f471771feef0e11e089d73bd934777e3554a448b88a3643203522fde98084fd7570a5cec400516166583a3433c000
  • Loading branch information
apoelstra committed May 10, 2023
2 parents ecdec75 + 8af2cf1 commit 5817d32
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/schnorr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ impl Signature {
}
}

/// Returns a signature as a byte array.
#[inline]
pub fn serialize(&self) -> [u8; constants::SCHNORR_SIGNATURE_SIZE] { self.0 }

/// Verifies a schnorr signature for `msg` using `pk` and the global [`SECP256K1`] context.
#[inline]
#[cfg(feature = "global-context")]
Expand Down Expand Up @@ -291,6 +295,19 @@ mod tests {
assert!(secp.verify_schnorr(&sig, &msg, &pubkey).is_ok());
}

#[test]
fn test_serialize() {
let sig = Signature::from_str("6470FD1303DDA4FDA717B9837153C24A6EAB377183FC438F939E0ED2B620E9EE5077C4A8B8DCA28963D772A94F5F0DDF598E1C47C137F91933274C7C3EDADCE8").unwrap();
let sig_bytes = sig.serialize();
let bytes = [
100, 112, 253, 19, 3, 221, 164, 253, 167, 23, 185, 131, 113, 83, 194, 74, 110, 171, 55,
113, 131, 252, 67, 143, 147, 158, 14, 210, 182, 32, 233, 238, 80, 119, 196, 168, 184,
220, 162, 137, 99, 215, 114, 169, 79, 95, 13, 223, 89, 142, 28, 71, 193, 55, 249, 25,
51, 39, 76, 124, 62, 218, 220, 232,
];
assert_eq!(sig_bytes, bytes);
}

#[test]
fn test_pubkey_from_slice() {
assert_eq!(XOnlyPublicKey::from_slice(&[]), Err(InvalidPublicKey));
Expand Down

0 comments on commit 5817d32

Please sign in to comment.