Skip to content

Commit 414ff21

Browse files
committed
wip
1 parent 779dd52 commit 414ff21

File tree

3 files changed

+186
-176
lines changed

3 files changed

+186
-176
lines changed

rust/jormungandr-vote-tx/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ workspace = true
1313
[dependencies]
1414
catalyst-voting = { path = "../catalyst-voting" }
1515
anyhow = "1.0.89"
16+
proptest = { version = "1.5.0", optional = true }
1617

1718
[dev-dependencies]
1819
catalyst-voting = { path = "../catalyst-voting", features = ["proptest-arbitrary"] }
@@ -25,4 +26,4 @@ test-strategy = "0.4.0"
2526
[features]
2627
# Feature which makes publicly available
2728
# `proptest::prelude::Arbitrary` trait implementation for some types
28-
proptest-arbitrary = ["catalyst-voting/proptest-arbitrary"]
29+
proptest-arbitrary = ["catalyst-voting/proptest-arbitrary", "dep:proptest"]

rust/jormungandr-vote-tx/src/decoding.rs

Lines changed: 75 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -214,129 +214,78 @@ impl Tx {
214214
}
215215
}
216216

217-
// #[cfg(test)]
218-
// mod tests {
219-
// use proptest::prelude::{any, any_with, Arbitrary, BoxedStrategy, Strategy};
220-
// use test_strategy::proptest;
221-
222-
// use super::*;
223-
// use crate::crypto::ed25519::PrivateKey;
224-
225-
// impl Arbitrary for Tx {
226-
// type Parameters = ();
227-
// type Strategy = BoxedStrategy<Self>;
228-
229-
// fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
230-
// any::<(
231-
// [u8; 32],
232-
// u8,
233-
// VotePayload,
234-
// PrivateKey,
235-
// [u8; Signature::BYTES_SIZE],
236-
// )>()
237-
// .prop_map(
238-
// |(vote_plan_id, proposal_index, vote, sk, signature_bytes)| {
239-
// Tx {
240-
// vote_plan_id,
241-
// proposal_index,
242-
// vote,
243-
// public_key: sk.public_key(),
244-
// signature: Signature::from_bytes(&signature_bytes),
245-
// }
246-
// },
247-
// )
248-
// .boxed()
249-
// }
250-
// }
251-
252-
// impl Arbitrary for VotePayload {
253-
// type Parameters = ();
254-
// type Strategy = BoxedStrategy<Self>;
255-
256-
// fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
257-
// any::<bool>()
258-
// .prop_flat_map(|b| {
259-
// if b {
260-
// any::<u8>().prop_map(VotePayload::Public).boxed()
261-
// } else {
262-
// any::<(u8, u8)>()
263-
// .prop_flat_map(|(s1, s2)| {
264-
// any_with::<(EncryptedVote, VoterProof)>((s1.into(),
265-
// s2.into())) .prop_map(|(v, p)|
266-
// VotePayload::Private(v, p)) })
267-
// .boxed()
268-
// }
269-
// })
270-
// .boxed()
271-
// }
272-
// }
273-
274-
// #[proptest]
275-
// fn tx_to_bytes_from_bytes_test(t1: Tx) {
276-
// let bytes = t1.to_bytes();
277-
278-
// let mut reader = bytes.as_slice();
279-
280-
// let size = read_be_u32(&mut reader).unwrap();
281-
// assert_eq!(size as usize, bytes.len() - 4);
282-
283-
// let padding_tag = read_be_u8(&mut reader).unwrap();
284-
// assert_eq!(padding_tag, PADDING_TAG);
285-
286-
// let fragment_tag = read_be_u8(&mut reader).unwrap();
287-
// assert_eq!(fragment_tag, FRAGMENT_TAG);
288-
289-
// let vote_plan_id = read_array(&mut reader).unwrap();
290-
// assert_eq!(vote_plan_id, t1.vote_plan_id);
291-
292-
// let proposal_index = read_be_u8(&mut reader).unwrap();
293-
// assert_eq!(proposal_index, t1.proposal_index);
294-
295-
// let vote_tag = read_be_u8(&mut reader).unwrap();
296-
// assert!(vote_tag == PUBLIC_VOTE_TAG || vote_tag == PRIVATE_VOTE_TAG);
297-
// match vote_tag {
298-
// PUBLIC_VOTE_TAG => {
299-
// let vote = read_be_u8(&mut reader).unwrap();
300-
// assert_eq!(VotePayload::Public(vote), t1.vote);
301-
// },
302-
// PRIVATE_VOTE_TAG => {
303-
// let size = read_be_u8(&mut reader).unwrap();
304-
// let vote = EncryptedVote::from_bytes(&mut reader,
305-
// size.into()).unwrap(); let size = read_be_u8(&mut reader).unwrap();
306-
// let proof = VoterProof::from_bytes(&mut reader, size.into()).unwrap();
307-
// assert_eq!(VotePayload::Private(vote, proof), t1.vote);
308-
// },
309-
// _ => {},
310-
// }
311-
312-
// let block_date = read_be_u64(&mut reader).unwrap();
313-
// assert_eq!(block_date, 0);
314-
315-
// let inputs_amount = read_be_u8(&mut reader).unwrap();
316-
// assert_eq!(inputs_amount, NUMBER_OF_INPUTS);
317-
318-
// let outputs_amount = read_be_u8(&mut reader).unwrap();
319-
// assert_eq!(outputs_amount, NUMBER_OF_OUTPUTS);
320-
321-
// let input_tag = read_be_u8(&mut reader).unwrap();
322-
// assert_eq!(input_tag, INPUT_TAG);
323-
324-
// let value = read_be_u64(&mut reader).unwrap();
325-
// assert_eq!(value, 0);
326-
327-
// let public_key = read_array(&mut reader).unwrap();
328-
// assert_eq!(PublicKey::from_bytes(&public_key).unwrap(), t1.public_key);
329-
330-
// let witness_tag = read_be_u8(&mut reader).unwrap();
331-
// assert_eq!(witness_tag, WITNESS_TAG);
332-
333-
// let nonce = read_be_u32(&mut reader).unwrap();
334-
// assert_eq!(nonce, 0);
335-
336-
// let signature = read_array(&mut reader).unwrap();
337-
// assert_eq!(Signature::from_bytes(&signature), t1.signature);
338-
339-
// let t2 = Tx::from_bytes(&mut bytes.as_slice()).unwrap();
340-
// assert_eq!(t1, t2);
341-
// }
342-
// }
217+
#[cfg(test)]
218+
mod tests {
219+
use test_strategy::proptest;
220+
221+
use super::*;
222+
223+
#[proptest]
224+
fn tx_to_bytes_from_bytes_test(t1: Tx) {
225+
let bytes = t1.to_bytes();
226+
227+
let mut reader = bytes.as_slice();
228+
229+
let size = read_be_u32(&mut reader).unwrap();
230+
assert_eq!(size as usize, bytes.len() - 4);
231+
232+
let padding_tag = read_be_u8(&mut reader).unwrap();
233+
assert_eq!(padding_tag, PADDING_TAG);
234+
235+
let fragment_tag = read_be_u8(&mut reader).unwrap();
236+
assert_eq!(fragment_tag, FRAGMENT_TAG);
237+
238+
let vote_plan_id = read_array(&mut reader).unwrap();
239+
assert_eq!(vote_plan_id, t1.vote_plan_id);
240+
241+
let proposal_index = read_be_u8(&mut reader).unwrap();
242+
assert_eq!(proposal_index, t1.proposal_index);
243+
244+
let vote_tag = read_be_u8(&mut reader).unwrap();
245+
assert!(vote_tag == PUBLIC_VOTE_TAG || vote_tag == PRIVATE_VOTE_TAG);
246+
match vote_tag {
247+
PUBLIC_VOTE_TAG => {
248+
let vote = read_be_u8(&mut reader).unwrap();
249+
assert_eq!(VotePayload::Public(vote), t1.vote);
250+
},
251+
PRIVATE_VOTE_TAG => {
252+
let size = read_be_u8(&mut reader).unwrap();
253+
let vote = EncryptedVote::from_bytes(&mut reader, size.into()).unwrap();
254+
let size = read_be_u8(&mut reader).unwrap();
255+
let proof = VoterProof::from_bytes(&mut reader, size.into()).unwrap();
256+
assert_eq!(VotePayload::Private(vote, proof), t1.vote);
257+
},
258+
_ => {},
259+
}
260+
261+
let block_date = read_be_u64(&mut reader).unwrap();
262+
assert_eq!(block_date, 0);
263+
264+
let inputs_amount = read_be_u8(&mut reader).unwrap();
265+
assert_eq!(inputs_amount, NUMBER_OF_INPUTS);
266+
267+
let outputs_amount = read_be_u8(&mut reader).unwrap();
268+
assert_eq!(outputs_amount, NUMBER_OF_OUTPUTS);
269+
270+
let input_tag = read_be_u8(&mut reader).unwrap();
271+
assert_eq!(input_tag, INPUT_TAG);
272+
273+
let value = read_be_u64(&mut reader).unwrap();
274+
assert_eq!(value, 0);
275+
276+
let public_key = read_array(&mut reader).unwrap();
277+
assert_eq!(PublicKey::from_bytes(&public_key).unwrap(), t1.public_key);
278+
279+
let witness_tag = read_be_u8(&mut reader).unwrap();
280+
assert_eq!(witness_tag, WITNESS_TAG);
281+
282+
let nonce = read_be_u32(&mut reader).unwrap();
283+
assert_eq!(nonce, 0);
284+
285+
let signature = read_array(&mut reader).unwrap();
286+
assert_eq!(Signature::from_bytes(&signature), t1.signature);
287+
288+
let t2 = Tx::from_bytes(&mut bytes.as_slice()).unwrap();
289+
assert_eq!(t1, t2);
290+
}
291+
}

