improvement: event signature validation is 100x faster

Switched to latest (git) release of secp256k1, which has more
efficient verification-only context for Schnorr.  Switched to single
pre-instantiated instance of the verifier.
This commit is contained in:
Greg Heartsfield
2022-01-01 09:08:19 -06:00
parent 620e227699
commit 1aa5a5458d
4 changed files with 29 additions and 17 deletions
+15 -7
View File
@@ -3,14 +3,19 @@ use crate::config;
use crate::error::Error::*;
use crate::error::Result;
use bitcoin_hashes::{sha256, Hash};
use lazy_static::lazy_static;
use log::*;
use secp256k1::{schnorrsig, Secp256k1};
use secp256k1::{schnorr, Secp256k1, VerifyOnly, XOnlyPublicKey};
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::value::Value;
use serde_json::Number;
use std::str::FromStr;
use std::time::SystemTime;
lazy_static! {
pub static ref SECP: Secp256k1<VerifyOnly> = Secp256k1::verification_only();
}
/// Event command in network format
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct EventCmd {
@@ -109,12 +114,15 @@ impl Event {
return false;
}
// * validate the message digest (sig) using the pubkey & computed sha256 message hash.
let secp = Secp256k1::new();
let sig = schnorrsig::Signature::from_str(&self.sig).unwrap();
let message = secp256k1::Message::from(digest);
let pubkey = schnorrsig::PublicKey::from_str(&self.pubkey).unwrap();
let verify = secp.schnorrsig_verify(&sig, &message, &pubkey);
matches!(verify, Ok(()))
let sig = schnorr::Signature::from_str(&self.sig).unwrap();
if let Ok(msg) = secp256k1::Message::from_slice(digest.as_ref()) {
let pubkey = XOnlyPublicKey::from_str(&self.pubkey).unwrap();
let verify = SECP.verify_schnorr(&sig, &msg, &pubkey);
matches!(verify, Ok(()))
} else {
warn!("Error converting digest to secp256k1 message");
false
}
}
/// Convert event to canonical representation for signing.
+3 -3
View File
@@ -98,9 +98,9 @@ async fn handle_web_request(
}
("/", false) => {
// handle request at root with no upgrade header
Ok(Response::new(Body::from(format!(
"This is a Nostr relay.\n"
))))
Ok(Response::new(Body::from(
"This is a Nostr relay.\n".to_string(),
)))
}
(_, _) => {
//handle any other url