Skip to content

Commit fdbd960

Browse files
authored
feat(rust): Create new jormungandr-vote-tx crate (#76)
* add new jormungandr-vote-tx crate * move code from the catalyst-voting crate to jorm-vote-tx * update doc test * fix link * refactor Arbitrary impl * refactor proptest::Arbitrary impl * wip * fix earthfile * fix cargo deny * remove feature flags
1 parent 9527127 commit fdbd960

File tree

23 files changed

+227
-153
lines changed

23 files changed

+227
-153
lines changed

rust/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ members = [
77
"cbork",
88
"cbork-abnf-parser",
99
"cbork-cddl-parser",
10-
"catalyst-voting",
10+
"catalyst-voting", "jormungandr-vote-tx",
1111
]
1212

1313
[workspace.package]

rust/Earthfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ COPY_SRC:
1010
.cargo .config \
1111
c509-certificate \
1212
cardano-chain-follower \
13-
catalyst-voting \
13+
catalyst-voting jormungandr-vote-tx \
1414
cbork cbork-abnf-parser cbork-cddl-parser \
1515
hermes-ipfs \
1616
.
@@ -53,7 +53,7 @@ build:
5353
--cmd="/scripts/std_build.py" \
5454
--args1="--libs=c509-certificate --libs=cardano-chain-follower --libs=hermes-ipfs" \
5555
--args2="--libs=cbork-cddl-parser --libs=cbork-abnf-parser" \
56-
--args3="--libs=catalyst-voting" \
56+
--args3="--libs=catalyst-voting --libs=jormungandr-vote-tx" \
5757
--args4="--bins=cbork/cbork" \
5858
--args5="--cov_report=$HOME/build/coverage-report.info" \
5959
--output="release/[^\./]+" \

rust/catalyst-voting/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ curve25519-dalek = { version = "4.1.3", features = ["digest", "rand_core"] }
2424
ed25519-dalek = { version = "2.1.1", features = ["rand_core"] }
2525
blake2b_simd = "1.0.2"
2626
rayon = "1.10.0"
27+
proptest = { version = "1.5.0" }
2728

2829
[dev-dependencies]
2930
criterion = "0.5.1"
30-
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"

rust/catalyst-voting/benches/vote_protocol.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
)]
1313

