Skip to content

Commit

Permalink
[schnorr] Make easy to use constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
LLFourn committed Aug 9, 2024
1 parent b0303c1 commit 9dcb91c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
2 changes: 1 addition & 1 deletion schnorr_fun/src/adaptor/encrypted_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<sha2::Sha256>();
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(
Expand Down
2 changes: 1 addition & 1 deletion schnorr_fun/src/adaptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<sha2::Sha256>();
/// 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;
Expand Down
8 changes: 0 additions & 8 deletions schnorr_fun/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,3 @@ mod message;
pub use message::*;

mod libsecp_compat;

#[macro_export]
#[doc(hidden)]
macro_rules! test_instance {
() => {
$crate::Schnorr::<sha2::Sha256, secp256kfun::nonce::Deterministic<sha2::Sha256>>::default()
};
}
56 changes: 43 additions & 13 deletions schnorr_fun/src/schnorr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -120,7 +120,7 @@ where
/// # Message,
/// # fun::{marker::*, Scalar},
/// # };
/// # let schnorr = schnorr_fun::test_instance!();
/// let schnorr = schnorr_fun::new_with_deterministic_nonces::<sha2::Sha256>();
/// let keypair = schnorr.new_keypair(Scalar::random(&mut rand::thread_rng()));
/// let message = Message::<Public>::plain(
/// "times-of-london",
Expand Down Expand Up @@ -156,7 +156,7 @@ impl<NG, CH: Hash32> Schnorr<CH, NG> {
///
/// [`KeyPair<EvenY>`]: crate::fun::KeyPair
pub fn new_keypair(&self, sk: Scalar) -> KeyPair<EvenY> {
KeyPair::<EvenY>::new(sk)
KeyPair::new_xonly(sk)
}

/// Produces the Fiat-Shamir challenge for a Schnorr signature in the form specified by [BIP-340].
Expand All @@ -169,11 +169,8 @@ impl<NG, CH: Hash32> Schnorr<CH, NG> {
/// 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::<sha2::Sha256>();
/// let message = Message::<Public>::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());
Expand Down Expand Up @@ -252,6 +249,39 @@ impl<NG, CH: Hash32> Schnorr<CH, NG> {
}
}

/// 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::<sha2::Sha256>();
/// ```
pub fn new_with_deterministic_nonces<H>() -> Schnorr<H, nonce::Deterministic<H>>
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::<sha2::Sha256, rand::rngs::ThreadRng>();
/// ```
pub fn new_with_synthetic_nonces<H, R>() -> Schnorr<H, nonce::Synthetic<H, nonce::GlobalRng<R>>>
where
H: Hash32,
R: rand_core::RngCore + Default + Clone,
{
Schnorr::default()
}

#[cfg(test)]
pub mod test {
use crate::fun::nonce::Deterministic;
Expand Down Expand Up @@ -294,7 +324,7 @@ pub mod test {

#[test]
fn anticipated_signature_on_should_correspond_to_actual_signature(sk in any::<Scalar>()) {
let schnorr = crate::test_instance!();
let schnorr = crate::new_with_deterministic_nonces::<sha2::Sha256>();
let keypair = schnorr.new_keypair(sk);
let msg = Message::<Public>::plain(
"test",
Expand All @@ -316,7 +346,7 @@ pub mod test {

#[test]
fn sign_deterministic(s1 in any::<Scalar>(), s2 in any::<Scalar>()) {
let schnorr = crate::test_instance!();
let schnorr = crate::new_with_deterministic_nonces::<sha2::Sha256>();
let keypair_1 = schnorr.new_keypair(s1);
let keypair_2 = schnorr.new_keypair(s2);
let msg_atkdwn = Message::<Public>::plain("test", b"attack at dawn");
Expand Down
2 changes: 1 addition & 1 deletion schnorr_fun/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<sha2::Sha256>();
let kp = schnorr.new_keypair(Scalar::random(&mut rand::thread_rng()));
let signature = schnorr.sign(&kp, Message::<Public>::plain("test", b"foo"));
let serialized = bincode::serialize(&signature).unwrap();
Expand Down

0 comments on commit 9dcb91c

Please sign in to comment.