Skip to content

Commit

Permalink
[crypto] Renames
Browse files Browse the repository at this point in the history
- verify_struct_msg -> verify
- batch_verify_struct_signatures -> batch_verify
- batch_verify_aggregated_struct_signature -> batch_verify_aggregated_signatures

Closes: aptos-labs#4846
Approved by: ankushagarwal
  • Loading branch information
huitseeker authored and bors-libra committed Jul 7, 2020
1 parent 74535fe commit 353c76d
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 69 deletions.
2 changes: 1 addition & 1 deletion client/swiss-knife/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,6 @@ fn verify_transaction_signature_using_ed25519(
))
})
.unwrap();
let valid_signature = signature.verify_struct_msg(&raw_txn, &public_key).is_ok();
let valid_signature = signature.verify(&raw_txn, &public_key).is_ok();
VerifyTransactionEd25519SignatureResponse { valid_signature }
}
4 changes: 2 additions & 2 deletions common/lcs/tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ fn ed25519_material() {
assert_eq!(deserialized_signature, signature);

// Verify signature
let verified_signature = signature.verify_struct_msg(&message, &public_key);
let verified_signature = signature.verify(&message, &public_key);
assert!(verified_signature.is_ok())
}

Expand Down Expand Up @@ -671,6 +671,6 @@ fn multi_ed25519_material() {

// Verify signature
assert!(multi_signature_7of10
.verify_struct_msg(&message, &multi_public_key_7of10)
.verify(&message, &multi_public_key_7of10)
.is_ok());
}
2 changes: 1 addition & 1 deletion consensus/safety-rules/src/safety_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl TSafetyRules for SafetyRules {
if let Some(public_key) = self.execution_public_key.as_ref() {
execution_signature
.ok_or_else(|| Error::VoteProposalSignatureNotFound)?
.verify_struct_msg(vote_proposal, public_key)?
.verify(vote_proposal, public_key)?
}

let proposed_block = vote_proposal.block();
Expand Down
4 changes: 2 additions & 2 deletions crypto/crypto-derive/src/unions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ pub fn impl_enum_signature(

match_struct_arms.extend(quote! {
(#name::#variant_ident(sig), #pub_kt::#variant_ident(pk)) => {
sig.verify_struct_msg(message, pk)
sig.verify(message, pk)
}
})
}
Expand All @@ -275,7 +275,7 @@ pub fn impl_enum_signature(
type VerifyingKeyMaterial = #pub_kt;
type SigningKeyMaterial = #priv_kt;

fn verify_struct_msg<T: libra_crypto::hash::CryptoHash + serde::Serialize>(&self, message: &T, public_key: &Self::VerifyingKeyMaterial) -> std::result::Result<(), libra_crypto::error::Error> {
fn verify<T: libra_crypto::hash::CryptoHash + serde::Serialize>(&self, message: &T, public_key: &Self::VerifyingKeyMaterial) -> std::result::Result<(), libra_crypto::error::Error> {
match (self, public_key) {
#match_struct_arms
_ => libra_crypto::error::bail!(
Expand Down
6 changes: 3 additions & 3 deletions crypto/crypto/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! let private_key = Ed25519PrivateKey::generate(&mut rng);
//! let public_key: Ed25519PublicKey = (&private_key).into();
//! let signature = private_key.sign(&message);
//! assert!(signature.verify_struct_msg(&message, &public_key).is_ok());
//! assert!(signature.verify(&message, &public_key).is_ok());
//! ```
//! **Note**: The above example generates a private key using a private function intended only for
//! testing purposes. Production code should find an alternate means for secure key generation.
Expand Down Expand Up @@ -391,7 +391,7 @@ impl Signature for Ed25519Signature {
type VerifyingKeyMaterial = Ed25519PublicKey;
type SigningKeyMaterial = Ed25519PrivateKey;

fn verify_struct_msg<T: CryptoHash + Serialize>(
fn verify<T: CryptoHash + Serialize>(
&self,
message: &T,
public_key: &Ed25519PublicKey,
Expand Down Expand Up @@ -423,7 +423,7 @@ impl Signature for Ed25519Signature {
/// Batch signature verification as described in the original EdDSA article
/// by Bernstein et al. "High-speed high-security signatures". Current implementation works for
/// signatures on the same message and it checks for malleability.
fn batch_verify_struct_signatures<T: CryptoHash + Serialize>(
fn batch_verify<T: CryptoHash + Serialize>(
message: &T,
keys_and_signatures: Vec<(Self::VerifyingKeyMaterial, Self)>,
) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion crypto/crypto/src/multi_ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ impl Signature for MultiEd25519Signature {
type VerifyingKeyMaterial = MultiEd25519PublicKey;
type SigningKeyMaterial = MultiEd25519PrivateKey;

fn verify_struct_msg<T: CryptoHash + Serialize>(
fn verify<T: CryptoHash + Serialize>(
&self,
message: &T,
public_key: &MultiEd25519PublicKey,
Expand Down
12 changes: 6 additions & 6 deletions crypto/crypto/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ pub trait VerifyingKey:
message: &T,
signature: &Self::SignatureMaterial,
) -> Result<()> {
signature.verify_struct_msg(message, self)
signature.verify(message, self)
}

/// We provide the implementation which dispatches to the signature.
fn batch_verify_struct_signatures<T: CryptoHash + Serialize>(
fn batch_verify<T: CryptoHash + Serialize>(
message: &T,
keys_and_signatures: Vec<(Self, Self::SignatureMaterial)>,
) -> Result<()> {
Self::SignatureMaterial::batch_verify_struct_signatures(message, keys_and_signatures)
Self::SignatureMaterial::batch_verify(message, keys_and_signatures)
}
}

Expand Down Expand Up @@ -227,7 +227,7 @@ pub trait Signature:

/// Verification for a struct we unabmiguously know how to serialize and
/// that we have a domain separation prefix for.
fn verify_struct_msg<T: CryptoHash + Serialize>(
fn verify<T: CryptoHash + Serialize>(
&self,
message: &T,
public_key: &Self::VerifyingKeyMaterial,
Expand All @@ -246,12 +246,12 @@ pub trait Signature:
/// The implementer can override a batch verification implementation
/// that by default iterates over each signature. More efficient
/// implementations exist and should be implemented for many schemes.
fn batch_verify_struct_signatures<T: CryptoHash + Serialize>(
fn batch_verify<T: CryptoHash + Serialize>(
message: &T,
keys_and_signatures: Vec<(Self::VerifyingKeyMaterial, Self)>,
) -> Result<()> {
for (key, signature) in keys_and_signatures {
signature.verify_struct_msg(message, &key)?
signature.verify(message, &key)?
}
Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions crypto/crypto/src/unit_tests/cross_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ proptest! {
let signature = ed_key.sign(&message);

// This is business as usual
prop_assert!(signature.verify_struct_msg(&message, &ed_keypair1.public_key).is_ok());
prop_assert!(signature.verify(&message, &ed_keypair1.public_key).is_ok());

// This is impossible to write, and generates:
// expected struct `ed25519::Ed25519PublicKey`, found struct `med12381::MultiEd25519PublicKey`
Expand All @@ -100,12 +100,12 @@ proptest! {

// This is still business as usual
let ed_pubkey2 = PublicK::Ed(ed_keypair2.public_key);
let good_sigver = ed_signature.verify_struct_msg(&message, &ed_pubkey2);
let good_sigver = ed_signature.verify(&message, &ed_pubkey2);
prop_assert!(good_sigver.is_ok(), "{:?}", good_sigver);

// but this still fails, as expected
let med_pubkey = PublicK::MultiEd(med_keypair.public_key);
let bad_sigver = ed_signature.verify_struct_msg(&message, &med_pubkey);
let bad_sigver = ed_signature.verify(&message, &med_pubkey);
prop_assert!(bad_sigver.is_err(), "{:?}", bad_sigver);

// And now just in case we're confused again, we pop in the
Expand All @@ -114,11 +114,11 @@ proptest! {
let med_signature = med_key.sign(&message);

// This is still business as usual
let good_sigver = med_signature.verify_struct_msg(&message, &med_pubkey);
let good_sigver = med_signature.verify(&message, &med_pubkey);
prop_assert!(good_sigver.is_ok(), "{:?}", good_sigver);

// but this still fails, as expected
let bad_sigver = med_signature.verify_struct_msg(&message, &ed_pubkey2);
let bad_sigver = med_signature.verify(&message, &ed_pubkey2);
prop_assert!(bad_sigver.is_err(), "{:?}", bad_sigver);
}
}
8 changes: 4 additions & 4 deletions crypto/crypto/src/unit_tests/ed25519_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ proptest! {
let mut signatures: Vec<(Ed25519PublicKey, Ed25519Signature)> = keypairs.iter().map(|keypair| {
(keypair.public_key.clone(), keypair.private_key.sign(&message))
}).collect();
prop_assert!(Ed25519Signature::batch_verify_struct_signatures(&message, signatures.clone()).is_ok());
prop_assert!(Ed25519Signature::batch_verify(&message, signatures.clone()).is_ok());
// We swap message and signature for the last element,
// resulting in an incorrect signature
let (key, _sig) = signatures.pop().unwrap();
let other_sig = signatures.last().unwrap().clone().1;
signatures.push((key, other_sig));
prop_assert!(Ed25519Signature::batch_verify_struct_signatures(&message, signatures).is_err());
prop_assert!(Ed25519Signature::batch_verify(&message, signatures).is_err());
}

#[test]
Expand Down Expand Up @@ -139,7 +139,7 @@ proptest! {
let serialized: &[u8] = &(signature.to_bytes());
prop_assert_eq!(ED25519_SIGNATURE_LENGTH, serialized.len());
let deserialized = Ed25519Signature::try_from(serialized).unwrap();
prop_assert!(deserialized.verify_struct_msg(&message, &keypair.public_key).is_ok());
prop_assert!(deserialized.verify(&message, &keypair.public_key).is_ok());
}

#[test]
Expand All @@ -165,7 +165,7 @@ proptest! {
let serialized: &[u8] = &(signature.to_bytes());
prop_assert_eq!(ED25519_SIGNATURE_LENGTH, serialized.len());
let deserialized = Ed25519Signature::try_from(serialized).unwrap();
prop_assert!(deserialized.verify_struct_msg(&hashable, &keypair.public_key).is_ok());
prop_assert!(deserialized.verify(&hashable, &keypair.public_key).is_ok());
}

// Check for canonical S.
Expand Down
36 changes: 17 additions & 19 deletions crypto/crypto/src/unit_tests/multi_ed25519_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ fn test_successful_signature_serialization(private_keys: &[Ed25519PrivateKey], t
let multi_signature_serialized_unwrapped = multi_signature_serialized.unwrap();
assert_eq!(multi_signature, multi_signature_serialized_unwrapped);
// Ensure that the signature verifies.
assert!(multi_signature
.verify_struct_msg(message(), &multi_public_key)
.is_ok());
assert!(multi_signature.verify(message(), &multi_public_key).is_ok());
}

// Test multi-sig Ed25519 public key serialization.
Expand Down Expand Up @@ -200,7 +198,7 @@ fn test_multi_ed25519_signature_serialization() {
let multi_priv_key_1of3 = MultiEd25519PrivateKey::new(priv_keys_3.to_vec(), 1).unwrap();
let multi_pub_key_1of3 = MultiEd25519PublicKey::from(&multi_priv_key_1of3);
assert!(multi_signature
.verify_struct_msg(message(), &multi_pub_key_1of3)
.verify(message(), &multi_pub_key_1of3)
.is_ok());

// We can construct signatures from 32 single signatures.
Expand All @@ -218,7 +216,7 @@ fn test_multi_ed25519_signature_serialization() {
let pub_key_32 = vec![priv_keys_3[0].public_key(); 32];
let multi_pub_key_32 = MultiEd25519PublicKey::new(pub_key_32, 32).unwrap();
assert!(multi_sig32_unwrapped
.verify_struct_msg(message(), &multi_pub_key_32)
.verify(message(), &multi_pub_key_32)
.is_ok());

// Fail to construct a MultiEd25519Signature object from 33 or more single signatures.
Expand Down Expand Up @@ -280,19 +278,19 @@ fn test_multi_ed25519_signature_verification() {
&[0b1111_1110, 0u8, 0u8, 0u8]
);
assert!(multi_signature_7of10
.verify_struct_msg(message(), &multi_public_key_7of10)
.verify(message(), &multi_public_key_7of10)
.is_ok());

// Verifying a 7-of-10 signature against a public key with bigger threshold (i.e., 8) should fail.
let multi_public_key_8of10 = MultiEd25519PublicKey::new(pub_keys_10.clone(), 8).unwrap();
assert!(multi_signature_7of10
.verify_struct_msg(message(), &multi_public_key_8of10)
.verify(message(), &multi_public_key_8of10)
.is_err());

// Verifying a 7-of-10 signature against a public key with smaller threshold (i.e., 6) should pass.
let multi_public_key_6of10 = MultiEd25519PublicKey::new(pub_keys_10.clone(), 6).unwrap();
assert!(multi_signature_7of10
.verify_struct_msg(message(), &multi_public_key_6of10)
.verify(message(), &multi_public_key_6of10)
.is_ok());

// Verifying a 7-of-10 signature against a reordered MultiEd25519PublicKey should fail.
Expand All @@ -303,7 +301,7 @@ fn test_multi_ed25519_signature_verification() {
let multi_public_key_7of10_reversed =
MultiEd25519PublicKey::new(pub_keys_10_reversed, 7).unwrap();
assert!(multi_signature_7of10
.verify_struct_msg(message(), &multi_public_key_7of10_reversed)
.verify(message(), &multi_public_key_7of10_reversed)
.is_err());

let priv_keys_3 = generate_keys(3);
Expand All @@ -321,7 +319,7 @@ fn test_multi_ed25519_signature_verification() {
&[0b0100_0000, 0u8, 0u8, 0u8]
);
assert!(multi_sig_signed_by_2nd_key_unwrapped
.verify_struct_msg(message(), &multi_public_key_1of3)
.verify(message(), &multi_public_key_1of3)
.is_ok());

// Signing with the 2nd key but using wrong index will fail.
Expand All @@ -331,7 +329,7 @@ fn test_multi_ed25519_signature_verification() {
assert!(multi_sig_signed_by_2nd_key_wrong_index.is_ok());
let failed_multi_sig_signed_by_2nd_key_wrong_index = multi_sig_signed_by_2nd_key_wrong_index
.unwrap()
.verify_struct_msg(message(), &multi_public_key_1of3);
.verify(message(), &multi_public_key_1of3);
assert!(failed_multi_sig_signed_by_2nd_key_wrong_index.is_err());

// Signing with the 2nd and 3rd keys must succeed, even if we surpass the threshold.
Expand All @@ -348,7 +346,7 @@ fn test_multi_ed25519_signature_verification() {
&[0b0110_0000, 0u8, 0u8, 0u8]
);
assert!(multi_sig_signed_by_2nd_and_3rd_key_unwrapped
.verify_struct_msg(message(), &multi_public_key_1of3)
.verify(message(), &multi_public_key_1of3)
.is_ok());

// Signing with the 2nd and 3rd keys will fail if we swap indexes.
Expand All @@ -359,7 +357,7 @@ fn test_multi_ed25519_signature_verification() {
let failed_multi_sig_signed_by_2nd_and_3rd_key_swapped =
multi_sig_signed_by_2nd_and_3rd_key_swapped
.unwrap()
.verify_struct_msg(message(), &multi_public_key_1of3);
.verify(message(), &multi_public_key_1of3);
assert!(failed_multi_sig_signed_by_2nd_and_3rd_key_swapped.is_err());

// Signing with the 2nd and an unrelated key. Although threshold is met, it should fail as
Expand All @@ -372,7 +370,7 @@ fn test_multi_ed25519_signature_verification() {
assert!(multi_sig_signed_by_2nd_and_unrelated_key.is_ok());
let failed_verified_sig = multi_sig_signed_by_2nd_and_unrelated_key
.unwrap()
.verify_struct_msg(message(), &multi_public_key_1of3);
.verify(message(), &multi_public_key_1of3);
assert!(failed_verified_sig.is_err());

// Testing all combinations for 2 of 3.
Expand All @@ -393,7 +391,7 @@ fn test_multi_ed25519_signature_verification() {
&[0b1100_0000, 0u8, 0u8, 0u8]
);
assert!(signed_by_1st_and_2nd_key_unwrapped
.verify_struct_msg(message(), &multi_public_key_2of3)
.verify(message(), &multi_public_key_2of3)
.is_ok());

// Signing with the 1st and 3rd keys must succeed.
Expand All @@ -408,7 +406,7 @@ fn test_multi_ed25519_signature_verification() {
&[0b1010_0000, 0u8, 0u8, 0u8]
);
assert!(signed_by_1st_and_3rd_key_unwrapped
.verify_struct_msg(message(), &multi_public_key_2of3)
.verify(message(), &multi_public_key_2of3)
.is_ok());

// Signing with the 2nd and 3rd keys must succeed.
Expand All @@ -423,7 +421,7 @@ fn test_multi_ed25519_signature_verification() {
&[0b0110_0000, 0u8, 0u8, 0u8]
);
assert!(signed_by_2nd_and_3rd_key_unwrapped
.verify_struct_msg(message(), &multi_public_key_2of3)
.verify(message(), &multi_public_key_2of3)
.is_ok());

// Signing with the 2nd and 3rd keys must succeed.
Expand All @@ -439,7 +437,7 @@ fn test_multi_ed25519_signature_verification() {
&[0b1110_0000, 0u8, 0u8, 0u8]
);
assert!(signed_by_all_3_keys_unwrapped
.verify_struct_msg(message(), &multi_public_key_2of3)
.verify(message(), &multi_public_key_2of3)
.is_ok());

// Signing with the 2nd only will fail.
Expand All @@ -451,6 +449,6 @@ fn test_multi_ed25519_signature_verification() {
&[0b0100_0000, 0u8, 0u8, 0u8]
);
assert!(signed_by_2nd_key_unwrapped
.verify_struct_msg(message(), &multi_public_key_2of3)
.verify(message(), &multi_public_key_2of3)
.is_err());
}
2 changes: 1 addition & 1 deletion execution/execution-correctness/src/tests/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn run_test_suite(executor_pair: (Box<dyn ExecutionCorrectness>, Option<Ed25
block,
result.epoch_state().clone(),
);
sig.verify_struct_msg(&vote_proposal, &execution_pubkey.unwrap())
sig.verify(&vote_proposal, &execution_pubkey.unwrap())
.unwrap();
}

Expand Down
8 changes: 3 additions & 5 deletions secure/storage/src/tests/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn test_import_key(storage: &mut Storage) {
let message = TestLibraCrypto("Hello, World".to_string());
let message_signature = storage.sign(imported_key_name, &message).unwrap();
message_signature
.verify_struct_msg(&message, &imported_public_key)
.verify(&message, &imported_public_key)
.unwrap();

// Ensure rotation still works
Expand All @@ -163,7 +163,7 @@ fn test_import_key(storage: &mut Storage) {

let rotated_message_signature = storage.sign(imported_key_name, &message).unwrap();
rotated_message_signature
.verify_struct_msg(&message, &rotated_imported_public_key)
.verify(&message, &rotated_imported_public_key)
.unwrap();

assert_ne!(imported_key, rotated_imported_key);
Expand Down Expand Up @@ -283,9 +283,7 @@ fn test_create_sign_rotate_sign(storage: &mut Storage) {
// Create then sign message and verify correct signature
let message = TestLibraCrypto("Hello, World".to_string());
let message_signature = storage.sign(CRYPTO_NAME, &message).unwrap();
assert!(message_signature
.verify_struct_msg(&message, &public_key)
.is_ok());
assert!(message_signature.verify(&message, &public_key).is_ok());

// Rotate the key pair and sign the message again using the previous key pair version
let _ = storage.rotate_key(CRYPTO_NAME).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions secure/storage/src/tests/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,6 @@ fn test_vault_crypto_policies() {
signer_store.get_public_key(key_name).unwrap_err();
signer_store.rotate_key(key_name).unwrap_err();
let signature = signer_store.sign(key_name, &message).unwrap();
signature.verify_struct_msg(&message, &pubkey).unwrap_err();
signature.verify_struct_msg(&message, &new_pubkey).unwrap();
signature.verify(&message, &pubkey).unwrap_err();
signature.verify(&message, &new_pubkey).unwrap();
}
Loading

0 comments on commit 353c76d

Please sign in to comment.