Skip to content

Commit

Permalink
Merge pull request #38 from 0xPolygonHermez/public_inputs
Browse files Browse the repository at this point in the history
Providing public inputs is now optional
  • Loading branch information
RogerTaule authored Sep 9, 2024
2 parents d66aee2 + 7cbf08f commit e051ffd
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cli/src/commands/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct ProveCmd {

/// Public inputs path
#[clap(short = 'i', long)]
pub public_inputs: PathBuf,
pub public_inputs: Option<PathBuf>,

/// Setup folder path
#[clap(long)]
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/verify_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct VerifyConstraintsCmd {

/// Public inputs path
#[clap(short = 'i', long)]
pub public_inputs: PathBuf,
pub public_inputs: Option<PathBuf>,

/// Setup folder path
#[clap(long)]
Expand Down
29 changes: 19 additions & 10 deletions examples/fibonacci-square/src/fibonacci_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use crate::{FibonacciSquare, Pilout, Module};

pub struct FibonacciWitness<F> {
pub wcm: WitnessManager<F>,
pub public_inputs_path: PathBuf,
pub public_inputs_path: Option<PathBuf>,
pub fibonacci: Arc<FibonacciSquare<F>>,
pub module: Arc<Module<F>>,
pub std_lib: Arc<Std<F>>,
}

impl<F: PrimeField> FibonacciWitness<F> {
pub fn new(public_inputs_path: PathBuf) -> Self {
pub fn new(public_inputs_path: Option<PathBuf>) -> Self {
let mut wcm = WitnessManager::new();

let std_lib = Std::new(&mut wcm, None);
Expand All @@ -35,16 +35,25 @@ impl<F: PrimeField> FibonacciWitness<F> {

impl<F: PrimeField> WitnessLibrary<F> for FibonacciWitness<F> {
fn start_proof(&mut self, pctx: &mut ProofCtx<F>, ectx: &ExecutionCtx, sctx: &SetupCtx) {
let mut file = File::open(&self.public_inputs_path).unwrap();

let public_inputs: FibonacciSquarePublics = if let Some(path) = &self.public_inputs_path {
let mut file = File::open(path).unwrap();

if !file.metadata().unwrap().is_file() {
panic!("Public inputs file not found");
}
if !file.metadata().unwrap().is_file() {
panic!("Public inputs file not found");
}

let mut contents = String::new();
let _ = file.read_to_string(&mut contents);
let mut contents = String::new();

let public_inputs: FibonacciSquarePublics = serde_json::from_str(&contents).unwrap();
let _ = file.read_to_string(&mut contents).map_err(|err| {
format!("Failed to read public inputs file: {}", err)
});

serde_json::from_str(&contents).unwrap()
} else {
FibonacciSquarePublics::default()
};

pctx.public_inputs = public_inputs.into();

self.wcm.start_proof(pctx, ectx, sctx);
Expand All @@ -70,7 +79,7 @@ impl<F: PrimeField> WitnessLibrary<F> for FibonacciWitness<F> {
#[no_mangle]
pub extern "Rust" fn init_library(
_rom_path: Option<PathBuf>,
public_inputs_path: PathBuf,
public_inputs_path: Option<PathBuf>,
) -> Result<Box<dyn WitnessLibrary<Goldilocks>>, Box<dyn Error>> {
env_logger::builder()
.format_timestamp(None)
Expand Down
2 changes: 1 addition & 1 deletion examples/fibonacci-square/src/public_inputs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
#[derive(Default, Debug, Serialize, Deserialize)]
pub struct FibonacciSquarePublics {
pub module: u64,
pub a: u64,
Expand Down
8 changes: 5 additions & 3 deletions proofman/src/proofman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<F: Field + 'static> ProofMan<F> {
pub fn generate_proof(
witness_lib_path: PathBuf,
rom_path: Option<PathBuf>,
public_inputs_path: PathBuf,
public_inputs_path: Option<PathBuf>,
proving_key_path: PathBuf,
output_dir_path: PathBuf,
debug_mode: u64,
Expand All @@ -50,8 +50,10 @@ impl<F: Field + 'static> ProofMan<F> {
}

// Check public_inputs_path is a folder
if !public_inputs_path.exists() {
return Err(format!("Public inputs file not found at path: {:?}", public_inputs_path).into());
if let Some(publics_path) = public_inputs_path.as_ref() {
if !publics_path.exists() {
return Err(format!("Public inputs file not found at path: {:?}", publics_path).into());
}
}

// Check proving_key_path exists
Expand Down
3 changes: 2 additions & 1 deletion proofman/src/witness_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::{error::Error, path::PathBuf};
use proofman_common::{ExecutionCtx, ProofCtx, WitnessPilout, SetupCtx};

/// This is the type of the function that is used to load a witness library.
pub type WitnessLibInitFn<F> = fn(Option<PathBuf>, PathBuf) -> Result<Box<dyn WitnessLibrary<F>>, Box<dyn Error>>;
pub type WitnessLibInitFn<F> =
fn(Option<PathBuf>, Option<PathBuf>) -> Result<Box<dyn WitnessLibrary<F>>, Box<dyn Error>>;

pub trait WitnessLibrary<F> {
fn start_proof(&mut self, pctx: &mut ProofCtx<F>, ectx: &ExecutionCtx, sctx: &SetupCtx);
Expand Down

0 comments on commit e051ffd

Please sign in to comment.