Skip to content

Commit

Permalink
refactor: Refactor PublicParams cache to remove obsolete std::sync::A…
Browse files Browse the repository at this point in the history
…rc usage

- reduced complexity by removing `std::sync::Arc` in `src/public_parameters/disk_cache.rs`.
- Modified `read` and `write` functions in `DiskCache` to work directly with `PublicParams`.
  • Loading branch information
huitseeker committed Jan 30, 2024
1 parent 34abc4d commit b48c847
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/public_parameters/disk_cache.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fs::create_dir_all;
use std::io::{BufReader, BufWriter, Read};
use std::marker::PhantomData;
use std::sync::Arc;

use abomonation::{encode, Abomonation};
use camino::{Utf8Path, Utf8PathBuf};
Expand Down Expand Up @@ -41,7 +40,7 @@ impl<F: CurveCycleEquipped, C: Coprocessor<F>> DiskCache<F, C> {
pub(crate) fn read<'a>(
&self,
instance: &Instance<F, C>,
) -> Result<Arc<PublicParams<F, C1LEM<'a, F, C>>>, Error> {
) -> Result<PublicParams<F, C1LEM<'a, F, C>>, Error> {
let file = instance.open(&self.dir)?;
let reader = BufReader::new(file);
bincode::deserialize_from(reader)
Expand All @@ -62,7 +61,7 @@ impl<F: CurveCycleEquipped, C: Coprocessor<F>> DiskCache<F, C> {
pub(crate) fn write(
&self,
instance: &Instance<F, C>,
data: &Arc<PublicParams<F, C1LEM<'_, F, C>>>,
data: &PublicParams<F, C1LEM<'_, F, C>>,
) -> Result<(), Error> {
let file = instance.create(&self.dir)?;
let writer = BufWriter::new(&file);
Expand Down
18 changes: 7 additions & 11 deletions src/public_parameters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use ::nova::{supernova::FlatAuxParams, traits::Engine, FlatPublicParams};
use abomonation::{decode, Abomonation};
use once_cell::sync::OnceCell;
use std::sync::Arc;
use tap::TapFallible;
use tracing::{info, warn};

Expand All @@ -20,13 +19,12 @@ use crate::public_parameters::instance::Instance;

pub fn public_params<'a, F: CurveCycleEquipped, C: Coprocessor<F> + 'static>(
instance: &Instance<F, C>,
) -> Result<Arc<PublicParams<F, C1LEM<'a, F, C>>>, Error>
) -> Result<PublicParams<F, C1LEM<'a, F, C>>, Error>
where
<<E1<F> as Engine>::Scalar as ff::PrimeField>::Repr: Abomonation,
<<E2<F> as Engine>::Scalar as ff::PrimeField>::Repr: Abomonation,
{
let default =
|instance: &Instance<F, C>| Arc::new(nova::public_params(instance.rc, instance.lang()));
let default = |instance: &Instance<F, C>| nova::public_params(instance.rc, instance.lang());

// subdirectory search
let disk_cache = DiskCache::new(public_params_dir()).unwrap();
Expand All @@ -44,23 +42,21 @@ where
assert!(rest.is_empty());
let pp =
PublicParams::from(NovaPublicParams::<F, C1LEM<'a, F, C>>::from(pp.clone())); // this clone is VERY expensive
Ok(Arc::new(pp))
Ok(pp)
}
Err(Error::IO(e)) => {
warn!("{e}");
info!("Generating fresh public parameters");
let pp = default(instance);
let pp = Arc::try_unwrap(pp).unwrap().pp;
let fp = FlatPublicParams::try_from(pp).unwrap();
let fp = FlatPublicParams::try_from(pp.pp).unwrap();
// maybe just directly write
disk_cache
.write_abomonated(instance, &fp)
.tap_ok(|_| info!("writing public params to disk-cache: {}", instance.key()))
.map_err(|e| Error::Cache(format!("Disk write error: {e}")))?;
Ok(Arc::new(PublicParams::from(NovaPublicParams::<
F,
C1LEM<'a, F, C>,
>::from(fp))))
Ok(PublicParams::from(
NovaPublicParams::<F, C1LEM<'a, F, C>>::from(fp),
))
}
_ => unreachable!(),
}
Expand Down

0 comments on commit b48c847

Please sign in to comment.