1414
use catalyst_voting::{
15-
crypto::default_rng,
15+
crypto::rng::default_rng,
1616
vote_protocol::{
1717
committee::{ElectionPublicKey, ElectionSecretKey},
1818
tally::{

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

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ mod decoding;
55
use ed25519_dalek::{
66
ed25519::signature::Signer, Signature as Ed25519Signature, SigningKey, VerifyingKey,
77
};
8-
use rand_core::CryptoRngCore;
8+
9+
use crate::crypto::rng::rand_core::CryptoRngCore;
910

1011
/// `Ed25519` private key struct.
1112
#[must_use]
@@ -45,12 +46,11 @@ pub fn verify_signature(pk: &PublicKey, msg: &[u8], sig: &Signature) -> bool {
4546
pk.0.verify_strict(msg, &sig.0).is_ok()
4647
}
4748

48-
#[cfg(test)]
49-
mod tests {
49+
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
50+
mod arbitrary_impl {
5051
use proptest::prelude::{any, Arbitrary, BoxedStrategy, Strategy};
51-
use test_strategy::proptest;
5252

53-
use super::*;
53+
use super::{PrivateKey, SigningKey};
5454

5555
impl Arbitrary for PrivateKey {
5656
type Parameters = ();
@@ -62,6 +62,13 @@ mod tests {
6262
.boxed()
6363
}
6464
}
65+
}
66+
67+
#[cfg(test)]
68+
mod tests {
69+
use test_strategy::proptest;
70+
71+
use super::*;
6572

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

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,14 @@ impl Add<&Ciphertext> for &Ciphertext {
6666
}
6767
}
6868

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

77-
use super::*;
76+
use super::{Ciphertext, GroupElement};
7877

7978
impl Arbitrary for Ciphertext {
8079
type Parameters = ();
@@ -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

+14-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ use curve25519_dalek::{
1515
traits::Identity,
1616
RistrettoPoint,
1717
};
18-
use rand_core::CryptoRngCore;
1918

20-
use crate::crypto::hash::digest::{consts::U64, Digest};
19+
use crate::crypto::{
20+
hash::digest::{consts::U64, Digest},
21+
rng::rand_core::CryptoRngCore,
22+
};
2123

2224
/// Ristretto group scalar.
2325
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -157,15 +159,14 @@ impl Sub<&GroupElement> for &GroupElement {
157159
}
158160
}
159161

160-
#[cfg(test)]
161-
mod tests {
162+
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
163+
mod arbitrary_impl {
162164
use proptest::{
163165
arbitrary::any,
164166
prelude::{Arbitrary, BoxedStrategy, Strategy},
165167
};
166-
use test_strategy::proptest;
167168

168-
use super::*;
169+
use super::{GroupElement, Mul, Scalar};
169170

170171
impl Arbitrary for Scalar {
171172
type Parameters = ();
@@ -186,6 +187,13 @@ mod tests {
186187
.boxed()
187188
}
188189
}
190+
}
191+
192+
#[cfg(test)]
193+
mod tests {
194+
use test_strategy::proptest;
195+
196+
use super::*;
189197

190198
#[proptest]
191199
fn scalar_arithmetic_tests(e1: Scalar, e2: Scalar, e3: Scalar) {
+1-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
//! Crypto primitives which are used by voting protocol.
22
3-
// cspell: words Seedable
4-
5-
use rand_chacha::ChaCha8Rng;
6-
use rand_core::{CryptoRngCore, SeedableRng};
7-
83
pub mod babystep_giantstep;
94
pub mod ed25519;
105
pub mod elgamal;
116
pub mod group;
127
pub mod hash;
8+
pub mod rng;
139
pub mod zk_dl_equality;
1410
pub mod zk_unit_vector;
15-
16-
/// Default random number generator `rand_chacha::ChaCha8Rng`.
17-
#[must_use]
18-
pub fn default_rng() -> impl CryptoRngCore {
19-
ChaCha8Rng::from_entropy()
20-
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Random number generator objects.
2+
3+
// cspell: words Seedable
4+
5+
use rand_chacha::ChaCha8Rng;
6+
pub use rand_core;
7+
use rand_core::{CryptoRngCore, SeedableRng};
8+
9+
/// Default random number generator `rand_chacha::ChaCha8Rng`.
10+
#[must_use]
11+
#[allow(clippy::module_name_repetitions)]
12+
pub fn default_rng() -> impl CryptoRngCore {
13+
ChaCha8Rng::from_entropy()
14+
}

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ use std::ops::Mul;
1616

1717
use challenges::{calculate_first_challenge_hash, calculate_second_challenge_hash};
1818
use polynomial::{calculate_polynomial_val, generate_polynomial, Polynomial};
19-
use rand_core::CryptoRngCore;
2019
use randomness_announcements::{Announcement, BlindingRandomness, ResponseRandomness};
2120
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
2221
use utils::get_bit;
2322

2423
use crate::crypto::{
2524
elgamal::{encrypt, Ciphertext},
2625
group::{GroupElement, Scalar},
26+
rng::rand_core::CryptoRngCore,
2727
};
2828

2929
/// Unit vector proof struct
@@ -235,16 +235,14 @@ fn check_2(
235235
&right_1 + &right_2 == left
236236
}
237237

238-
#[cfg(test)]
239-
mod tests {
238+
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
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::{Announcement, Ciphertext, ResponseRandomness, Scalar, UnitVectorProof};
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

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use std::ops::Mul;
66

7-
use rand_core::CryptoRngCore;
8-
9-
use crate::crypto::group::{GroupElement, Scalar};
7+
use crate::crypto::{
8+
group::{GroupElement, Scalar},
9+
rng::rand_core::CryptoRngCore,
10+
};
1011

1112
/// Randomness generated in the proof, used for the hiding property.
1213
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -78,14 +79,14 @@ impl ResponseRandomness {
7879
}
7980
}
8081

81-
#[cfg(test)]
82-
mod tests {
82+
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
83+
mod arbitrary_impl {
8384
use proptest::{
8485
arbitrary::any,
8586
prelude::{Arbitrary, BoxedStrategy, Strategy},
8687
};
8788

88-
use super::*;
89+
use super::{Announcement, BlindingRandomness, GroupElement, ResponseRandomness, Scalar};
8990

9091
impl Arbitrary for BlindingRandomness {
9192
type Parameters = ();

rust/catalyst-voting/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Voting primitives which are used among Catalyst ecosystem.
22
33
pub mod crypto;
4-
pub mod txs;
54
mod utils;
65
pub mod vote_protocol;

rust/catalyst-voting/src/txs/mod.rs

-3
This file was deleted.

rust/catalyst-voting/src/utils.rs

-24
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,6 @@
22
33
use std::io::Read;
44

5-
/// Read a single byte from the reader.
6-
#[inline]
7-
pub(crate) fn read_be_u8<R: Read>(reader: &mut R) -> anyhow::Result<u8> {
8-
let mut buf = [0u8; 1];
9-
reader.read_exact(&mut buf)?;
10-
Ok(u8::from_be_bytes(buf))
11-
}
12-
13-
/// Read a big-endian u32 from the reader.
14-
#[inline]
15-
pub(crate) fn read_be_u32<R: Read>(reader: &mut R) -> anyhow::Result<u32> {
16-
let mut buf = [0u8; 4];
17-
reader.read_exact(&mut buf)?;
18-
Ok(u32::from_be_bytes(buf))
19-
}
20-
21-
/// Read a big-endian u64 from the reader.
22-
#[inline]
23-
pub(crate) fn read_be_u64<R: Read>(reader: &mut R) -> anyhow::Result<u64> {
24-
let mut buf = [0u8; 8];
25-
reader.read_exact(&mut buf)?;
26-
Ok(u64::from_be_bytes(buf))
27-
}
28-
295
/// Read a N-byte array from the reader.
306
#[inline]
317
pub(crate) fn read_array<R: Read, const N: usize>(reader: &mut R) -> anyhow::Result<[u8; N]> {

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
33
mod decoding;
44

5-
use rand_core::CryptoRngCore;
6-
75
use crate::crypto::{
8-
default_rng,
96
elgamal::generate_public_key,
107
group::{GroupElement, Scalar},
8+
rng::{default_rng, rand_core::CryptoRngCore},
119
};
1210

1311
/// Election secret key.
@@ -38,11 +36,11 @@ impl ElectionSecretKey {
3836
#[derive(Debug, Clone, PartialEq, Eq)]
3937
pub struct ElectionPublicKey(pub(crate) GroupElement);
4038

41-
#[cfg(test)]
42-
mod tests {
39+
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
40+
mod arbitrary_impl {
4341
use proptest::prelude::{any, Arbitrary, BoxedStrategy, Strategy};
4442

45-
use super::*;
43+
use super::{ElectionSecretKey, Scalar};
4644

4745
impl Arbitrary for ElectionSecretKey {
4846
type Parameters = ();

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
44
use std::ops::Mul;
55

6-
use rand_core::CryptoRngCore;
7-
86
use super::EncryptedTally;
97
use crate::{
108
crypto::{
11-
default_rng,
129
group::{GroupElement, Scalar},
10+
rng::{default_rng, rand_core::CryptoRngCore},
1311
zk_dl_equality::{generate_dleq_proof, verify_dleq_proof, DleqProof},
1412
},
1513
vote_protocol::committee::{ElectionPublicKey, ElectionSecretKey},

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use crate::{
1212

1313
impl EncryptedVote {
1414
/// Get an underlying vector length.
15-
pub(crate) fn size(&self) -> usize {
15+
#[must_use]
16+
pub fn size(&self) -> usize {
1617
self.0.len()
1718
}
1819

0 commit comments

Comments
 (0)