Skip to content

Commit

Permalink
Merge pull request tlspuffin#320 from tlspuffin/new-queries
Browse files Browse the repository at this point in the history
Preliminary work for new queries and precomputations
  • Loading branch information
LCBH authored Jul 3, 2024
2 parents 0cd39c8 + ecd330d commit b6e5a89
Show file tree
Hide file tree
Showing 29 changed files with 1,795 additions and 1,278 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions puffin/src/algebra/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ macro_rules! term {
(($agent:expr, $counter:expr) $(>$req_type:expr)?) => {{
use $crate::algebra::signature::Signature;
use $crate::algebra::Term;
use $crate::trace::Source;

let var = Signature::new_var($($req_type)?, $agent, None, $counter); // TODO: verify hat using here None is fine. Before a refactor it was: Some(TlsMessageType::Handshake(None))
let var = Signature::new_var($($req_type)?, Some(Source::Agent($agent)), None, $counter); // TODO: verify hat using here None is fine. Before a refactor it was: Some(TlsMessageType::Handshake(None))
Term::Variable(var)
}};

Expand All @@ -34,8 +35,9 @@ macro_rules! term {
(($agent:expr, $counter:expr) [$message_type:expr] $(>$req_type:expr)?) => {{
use $crate::algebra::signature::Signature;
use $crate::algebra::Term;
use $crate::trace::Source;

let var = Signature::new_var($($req_type)?, $agent, $message_type, $counter);
let var = Signature::new_var($($req_type)?, Some(Source::Agent($agent)), $message_type, $counter);
Term::Variable(var)
}};

Expand Down
161 changes: 132 additions & 29 deletions puffin/src/algebra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ use once_cell::sync::OnceCell;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

pub use self::term::*;
use crate::{
algebra::signature::Signature,
error::Error,
protocol::{MessageResult, OpaqueProtocolMessage, ProtocolMessage},
};
use crate::algebra::signature::Signature;

pub mod atoms;
pub mod dynamic_function;
Expand Down Expand Up @@ -102,14 +98,6 @@ impl Matcher for AnyMatcher {
}
}

impl<M: ProtocolMessage<O>, O: OpaqueProtocolMessage> TryFrom<&MessageResult<M, O>> for AnyMatcher {
type Error = Error;

fn try_from(_: &MessageResult<M, O>) -> Result<Self, Self::Error> {
Ok(AnyMatcher)
}
}

#[cfg(test)]
#[allow(clippy::ptr_arg)]
pub mod test_signature {
Expand All @@ -127,12 +115,13 @@ pub mod test_signature {
define_signature,
error::Error,
protocol::{
OpaqueProtocolMessage, ProtocolBehavior, ProtocolMessage, ProtocolMessageDeframer,
ExtractKnowledge, OpaqueProtocolMessage, OpaqueProtocolMessageFlight, ProtocolBehavior,
ProtocolMessage, ProtocolMessageDeframer, ProtocolMessageFlight,
},
put::{Put, PutName},
put_registry::{Factory, PutKind},
term,
trace::{Action, InputAction, Step, Trace, TraceContext},
trace::{Action, InputAction, Knowledge, Source, Step, Trace, TraceContext},
variable_data::VariableData,
VERSION_STR,
};
Expand Down Expand Up @@ -427,12 +416,19 @@ pub mod test_signature {
}
}

impl OpaqueProtocolMessage for TestOpaqueMessage {
impl OpaqueProtocolMessage<AnyMatcher> for TestOpaqueMessage {
fn debug(&self, _info: &str) {
panic!("Not implemented for test stub");
}
}

fn extract_knowledge(&self) -> Result<Vec<Box<dyn VariableData>>, Error> {
impl ExtractKnowledge<AnyMatcher> for TestOpaqueMessage {
fn extract_knowledge(
&self,
_: &mut Vec<Knowledge<AnyMatcher>>,
_: Option<AnyMatcher>,
_: &Source,
) -> Result<(), Error> {
panic!("Not implemented for test stub");
}
}
Expand All @@ -451,23 +447,30 @@ pub mod test_signature {
}
}

impl ProtocolMessage<TestOpaqueMessage> for TestMessage {
impl ProtocolMessage<AnyMatcher, TestOpaqueMessage> for TestMessage {
fn create_opaque(&self) -> TestOpaqueMessage {
panic!("Not implemented for test stub");
}

fn debug(&self, _info: &str) {
panic!("Not implemented for test stub");
}
}

fn extract_knowledge(&self) -> Result<Vec<Box<dyn VariableData>>, Error> {
impl ExtractKnowledge<AnyMatcher> for TestMessage {
fn extract_knowledge(
&self,
_: &mut Vec<Knowledge<AnyMatcher>>,
_: Option<AnyMatcher>,
_: &Source,
) -> Result<(), Error> {
panic!("Not implemented for test stub");
}
}

pub struct TestMessageDeframer;

impl ProtocolMessageDeframer for TestMessageDeframer {
impl ProtocolMessageDeframer<AnyMatcher> for TestMessageDeframer {
type OpaqueProtocolMessage = TestOpaqueMessage;

fn pop_frame(&mut self) -> Option<TestOpaqueMessage> {
Expand All @@ -486,6 +489,100 @@ pub mod test_signature {
}
}

#[derive(Debug, Clone)]
pub struct TestMessageFlight;

impl ProtocolMessageFlight<AnyMatcher, TestMessage, TestOpaqueMessage, TestOpaqueMessageFlight>
for TestMessageFlight
{
fn new() -> Self {
Self {}
}

fn push(&mut self, _msg: TestMessage) {
panic!("Not implemented for test stub");
}

fn debug(&self, _info: &str) {
panic!("Not implemented for test stub");
}
}

impl TryFrom<TestOpaqueMessageFlight> for TestMessageFlight {
type Error = ();

fn try_from(_value: TestOpaqueMessageFlight) -> Result<Self, Self::Error> {
Ok(Self)
}
}

impl ExtractKnowledge<AnyMatcher> for TestMessageFlight {
fn extract_knowledge(
&self,
_: &mut Vec<Knowledge<AnyMatcher>>,
_: Option<AnyMatcher>,
_: &Source,
) -> Result<(), Error> {
panic!("Not implemented for test stub");
}
}

impl From<TestMessage> for TestMessageFlight {
fn from(_value: TestMessage) -> Self {
Self {}
}
}

#[derive(Debug, Clone)]
pub struct TestOpaqueMessageFlight;

impl OpaqueProtocolMessageFlight<AnyMatcher, TestOpaqueMessage> for TestOpaqueMessageFlight {
fn new() -> Self {
Self {}
}

fn push(&mut self, _msg: TestOpaqueMessage) {
panic!("Not implemented for test stub");
}

fn debug(&self, _info: &str) {
panic!("Not implemented for test stub");
}
}

impl ExtractKnowledge<AnyMatcher> for TestOpaqueMessageFlight {
fn extract_knowledge(
&self,
_: &mut Vec<Knowledge<AnyMatcher>>,
_: Option<AnyMatcher>,
_: &Source,
) -> Result<(), Error> {
panic!("Not implemented for test stub");
}
}

impl From<TestOpaqueMessage> for TestOpaqueMessageFlight {
fn from(_value: TestOpaqueMessage) -> Self {
Self {}
}
}

impl Codec for TestOpaqueMessageFlight {
fn encode(&self, _bytes: &mut Vec<u8>) {
panic!("Not implemented for test stub");
}

fn read(_: &mut Reader) -> Option<Self> {
panic!("Not implemented for test stub");
}
}

impl From<TestMessageFlight> for TestOpaqueMessageFlight {
fn from(_value: TestMessageFlight) -> Self {
Self {}
}
}

#[derive(Debug, PartialEq)]
pub struct TestProtocolBehavior;

Expand All @@ -495,6 +592,8 @@ pub mod test_signature {
type ProtocolMessage = TestMessage;
type OpaqueProtocolMessage = TestOpaqueMessage;
type Matcher = AnyMatcher;
type ProtocolMessageFlight = TestMessageFlight;
type OpaqueProtocolMessageFlight = TestOpaqueMessageFlight;

fn signature() -> &'static Signature {
panic!("Not implemented for test stub");
Expand Down Expand Up @@ -557,7 +656,7 @@ mod tests {
put::PutOptions,
put_registry::{Factory, PutRegistry},
term,
trace::{Knowledge, TraceContext},
trace::{Knowledge, Source, TraceContext},
};

#[allow(dead_code)]
Expand Down Expand Up @@ -616,8 +715,12 @@ mod tests {

//println!("TypeId of vec array {:?}", data.type_id());

let variable: Variable<AnyMatcher> =
Signature::new_var(TypeShape::of::<Vec<u8>>(), AgentName::first(), None, 0);
let variable: Variable<AnyMatcher> = Signature::new_var(
TypeShape::of::<Vec<u8>>(),
Some(Source::Agent(AgentName::first())),
None,
0,
);

let generated_term = Term::Application(
hmac256,
Expand All @@ -636,8 +739,8 @@ mod tests {
let put_registry =
PutRegistry::<TestProtocolBehavior>::new([("teststub", dummy_factory())], "teststub");
let mut context = TraceContext::new(&put_registry, PutOptions::default());
context.add_knowledge(Knowledge {
agent_name: AgentName::first(),
context.knowledge_store.add_knowledge(Knowledge {
source: Source::Agent(AgentName::first()),
matcher: None,
data: Box::new(data),
});
Expand Down Expand Up @@ -681,15 +784,15 @@ mod tests {
Term::Application(Signature::new_function(&example_op_c), vec![]),
Term::Variable(
Signature::new_var_with_type::<SessionID, AnyMatcher>(
AgentName::first(),
Some(Source::Agent(AgentName::first())),
None,
0,
),
),
],
),
Term::Variable(Signature::new_var_with_type::<SessionID, AnyMatcher>(
AgentName::first(),
Some(Source::Agent(AgentName::first())),
None,
0,
)),
Expand All @@ -702,15 +805,15 @@ mod tests {
Signature::new_function(&example_op_c),
vec![
Term::Variable(Signature::new_var_with_type::<SessionID, _>(
AgentName::first(),
Some(Source::Agent(AgentName::first())),
None,
0,
)),
Term::Application(Signature::new_function(&example_op_c), vec![]),
],
),
Term::Variable(Signature::new_var_with_type::<SessionID, _>(
AgentName::first(),
Some(Source::Agent(AgentName::first())),
None,
0,
)),
Expand Down
11 changes: 5 additions & 6 deletions puffin/src/algebra/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ use once_cell::sync::Lazy;

use super::atoms::Function;
use crate::{
agent::AgentName,
algebra::{
atoms::Variable,
dynamic_function::{
make_dynamic, DescribableFunction, DynamicFunction, DynamicFunctionShape, TypeShape,
},
Matcher,
},
trace::Query,
trace::{Query, Source},
};

pub type FunctionDefinition = (DynamicFunctionShape, Box<dyn DynamicFunction>);
Expand Down Expand Up @@ -87,22 +86,22 @@ impl Signature {
}

pub fn new_var_with_type<T: 'static, M: Matcher>(
agent_name: AgentName,
source: Option<Source>,
matcher: Option<M>,
counter: u16,
) -> Variable<M> {
let type_shape = TypeShape::of::<T>();
Self::new_var(type_shape, agent_name, matcher, counter)
Self::new_var(type_shape, source, matcher, counter)
}

pub fn new_var<M: Matcher>(
type_shape: TypeShape,
agent_name: AgentName,
source: Option<Source>,
matcher: Option<M>,
counter: u16,
) -> Variable<M> {
let query = Query {
agent_name,
source,
matcher,
counter,
};
Expand Down
10 changes: 8 additions & 2 deletions puffin/src/algebra/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
algebra::{dynamic_function::TypeShape, error::FnError, Matcher},
error::Error,
protocol::ProtocolBehavior,
trace::TraceContext,
trace::{Source, TraceContext},
};

/// A first-order term: either a [`Variable`] or an application of an [`Function`].
Expand Down Expand Up @@ -114,7 +114,13 @@ impl<M: Matcher> Term<M> {
Term::Variable(variable) => context
.find_variable(variable.typ, &variable.query)
.map(|data| data.boxed_any())
.or_else(|| context.find_claim(variable.query.agent_name, variable.typ))
.or_else(|| {
if let Some(Source::Agent(agent_name)) = variable.query.source {
context.find_claim(agent_name, variable.typ)
} else {
todo!("Implement querying by label");
}
})
.ok_or_else(|| Error::Term(format!("Unable to find variable {}!", variable))),
Term::Application(func, args) => {
let mut dynamic_args: Vec<Box<dyn Any>> = Vec::new();
Expand Down
Loading

0 comments on commit b6e5a89

Please sign in to comment.