Skip to content

Commit

Permalink
refactor: Refactor Nova struct to make them generic on the field type
Browse files Browse the repository at this point in the history
- makes all of our Nova-related structures generic in the type of the used primary scalar field, provided the field can be attached to a curve cycle with the suitable properties
- sets up the curve cycle attachments for the pallas::Scalar and bn256::Scalar fields,
- allows us to start using bn256.

- making public parameter caching generic,
- making clutch generic, or supporting SnarkPack,
- making fcomm generic, or supporting SnarkPack,
- making the repl generic.

- Implemented the `CurveCycleEquipped` trait, replacing previous curve-specific scalar types for improved generic programming over curve cycles.
- Updated various files, including `fcomm.rs`, `lurk_proof.rs`, `lib.rs` and `nova.rs` to accommodate the new `CurveCycleEquipped` type.
- Extended `LanguageField` enum with `BN256` and `Grumpkin` values and updated respective `fmt` display.
- Refactored the `public_params` function in `public_parameters/mod.rs and registry.rs` to use the new `CurveCycleEquipped` type.
- Modified proofs in `nova.rs` to work with the new `CurveCycleEquipped` type.
- Removed `nova` and `pasta_curves::pallas` dependencies from `lurk-tests.rs` and `registry.rs` respectively, demonstrating reduction in dependency.
  • Loading branch information
huitseeker committed Jul 21, 2023
1 parent 926c7f4 commit f796c6c
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 191 deletions.
94 changes: 24 additions & 70 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ clap = "4.3.17"
ff = "0.13"
log = "0.4.19"
neptune = { version = "10.0.0" }
nova = { package = "nova-snark", version = "0.22", default-features = false }
nova = { git = "https://github.com/microsoft/Nova.git", package = "nova-snark", version = "0.22", default-features = false }
once_cell = "1.18.0"
pairing = { version = "0.23" }
pasta_curves = { version = "0.5.1" }
Expand Down
2 changes: 1 addition & 1 deletion examples/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn main() {
println!("Setting up public parameters...");

let pp_start = Instant::now();
let pp = public_params::<Sha256Coproc<Fr>>(REDUCTION_COUNT, lang_rc.clone()).unwrap();
let pp = public_params::<_, Sha256Coproc<Fr>>(REDUCTION_COUNT, lang_rc.clone()).unwrap();
let pp_end = pp_start.elapsed();

println!("Public parameters took {:?}", pp_end);
Expand Down
3 changes: 2 additions & 1 deletion fcomm/src/bin/fcomm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use log::info;
use lurk::proof::nova::CurveCycleEquipped;
use std::convert::TryFrom;
use std::env;
use std::fs::read_to_string;
Expand Down Expand Up @@ -492,7 +493,7 @@ fn opening_request<P: AsRef<Path>, F: LurkField + Serialize + DeserializeOwned>(
}

// Get proof from supplied path or else from stdin.
fn proof<'a, P: AsRef<Path>, F: LurkField>(
fn proof<'a, P: AsRef<Path>, F: CurveCycleEquipped>(
proof_path: Option<P>,
) -> Result<Proof<'a, F>, error::Error>
where
Expand Down
16 changes: 8 additions & 8 deletions fcomm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use proptest_derive::Arbitrary;

