Skip to content

Commit 266430c

Browse files
authored
begin migration from quickcheck -> proptest in chain-impl-mockchain (#775)
* begin migration from quickcheck -> proptest * all working except strange imhamt bug * a few more tests, needs evm migration * workaround for hamt issue * fmt and clippy * merge conflict * couple of merge conflicts * fmt again * increase proptest shrink iters * fix arbitrary impls so tests pass * compile error * compile error * fmt * remove old failure seed * merge proptest impl modules into test modules * some fixes for upstream proptest impls * fix compile errors from master merge * fix imports * fix compile errors
1 parent f635809 commit 266430c

File tree

34 files changed

+366
-62
lines changed

34 files changed

+366
-62
lines changed

.github/workflows/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ jobs:
8282
continue-on-error: ${{ matrix.experimental }}
8383
timeout-minutes: 120
8484
env:
85+
PROPTEST_MAX_SHRINK_ITERS: 10000
8586
CARGO_INCREMENTAL: 0
8687
steps:
8788
- uses: actions/checkout@v2

chain-crypto/src/algorithms/ed25519.rs

+16
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,31 @@ use cryptoxide::ed25519;
66
use rand_core::{CryptoRng, RngCore};
77

88
/// ED25519 Signing Algorithm
9+
#[cfg_attr(
10+
any(test, feature = "property-test-api"),
11+
derive(test_strategy::Arbitrary, Debug)
12+
)]
913
pub struct Ed25519;
1014

1115
#[derive(Clone)]
16+
#[cfg_attr(
17+
any(test, feature = "property-test-api"),
18+
derive(test_strategy::Arbitrary, Debug)
19+
)]
1220
pub struct Priv([u8; ed25519::PRIVATE_KEY_LENGTH]);
1321

1422
#[derive(Clone, PartialEq, Eq, Hash)]
23+
#[cfg_attr(
24+
any(test, feature = "property-test-api"),
25+
derive(test_strategy::Arbitrary, Debug)
26+
)]
1527
pub struct Pub(pub(crate) [u8; ed25519::PUBLIC_KEY_LENGTH]);
1628

1729
#[derive(Clone)]
30+
#[cfg_attr(
31+
any(test, feature = "property-test-api"),
32+
derive(test_strategy::Arbitrary, Debug)
33+
)]
1834
pub struct Sig(pub(crate) [u8; ed25519::SIGNATURE_LENGTH]);
1935

2036
impl AsRef<[u8]> for Priv {

chain-crypto/src/algorithms/sumed25519/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ use rand_core::{CryptoRng, RngCore};
2121

2222
// MMM sum scheme instanciated over the Ed25519 signature system
2323
// and a specific depth of 12
24+
#[cfg_attr(
25+
any(test, feature = "property-test-api"),
26+
derive(test_strategy::Arbitrary, Debug)
27+
)]
2428
pub struct SumEd25519_12;
2529

2630
const DEPTH: common::Depth = common::Depth(12);

chain-crypto/src/algorithms/sumed25519/sum.rs

+4
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ impl SecretKey {
389389
}
390390

391391
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
392+
#[cfg_attr(
393+
any(test, feature = "property-test-api"),
394+
derive(test_strategy::Arbitrary)
395+
)]
392396
pub struct PublicKey([u8; PUBLIC_KEY_SIZE]);
393397

394398
impl PublicKey {

chain-crypto/src/algorithms/vrf/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use rand_core::{CryptoRng, RngCore};
1010
pub use vrf::ProvenOutputSeed;
1111

1212
/// VRF
13+
#[cfg_attr(
14+
any(test, feature = "property-test-api"),
15+
derive(test_strategy::Arbitrary, Debug)
16+
)]
1317
pub struct RistrettoGroup2HashDh;
1418

1519
impl AsymmetricPublicKey for RistrettoGroup2HashDh {

chain-crypto/src/algorithms/vrf/vrf.rs

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ impl AsRef<[u8]> for SecretKey {
2626

2727
/// VRF Public Key
2828
#[derive(Debug, Clone, PartialEq, Eq)]
29+
#[cfg_attr(
30+
any(test, feature = "property-test-api"),
31+
derive(test_strategy::Arbitrary)
32+
)]
2933
pub struct PublicKey(GroupElement, [u8; Self::BYTES_LEN]);
3034

3135
#[allow(clippy::derive_hash_xor_eq)]

chain-crypto/src/digest.rs

+8
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ impl<H: DigestAlg> Context<H> {
136136
}
137137
}
138138

139+
#[cfg_attr(
140+
any(test, feature = "property-test-api"),
141+
derive(test_strategy::Arbitrary)
142+
)]
139143
pub struct Digest<H: DigestAlg>(H::DigestData);
140144

