From 9dcb91c5cc77523a513e1177bb7442d9af4e9ef8 Mon Sep 17 00:00:00 2001 From: LLFourn Date: Fri, 9 Aug 2024 10:58:41 +1000 Subject: [PATCH] [schnorr] Make easy to use constructors --- .../src/adaptor/encrypted_signature.rs | 2 +- schnorr_fun/src/adaptor/mod.rs | 2 +- schnorr_fun/src/lib.rs | 8 --- schnorr_fun/src/schnorr.rs | 56 ++++++++++++++----- schnorr_fun/src/signature.rs | 2 +- 5 files changed, 46 insertions(+), 24 deletions(-) diff --git a/schnorr_fun/src/adaptor/encrypted_signature.rs b/schnorr_fun/src/adaptor/encrypted_signature.rs index 89b6c0dc..46e03b6d 100644 --- a/schnorr_fun/src/adaptor/encrypted_signature.rs +++ b/schnorr_fun/src/adaptor/encrypted_signature.rs @@ -48,7 +48,7 @@ mod test { fn encrypted_signature_serialization_roundtrip() { use super::*; use crate::{adaptor::*, fun::Scalar, Message}; - let schnorr = crate::test_instance!(); + let schnorr = crate::new_with_deterministic_nonces::(); let kp = schnorr.new_keypair(Scalar::random(&mut rand::thread_rng())); let encryption_key = Point::random(&mut rand::thread_rng()); let encrypted_signature = schnorr.encrypted_sign( diff --git a/schnorr_fun/src/adaptor/mod.rs b/schnorr_fun/src/adaptor/mod.rs index a4504cb5..560ac281 100644 --- a/schnorr_fun/src/adaptor/mod.rs +++ b/schnorr_fun/src/adaptor/mod.rs @@ -123,7 +123,7 @@ pub trait Adaptor { /// # Example /// ``` /// # use schnorr_fun::{adaptor::Adaptor, fun::Scalar, Schnorr}; - /// # let schnorr = schnorr_fun::test_instance!(); + /// let schnorr = schnorr_fun::new_with_deterministic_nonces::(); /// let decryption_key = Scalar::random(&mut rand::thread_rng()); /// let encryption_key = schnorr.encryption_key_for(&decryption_key); fn encryption_key_for(&self, decryption_key: &Scalar) -> Point; diff --git a/schnorr_fun/src/lib.rs b/schnorr_fun/src/lib.rs index cfd4bd4b..9f66137c 100755 --- a/schnorr_fun/src/lib.rs +++ b/schnorr_fun/src/lib.rs @@ -35,11 +35,3 @@ mod message; pub use message::*; mod libsecp_compat; - -#[macro_export] -#[doc(hidden)] -macro_rules! test_instance { - () => { - $crate::Schnorr::>::default() - }; -} diff --git a/schnorr_fun/src/schnorr.rs b/schnorr_fun/src/schnorr.rs index c1f44590..429521ba 100644 --- a/schnorr_fun/src/schnorr.rs +++ b/schnorr_fun/src/schnorr.rs @@ -2,11 +2,11 @@ use secp256kfun::{hash::Hash32, nonce::NoNonces}; use crate::{ fun::{ - derive_nonce, g, + derive_nonce, hash::{HashAdd, Tag}, - marker::*, - nonce::NonceGen, - s, KeyPair, Point, Scalar, G, + nonce::{self, NonceGen}, + prelude::*, + rand_core, KeyPair, }, Message, Signature, }; @@ -120,7 +120,7 @@ where /// # Message, /// # fun::{marker::*, Scalar}, /// # }; - /// # let schnorr = schnorr_fun::test_instance!(); + /// let schnorr = schnorr_fun::new_with_deterministic_nonces::(); /// let keypair = schnorr.new_keypair(Scalar::random(&mut rand::thread_rng())); /// let message = Message::::plain( /// "times-of-london", @@ -156,7 +156,7 @@ impl Schnorr { /// /// [`KeyPair`]: crate::fun::KeyPair pub fn new_keypair(&self, sk: Scalar) -> KeyPair { - KeyPair::::new(sk) + KeyPair::new_xonly(sk) } /// Produces the Fiat-Shamir challenge for a Schnorr signature in the form specified by [BIP-340]. @@ -169,11 +169,8 @@ impl Schnorr { /// Here's how you could use this to roll your own signatures. /// /// ``` - /// use schnorr_fun::{ - /// fun::{marker::*, s, Point, Scalar, G}, - /// Message, Schnorr, Signature, - /// }; - /// # let schnorr = schnorr_fun::test_instance!(); + /// use schnorr_fun::{fun::prelude::*, Message, Schnorr, Signature}; + /// let schnorr = schnorr_fun::new_with_deterministic_nonces::(); /// let message = Message::::plain("my-app", b"we rolled our own schnorr!"); /// let keypair = schnorr.new_keypair(Scalar::random(&mut rand::thread_rng())); /// let mut r = Scalar::random(&mut rand::thread_rng()); @@ -252,6 +249,39 @@ impl Schnorr { } } +/// Create a new [`Schnorr`] instance with deterministic nonce generation from a given hash as a type +/// paramater. +/// +/// This exists to avoid having to write out the right type parameters +/// +/// # Example +/// +/// ``` +/// let schnorr = schnorr_fun::new_with_deterministic_nonces::(); +/// ``` +pub fn new_with_deterministic_nonces() -> Schnorr> +where + H: Hash32, +{ + Schnorr::default() +} + +/// Create a new [`Schnorr`] instance with synthetic nonce generation from a given hash and rng as a +/// type parameter. +/// +/// # Example +/// +/// ``` +/// let schnorr = schnorr_fun::new_with_synthetic_nonces::(); +/// ``` +pub fn new_with_synthetic_nonces() -> Schnorr>> +where + H: Hash32, + R: rand_core::RngCore + Default + Clone, +{ + Schnorr::default() +} + #[cfg(test)] pub mod test { use crate::fun::nonce::Deterministic; @@ -294,7 +324,7 @@ pub mod test { #[test] fn anticipated_signature_on_should_correspond_to_actual_signature(sk in any::()) { - let schnorr = crate::test_instance!(); + let schnorr = crate::new_with_deterministic_nonces::(); let keypair = schnorr.new_keypair(sk); let msg = Message::::plain( "test", @@ -316,7 +346,7 @@ pub mod test { #[test] fn sign_deterministic(s1 in any::(), s2 in any::()) { - let schnorr = crate::test_instance!(); + let schnorr = crate::new_with_deterministic_nonces::(); let keypair_1 = schnorr.new_keypair(s1); let keypair_2 = schnorr.new_keypair(s2); let msg_atkdwn = Message::::plain("test", b"attack at dawn"); diff --git a/schnorr_fun/src/signature.rs b/schnorr_fun/src/signature.rs index fc519d78..2a3872bc 100644 --- a/schnorr_fun/src/signature.rs +++ b/schnorr_fun/src/signature.rs @@ -130,7 +130,7 @@ mod test { fn signature_serialization_roundtrip() { use super::*; use crate::{fun::Scalar, Message}; - let schnorr = crate::test_instance!(); + let schnorr = crate::new_with_deterministic_nonces::(); let kp = schnorr.new_keypair(Scalar::random(&mut rand::thread_rng())); let signature = schnorr.sign(&kp, Message::::plain("test", b"foo")); let serialized = bincode::serialize(&signature).unwrap();