Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Runtime: avoid duplication and test all signature (#14663)
Browse files Browse the repository at this point in the history
* runtime: all signature test

* test-utils: remove std duplication

* runtime: add bls verify test
  • Loading branch information
ashWhiteHat authored Aug 17, 2023
1 parent aa70241 commit ec6be6e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
43 changes: 36 additions & 7 deletions primitives/runtime/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2299,11 +2299,27 @@ pub trait BlockNumberProvider {
mod tests {
use super::*;
use crate::codec::{Decode, Encode, Input};
#[cfg(feature = "bls-experimental")]
use sp_core::{bls377, bls381};
use sp_core::{
crypto::{Pair, UncheckedFrom},
ecdsa,
ecdsa, ed25519, sr25519,
};

macro_rules! signature_verify_test {
($algorithm:ident) => {
let msg = &b"test-message"[..];
let wrong_msg = &b"test-msg"[..];
let (pair, _) = $algorithm::Pair::generate();

let signature = pair.sign(&msg);
assert!($algorithm::Pair::verify(&signature, msg, &pair.public()));

assert!(signature.verify(msg, &pair.public()));
assert!(!signature.verify(wrong_msg, &pair.public()));
};
}

mod t {
use sp_application_crypto::{app_crypto, sr25519};
use sp_core::crypto::KeyTypeId;
Expand Down Expand Up @@ -2413,15 +2429,28 @@ mod tests {
assert_eq!(buffer, [0, 0]);
}

#[test]
fn ed25519_verify_works() {
signature_verify_test!(ed25519);
}

#[test]
fn sr25519_verify_works() {
signature_verify_test!(sr25519);
}

#[test]
fn ecdsa_verify_works() {
let msg = &b"test-message"[..];
let (pair, _) = ecdsa::Pair::generate();
signature_verify_test!(ecdsa);
}

let signature = pair.sign(&msg);
assert!(ecdsa::Pair::verify(&signature, msg, &pair.public()));
#[cfg(feature = "bls-experimental")]
fn bls377_verify_works() {
signature_verify_test!(bls377)
}

assert!(signature.verify(msg, &pair.public()));
assert!(signature.verify(msg, &pair.public()));
#[cfg(feature = "bls-experimental")]
fn bls381_verify_works() {
signature_verify_test!(bls381)
}
}
1 change: 0 additions & 1 deletion test-utils/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ std = [
"log/std",
"sp-offchain/std",
"sp-core/std",
"sp-core/std",
"sp-std/std",
"sp-io/std",
"frame-support/std",
Expand Down

0 comments on commit ec6be6e

Please sign in to comment.