2019-10-28 20:09:59 +00:00
|
|
|
extern crate bitcoin_hashes;
|
|
|
|
extern crate secp256k1;
|
|
|
|
|
|
|
|
use bitcoin_hashes::{sha256, Hash};
|
2022-11-16 00:06:12 +00:00
|
|
|
use secp256k1::{ecdsa, Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification};
|
|
|
|
|
|
|
|
fn verify<C: Verification>(
|
|
|
|
secp: &Secp256k1<C>,
|
|
|
|
msg: &[u8],
|
|
|
|
sig: [u8; 64],
|
|
|
|
pubkey: [u8; 33],
|
|
|
|
) -> Result<bool, Error> {
|
2019-10-28 20:09:59 +00:00
|
|
|
let msg = sha256::Hash::hash(msg);
|
2023-02-01 13:31:53 +00:00
|
|
|
let msg = Message::from_slice(msg.as_ref())?;
|
2021-09-09 09:25:06 +00:00
|
|
|
let sig = ecdsa::Signature::from_compact(&sig)?;
|
2019-10-28 20:09:59 +00:00
|
|
|
let pubkey = PublicKey::from_slice(&pubkey)?;
|
|
|
|
|
2021-09-09 09:25:06 +00:00
|
|
|
Ok(secp.verify_ecdsa(&msg, &sig, &pubkey).is_ok())
|
2019-10-28 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 00:06:12 +00:00
|
|
|
fn sign<C: Signing>(
|
|
|
|
secp: &Secp256k1<C>,
|
|
|
|
msg: &[u8],
|
|
|
|
seckey: [u8; 32],
|
|
|
|
) -> Result<ecdsa::Signature, Error> {
|
2019-10-28 20:09:59 +00:00
|
|
|
let msg = sha256::Hash::hash(msg);
|
2023-02-01 13:31:53 +00:00
|
|
|
let msg = Message::from_slice(msg.as_ref())?;
|
2019-10-28 20:09:59 +00:00
|
|
|
let seckey = SecretKey::from_slice(&seckey)?;
|
2021-09-09 09:25:06 +00:00
|
|
|
Ok(secp.sign_ecdsa(&msg, &seckey))
|
2019-10-28 20:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let secp = Secp256k1::new();
|
|
|
|
|
2022-11-16 00:06:12 +00:00
|
|
|
let seckey = [
|
|
|
|
59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107, 94, 203, 174, 253,
|
|
|
|
102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28,
|
|
|
|
];
|
|
|
|
let pubkey = [
|
|
|
|
2, 29, 21, 35, 7, 198, 183, 43, 14, 208, 65, 139, 14, 112, 205, 128, 231, 245, 41, 91, 141,
|
|
|
|
134, 245, 114, 45, 63, 82, 19, 251, 210, 57, 79, 54,
|
|
|
|
];
|
2019-10-28 20:09:59 +00:00
|
|
|
let msg = b"This is some message";
|
|
|
|
|
|
|
|
let signature = sign(&secp, msg, seckey).unwrap();
|
|
|
|
|
|
|
|
let serialize_sig = signature.serialize_compact();
|
|
|
|
|
|
|
|
assert!(verify(&secp, msg, serialize_sig, pubkey).unwrap());
|
|
|
|
}
|