diff --git a/src/ring.rs b/src/ring.rs index 73e6b5f..d8bb8ab 100644 --- a/src/ring.rs +++ b/src/ring.rs @@ -148,6 +148,8 @@ where pub domain_size: usize, } +const RING_DOMAIN_OVERHEAD: usize = 257; + impl RingContext where BaseField: ark_ff::PrimeField, @@ -164,16 +166,26 @@ where /// Construct a new random ring context suitable for the given ring size. pub fn new_random(ring_size: usize, rng: &mut R) -> Self { use fflonk::pcs::PCS; - const RING_DOMAIN_OVERHEAD: usize = 257; - let domain_size = 1 << ark_std::log2(ring_size + RING_DOMAIN_OVERHEAD); let pcs_params = Pcs::::setup(3 * domain_size, rng); + Self::from_srs(pcs_params, ring_size).expect("PCS params is correct") + } + + pub fn from_srs(mut pcs_params: PcsParams, ring_size: usize) -> Result { + let domain_size = 1 << ark_std::log2(ring_size + RING_DOMAIN_OVERHEAD); + if pcs_params.powers_in_g1.len() < 3 * domain_size + 1 || pcs_params.powers_in_g2.len() < 2 + { + return Err(()); + } + // Keep only the required powers of tau. + pcs_params.powers_in_g1.truncate(3 * domain_size + 1); + pcs_params.powers_in_g2.truncate(2); let piop_params = make_piop_params::(domain_size); - Self { + Ok(Self { pcs_params, piop_params, domain_size, - } + }) } /// The max ring size this context is able to manage. @@ -291,7 +303,7 @@ impl IntoSW for ark_ec::twisted_edwards::Affin } } -fn make_piop_params(domain_size: usize) -> PiopParams +pub(crate) fn make_piop_params(domain_size: usize) -> PiopParams where BaseField: ark_ff::PrimeField, CurveConfig: SWCurveConfig, diff --git a/src/suites/bandersnatch.rs b/src/suites/bandersnatch.rs index d44bdb8..ce53e17 100644 --- a/src/suites/bandersnatch.rs +++ b/src/suites/bandersnatch.rs @@ -89,6 +89,7 @@ pub mod weierstrass { use super::*; use crate::ring as ring_suite; + pub type PcsParams = ring_suite::PcsParams; pub type RingContext = ring_suite::RingContext; pub type VerifierKey = ring_suite::VerifierKey; pub type RingProver = ring_suite::RingProver; @@ -176,6 +177,7 @@ pub mod edwards { use super::*; use crate::ring as ring_suite; + pub type PcsParams = ring_suite::PcsParams; pub type RingContext = ring_suite::RingContext; pub type VerifierKey = ring_suite::VerifierKey; pub type RingProver = ring_suite::RingProver;