diff --git a/src/channel/mod.rs b/src/channel/mod.rs index df11db1..4f3e007 100644 --- a/src/channel/mod.rs +++ b/src/channel/mod.rs @@ -1,5 +1,50 @@ use ark_ff::Field; +#[derive(Default)] +struct Annotation { + scope: Vec, + annotations: Vec, + annotations_enabled: bool, + extra_annotations_enabled: bool, + prover_to_verifier_bytes: usize, + expected_annotations: Option>, + annotation_prefix: String, +} + +impl Annotation { + /// Call this function every time that the annotation scope is updated to recalculate the prefix to + /// be added to annotations. It takes all annotation scopes in the annotation_scope_ vector and + /// concatenates them with "/" delimiters. + fn update_annotation_prefix(&mut self) { + self.annotation_prefix = self + .scope + .iter() + .fold(String::new(), |acc, s| acc + "/" + s) + + ": "; + } + + fn add_annotation(&mut self, annotation: String) { + if self.annotations_enabled { + self.annotations.push(annotation); + } + } + + fn annotate_prover_to_verifier(&mut self, annotation: String, n_bytes: usize) { + let start = self.prover_to_verifier_bytes; + self.prover_to_verifier_bytes += n_bytes; + let end = self.prover_to_verifier_bytes; + + self.add_annotation(format!( + "P->V[{}:{}]: {}{}\n", + start, end, self.annotation_prefix, annotation + )); + } + + fn annotate_verifier_to_prover(&mut self, annotation: String) { + self.add_annotation(format!("V->P: {}{}\n", self.annotation_prefix, annotation)); + } +} + #[allow(dead_code)] trait Channel { type Field: Field; @@ -11,4 +56,26 @@ trait Channel { fn random_number(&mut self, bound: u64) -> u64; fn random_field(&mut self) -> Self::Field; + + fn apply_proof_of_work(&mut self, security_bits: usize); + + fn begin_query_phase(&mut self); + + fn enter_annotation_scope(&mut self, scope: String); + fn exit_annotation_scope(&mut self); + fn disable_annotations(&mut self); + fn disable_extra_annotations(&mut self); + fn extra_annotations_disabled(&self) -> bool; + fn set_expected_annotations(&mut self, expected_annotations: Vec); + + fn get_annotations(&self) -> &Vec; + fn get_annotations_mut(&mut self) -> &mut Annotation; + + fn annotate_prover_to_verifier(&mut self, annotation: String, n_bytes: usize) { + self.get_annotations_mut().annotate_prover_to_verifier(annotation, n_bytes); + } + + fn annotate_verifier_to_prover(&mut self, annotation: String) { + self.get_annotations_mut().annotate_verifier_to_prover(annotation) + } }