Skip to main content

dory_pcs/backends/arkworks/
ark_setup.rs

1//! Arkworks-specific setup wrappers
2//!
3//! Provides wrapper types for `ProverSetup` and `VerifierSetup` with transparent access.
4//! Serialization implementations via `CanonicalSerialize` and `CanonicalDeserialize`
5//! are in the `ark_serde` module.
6
7use crate::setup::{ProverSetup, VerifierSetup};
8use std::ops::{Deref, DerefMut};
9
10use super::BN254;
11
12/// Wrapper around `ProverSetup<BN254>` with arkworks canonical serialization
13///
14/// Provides transparent access to the inner setup while adding support for
15/// arkworks' CanonicalSerialize and CanonicalDeserialize traits, allowing
16/// easy serialization for users of the arkworks ecosystem.
17#[derive(Clone, Debug)]
18#[repr(transparent)]
19pub struct ArkworksProverSetup(pub ProverSetup<BN254>);
20
21/// Wrapper around `VerifierSetup<BN254>` with arkworks canonical serialization
22///
23/// Provides transparent access to the inner setup while adding support for
24/// arkworks' CanonicalSerialize and CanonicalDeserialize traits, allowing
25/// easy serialization for users of the arkworks ecosystem.
26#[derive(Clone, Debug)]
27#[repr(transparent)]
28pub struct ArkworksVerifierSetup(pub VerifierSetup<BN254>);
29
30impl ArkworksProverSetup {
31    /// Generate new prover setup with transparent randomness
32    ///
33    /// For square matrices, generates n = 2^((max_log_n+1)/2) generators for both G1 and G2,
34    /// supporting polynomials up to 2^max_log_n coefficients arranged as n×n matrices.
35    ///
36    /// # Parameters
37    /// - `max_log_n`: Maximum log₂ of polynomial size (for n×n matrix with n² = 2^max_log_n)
38    pub fn new(max_log_n: usize) -> Self {
39        #[cfg(feature = "cache")]
40        super::invalidate_cache();
41
42        Self(ProverSetup::new(max_log_n))
43    }
44
45    /// Load prover setup from disk cache, or generate and cache if not available
46    #[cfg(all(feature = "disk-persistence", not(target_arch = "wasm32")))]
47    pub fn new_from_urs(max_log_n: usize) -> Self {
48        let (prover_setup, _) = crate::setup::<BN254>(max_log_n);
49        Self(prover_setup)
50    }
51
52    /// Derive verifier setup from this prover setup
53    pub fn to_verifier_setup(&self) -> ArkworksVerifierSetup {
54        ArkworksVerifierSetup(self.0.to_verifier_setup())
55    }
56
57    /// Unwrap into inner `ProverSetup<BN254>`
58    pub fn into_inner(self) -> ProverSetup<BN254> {
59        self.0
60    }
61}
62
63impl ArkworksVerifierSetup {
64    /// Unwrap into inner `VerifierSetup<BN254>`
65    pub fn into_inner(self) -> VerifierSetup<BN254> {
66        self.0
67    }
68}
69
70impl From<ProverSetup<BN254>> for ArkworksProverSetup {
71    fn from(setup: ProverSetup<BN254>) -> Self {
72        Self(setup)
73    }
74}
75
76impl From<ArkworksProverSetup> for ProverSetup<BN254> {
77    fn from(setup: ArkworksProverSetup) -> Self {
78        setup.0
79    }
80}
81
82impl From<VerifierSetup<BN254>> for ArkworksVerifierSetup {
83    fn from(setup: VerifierSetup<BN254>) -> Self {
84        Self(setup)
85    }
86}
87
88impl From<ArkworksVerifierSetup> for VerifierSetup<BN254> {
89    fn from(setup: ArkworksVerifierSetup) -> Self {
90        setup.0
91    }
92}
93
94impl Deref for ArkworksProverSetup {
95    type Target = ProverSetup<BN254>;
96
97    fn deref(&self) -> &Self::Target {
98        &self.0
99    }
100}
101
102impl DerefMut for ArkworksProverSetup {
103    fn deref_mut(&mut self) -> &mut Self::Target {
104        &mut self.0
105    }
106}
107
108impl Deref for ArkworksVerifierSetup {
109    type Target = VerifierSetup<BN254>;
110
111    fn deref(&self) -> &Self::Target {
112        &self.0
113    }
114}
115
116impl DerefMut for ArkworksVerifierSetup {
117    fn deref_mut(&mut self) -> &mut Self::Target {
118        &mut self.0
119    }
120}