Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ProofOptions as command arguments #14

Merged
merged 3 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions air/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ impl ProofOptions {
))
}

// TODO: Use parameters appropriate for 252-bit field (see ethSTARK paper,
// sections 5.10.1 and 5.10.2)
pub fn with_96_bit_security() -> Self {
pub fn with_proof_options(
num_queries: Option<usize>,
blowup_factor: Option<usize>,
grinding_factor: Option<u32>,
fri_folding_factor: Option<usize>,
fri_max_remainder_size: Option<usize>,
) -> Self {
Self(WinterProofOptions::new(
54, // 27
4, //8,
16,
num_queries.unwrap_or(54), // 27
blowup_factor.unwrap_or(4), //8,
grinding_factor.unwrap_or(16),
HashFunction::Blake3_192,
FieldExtension::None,
8,
256,
fri_folding_factor.unwrap_or(8),
fri_max_remainder_size.unwrap_or(256),
))
}

Expand All @@ -47,7 +51,7 @@ impl ProofOptions {

impl Default for ProofOptions {
fn default() -> Self {
Self::with_96_bit_security()
Self::with_proof_options(None, None, None, None, None)
}
}

Expand Down
82 changes: 0 additions & 82 deletions cli/src/cmd/prove.rs

This file was deleted.

149 changes: 149 additions & 0 deletions cli/src/cmd/prove/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
use clap::{Error, ErrorKind, Parser, ValueHint};
use std::path::PathBuf;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
pub struct ProveArgs {
#[clap(
help = "Path to the compiled Cairo program JSON file",
long,
value_hint = ValueHint::FilePath
)]
pub program: PathBuf,

#[clap(
help = "Path to the execution trace output file",
long,
value_hint = ValueHint::FilePath
)]
pub trace: PathBuf,

#[clap(
help = "Path to the memory output file",
long,
value_hint = ValueHint::FilePath
)]
pub memory: PathBuf,

#[clap(
help = "Path to write the STARK proof",
long,
value_hint = ValueHint::FilePath
)]
pub output: PathBuf,

#[clap(help = "Number of serialized outputs", long)]
pub num_outputs: Option<u64>,

#[clap(
help = "Number of queries for a STARK proof",
long,
value_parser(clap::builder::ValueParser::new(parse_num_queries))
)]
pub num_queries: Option<usize>,

#[clap(
help = "Blowup factor for a STARK proof",
long,
value_parser(clap::builder::ValueParser::new(parse_blowup_factor))
)]
pub blowup_factor: Option<usize>,

#[clap(
help = "Query seed grinding factor for a STARK proof",
long,
value_parser(clap::value_parser!(u32).range(..33))
)]
pub grinding_factor: Option<u32>,

#[clap(
help = "Factor by which the degree of a polynomial is reduced with each FRI layer",
long,
value_parser(clap::builder::ValueParser::new(parse_fri_folding_factor))
)]
pub fri_folding_factor: Option<usize>,

#[clap(
help = "Maximum allowed remainder (last FRI layer) size",
long,
value_parser(clap::builder::ValueParser::new(parse_fri_max_remainder_size))
)]
pub fri_max_remainder_size: Option<usize>,
}

fn parse_num_queries(value: &str) -> Result<usize, Error> {
let value = value
.parse::<usize>()
.map_err(|e| Error::raw(ErrorKind::InvalidValue, format!("{}", e)))?;

match value {
0 => Err(Error::raw(ErrorKind::ValueValidation, "cannot be 0")),
129.. => Err(Error::raw(
ErrorKind::ValueValidation,
"cannot be more than 128",
)),
_ => Ok(value),
}
}

fn parse_blowup_factor(value: &str) -> Result<usize, Error> {
let value = value
.parse::<usize>()
.map_err(|e| Error::raw(ErrorKind::InvalidValue, format!("{}", e)))?;

if !value.is_power_of_two() {
return Err(Error::raw(
ErrorKind::ValueValidation,
"must be a power of two",
));
}

match value {
0..=3 => Err(Error::raw(
ErrorKind::ValueValidation,
"cannot be smaller than 4",
)),
257.. => Err(Error::raw(
ErrorKind::ValueValidation,
"cannot be more than 256",
)),
_ => Ok(value),
}
}

