use std::collections::BTreeMap;
use super::{Presentation, append_verified_route, claim, digest, register_bound_metadata};
#[derive(Debug, Clone)]
pub struct LiveBound {
pub n: i64,
pub k: i64,
pub artifact_digest: String,
pub finding_id: String,
}
pub fn parse_bound(text: &str) -> Option<(i64, i64)> {
let mut rest = text;
while let Some(idx) = rest.find("a(") {
let after = &rest[idx + 2..];
if let Some(close) = after.find(')')
&& let Ok(n) = after[..close].trim().parse::<i64>()
{
let tail = &after[close + 1..];
if let Some(ge) = tail.find(">=") {
let kpart = tail[ge + 2..].trim_start();
let kdigits: String = kpart.chars().take_while(char::is_ascii_digit).collect();
if let Ok(k) = kdigits.parse::<i64>() {
return Some((n, k));
}
}
}
rest = &rest[idx + 2..];
}
None
}
pub fn collect_live_bounds(rows: &[(String, String, Option<String>)]) -> Vec<LiveBound> {
let mut best: BTreeMap<i64, LiveBound> = BTreeMap::new();
for (id, text, hash) in rows {
let Some((n, k)) = parse_bound(text) else {
continue;
};
let Some(h) = hash else {
continue; };
let cand = LiveBound {
n,
k,
artifact_digest: h.clone(),
finding_id: id.clone(),
};
best.entry(n)
.and_modify(|e| {
if k > e.k {
*e = cand.clone();
}
})
.or_insert(cand);
}
best.into_values().collect()
}
pub fn presentation_from_bounds(bounds: &[LiveBound]) -> Result<Presentation, String> {
let mut p = Presentation {
cell_ranks: Default::default(),
clauses: Vec::new(),
accepted_events: Vec::new(),
cell_metadata: Default::default(),
};
for b in bounds {
register_bound_metadata(&mut p, b.n, b.k)?;
let claim_digest = digest(&claim(b.n, b.k))?;
append_verified_route(
&mut p,
b.n,
b.k,
&b.artifact_digest,
&claim_digest,
&["verifier:vela-verify.sidon".to_string()],
&format!("acceptance:{}", b.finding_id),
)?;
}
Ok(p)
}
pub fn live_presentation(project: &crate::project::Project) -> Result<Presentation, String> {
let mut witness: BTreeMap<&str, &str> = BTreeMap::new();
for a in &project.artifacts {
if a.retracted {
continue;
}
for fid in &a.target_findings {
witness
.entry(fid.as_str())
.or_insert(a.content_hash.as_str());
}
}
let rows: Vec<(String, String, Option<String>)> = project
.findings
.iter()
.map(|f| {
(
f.id.clone(),
f.assertion.text.clone(),
witness.get(f.id.as_str()).map(|s| s.to_string()),
)
})
.collect();
presentation_from_bounds(&collect_live_bounds(&rows))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::sidon_profile::best_bounds;
use std::collections::BTreeSet;
#[test]
fn parses_bound_not_the_sequence_id_or_cube() {
assert_eq!(
parse_bound("OEIS A309370 a(9) >= 47: a Sidon set in {0,1}^9 ..."),
Some((9, 47))
);
assert_eq!(parse_bound("a(24) >= 7179: ..."), Some((24, 7179)));
assert_eq!(parse_bound("no bound here, A309370"), None);
}
#[test]
fn keeps_best_bound_per_n_and_needs_a_witness() {
let rows = vec![
("vf_a".into(), "a(7) >= 24".into(), Some("sha256:aa".into())),
("vf_b".into(), "a(7) >= 20".into(), Some("sha256:bb".into())), ("vf_c".into(), "a(9) >= 47".into(), Some("sha256:cc".into())),
("vf_d".into(), "a(11) >= 99".into(), None), ];
let bounds = collect_live_bounds(&rows);
assert_eq!(bounds.len(), 2); let n7 = bounds.iter().find(|b| b.n == 7).unwrap();
assert_eq!(n7.k, 24);
assert_eq!(n7.artifact_digest, "sha256:aa");
}
#[test]
fn presentation_routes_best_bounds_and_replays() {
let rows = vec![
("vf_a".into(), "a(7) >= 24".into(), Some("sha256:aa".into())),
("vf_c".into(), "a(9) >= 47".into(), Some("sha256:cc".into())),
];
let p = presentation_from_bounds(&collect_live_bounds(&rows)).unwrap();
let bounds = best_bounds(&p, &BTreeSet::new()).unwrap();
let by_n: std::collections::BTreeMap<i64, i64> = bounds
.iter()
.map(|r| {
(
r["n"].as_i64().unwrap(),
r["best_lower_bound"].as_i64().unwrap(),
)
})
.collect();
assert_eq!(by_n.get(&7), Some(&24));
assert_eq!(by_n.get(&9), Some(&47));
assert!(p.presentation_root().unwrap().starts_with("vpr_"));
}
}