Skip to content

Commit

Permalink
Move Towards Generic API (#170)
Browse files Browse the repository at this point in the history
* Start to rearrange code to concentrate under api.

* More moving.

* More renaming.

* Push more stuff through the api.

* Ditto.

* Remove backups accidently committed.

* Start to reorganize.

* More work on new abstractions.

* Yet more.

* More generics.

* Finish converting key.

* Various fixes to bounds.

* Revert circuit.

* Revert key.

* Revert mod, partially.

* Finish most of the conversion.

* Format.

* Finish conversion.

* Significant cleanup.

* Getting there.

* Closer.

* Mostly clippy.

* More clippy.

* Pass Jolt interfaces through the API as well.

* Improve misuse resistance.

* Minor.

* Format.

* Restore independent config crate.

* Clippy.

* Format again.

* Remove tools-dev accidently restored.

* Post-rebase cleanup.

* Push RPC through API.

* Formatting.

* Fix reversion of config.

* Further cleanup.

* Cleanup cargo.

* Fix rpc building.

* Formatting.

* Adapt to removal of tools-dev.

* Fix imports.

* Move to purer reexport model for Jolt.

* Formatting.

* Fix rebasing mistake.

* Remove unnecessary imports.

* Add feature gating.

* Formatting.

* Remove unnecessary imports.
  • Loading branch information
sjudson authored Jun 21, 2024
1 parent 45a9602 commit a6b3e24
Show file tree
Hide file tree
Showing 41 changed files with 810 additions and 1,039 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ members = [
"tools/tui",
"config",
"config/serde_wrapper",
"prover",
"network",
"network/rpc/common",
"network/rpc/server",
Expand All @@ -21,7 +20,6 @@ members = [
default-members = [
"vm",
"tools",
"prover",
"network",
"nova",
"spartan",
Expand Down
36 changes: 34 additions & 2 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,38 @@ keywords = { workspace = true }
categories = { workspace = true }

[dependencies]
nexus-config = { path = "../config" }
anyhow = "1.0"

zstd = { version = "0.12", default-features = false }

nexus-config = { path = "../config", features = ["clap_derive"] }
nexus-vm = { path = "../vm" }
nexus-prover = { path = "../prover" }
nexus-jolt = { path = "../jolt", optional = true }
spartan = { path = "../spartan", package = "ark-spartan", optional = true }
nexus-nova = { path = "../nova", optional = true }

tracing = "0.1"

ark-ff.workspace = true
ark-ec.workspace = true
ark-std.workspace = true
ark-crypto-primitives.workspace = true
ark-relations.workspace = true
ark-r1cs-std.workspace = true
ark-serialize.workspace = true

ark-bn254.workspace = true
ark-grumpkin.workspace = true

[features]
default = ["parallel", "prover_nova"]
parallel = [
"ark-ff/parallel",
"ark-ec/parallel",
"ark-std/parallel",
"ark-crypto-primitives/parallel",
"ark-r1cs-std/parallel",
"nexus-nova/parallel",
]
prover_nova = ["dep:nexus-nova", "dep:spartan", "nexus-nova/spartan"]
prover_jolt = ["dep:nexus-jolt"]
7 changes: 4 additions & 3 deletions api/examples/prover_run.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// An example of loading and running the NVM.

use nexus_api::config::vm::NovaImpl;
use nexus_api::{
config::vm::{ProverImpl, VmConfig},
nvm::{self, memory::MerkleTrie, NexusVM},
prover::{self},
};
use nexus_config::vm::NovaImpl;
use std::path::PathBuf;

const CONFIG: VmConfig = VmConfig {
Expand All @@ -21,7 +21,7 @@ fn main() {

println!("Setting up public parameters...");
let public_params =
prover::setup::gen_vm_pp(CONFIG.k, &()).expect("error generating public parameters");
prover::nova::pp::gen_vm_pp(CONFIG.k, &()).expect("error generating public parameters");

println!("Reading and translating vm...");
let mut vm: NexusVM<MerkleTrie> =
Expand All @@ -40,7 +40,8 @@ fn main() {
println!("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

println!("Proving execution of length {}...", trace.blocks.len());
let proof = prover::prove::prove_seq(&public_params, trace).expect("error proving execution");
let proof =
nexus_api::prover::nova::prove_seq(&public_params, trace).expect("error proving execution");

print!("Verifying execution...");
proof
Expand Down
24 changes: 7 additions & 17 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,24 @@

/// Configurations
pub mod config {
pub use nexus_config::{Config, MiscConfig};
pub mod vm {
pub use nexus_config::vm::*;
}
pub mod network {
pub use nexus_config::network::*;
}
}

/// RISC-V processing
pub mod nvm {
pub mod interactive {
pub use nexus_vm::{eval, load_elf, parse_elf, trace::trace};
pub use nexus_vm::{eval, load_elf, parse_elf, trace::trace, trace::Trace};
}
pub use nexus_vm::{error::NexusVMError, eval::NexusVM, run_vm, trace_vm, VMOpts};
pub mod memory {
pub use nexus_vm::memory::{paged::Paged, trie::MerkleTrie};
pub use nexus_vm::memory::{paged::Paged, path::Path, trie::MerkleTrie};
}
}

/// Nova-based provers
pub mod prover {
pub use nexus_prover::error::ProofError;
pub mod setup {
pub use nexus_prover::pp::{gen_or_load, gen_pp, gen_to_file, gen_vm_pp, load_pp, save_pp};
}
pub mod prove {
pub use nexus_prover::{
compress, load_proof, prove_par, prove_par_com, prove_seq, run, save_proof,
};
}
pub mod verify {
pub use nexus_prover::verify_compressed;
}
}
pub mod prover;
3 changes: 3 additions & 0 deletions api/src/prover/jolt/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub use nexus_jolt::{parse, preprocess, prove, trace, verify, VM};

pub mod types;
3 changes: 3 additions & 0 deletions api/src/prover/jolt/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Default types and traits for use by zkVM + Jolt pipeline

pub use nexus_jolt::{JoltCommitments, JoltPreprocessing, JoltProof, F, PCS};
4 changes: 4 additions & 0 deletions api/src/prover/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[cfg(feature = "prover_jolt")]
pub mod jolt;
#[cfg(feature = "prover_nova")]
pub mod nova;
19 changes: 11 additions & 8 deletions prover/src/circuit.rs → api/src/prover/nova/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ pub use ark_relations::{
use nexus_vm::{
circuit::{build_constraints, ARITY},
machines::nop_vm,
memory::{path::Path, trie::MerkleTrie},
memory::Memory,
trace::{trace, Trace},
};

use crate::error::*;
use crate::types::*;
use super::error::*;
use super::types::*;

pub struct Tr(pub Trace<Path>);
pub struct Tr<M: Memory>(pub Trace<M::Proof>);

impl Tr {
impl<M: Memory> Tr<M> {
pub fn steps(&self) -> usize {
self.0.blocks.len()
}
Expand All @@ -38,13 +38,16 @@ impl Tr {
}
}

pub fn nop_circuit(k: usize) -> Result<Tr, ProofError> {
let mut vm = nop_vm::<MerkleTrie>(1);
pub fn nop_circuit<M: Memory>(k: usize) -> Result<Tr<M>, ProofError> {
let mut vm = nop_vm::<M>(1);
let trace = trace(&mut vm, k, false)?;
Ok(Tr(trace))
}

impl StepCircuit<F1> for Tr {
impl<M: Memory> StepCircuit<F1> for Tr<M>
where
M::Proof: Send + Sync,
{
const ARITY: usize = ARITY;

fn generate_constraints(
Expand Down
File renamed without changes.
52 changes: 52 additions & 0 deletions api/src/prover/nova/key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::fs::File;
use zstd::stream::{Decoder, Encoder};

pub use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use nexus_nova::nova::pcd::compression::SNARK;

use super::error::*;
use super::types::*;
use super::LOG_TARGET;

pub fn gen_key(pp: &ComPP, srs: &SRS) -> Result<SpartanKey, ProofError> {
tracing::info!(
target: LOG_TARGET,
"Generating Spartan key parameters",
);

let key = SNARK::setup(pp, srs)?;
Ok(key)
}

pub fn save_key(key: SpartanKey, file: &str) -> Result<(), ProofError> {
tracing::info!(
target: LOG_TARGET,
pp_file =?file,
"Saving Spartan key parameters",
);

let f = File::create(file)?;
let mut enc = Encoder::new(&f, 0)?;
key.serialize_compressed(&mut enc)?;
enc.finish()?;
f.sync_all()?;
Ok(())
}

pub fn load_key(file: &str) -> Result<SpartanKey, ProofError> {
tracing::info!(
target: LOG_TARGET,
pp_file =?file,
"Loading Spartan key parameters",
);

let f = File::open(file)?;
let mut dec = Decoder::new(&f)?;
let key = SpartanKey::deserialize_compressed_unchecked(&mut dec)?;
Ok(key)
}

pub fn gen_key_to_file(pp: &ComPP, srs: &SRS, key_file: &str) -> Result<(), ProofError> {
let key: SpartanKey = gen_key(pp, srs)?;
save_key(key, key_file)
}
Loading

0 comments on commit a6b3e24

Please sign in to comment.