fn parse_fri_folding_factor(value: &str) -> Result<usize, Error> {
let value = value
.parse::<usize>()
.map_err(|e| Error::raw(ErrorKind::InvalidValue, format!("{}", e)))?;

if value != 4 && value != 8 && value != 16 {
Err(Error::raw(ErrorKind::ValueValidation, "must be 4, 8 or 16"))
} else {
Ok(value)
}
}

fn parse_fri_max_remainder_size(value: &str) -> Result<usize, Error> {
let value = value
.parse::<usize>()
.map_err(|e| Error::raw(ErrorKind::InvalidValue, format!("{}", e)))?;

if !value.is_power_of_two() {
return Err(Error::raw(
ErrorKind::ValueValidation,
"must be a power of two",
));
}

match value {
0..=31 => Err(Error::raw(
ErrorKind::ValueValidation,
"cannot be smaller than 32",
)),
1025.. => Err(Error::raw(
ErrorKind::ValueValidation,
"cannot be more than 1024",
)),
_ => Ok(value),
}
}
4 changes: 4 additions & 0 deletions cli/src/cmd/prove/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod args;
mod prove;

pub use args::ProveArgs;
47 changes: 47 additions & 0 deletions cli/src/cmd/prove/prove.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::fs::File;
use std::io::Write;

use super::ProveArgs;
use crate::{cmd::ProofData, utils::Cmd};
use air::ProofOptions;
use runner::ExecutionTrace;
use winter_utils::Serializable;

pub struct ProveOutput {}

#[derive(Debug)]
pub enum Error {}

impl Cmd for ProveArgs {
type Output = Result<ProveOutput, Error>;

fn run(self) -> Self::Output {
// Load trace from file
let trace =
ExecutionTrace::from_file(self.program, self.trace, self.memory, self.num_outputs);

// Generate proof
let proof_options = ProofOptions::with_proof_options(
self.num_queries,
self.blowup_factor,
self.grinding_factor,
self.fri_folding_factor,
self.fri_max_remainder_size,
);
let (proof, pub_inputs) = prover::prove_trace(trace, &proof_options).unwrap();
let input_bytes = pub_inputs.to_bytes();
let proof_bytes = proof.to_bytes();
println!("Proof size: {:.1} KB", proof_bytes.len() as f64 / 1024f64);

// Write proof to disk
let data = ProofData {
input_bytes,
proof_bytes,
};
let b = bincode::serialize(&data).unwrap();
let mut f = File::create(self.output).unwrap();
f.write_all(&b).unwrap();

Ok(ProveOutput {})
}
}
3 changes: 1 addition & 2 deletions cli/src/giza.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ mod utils;

use crate::utils::Cmd;
use clap::{Parser, Subcommand};
use cmd::prove::ProveArgs;
use cmd::verify::VerifyArgs;
use cmd::{prove::ProveArgs, verify::VerifyArgs};

#[derive(Debug, Parser)]
#[clap(name = "giza")]
Expand Down
2 changes: 1 addition & 1 deletion examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn main() {
let trace = program.execute().unwrap();

// generate the proof of execution
let proof_options = ProofOptions::with_96_bit_security();
let proof_options = ProofOptions::with_proof_options(None, None, None, None, None);
let (proof, pub_inputs) = prover::prove_trace(trace, &proof_options).unwrap();
let proof_bytes = proof.to_bytes();
println!("Proof size: {:.1} KB", proof_bytes.len() as f64 / 1024f64);
Expand Down
Empty file removed memory.bin
Empty file.
6 changes: 3 additions & 3 deletions runner/src/cairo_interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ mod tests {

#[test]
fn test_trace_bin() {
let trace = read_trace_bin(PathBuf::from("../tmp/trace.bin"));
let trace = read_trace_bin(&PathBuf::from("../tmp/trace.bin"));
println!("{:?}", trace);
}

#[test]
fn test_memory_bin() {
let mem = read_memory_bin(
PathBuf::from("../tmp/memory.bin"),
PathBuf::from("../tmp/program.json"),
&PathBuf::from("../tmp/memory.bin"),
&PathBuf::from("../tmp/program.json"),
);
println!("{:?}", mem.data);
}
Expand Down