saffron/
env.rs

1use std::{fs::File, path::Path};
2
3use poly_commitment::{commitment::CommitmentCurve, ipa::SRS, precomputed_srs::TestSRS};
4use time::macros::format_description;
5use tracing::debug;
6use tracing_subscriber::{
7    fmt::{format::FmtSpan, time::UtcTime},
8    EnvFilter,
9};
10
11pub fn get_srs_from_cache<G: CommitmentCurve>(cache: String) -> SRS<G> {
12    debug!("Loading SRS from cache {}", cache);
13    let file_path = Path::new(&cache);
14    let file = File::open(file_path).expect("Error opening SRS cache file");
15    let srs: SRS<G> = {
16        // By convention, proof systems serializes a TestSRS with filename 'test_<CURVE_NAME>.srs'.
17        // The benefit of using this is you don't waste time verifying the SRS.
18        if file_path
19            .file_name()
20            .unwrap()
21            .to_str()
22            .unwrap()
23            .starts_with("test_")
24        {
25            let test_srs: TestSRS<G> = rmp_serde::from_read(&file).unwrap();
26            From::from(test_srs)
27        } else {
28            rmp_serde::from_read(&file).unwrap()
29        }
30    };
31    debug!("SRS loaded successfully from cache");
32    srs
33}
34
35pub fn init_console_subscriber() {
36    let timer = UtcTime::new(format_description!(
37        "[year]-[month]-[day]T[hour repr:24]:[minute]:[second].[subsecond digits:3]Z"
38    ));
39    tracing_subscriber::fmt()
40        .with_env_filter(EnvFilter::from_default_env())
41        .with_span_events(FmtSpan::CLOSE)
42        .with_timer(timer)
43        .with_target(true)
44        .with_thread_ids(false)
45        .with_line_number(false)
46        .with_file(false)
47        .with_level(true)
48        .with_ansi(true)
49        .with_writer(std::io::stdout)
50        .init();
51}
52
53#[cfg(test)]
54#[ctor::ctor]
55fn init_test_logging() {
56    init_console_subscriber();
57}