-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
189 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
use alloc::collections::{BTreeMap, BTreeSet}; | ||
use core::marker::PhantomData; | ||
|
||
use manul::{ | ||
combinators::misbehave::{Misbehaving, MisbehavingEntryPoint}, | ||
dev::{run_sync, BinaryFormat, TestSessionParams, TestSigner, TestVerifier}, | ||
protocol::{ | ||
BoxedRound, Deserializer, EchoBroadcast, EntryPoint, LocalError, NormalBroadcast, PartyId, ProtocolMessagePart, | ||
RoundId, Serializer, | ||
}, | ||
session::SessionReport, | ||
signature::Keypair, | ||
}; | ||
use rand_core::{CryptoRngCore, OsRng}; | ||
|
||
use super::{ | ||
key_init::{KeyInit, KeyInitProtocol, Round2EchoBroadcast, Round3, Round3Broadcast}, | ||
params::{SchemeParams, TestParams}, | ||
sigma::SchProof, | ||
}; | ||
use crate::{ | ||
curve::Scalar, | ||
tools::{bitvec::BitVec, Secret}, | ||
}; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
enum Behavior { | ||
R2RandomU, | ||
R3InvalidSchProof, | ||
} | ||
|
||
struct MaliciousKeyInitOverride<P>(PhantomData<P>); | ||
|
||
impl<P: SchemeParams, Id: PartyId> Misbehaving<Id, Behavior> for MaliciousKeyInitOverride<P> { | ||
type EntryPoint = KeyInit<P, Id>; | ||
|
||
fn modify_echo_broadcast( | ||
rng: &mut impl CryptoRngCore, | ||
round: &BoxedRound<Id, <Self::EntryPoint as EntryPoint<Id>>::Protocol>, | ||
behavior: &Behavior, | ||
serializer: &Serializer, | ||
deserializer: &Deserializer, | ||
echo_broadcast: EchoBroadcast, | ||
) -> Result<EchoBroadcast, LocalError> { | ||
if round.id() == RoundId::new(2) && behavior == &Behavior::R2RandomU { | ||
let orig_message = echo_broadcast | ||
.deserialize::<Round2EchoBroadcast<P>>(deserializer) | ||
.unwrap(); | ||
let mut data = orig_message.data; | ||
|
||
// Replace `u` with something other than we committed to when hashing it in Round 1. | ||
data.u = BitVec::random(rng, data.u.bits().len()); | ||
|
||
let message = Round2EchoBroadcast { data }; | ||
return EchoBroadcast::new(serializer, message); | ||
} | ||
|
||
Ok(echo_broadcast) | ||
} | ||
|
||
fn modify_normal_broadcast( | ||
rng: &mut impl CryptoRngCore, | ||
round: &BoxedRound<Id, <Self::EntryPoint as EntryPoint<Id>>::Protocol>, | ||
behavior: &Behavior, | ||
serializer: &Serializer, | ||
_deserializer: &Deserializer, | ||
normal_broadcast: NormalBroadcast, | ||
) -> Result<NormalBroadcast, LocalError> { | ||
if round.id() == RoundId::new(3) && behavior == &Behavior::R3InvalidSchProof { | ||
let round3 = round.downcast_ref::<Round3<P, Id>>()?; | ||
let context = &round3.context; | ||
let aux = (&context.sid_hash, &context.my_id, &round3.rho); | ||
|
||
// Make a proof for a random secret. This won't pass verification. | ||
let x = Secret::init_with(|| Scalar::random(rng)); | ||
let psi = SchProof::new( | ||
&context.tau, | ||
&x, | ||
&context.public_data.cap_a, | ||
&x.mul_by_generator(), | ||
&aux, | ||
); | ||
|
||
let message = Round3Broadcast { psi }; | ||
return NormalBroadcast::new(serializer, message); | ||
} | ||
|
||
Ok(normal_broadcast) | ||
} | ||
} | ||
|
||
type MaliciousKeyEP<P, Id> = MisbehavingEntryPoint<Id, Behavior, MaliciousKeyInitOverride<P>>; | ||
|
||
type Protocol = KeyInitProtocol<TestParams, TestVerifier>; | ||
type SP = TestSessionParams<BinaryFormat>; | ||
|
||
fn run_with_one_malicious_party( | ||
behavior: Behavior, | ||
) -> (Vec<TestVerifier>, BTreeMap<TestVerifier, SessionReport<Protocol, SP>>) { | ||
let signers = (0..3).map(TestSigner::new).collect::<Vec<_>>(); | ||
let ids = signers.iter().map(|signer| signer.verifying_key()).collect::<Vec<_>>(); | ||
let ids_set = BTreeSet::from_iter(ids.clone()); | ||
|
||
let entry_points = signers | ||
.into_iter() | ||
.map(|signer| { | ||
let id = signer.verifying_key(); | ||
let entry_point = KeyInit::<TestParams, TestVerifier>::new(ids_set.clone()).unwrap(); | ||
let behavior = if id == ids[0] { Some(behavior) } else { None }; | ||
let entry_point = MaliciousKeyEP::new(entry_point, behavior); | ||
(signer, entry_point) | ||
}) | ||
.collect(); | ||
|
||
let reports = run_sync::<_, SP>(&mut OsRng, entry_points).unwrap().reports; | ||
(ids, reports) | ||
} | ||
|
||
#[test] | ||
fn r2_hash_mismatch() { | ||
let (ids, mut reports) = run_with_one_malicious_party(Behavior::R2RandomU); | ||
|
||
let report0 = reports.remove(&ids[0]).unwrap(); | ||
let report1 = reports.remove(&ids[1]).unwrap(); | ||
let report2 = reports.remove(&ids[2]).unwrap(); | ||
|
||
assert!(report0.provable_errors.is_empty()); | ||
assert!(report1.provable_errors[&ids[0]].verify().is_ok()); | ||
assert!(report2.provable_errors[&ids[0]].verify().is_ok()); | ||
} | ||
|
||
#[test] | ||
fn r3_invalid_sch_proof() { | ||
let (ids, mut reports) = run_with_one_malicious_party(Behavior::R3InvalidSchProof); | ||
|
||
let report0 = reports.remove(&ids[0]).unwrap(); | ||
let report1 = reports.remove(&ids[1]).unwrap(); | ||
let report2 = reports.remove(&ids[2]).unwrap(); | ||
|
||
assert!(report0.provable_errors.is_empty()); | ||
assert!(report1.provable_errors[&ids[0]].verify().is_ok()); | ||
assert!(report2.provable_errors[&ids[0]].verify().is_ok()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters