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

Add fuzzy tests to signature #5

Open
vlopes11 opened this issue Feb 9, 2022 · 0 comments
Open

Add fuzzy tests to signature #5

vlopes11 opened this issue Feb 9, 2022 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@vlopes11
Copy link
Contributor

vlopes11 commented Feb 9, 2022

This seems quite costly to exhaustively fuzz the entire search space every test run. Using prop-tests we could randomly sample the distribution on each test run and easily track which input params cause a failure:


use proptest::prelude::{
    prop::test_runner::{RngAlgorithm, TestRng}, *,
};
prop_compose! {
    fn arb_rng()(
        bytes: [u8; 32],
    ) -> TestRng {
        TestRng::from_seed(RngAlgorithm::ChaCha, &bytes)
    }
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(50))]
    #[test]
    fn corrupted_signature(
        mut rng in arb_rng(),
        position in 0..Signature::LEN,
        bit in 0..7,
    ) {
        let message = b"When life itself seems lunatic, who knows where madness lies?";
        let message = Message::new(message);

        let secret = SecretKey::random(&mut rng);
        let public = secret.public_key();

        let signature = Signature::sign(&secret, &message);

        // Tamper a random bit of the signature
        //
        // The recover and verify operations should fail in all cases.

        let mut s = signature;
        let m = 1u8 << bit;
        s.as_mut()[position] ^= m;

        // check recover
        match s.recover(&message) {
            Ok(pk) => assert_ne!(public, pk),
            Err(Error::InvalidSignature) => (),
            Err(e) => panic!("Unexpected error: {}", e),
        }

        // check verify
        assert!(s.verify(&public, &message).is_err());
    }

    #[test]
    fn corrupted_public_key(
        mut rng in arb_rng(),
        position in 0..PublicKey::LEN,
        bit in 0..7,
    ) {
        let message = b"When life itself seems lunatic, who knows where madness lies?";
        let message = Message::new(message);

        let secret = SecretKey::random(&mut rng);
        let mut public = secret.public_key();

        let signature = Signature::sign(&secret, &message);

        // Tamper a random bit of the public key.
        //
        // The verify operations should fail in all cases.
        let m = 1u8 << bit;
        public.as_mut()[position] ^= m;
        assert!(signature.verify(&public, &message).is_err());
    }
}

Originally posted by @Voxelot in #2 (comment)

@vlopes11 vlopes11 added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Feb 9, 2022
@adlerjohn adlerjohn moved this to Todo in Fuel Network Apr 7, 2022
@sdankel sdankel removed this from Fuel Network Sep 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant