Adds proving key file

This commit is contained in:
howardwu 2020-05-02 17:28:20 -07:00
parent 219f90cb17
commit f86c9d8076
11 changed files with 125 additions and 729 deletions

5
.gitignore vendored
View File

@ -1,2 +1,7 @@
/target
**.idea/
Leo.toml
src/
inputs/
outputs/
*.DS_Store

733
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -42,7 +42,7 @@ impl<F: Field + PrimeField> Compiler<F> {
// Build program from abstract syntax tree
let package_name = self.package_name.clone();
let program = Program::<'_, F>::from(syntax_tree).name(package_name);
log::info!("Compiled -\n{:#?}", program);
log::info!("Compilation complete:\n{:#?}", program);
Ok(ResolvedProgram::generate_constraints(cs, program))
}

View File

@ -79,7 +79,7 @@ impl<F: Field + PrimeField> fmt::Display for ResolvedValue<F> {
write!(f, "}}")
}
ResolvedValue::Return(ref values) => {
write!(f, "Return values : [")?;
write!(f, "Program output: [")?;
for (i, value) in values.iter().enumerate() {
write!(f, "{}", value)?;
if i < values.len() - 1 {

View File

@ -1,8 +1,8 @@
use crate::{cli::*, cli_types::*};
use crate::directories::{source::SOURCE_DIRECTORY_NAME, OutputsDirectory};
use crate::errors::{BuildError, CLIError};
use crate::files::{MainFile, MAIN_FILE_NAME};
use crate::manifest::Manifest;
use crate::{cli::*, cli_types::*};
use leo_compiler::compiler::Compiler;
use snarkos_curves::bls12_377::Fr;
@ -62,8 +62,8 @@ impl CLI for BuildCommand {
log::info!("Compiling program located in {:?}", main_file_path);
// Compile from the main file path
let circuit = Compiler::<Fr>::init(package_name, main_file_path);
let program = Compiler::<Fr>::init(package_name, main_file_path);
Ok(circuit)
Ok(program)
}
}

View File

@ -29,7 +29,7 @@ impl CLI for RunCommand {
#[cfg_attr(tarpaulin, skip)]
fn output(options: Self::Options) -> Result<(), CLIError> {
let (circuit, parameters, prepared_verifying_key) = SetupCommand::output(options)?;
let (program, parameters, prepared_verifying_key) = SetupCommand::output(options)?;
let rng = &mut thread_rng();
@ -37,7 +37,7 @@ impl CLI for RunCommand {
let mut verifying = Duration::new(0, 0);
let start = Instant::now();
let proof = create_random_proof(circuit, &parameters, rng).unwrap();
let proof = create_random_proof(program, &parameters, rng).unwrap();
proving += start.elapsed();

View File

@ -1,6 +1,8 @@
use crate::{cli::*, cli_types::*};
use crate::commands::BuildCommand;
use crate::errors::CLIError;
use crate::{cli::*, cli_types::*};
use crate::files::ProvingKeyFile;
use crate::manifest::Manifest;
use leo_compiler::compiler::Compiler;
use snarkos_algorithms::snark::{
@ -10,6 +12,8 @@ use snarkos_curves::bls12_377::{Bls12_377, Fr};
use clap::ArgMatches;
use rand::thread_rng;
use std::convert::TryFrom;
use std::env::current_dir;
use std::time::Instant;
#[derive(Debug)]
@ -37,21 +41,26 @@ impl CLI for SetupCommand {
#[cfg_attr(tarpaulin, skip)]
fn output(options: Self::Options) -> Result<Self::Output, CLIError> {
let circuit = BuildCommand::output(options)?;
let program = BuildCommand::output(options)?;
// Get the package name
let path = current_dir()?;
let package_name = Manifest::try_from(&path)?.get_package_name();
let start = Instant::now();
let rng = &mut thread_rng();
let parameters =
generate_random_parameters::<Bls12_377, _, _>(circuit.clone(), rng).unwrap();
generate_random_parameters::<Bls12_377, _, _>(program.clone(), rng).unwrap();
let prepared_verifying_key = prepare_verifying_key::<Bls12_377>(&parameters.vk);
let finish = start.elapsed();
log::info!("Setup completed in {:?} milliseconds", start.elapsed().as_millis());
println!(" ");
println!(" Setup time : {:?} milliseconds", finish.as_millis());
println!(" ");
// Write the proving key file to the inputs directory
let mut proving_key = vec![];
parameters.write(&mut proving_key);
ProvingKeyFile::new(&package_name).write_to(&path, &proving_key)?;
Ok((circuit, parameters, prepared_verifying_key))
Ok((program, parameters, prepared_verifying_key))
}
}

View File

@ -1,2 +1,5 @@
pub mod main;
pub use self::main::*;
pub mod proving_key;
pub use self::proving_key::*;

View File

@ -0,0 +1,19 @@
use std::io;
#[derive(Debug, Fail)]
pub enum ProvingKeyFileError {
#[fail(display = "{}: {}", _0, _1)]
Crate(&'static str, String),
#[fail(display = "creating: {}", _0)]
Creating(io::Error),
#[fail(display = "writing: {}", _0)]
Writing(io::Error),
}
impl From<std::io::Error> for ProvingKeyFileError {
fn from(error: std::io::Error) -> Self {
ProvingKeyFileError::Crate("std::io", format!("{}", error))
}
}

View File

@ -1,2 +1,5 @@
pub mod main;
pub use self::main::*;
pub mod proving_key;
pub use self::proving_key::*;

52
leo/files/proving_key.rs Normal file
View File

@ -0,0 +1,52 @@
//! The `main.leo` file.
use crate::directories::inputs::INPUTS_DIRECTORY_NAME;
use crate::errors::MainFileError;
use serde::Deserialize;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
pub static PROVING_FILE_EXTENSION: &str = ".leo.pk";
#[derive(Deserialize)]
pub struct ProvingKeyFile {
pub package_name: String,
}
impl ProvingKeyFile {
pub fn new(package_name: &str) -> Self {
Self {
package_name: package_name.to_string(),
}
}
pub fn exists_at(self, path: &PathBuf) -> bool {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(INPUTS_DIRECTORY_NAME) {
path.push(PathBuf::from(INPUTS_DIRECTORY_NAME));
}
path.push(PathBuf::from(format!("{}{}", self.package_name, PROVING_FILE_EXTENSION)));
}
path.exists()
}
pub fn write_to(self, path: &PathBuf, proving_key: &[u8]) -> Result<(), MainFileError> {
let mut path = path.to_owned();
if path.is_dir() {
if !path.ends_with(INPUTS_DIRECTORY_NAME) {
path.push(PathBuf::from(INPUTS_DIRECTORY_NAME));
}
path.push(PathBuf::from(format!("{}{}", self.package_name, PROVING_FILE_EXTENSION)));
}
let mut file = File::create(&path)?;
file.write_all(proving_key)?;
log::info!("Proving key stored in {:?}", path);
Ok(())
}
}