saffron/
env.rs

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