Skip to content

Commit

Permalink
Expose test_for_all_curves
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Varlakov committed Dec 10, 2020
1 parent b2d25d4 commit ce9589d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ cache: cargo
rust:
- stable

env:
global:
- FEATURES=tesitng-utils

before_script:
- rustup component add rustfmt-preview
- cargo fmt --all -- --check

script:
- cargo build --verbose
- cargo test --verbose
- cargo build --features $FEATURES --verbose
- cargo test --features $FEATURES --verbose
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ repository = "https:/ZenGo-X/curv"
[lib]
crate-type = ["lib"]

[features]
default = []
# Exposes utils useful for testing
testing-utils = []

[dependencies]
rand = "0.6"
serde = "1.0"
Expand Down
69 changes: 68 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,74 @@ pub enum ErrorSS {
VerifyShareError,
}

#[cfg(test)]
/// Helps write tests generic over choice of elliptic curve
///
/// As input it expects name of a function which takes one generic parameter:
/// curve implementation. Function restricted to have no arguments and return `()`.
/// Macro outputs several tests each one runs given function with specific curve implementation.
///
/// ## Example
/// Suppose you have following generic test:
/// ```rust
/// # use curv::elliptic::curves::traits::*;
/// # use curv::test_for_all_curves;
/// test_for_all_curves!(test_dh);
/// fn test_dh<P: ECPoint>()
/// where P: ECPoint + Clone,
/// P::Scalar: Clone,
/// {
/// let party_a_secret: P::Scalar = ECScalar::new_random();
/// let party_a_public = P::generator() * party_a_secret.clone();
///
/// let party_b_secret: P::Scalar = ECScalar::new_random();
/// let party_b_public = P::generator() * party_b_secret.clone();
///
/// let party_a_share = party_b_public * party_a_secret;
/// let party_b_share = party_a_public * party_b_secret;
///
/// assert!(party_a_share == party_b_share, "Parties share expected to be the same")
/// }
/// # test_dh::<curv::elliptic::curves::secp256_k1::GE>();
/// ```
///
/// Macro will generate this code for you:
/// ```rust
/// # use curv::elliptic::curves::traits::*;
/// # fn test_dh<P: ECPoint>() { /* see code snippet above */ }
/// #[test]
/// fn test_dh_secp256k1() {
/// test_dh::<curv::elliptic::curves::secp256_k1::GE>()
/// }
/// #[test]
/// fn test_dh_ristretto() {
/// test_dh::<curv::elliptic::curves::curve_ristretto::GE>()
/// }
/// #[test]
/// fn test_dh_ed25519() {
/// test_dh::<curv::elliptic::curves::ed25519::GE>()
/// }
/// #[test]
/// fn test_dh_bls12_381() {
/// test_dh::<curv::elliptic::curves::bls12_381::GE>()
/// }
/// #[test]
/// fn test_dh_p256() {
/// test_dh::<curv::elliptic::curves::p256::GE>()
/// }
/// ```
///
/// ## Attributes
/// You can also pass `#[should_panic]` attribute:
/// ```rust
/// # use curv::elliptic::curves::traits::*;
/// # use curv::test_for_all_curves;
/// test_for_all_curves!(#[should_panic] failure_test);
/// fn failure_test<P: ECPoint>() { /* ... */ }
/// ```
///
/// This will require every produced test to panic.
#[cfg(any(test, feature = "testing-utils"))]
#[cfg_attr(docsrs, doc(cfg(feature = "testing-utils")))]
#[macro_export]
macro_rules! test_for_all_curves {
(#[should_panic] $fn: ident) => {
Expand Down

0 comments on commit ce9589d

Please sign in to comment.