141145
impl<H: DigestAlg> Clone for Digest<H> {
@@ -264,6 +268,10 @@ impl<H: DigestAlg> Digest<H> {
264268
use std::marker::PhantomData;
265269

266270
/// A typed version of Digest
271+
#[cfg_attr(
272+
any(test, feature = "property-test-api"),
273+
derive(test_strategy::Arbitrary)
274+
)]
267275
pub struct DigestOf<H: DigestAlg, T> {
268276
inner: Digest<H>,
269277
marker: PhantomData<T>,

chain-crypto/src/ec/ristretto255.rs

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use curve25519_dalek_ng::{
88
use cryptoxide::blake2b::Blake2b;
99
use cryptoxide::digest::Digest;
1010

11+
#[cfg(any(test, feature = "property-test-api"))]
12+
use proptest::strategy::{BoxedStrategy, Strategy};
1113
use rand_core::{CryptoRng, RngCore};
1214
use std::hash::{Hash, Hasher};
1315
use std::ops::{Add, Mul, Sub};
@@ -21,6 +23,18 @@ pub struct Scalar(IScalar);
2123
#[derive(Debug, Clone, PartialEq, Eq)]
2224
pub struct GroupElement(Point);
2325

26+
#[cfg(any(test, feature = "property-test-api"))]
27+
impl proptest::arbitrary::Arbitrary for GroupElement {
28+
type Parameters = ();
29+
type Strategy = BoxedStrategy<Self>;
30+
31+
fn arbitrary_with((): ()) -> Self::Strategy {
32+
proptest::arbitrary::any::<[u8; 64]>()
33+
.prop_map(|bytes| GroupElement(Point::from_uniform_bytes(&bytes)))
34+
.boxed()
35+
}
36+
}
37+
2438
#[allow(clippy::derive_hash_xor_eq)]
2539
impl Hash for GroupElement {
2640
fn hash<H: Hasher>(&self, state: &mut H) {

chain-crypto/src/hash.rs

+5
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ pub const HASH_SIZE_256: usize = 32;
136136

137137
/// Blake2b 256 bits
138138
#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Hash)]
139+
#[cfg_attr(
140+
any(test, feature = "property-test-api"),
141+
derive(test_strategy::Arbitrary)
142+
)]
139143
pub struct Blake2b256([u8; HASH_SIZE_256]);
144+
140145
define_hash_object!(Blake2b256, Blake2b256, HASH_SIZE_256, "blake2b256");
141146
define_blake2b_new!(Blake2b256);

chain-crypto/src/key.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::bech32::{self, Bech32};
22
use hex::FromHexError;
3+
#[cfg(any(test, feature = "property-test-api"))]
4+
use proptest::strategy::{BoxedStrategy, Strategy};
35
use rand_core::{CryptoRng, RngCore};
46
use std::fmt;
57
use std::hash::Hash;
@@ -52,8 +54,19 @@ pub trait SecretKeySizeStatic: AsymmetricKey {
5254

5355
pub struct SecretKey<A: AsymmetricKey>(pub(crate) A::Secret);
5456

57+
#[cfg(any(test, feature = "property-test-api"))]
58+
impl<A: AsymmetricPublicKey + 'static> proptest::arbitrary::Arbitrary for PublicKey<A>
59+
where
60+
A::Public: proptest::arbitrary::Arbitrary,
61+
{
62+
type Parameters = ();
63+
type Strategy = BoxedStrategy<Self>;
64+
fn arbitrary_with((): ()) -> Self::Strategy {
65+
let x = proptest::arbitrary::any::<A::Public>();
66+
x.prop_map(|x| Self(x)).boxed()
67+
}
68+
}
5569
pub struct PublicKey<A: AsymmetricPublicKey>(pub(crate) A::Public);
56-
5770
pub struct KeyPair<A: AsymmetricKey>(SecretKey<A>, PublicKey<A::PubAlg>);
5871

5972
impl<A: AsymmetricKey> KeyPair<A> {

chain-impl-mockchain/Cargo.toml

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ strum_macros = "0.23.1"
2222
hex = { version = "0.4.2", default-features = false, features = [ "std" ] }
2323
quickcheck = { version = "0.9", optional = true }
2424
quickcheck_macros = { version = "0.9", optional = true }
25+
proptest = { git = "https://github.com/input-output-hk/proptest.git", optional = true }
26+
test-strategy = { version = "0.1", optional = true }
2527
ed25519-bip32 = { version = "0.4.1", optional = true }
2628
thiserror = "1.0"
2729
lazy_static = { version = "1.3.0", optional = true }
@@ -37,17 +39,22 @@ property-test-api = [
3739
"chain-time/property-test-api",
3840
"chain-addr/property-test-api",
3941
"chain-evm/property-test-api",
42+
"imhamt/property-test-api",
4043
"quickcheck",
4144
"quickcheck_macros",
45+
"proptest",
46+
"test-strategy",
4247
"lazy_static",
4348
"rand_chacha",
4449
"ed25519-bip32"]
4550
with-bench = ["criterion","property-test-api"]
46-
evm = ["chain-evm"]
51+
evm = ["chain-evm", "proptest/evm"]
4752

4853
[dev-dependencies]
4954
quickcheck = "0.9"
5055
quickcheck_macros = "0.9"
56+
proptest = { git = "https://github.com/input-output-hk/proptest.git" }
57+
test-strategy = "0.1"
5158
chain-core = { path = "../chain-core"}
5259
chain-crypto = { path = "../chain-crypto", features=["property-test-api"]}
5360
chain-time = { path = "../chain-time", features=["property-test-api"]}

chain-impl-mockchain/src/account.rs

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ pub type Witness = Signature<WitnessAccountData, AccountAlg>;
1616

1717
/// Account Identifier (also used as Public Key)
1818
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
19+
#[cfg_attr(
20+
any(test, feature = "property-test-api"),
21+
derive(test_strategy::Arbitrary)
22+
)]
1923
pub struct Identifier(PublicKey<AccountAlg>);
2024

2125
impl From<PublicKey<AccountAlg>> for Identifier {

0 commit comments

Comments
 (0)