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

Remove superfluous Pair::verify_weak #13972

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions primitives/application-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,6 @@ macro_rules! app_crypto_pair {
) -> bool {
<$pair>::verify(&sig.0, message, pubkey.as_ref())
}
fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(
sig: &[u8],
message: M,
pubkey: P,
) -> bool {
<$pair>::verify_weak(sig, message, pubkey)
}
fn public(&self) -> Self::Public {
Public(self.0.public())
}
Expand Down
15 changes: 0 additions & 15 deletions primitives/core/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,10 +716,6 @@ mod dummy {
true
}

fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(_: &[u8], _: M, _: P) -> bool {
true
}

fn public(&self) -> Self::Public {
Self
}
Expand Down Expand Up @@ -917,9 +913,6 @@ pub trait Pair: CryptoType + Sized + Clone + Send + Sync + 'static {
/// Verify a signature on a message. Returns true if the signature is good.
fn verify<M: AsRef<[u8]>>(sig: &Self::Signature, message: M, pubkey: &Self::Public) -> bool;

/// Verify a signature on a message. Returns true if the signature is good.
fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(sig: &[u8], message: M, pubkey: P) -> bool;

/// Get the public key.
fn public(&self) -> Self::Public;

Expand Down Expand Up @@ -1272,14 +1265,6 @@ mod tests {
true
}

fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(
_sig: &[u8],
_message: M,
_pubkey: P,
) -> bool {
true
}

fn public(&self) -> Self::Public {
TestPublic
}
Expand Down
18 changes: 2 additions & 16 deletions primitives/core/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,22 +407,8 @@ impl TraitPair for Pair {
}

/// Verify a signature on a message. Returns true if the signature is good.
fn verify<M: AsRef<[u8]>>(sig: &Self::Signature, message: M, pubkey: &Self::Public) -> bool {
match sig.recover(message) {
Some(actual) => actual == *pubkey,
None => false,
}
}

/// Verify a signature on a message. Returns true if the signature is good.
///
/// This doesn't use the type system to ensure that `sig` and `pubkey` are the correct
/// size. Use it only if you're coming from byte buffers and need the speed.
fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(sig: &[u8], message: M, pubkey: P) -> bool {
match Signature::from_slice(sig).and_then(|sig| sig.recover(message)) {
Some(actual) => actual.as_ref() == pubkey.as_ref(),
None => false,
}
fn verify<M: AsRef<[u8]>>(sig: &Self::Signature, message: M, public: &Self::Public) -> bool {
sig.recover(message).map(|actual| actual == *public).unwrap_or_default()
}

/// Return a vec filled with raw data.
Expand Down
26 changes: 8 additions & 18 deletions primitives/core/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,27 +406,17 @@ impl TraitPair for Pair {
Signature::from_raw(self.secret.sign(message).into())
}

/// Verify a signature on a message. Returns true if the signature is good.
fn verify<M: AsRef<[u8]>>(sig: &Self::Signature, message: M, pubkey: &Self::Public) -> bool {
Self::verify_weak(&sig.0[..], message.as_ref(), pubkey)
}

/// Verify a signature on a message. Returns true if the signature is good.
/// Verify a signature on a message.
///
/// This doesn't use the type system to ensure that `sig` and `pubkey` are the correct
/// size. Use it only if you're coming from byte buffers and need the speed.
fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(sig: &[u8], message: M, pubkey: P) -> bool {
let public_key = match VerificationKey::try_from(pubkey.as_ref()) {
Ok(pk) => pk,
Err(_) => return false,
/// Returns true if the signature is good.
fn verify<M: AsRef<[u8]>>(sig: &Self::Signature, message: M, public: &Self::Public) -> bool {
let Ok(public) = VerificationKey::try_from(public.as_slice()) else {
return false
};

let sig = match ed25519_zebra::Signature::try_from(sig) {
Ok(s) => s,
Err(_) => return false,
let Ok(signature) = ed25519_zebra::Signature::try_from(sig.as_ref()) else {
return false
};

public_key.verify(&sig, message.as_ref()).is_ok()
public.verify(&signature, message.as_ref()).is_ok()
}

/// Return a vec filled with raw data.
Expand Down
18 changes: 5 additions & 13 deletions primitives/core/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,21 +487,13 @@ impl TraitPair for Pair {
}

fn verify<M: AsRef<[u8]>>(sig: &Self::Signature, message: M, pubkey: &Self::Public) -> bool {
Self::verify_weak(&sig.0[..], message, pubkey)
}

fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(sig: &[u8], message: M, pubkey: P) -> bool {
let signature = match schnorrkel::Signature::from_bytes(sig) {
Ok(signature) => signature,
Err(_) => return false,
let Ok(signature) = schnorrkel::Signature::from_bytes(sig.as_ref()) else {
return false
};

let pub_key = match PublicKey::from_bytes(pubkey.as_ref()) {
Ok(pub_key) => pub_key,
Err(_) => return false,
let Ok(public) = PublicKey::from_bytes(pubkey.as_ref()) else {
return false
};

pub_key.verify_simple(SIGNING_CTX, message.as_ref(), &signature).is_ok()
public.verify_simple(SIGNING_CTX, message.as_ref(), &signature).is_ok()
}

fn to_raw_vec(&self) -> Vec<u8> {
Expand Down