rust/jormungandr-vote-tx/src/lib.rs

Lines changed: 109 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//! A Jörmungandr transaction object structured following this [spec](https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/catalyst_voting/jorm/)
1+
//! A Jörmungandr transaction object structured following this
2+
//! [spec](https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/catalyst_voting/jorm/)
23
//!
34
//! ```rust
45
//! use catalyst_voting::{
@@ -294,51 +295,110 @@ impl VotePayload {
294295
}
295296
}
296297

297-
// #[cfg(test)]
298-
// mod tests {
299-
// use test_strategy::proptest;
300-
301-
// use super::*;
302-
// use crate::{crypto::ed25519::PrivateKey,
303-
// vote_protocol::committee::ElectionSecretKey};
304-
305-
// #[proptest]
306-
// fn tx_test(
307-
// vote_plan_id: [u8; 32], proposal_index: u8, #[strategy(1u8..5)] voting_options:
308-
// u8, #[strategy(0..#voting_options)] choice: u8, users_private_key: PrivateKey,
309-
// election_secret_key: ElectionSecretKey,
310-
// ) {
311-
// let election_public_key = election_secret_key.public_key();
312-
313-
// let tx = Tx::new_public(
314-
// vote_plan_id,
315-
// proposal_index,
316-
// voting_options,
317-
// choice,
318-
// &users_private_key,
319-
// )
320-
// .unwrap();
321-
// assert!(tx.is_public());
322-
// assert!(!tx.is_private());
323-
// tx.verify_signature().unwrap();
324-
// tx.verify_proof(&election_public_key).unwrap();
325-
// assert_eq!(tx.public_choice().unwrap(), choice);
326-
// assert!(tx.private_choice(&election_secret_key).is_err());
327-
328-
// let tx = Tx::new_private_with_default_rng(
329-
// vote_plan_id,
330-
// proposal_index,
331-
// voting_options,
332-
// choice,
333-
// &election_public_key,
334-
// &users_private_key,
335-
// )
336-
// .unwrap();
337-
// assert!(!tx.is_public());
338-
// assert!(tx.is_private());
339-
// tx.verify_signature().unwrap();
340-
// tx.verify_proof(&election_public_key).unwrap();
341-
// assert_eq!(tx.private_choice(&election_secret_key).unwrap(), choice);
342-
// assert!(tx.public_choice().is_err());
343-
// }
344-
// }
298+
#[cfg(any(test, feature = "proptest-arbitrary"))]
299+
#[allow(missing_docs, clippy::missing_docs_in_private_items)]
300+
mod arbitrary_impl {
301+
use catalyst_voting::crypto::ed25519::PrivateKey;
302+
use proptest::prelude::{any, any_with, Arbitrary, BoxedStrategy, Strategy};
303+
304+
use super::{EncryptedVote, Signature, Tx, VotePayload, VoterProof};
305+
306+
impl Arbitrary for Tx {
307+
type Parameters = ();
308+
type Strategy = BoxedStrategy<Self>;
309+
310+
fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
311+
any::<(
312+
[u8; 32],
313+
u8,
314+
VotePayload,
315+
PrivateKey,
316+
[u8; Signature::BYTES_SIZE],
317+
)>()
318+
.prop_map(
319+
|(vote_plan_id, proposal_index, vote, sk, signature_bytes)| {
320+
Tx {
321+
vote_plan_id,
322+
proposal_index,
323+
vote,
324+
public_key: sk.public_key(),
325+
signature: Signature::from_bytes(&signature_bytes),
326+
}
327+
},
328+
)
329+
.boxed()
330+
}
331+
}
332+
333+
impl Arbitrary for VotePayload {
334+
type Parameters = ();
335+
type Strategy = BoxedStrategy<Self>;
336+
337+
fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
338+
any::<bool>()
339+
.prop_flat_map(|b| {
340+
if b {
341+
any::<u8>().prop_map(VotePayload::Public).boxed()
342+
} else {
343+
any::<(u8, u8)>()
344+
.prop_flat_map(|(s1, s2)| {
345+
any_with::<(EncryptedVote, VoterProof)>((s1.into(), s2.into()))
346+
.prop_map(|(v, p)| VotePayload::Private(v, p))
347+
})
348+
.boxed()
349+
}
350+
})
351+
.boxed()
352+
}
353+
}
354+
}
355+
356+
#[cfg(test)]
357+
mod tests {
358+
use catalyst_voting::{
359+
crypto::ed25519::PrivateKey, vote_protocol::committee::ElectionSecretKey,
360+
};
361+
use test_strategy::proptest;
362+
363+
use super::*;
364+
365+
#[proptest]
366+
fn tx_test(
367+
vote_plan_id: [u8; 32], proposal_index: u8, #[strategy(1u8..5)] voting_options: u8,
368+
#[strategy(0..#voting_options)] choice: u8, users_private_key: PrivateKey,
369+
election_secret_key: ElectionSecretKey,
370+
) {
371+
let election_public_key = election_secret_key.public_key();
372+
373+
let tx = Tx::new_public(
374+
vote_plan_id,
375+
proposal_index,
376+
voting_options,
377+
choice,
378+
&users_private_key,
379+
)
380+
.unwrap();
381+
assert!(tx.is_public());
382+
assert!(!tx.is_private());
383+
tx.verify_signature().unwrap();
384+
tx.verify_proof(&election_public_key).unwrap();
385+
assert_eq!(tx.public_choice().unwrap(), choice);
386+
assert!(tx.private_choice(&election_secret_key).is_err());
387+
388+
let tx = Tx::new_private_with_default_rng(
389+
vote_plan_id,
390+
proposal_index,
391+
voting_options,
392+
choice,
393+
&election_public_key,
394+
&users_private_key,
395+
)
396+
.unwrap();
397+
assert!(!tx.is_public());
398+
assert!(tx.is_private());
399+
tx.verify_signature().unwrap();
400+
tx.verify_proof(&election_public_key).unwrap();
401+
assert_eq!(tx.private_choice(&election_secret_key).unwrap(), choice);
402+
assert!(tx.public_choice().is_err());
403+
}
404+
}

0 commit comments

Comments
 (0)