use ff::PrimeField;
use hex::FromHex;
use lurk::error::ReductionError;
#[cfg(not(target_arch = "wasm32"))]
use lurk::field::FWrap;
use lurk::{
Expand All @@ -31,6 +30,7 @@ use lurk::{
z_ptr::ZExprPtr,
z_store::ZStore,
};
use lurk::{error::ReductionError, proof::nova::CurveCycleEquipped};
use once_cell::sync::OnceCell;
use pasta_curves::pallas;
use rand::rngs::OsRng;
Expand Down Expand Up @@ -256,9 +256,9 @@ pub struct VerificationResult {
}

#[derive(Serialize, Deserialize)]
pub struct Proof<'a, F: LurkField> {
pub struct Proof<'a, F: CurveCycleEquipped> {
pub claim: Claim<F>,
pub proof: nova::Proof<'a, Coproc<S1>>,
pub proof: nova::Proof<'a, F, Coproc<F>>,
pub num_steps: usize,
pub reduction_count: ReductionCount,
}
Expand Down Expand Up @@ -619,7 +619,7 @@ impl<'a> Opening<S1> {
chain: bool,
only_use_cached_proofs: bool,
nova_prover: &'a NovaProver<S1, Coproc<S1>>,
pp: &'a PublicParams<'_, Coproc<S1>>,
pp: &'a PublicParams<'_, S1, Coproc<S1>>,
lang: Arc<Lang<S1, Coproc<S1>>>,
) -> Result<Proof<'a, S1>, Error> {
let claim = Self::apply(s, input, function, limit, chain, &lang)?;
Expand All @@ -640,7 +640,7 @@ impl<'a> Opening<S1> {
limit: usize,
only_use_cached_proofs: bool,
nova_prover: &'a NovaProver<S1, Coproc<S1>>,
pp: &'a PublicParams<'_, Coproc<S1>>,
pp: &'a PublicParams<'_, S1, Coproc<S1>>,
lang: Arc<Lang<S1, Coproc<S1>>>,
) -> Result<Proof<'a, S1>, Error> {
let input = request.input.expr.ptr(s, limit, &lang);
Expand Down Expand Up @@ -774,7 +774,7 @@ impl<'a> Proof<'a, S1> {
limit: usize,
only_use_cached_proofs: bool,
nova_prover: &'a NovaProver<S1, Coproc<S1>>,
pp: &'a PublicParams<'_, Coproc<S1>>,
pp: &'a PublicParams<'_, S1, Coproc<S1>>,
lang: Arc<Lang<S1, Coproc<S1>>>,
) -> Result<Self, Error> {
let env = supplied_env.unwrap_or_else(|| empty_sym_env(s));
Expand Down Expand Up @@ -812,7 +812,7 @@ impl<'a> Proof<'a, S1> {
limit: usize,
only_use_cached_proofs: bool,
nova_prover: &'a NovaProver<S1, Coproc<S1>>,
pp: &'a PublicParams<'_, Coproc<S1>>,
pp: &'a PublicParams<'_, S1, Coproc<S1>>,
lang: &Arc<Lang<S1, Coproc<S1>>>,
) -> Result<Self, Error> {
let reduction_count = nova_prover.reduction_count();
Expand Down Expand Up @@ -898,7 +898,7 @@ impl<'a> Proof<'a, S1> {

pub fn verify(
&self,
pp: &PublicParams<'_, Coproc<S1>>,
pp: &PublicParams<'_, S1, Coproc<S1>>,
lang: &Lang<S1, Coproc<S1>>,
) -> Result<VerificationResult, Error> {
let (public_inputs, public_outputs) = self.io_vecs(lang)?;
Expand Down
20 changes: 9 additions & 11 deletions src/cli/lurk_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use lurk::{
coprocessor::Coprocessor,
eval::lang::{Coproc, Lang},
field::LurkField,
proof::nova,
proof::nova::{self, CurveCycleEquipped},
z_ptr::{ZContPtr, ZExprPtr},
z_store::ZStore,
};
Expand Down Expand Up @@ -35,16 +35,14 @@ impl<F: LurkField> HasFieldModulus for LurkProofMeta<F> {
}
}

type Pallas = pasta_curves::pallas::Scalar; // TODO: generalize this

/// Minimal data structure containing just enough for proof verification
#[derive(Serialize, Deserialize)]
pub enum LurkProof<'a, F: LurkField>
pub enum LurkProof<'a, F: CurveCycleEquipped>
where
Coproc<F>: Coprocessor<Pallas>,
Coproc<F>: Coprocessor<F>,
{
Nova {
proof: nova::Proof<'a, Coproc<F>>,
proof: nova::Proof<'a, F, Coproc<F>>,
public_inputs: Vec<F>,
public_outputs: Vec<F>,
num_steps: usize,
Expand All @@ -53,9 +51,9 @@ where
},
}

impl<'a, F: LurkField> HasFieldModulus for LurkProof<'a, F>
impl<'a, F: CurveCycleEquipped> HasFieldModulus for LurkProof<'a, F>
where
Coproc<F>: Coprocessor<Pallas>,
Coproc<F>: Coprocessor<F>,
{
fn field_modulus() -> String {
F::MODULUS.to_owned()
Expand All @@ -71,7 +69,7 @@ mod non_wasm {
use anyhow::Result;
use lurk::{
coprocessor::Coprocessor, eval::lang::Coproc, field::LurkField,
public_parameters::public_params,
proof::nova::CurveCycleEquipped, public_parameters::public_params,
};
use serde::Serialize;

Expand All @@ -84,9 +82,9 @@ mod non_wasm {
}
}

impl<'a, F: LurkField + Serialize> LurkProof<'a, F>
impl<'a, F: CurveCycleEquipped + Serialize> LurkProof<'a, F>
where
Coproc<F>: Coprocessor<Pallas>,
Coproc<F>: Coprocessor<F>,
{
#[inline]
pub fn persist(self, id: &str) -> Result<()> {
Expand Down
4 changes: 4 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ impl ReplCli {
// LanguageField::BLS12_381 => repl!(limit, rc, blstrs::Scalar, backend),
LanguageField::Vesta => todo!(),
LanguageField::BLS12_381 => todo!(),
LanguageField::BN256 => todo!(),
LanguageField::Grumpkin => todo!(),
}
}
}
Expand Down Expand Up @@ -349,6 +351,8 @@ impl LoadCli {
// LanguageField::BLS12_381 => load!(limit, rc, blstrs::Scalar, backend),
LanguageField::Vesta => todo!(),
LanguageField::BLS12_381 => todo!(),
LanguageField::BN256 => todo!(),
LanguageField::Grumpkin => todo!(),
}
}
}
Expand Down
Loading

0 comments on commit f796c6c

Please sign in to comment.