use super::*;
#[derive(Clone)]
pub struct UniversalSRS<N: Network> {
srs: Arc<OnceCell<varuna::UniversalSRS<N::PairingCurve>>>,
}
impl<N: Network> UniversalSRS<N> {
pub fn load() -> Result<Self> {
Ok(Self { srs: Arc::new(OnceCell::new()) })
}
pub fn to_circuit_key(
&self,
function_name: &str,
assignment: &circuit::Assignment<N::Field>,
) -> Result<(ProvingKey<N>, VerifyingKey<N>)> {
#[cfg(feature = "aleo-cli")]
let timer = std::time::Instant::now();
let (proving_key, verifying_key) = Varuna::<N>::circuit_setup(self, assignment)?;
#[cfg(feature = "aleo-cli")]
println!("{}", format!(" • Built '{function_name}' (in {} ms)", timer.elapsed().as_millis()).dimmed());
Ok((ProvingKey::new(Arc::new(proving_key)), VerifyingKey::new(Arc::new(verifying_key))))
}
}
impl<N: Network> FromBytes for UniversalSRS<N> {
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
Ok(Self { srs: Arc::new(OnceCell::with_value(FromBytes::read_le(&mut reader)?)) })
}
}
impl<N: Network> ToBytes for UniversalSRS<N> {
fn write_le<W: Write>(&self, writer: W) -> IoResult<()> {
self.deref().write_le(writer)
}
}
impl<N: Network> Deref for UniversalSRS<N> {
type Target = varuna::UniversalSRS<N::PairingCurve>;
#[allow(clippy::let_and_return)]
fn deref(&self) -> &Self::Target {
self.srs.get_or_init(|| {
#[cfg(feature = "aleo-cli")]
let timer = std::time::Instant::now();
let universal_srs = varuna::UniversalSRS::load().expect("Failed to load the universal SRS");
#[cfg(feature = "aleo-cli")]
println!("{}", format!(" • Loaded universal setup (in {} ms)", timer.elapsed().as_millis()).dimmed());
universal_srs
})
}
}