Passes references to the public and secret keys to sign/verify

sign/verify should not take ownership of the keys.
This commit is contained in:
Sergi Delgado Segura 2021-06-29 15:26:21 +02:00
parent ecddfe1766
commit c1d2d156c9
No known key found for this signature in database
GPG key ID: 633B3A2298D70DD8

View file

@ -48,11 +48,11 @@ fn sigrec_decode(sig_rec: Vec<u8>) -> Result<RecoverableSignature, Error> {
/// Creates a digital signature of a message given a SecretKey, like the node's secret. /// Creates a digital signature of a message given a SecretKey, like the node's secret.
/// A receiver knowing the PublicKey (e.g. the node's id) and the message can be sure that the signature was generated by the caller. /// A receiver knowing the PublicKey (e.g. the node's id) and the message can be sure that the signature was generated by the caller.
/// Signatures are EC recoverable, meaning that given the message and the signature the PublicKey of the signer can be extracted. /// Signatures are EC recoverable, meaning that given the message and the signature the PublicKey of the signer can be extracted.
pub fn sign(msg: &[u8], sk: SecretKey) -> Result<String, Error> { pub fn sign(msg: &[u8], sk: &SecretKey) -> Result<String, Error> {
let secp_ctx = Secp256k1::signing_only(); let secp_ctx = Secp256k1::signing_only();
let msg_hash = sha256d::Hash::hash(&[LN_MESSAGE_PREFIX, msg].concat()); let msg_hash = sha256d::Hash::hash(&[LN_MESSAGE_PREFIX, msg].concat());
let sig = secp_ctx.sign_recoverable(&Message::from_slice(&msg_hash)?, &sk); let sig = secp_ctx.sign_recoverable(&Message::from_slice(&msg_hash)?, sk);
Ok(zbase32::encode(&sigrec_encode(sig))) Ok(zbase32::encode(&sigrec_encode(sig)))
} }
@ -74,9 +74,9 @@ pub fn recover_pk(msg: &[u8], sig: &str) -> Result<PublicKey, Error> {
/// Verifies a message was signed by a PrivateKey that derives to a given PublicKey, given a message, a signature, /// Verifies a message was signed by a PrivateKey that derives to a given PublicKey, given a message, a signature,
/// and the PublicKey. /// and the PublicKey.
pub fn verify(msg: &[u8], sig: &str, pk: PublicKey) -> bool { pub fn verify(msg: &[u8], sig: &str, pk: &PublicKey) -> bool {
match recover_pk(msg, sig) { match recover_pk(msg, sig) {
Ok(x) => x == pk, Ok(x) => x == *pk,
Err(_) => false Err(_) => false
} }
} }
@ -91,7 +91,7 @@ mod test {
#[test] #[test]
fn test_sign() { fn test_sign() {
let message = "test message"; let message = "test message";
let zbase32_sig = sign(message.as_bytes(), ONE_KEY); let zbase32_sig = sign(message.as_bytes(), &ONE_KEY);
assert_eq!(zbase32_sig.unwrap(), "d9tibmnic9t5y41hg7hkakdcra94akas9ku3rmmj4ag9mritc8ok4p5qzefs78c9pqfhpuftqqzhydbdwfg7u6w6wdxcqpqn4sj4e73e") assert_eq!(zbase32_sig.unwrap(), "d9tibmnic9t5y41hg7hkakdcra94akas9ku3rmmj4ag9mritc8ok4p5qzefs78c9pqfhpuftqqzhydbdwfg7u6w6wdxcqpqn4sj4e73e")
} }
@ -108,10 +108,10 @@ mod test {
#[test] #[test]
fn test_verify() { fn test_verify() {
let message = "another message"; let message = "another message";
let sig = sign(message.as_bytes(), ONE_KEY).unwrap(); let sig = sign(message.as_bytes(), &ONE_KEY).unwrap();
let pk = PublicKey::from_secret_key(&Secp256k1::signing_only(), &ONE_KEY); let pk = PublicKey::from_secret_key(&Secp256k1::signing_only(), &ONE_KEY);
assert!(verify(message.as_bytes(), &sig, pk)) assert!(verify(message.as_bytes(), &sig, &pk))
} }
#[test] #[test]
@ -135,7 +135,7 @@ mod test {
]; ];
for c in &corpus { for c in &corpus {
assert!(verify(c[1].as_bytes(), c[2], PublicKey::from_str(c[3]).unwrap())) assert!(verify(c[1].as_bytes(), c[2], &PublicKey::from_str(c[3]).unwrap()))
} }
} }
} }