1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::cell::{Ref, RefCell, RefMut};
use std::fmt::Debug;
use std::marker::PhantomData;
use std::ops::Deref;
use std::rc::Rc;
use std::slice::Iter;

use itertools::Itertools;

use crate::agent::AgentName;
use crate::algebra::dynamic_function::TypeShape;
use crate::protocol::{EvaluatedTerm, ProtocolTypes};
use crate::variable_data::VariableData;

pub trait Claim<PT: ProtocolTypes>: EvaluatedTerm<PT> + VariableData<PT> + Debug {
    fn agent_name(&self) -> AgentName;
    fn id(&self) -> TypeShape<PT>;
    fn inner(&self) -> Box<dyn EvaluatedTerm<PT>>;
}

pub trait SecurityViolationPolicy<PT: ProtocolTypes, C: Claim<PT>> {
    fn check_violation(claims: &[C]) -> Option<&'static str>;
}

#[derive(Default, Clone, Debug, PartialEq)]
pub struct ClaimList<PT: ProtocolTypes, C: Claim<PT>> {
    claims: Vec<C>,
    phantom: PhantomData<PT>,
}

impl<PT: ProtocolTypes, C: Claim<PT>> ClaimList<PT, C> {
    pub fn iter(&self) -> Iter<'_, C> {
        self.claims.iter()
    }

    /// finds the last claim matching `type`
    pub fn find_last_claim_by_type<T: 'static>(&self, agent_name: AgentName) -> Option<&C> {
        self.find_last_claim(agent_name, TypeShape::<PT>::of::<T>())
    }

    pub fn find_last_claim(&self, agent_name: AgentName, shape: TypeShape<PT>) -> Option<&C> {
        self.claims
            .iter()
            .rev()
            .find(|claim| claim.id() == shape && claim.agent_name() == agent_name)
    }

    pub fn slice(&self) -> &[C] {
        &self.claims
    }
}

impl<PT: ProtocolTypes, C: Claim<PT>> ClaimList<PT, C> {
    pub fn log(&self) {
        // TODO: skip logging completely during fuzzing -> more performance
        log::debug!(
            "New Claims: {}",
            &self
                .claims
                .iter()
                .map(|claim| claim.type_name().to_string())
                .join(", ")
        );
        for claim in &self.claims {
            log::trace!("{:?}", claim);
        }
    }
}

impl<PT: ProtocolTypes, C: Claim<PT>> From<Vec<C>> for ClaimList<PT, C> {
    fn from(claims: Vec<C>) -> Self {
        Self {
            claims,
            phantom: PhantomData,
        }
    }
}

impl<PT: ProtocolTypes, C: Claim<PT>> ClaimList<PT, C> {
    pub const fn new() -> Self {
        Self {
            claims: vec![],
            phantom: PhantomData,
        }
    }

    pub fn claim_sized(&mut self, claim: C) {
        self.claims.push(claim);
    }
}

#[derive(Default, Clone, PartialEq, Debug)]
pub struct GlobalClaimList<PT: ProtocolTypes, C: Claim<PT>> {
    claims: Rc<RefCell<ClaimList<PT, C>>>,
}

impl<PT: ProtocolTypes, C: Claim<PT>> GlobalClaimList<PT, C> {
    pub fn new() -> Self {
        Self {
            claims: Rc::new(RefCell::new(ClaimList::new())),
        }
    }

    pub fn deref_borrow(&self) -> Ref<'_, ClaimList<PT, C>> {
        self.claims.deref().borrow()
    }

    pub fn deref_borrow_mut(&self) -> RefMut<'_, ClaimList<PT, C>> {
        self.claims.deref().borrow_mut()
    }
}