|
| 1 | +use alloc::collections::BTreeSet; |
| 2 | + |
| 3 | +use core::marker::PhantomData; |
| 4 | + |
| 5 | +use manul::{ |
| 6 | + combinators::extend::{Extendable, Extension}, |
| 7 | + dev::{run_sync, BinaryFormat, TestSessionParams, TestSigner, TestVerifier}, |
| 8 | + protocol::{LocalError, PartyId, StaticRound}, |
| 9 | + signature::Keypair, |
| 10 | +}; |
| 11 | +use rand_core::{CryptoRngCore, OsRng}; |
| 12 | +use test_log::test; |
| 13 | + |
| 14 | +use crate::simple::{Round1, Round1Message, Round2, Round2Message, SimpleProtocolEntryPoint}; |
| 15 | + |
| 16 | +#[derive(Debug, Clone)] |
| 17 | +struct Round1InvalidDirectMessage<Id>(PhantomData<Id>); |
| 18 | + |
| 19 | +impl<Id> Extension<Id> for Round1InvalidDirectMessage<Id> |
| 20 | +where |
| 21 | + Id: PartyId, |
| 22 | +{ |
| 23 | + type Round = Round1<Id>; |
| 24 | + |
| 25 | + fn extend_direct_message( |
| 26 | + &self, |
| 27 | + _rng: &mut dyn CryptoRngCore, |
| 28 | + round: &Self::Round, |
| 29 | + _destination: &Id, |
| 30 | + ) -> Result< |
| 31 | + Option<( |
| 32 | + <Self::Round as StaticRound<Id>>::DirectMessage, |
| 33 | + <Self::Round as StaticRound<Id>>::Artifact, |
| 34 | + )>, |
| 35 | + LocalError, |
| 36 | + > { |
| 37 | + Ok(Some(( |
| 38 | + Round1Message { |
| 39 | + my_position: round.context.ids_to_positions[&round.context.id], |
| 40 | + your_position: round.context.ids_to_positions[&round.context.id], |
| 41 | + }, |
| 42 | + (), |
| 43 | + ))) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[test] |
| 48 | +fn round1_attributable_failure() { |
| 49 | + let signers = (0..3).map(TestSigner::new).collect::<Vec<_>>(); |
| 50 | + let all_ids = signers |
| 51 | + .iter() |
| 52 | + .map(|signer| signer.verifying_key()) |
| 53 | + .collect::<BTreeSet<_>>(); |
| 54 | + |
| 55 | + let entry_points = signers |
| 56 | + .iter() |
| 57 | + .enumerate() |
| 58 | + .map(|(idx, signer)| { |
| 59 | + let entry_point = SimpleProtocolEntryPoint::new(all_ids.clone()); |
| 60 | + let mut entry_point = Extendable::new(entry_point); |
| 61 | + if idx == 0 { |
| 62 | + entry_point.extend(Round1InvalidDirectMessage::<TestVerifier>(PhantomData)); |
| 63 | + } |
| 64 | + |
| 65 | + (*signer, entry_point) |
| 66 | + }) |
| 67 | + .collect::<Vec<_>>(); |
| 68 | + |
| 69 | + let mut reports = run_sync::<_, TestSessionParams<BinaryFormat>>(&mut OsRng, entry_points) |
| 70 | + .unwrap() |
| 71 | + .reports; |
| 72 | + |
| 73 | + let v0 = signers[0].verifying_key(); |
| 74 | + let v1 = signers[1].verifying_key(); |
| 75 | + let v2 = signers[2].verifying_key(); |
| 76 | + |
| 77 | + let _report0 = reports.remove(&v0).unwrap(); |
| 78 | + let report1 = reports.remove(&v1).unwrap(); |
| 79 | + let report2 = reports.remove(&v2).unwrap(); |
| 80 | + |
| 81 | + assert!(report1.provable_errors[&v0].verify(&()).is_ok()); |
| 82 | + assert!(report2.provable_errors[&v0].verify(&()).is_ok()); |
| 83 | +} |
| 84 | + |
| 85 | +#[derive(Debug, Clone)] |
| 86 | +struct Round2InvalidDirectMessage<Id>(PhantomData<Id>); |
| 87 | + |
| 88 | +impl<Id> Extension<Id> for Round2InvalidDirectMessage<Id> |
| 89 | +where |
| 90 | + Id: PartyId, |
| 91 | +{ |
| 92 | + type Round = Round2<Id>; |
| 93 | + |
| 94 | + fn extend_direct_message( |
| 95 | + &self, |
| 96 | + _rng: &mut dyn CryptoRngCore, |
| 97 | + round: &Self::Round, |
| 98 | + _destination: &Id, |
| 99 | + ) -> Result< |
| 100 | + Option<( |
| 101 | + <Self::Round as StaticRound<Id>>::DirectMessage, |
| 102 | + <Self::Round as StaticRound<Id>>::Artifact, |
| 103 | + )>, |
| 104 | + LocalError, |
| 105 | + > { |
| 106 | + Ok(Some(( |
| 107 | + Round2Message { |
| 108 | + my_position: round.context.ids_to_positions[&round.context.id], |
| 109 | + your_position: round.context.ids_to_positions[&round.context.id], |
| 110 | + }, |
| 111 | + (), |
| 112 | + ))) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +#[test] |
| 117 | +fn round2_attributable_failure() { |
| 118 | + let signers = (0..3).map(TestSigner::new).collect::<Vec<_>>(); |
| 119 | + let all_ids = signers |
| 120 | + .iter() |
| 121 | + .map(|signer| signer.verifying_key()) |
| 122 | + .collect::<BTreeSet<_>>(); |
| 123 | + |
| 124 | + let entry_points = signers |
| 125 | + .iter() |
| 126 | + .enumerate() |
| 127 | + .map(|(idx, signer)| { |
| 128 | + let entry_point = SimpleProtocolEntryPoint::new(all_ids.clone()); |
| 129 | + let mut entry_point = Extendable::new(entry_point); |
| 130 | + if idx == 0 { |
| 131 | + entry_point.extend(Round2InvalidDirectMessage::<TestVerifier>(PhantomData)); |
| 132 | + } |
| 133 | + |
| 134 | + (*signer, entry_point) |
| 135 | + }) |
| 136 | + .collect::<Vec<_>>(); |
| 137 | + |
| 138 | + let mut reports = run_sync::<_, TestSessionParams<BinaryFormat>>(&mut OsRng, entry_points) |
| 139 | + .unwrap() |
| 140 | + .reports; |
| 141 | + |
| 142 | + let v0 = signers[0].verifying_key(); |
| 143 | + let v1 = signers[1].verifying_key(); |
| 144 | + let v2 = signers[2].verifying_key(); |
| 145 | + |
| 146 | + let _report0 = reports.remove(&v0).unwrap(); |
| 147 | + let report1 = reports.remove(&v1).unwrap(); |
| 148 | + let report2 = reports.remove(&v2).unwrap(); |
| 149 | + |
| 150 | + assert!(report1.provable_errors[&v0].verify(&()).is_ok()); |
| 151 | + assert!(report2.provable_errors[&v0].verify(&()).is_ok()); |
| 152 | +} |
0 commit comments