Skip to content

Commit 7c70da0

Browse files
committed
refactor Arbitrary impl
1 parent 7d008ee commit 7c70da0

File tree

9 files changed

+57
-22
lines changed

9 files changed

+57
-22
lines changed

rust/catalyst-voting/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ proptest = { version = "1.5.0" }
3131
# Potentially it could be replaced with using `proptest::property_test` attribute macro,
3232
# after this PR will be merged https://github.com/proptest-rs/proptest/pull/523
3333
test-strategy = "0.4.0"
34+
35+
[features]
36+
# Feature which makes publicly available
37+
# `proptest::prelude::Arbitrary` trait implementation for some types
38+
proptest-arbitrary = []

rust/catalyst-voting/src/crypto/ed25519/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ pub fn verify_signature(pk: &PublicKey, msg: &[u8], sig: &Signature) -> bool {
4646
pk.0.verify_strict(msg, &sig.0).is_ok()
4747
}
4848

49-
#[cfg(test)]
50-
mod tests {
49+
#[cfg(any(test, feature = "proptest-arbitrary"))]
50+
mod arbitrary_impl {
5151
use proptest::prelude::{any, Arbitrary, BoxedStrategy, Strategy};
52-
use test_strategy::proptest;
5352

5453
use super::*;
5554

@@ -63,6 +62,13 @@ mod tests {
6362
.boxed()
6463
}
6564
}
65+
}
66+
67+
#[cfg(test)]
68+
mod tests {
69+
use test_strategy::proptest;
70+
71+
use super::*;
6672

6773
#[proptest]
6874
fn sign_test(private_key: PrivateKey, msg: Vec<u8>) {

rust/catalyst-voting/src/crypto/elgamal/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,12 @@ impl Add<&Ciphertext> for &Ciphertext {
6666
}
6767
}
6868

69-
#[cfg(test)]
70-
mod tests {
69+
#[cfg(any(test, feature = "proptest-arbitrary"))]
70+
mod arbitrary_impl {
7171
use proptest::{
7272
arbitrary::any,
7373
prelude::{Arbitrary, BoxedStrategy, Strategy},
7474
};
75-
use test_strategy::proptest;
7675

7776
use super::*;
7877

@@ -86,6 +85,13 @@ mod tests {
8685
.boxed()
8786
}
8887
}
88+
}
89+
90+
#[cfg(test)]
91+
mod tests {
92+
use test_strategy::proptest;
93+
94+
use super::*;
8995

9096
#[proptest]
9197
fn ciphertext_add_test(e1: Scalar, e2: Scalar, e3: Scalar, e4: Scalar) {

rust/catalyst-voting/src/crypto/group/ristretto255/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,12 @@ impl Sub<&GroupElement> for &GroupElement {
159159
}
160160
}
161161

162-
#[cfg(test)]
163-
mod tests {
162+
#[cfg(any(test, feature = "proptest-arbitrary"))]
163+
mod arbitrary_impl {
164164
use proptest::{
165165
arbitrary::any,
166166
prelude::{Arbitrary, BoxedStrategy, Strategy},
167167
};
168-
use test_strategy::proptest;
169168

170169
use super::*;
171170

@@ -188,6 +187,13 @@ mod tests {
188187
.boxed()
189188
}
190189
}
190+
}
191+
192+
#[cfg(test)]
193+
mod tests {
194+
use test_strategy::proptest;
195+
196+
use super::*;
191197

192198
#[proptest]
193199
fn scalar_arithmetic_tests(e1: Scalar, e2: Scalar, e3: Scalar) {

rust/catalyst-voting/src/crypto/zk_unit_vector/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,14 @@ fn check_2(
235235
&right_1 + &right_2 == left
236236
}
237237

238-
#[cfg(test)]
239-
mod tests {
238+
#[cfg(any(test, feature = "proptest-arbitrary"))]
239+
mod arbitrary_impl {
240240
use proptest::{
241241
prelude::{any_with, Arbitrary, BoxedStrategy, Strategy},
242242
sample::size_range,
243243
};
244-
use rand_core::OsRng;
245-
use test_strategy::proptest;
246244

247-
use super::{super::elgamal::generate_public_key, *};
245+
use super::*;
248246

249247
impl Arbitrary for UnitVectorProof {
250248
type Parameters = usize;
@@ -263,6 +261,15 @@ mod tests {
263261
.boxed()
264262
}
265263
}
264+
}
265+
266+
#[cfg(test)]
267+
mod tests {
268+
use proptest::sample::size_range;
269+
use rand_core::OsRng;
270+
use test_strategy::proptest;
271+
272+
use super::{super::elgamal::generate_public_key, *};
266273

267274
fn is_unit_vector(vector: &[Scalar]) -> bool {
268275
let ones = vector.iter().filter(|s| s == &&Scalar::one()).count();

rust/catalyst-voting/src/crypto/zk_unit_vector/randomness_announcements.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ impl ResponseRandomness {
7979
}
8080
}
8181

82-
#[cfg(test)]
83-
mod tests {
82+
#[cfg(any(test, feature = "proptest-arbitrary"))]
83+
mod arbitrary_impl {
8484
use proptest::{
8585
arbitrary::any,
8686
prelude::{Arbitrary, BoxedStrategy, Strategy},

rust/catalyst-voting/src/vote_protocol/committee/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ impl ElectionSecretKey {
3636
#[derive(Debug, Clone, PartialEq, Eq)]
3737
pub struct ElectionPublicKey(pub(crate) GroupElement);
3838

39-
#[cfg(test)]
40-
mod tests {
39+
#[cfg(any(test, feature = "proptest-arbitrary"))]
40+
mod arbitrary_impl {
4141
use proptest::prelude::{any, Arbitrary, BoxedStrategy, Strategy};
4242

4343
use super::*;

rust/catalyst-voting/src/vote_protocol/voter/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ pub fn decrypt_vote(vote: &EncryptedVote, secret_key: &ElectionSecretKey) -> any
141141
bail!("Invalid encrypted vote, not a valid unit vector.")
142142
}
143143

144-
#[cfg(test)]
145-
mod tests {
144+
#[cfg(any(test, feature = "proptest-arbitrary"))]
145+
mod arbitrary_impl {
146146
use proptest::{
147147
prelude::{any_with, Arbitrary, BoxedStrategy, Strategy},
148148
sample::size_range,
@@ -160,6 +160,11 @@ mod tests {
160160
.boxed()
161161
}
162162
}
163+
}
164+
165+
#[cfg(test)]
166+
mod tests {
167+
use super::*;
163168

164169
#[test]
165170
fn vote_test() {

rust/catalyst-voting/src/vote_protocol/voter/proof.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ pub fn verify_voter_proof(
107107
verify_unit_vector_proof(&proof.0, encrypted_vote.0, &public_key.0, &commitment.0)
108108
}
109109

110-
#[cfg(test)]
111-
mod tests {
110+
#[cfg(any(test, feature = "proptest-arbitrary"))]
111+
mod arbitrary_impl {
112112
use proptest::prelude::{any_with, Arbitrary, BoxedStrategy, Strategy};
113113

114114
use super::*;

0 commit comments

Comments
 (0)