Skip to content

Commit

Permalink
Add annotation impl
Browse files Browse the repository at this point in the history
  • Loading branch information
jaehunkim committed Jul 12, 2024
1 parent a365d0d commit 78a4298
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/channel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
use ark_ff::Field;

#[derive(Default)]
struct Annotation {
scope: Vec<String>,
annotations: Vec<String>,
annotations_enabled: bool,
extra_annotations_enabled: bool,
prover_to_verifier_bytes: usize,
expected_annotations: Option<Vec<String>>,
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;
Expand All @@ -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<String>);

fn get_annotations(&self) -> &Vec<String>;
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)
}
}

0 comments on commit 78a4298

Please sign in to comment.