avoid clones in nova. wip
This commit is contained in:
parent
80b6ad12df
commit
9367a98968
8 changed files with 75 additions and 28 deletions
|
@ -125,8 +125,24 @@ impl<'ast, T: Field> fmt::Display for Statement<'ast, T> {
|
|||
|
||||
pub type Prog<'ast, T> = ProgIterator<'ast, T, Vec<Statement<'ast, T>>>;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default)]
|
||||
pub struct ProgIterator<'ast, T, I: IntoIterator<Item = Statement<'ast, T>>> {
|
||||
impl<'ast, T> Prog<'ast, T> {
|
||||
pub fn to_ref_iterator<'a>(
|
||||
&'a self,
|
||||
) -> ProgRefIterator<'ast, 'a, T, std::slice::Iter<'a, Statement<'ast, T>>> {
|
||||
ProgRefIterator {
|
||||
arguments: self.arguments.clone(),
|
||||
return_count: self.return_count,
|
||||
statements: self.statements.iter(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type ProgRefIterator<'ast, 'a, T, I> = GProgIterator<&'a Statement<'ast, T>, I>;
|
||||
|
||||
pub type ProgIterator<'ast, T, I> = GProgIterator<Statement<'ast, T>, I>;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
||||
pub struct GProgIterator<S, I: IntoIterator<Item = S>> {
|
||||
pub arguments: Vec<Parameter>,
|
||||
pub return_count: usize,
|
||||
pub statements: I,
|
||||
|
|
|
@ -173,8 +173,14 @@ fn cli_compute<'a, T: Field, I: Iterator<Item = ir::Statement<'a, T>>>(
|
|||
|
||||
let public_inputs = ir_prog.public_inputs();
|
||||
|
||||
let ir_prog = ir_prog.collect();
|
||||
|
||||
let witness = interpreter
|
||||
.execute_with_log_stream(ir_prog, &arguments.encode(), &mut std::io::stdout())
|
||||
.execute_with_log_stream(
|
||||
ir_prog.to_ref_iterator(),
|
||||
&arguments.encode(),
|
||||
&mut std::io::stdout(),
|
||||
)
|
||||
.map_err(|e| format!("Execution failed: {}", e))?;
|
||||
|
||||
use zokrates_abi::Decode;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use clap::{App, ArgMatches, SubCommand, AppSettings};
|
||||
use clap::{App, AppSettings, ArgMatches, SubCommand};
|
||||
|
||||
pub mod prove;
|
||||
|
||||
|
|
|
@ -14,17 +14,19 @@ use zokrates_bellperson::nova::{self, NovaField};
|
|||
pub fn subcommand() -> App<'static, 'static> {
|
||||
SubCommand::with_name("prove")
|
||||
.about("Proves a many steps of an incremental computation")
|
||||
.arg(Arg::with_name("init")
|
||||
.long("init")
|
||||
.help("Path to the initial value of the public input")
|
||||
.takes_value(true)
|
||||
.default_value(NOVA_PUBLIC_INIT),
|
||||
.arg(
|
||||
Arg::with_name("init")
|
||||
.long("init")
|
||||
.help("Path to the initial value of the public input")
|
||||
.takes_value(true)
|
||||
.default_value(NOVA_PUBLIC_INIT),
|
||||
)
|
||||
.arg(Arg::with_name("steps")
|
||||
.long("steps")
|
||||
.help("Path to the value of the private input for each step")
|
||||
.takes_value(true)
|
||||
.default_value(NOVA_STEPS_PRIVATE_INPUTS),
|
||||
.arg(
|
||||
Arg::with_name("steps")
|
||||
.long("steps")
|
||||
.help("Path to the value of the private input for each step")
|
||||
.takes_value(true)
|
||||
.default_value(NOVA_STEPS_PRIVATE_INPUTS),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("input")
|
||||
|
@ -44,7 +46,7 @@ pub fn subcommand() -> App<'static, 'static> {
|
|||
.value_name("FILE")
|
||||
.takes_value(true)
|
||||
.required(false)
|
||||
.default_value(cli_constants::ABI_SPEC_DEFAULT_PATH)
|
||||
.default_value(cli_constants::ABI_SPEC_DEFAULT_PATH),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("proof-path")
|
||||
|
|
|
@ -19,7 +19,7 @@ use zokrates_ast::typed::abi::Abi;
|
|||
use zokrates_ast::untyped::{Module, OwnedModuleId, Program};
|
||||
use zokrates_ast::zir::ZirProgram;
|
||||
use zokrates_codegen::from_function_and_config;
|
||||
use zokrates_common::{CompileConfig, Resolver};
|
||||
pub use zokrates_common::{CompileConfig, Resolver};
|
||||
use zokrates_field::Field;
|
||||
use zokrates_pest_ast as pest;
|
||||
|
||||
|
|
|
@ -3,7 +3,8 @@ use std::collections::HashMap;
|
|||
use std::fmt;
|
||||
use zokrates_abi::{Decode, Value};
|
||||
use zokrates_ast::ir::{
|
||||
LinComb, ProgIterator, QuadComb, RuntimeError, Solver, Statement, Variable, Witness,
|
||||
LinComb, ProgRefIterator, QuadComb, RuntimeError, Solver, Statement, Variable,
|
||||
Witness,
|
||||
};
|
||||
use zokrates_ast::zir;
|
||||
use zokrates_field::Field;
|
||||
|
@ -26,9 +27,9 @@ impl Interpreter {
|
|||
}
|
||||
|
||||
impl Interpreter {
|
||||
pub fn execute<'ast, T: Field, I: IntoIterator<Item = Statement<'ast, T>>>(
|
||||
pub fn execute<'ast, 'a, T: Field, I: IntoIterator<Item = &'a Statement<'ast, T>>>(
|
||||
&self,
|
||||
program: ProgIterator<'ast, T, I>,
|
||||
program: ProgRefIterator<'ast, 'a, T, I>,
|
||||
inputs: &[T],
|
||||
) -> ExecutionResult<T> {
|
||||
self.execute_with_log_stream(program, inputs, &mut std::io::sink())
|
||||
|
@ -36,12 +37,13 @@ impl Interpreter {
|
|||
|
||||
pub fn execute_with_log_stream<
|
||||
'ast,
|
||||
'a,
|
||||
W: std::io::Write,
|
||||
T: Field,
|
||||
I: IntoIterator<Item = Statement<'ast, T>>,
|
||||
I: IntoIterator<Item = &'a Statement<'ast, T>>,
|
||||
>(
|
||||
&self,
|
||||
program: ProgIterator<'ast, T, I>,
|
||||
program: ProgRefIterator<'ast, 'a, T, I>,
|
||||
inputs: &[T],
|
||||
log_stream: &mut W,
|
||||
) -> ExecutionResult<T> {
|
||||
|
@ -65,7 +67,9 @@ impl Interpreter {
|
|||
let lhs_value = evaluate_quad(&witness, &quad).unwrap();
|
||||
let rhs_value = evaluate_lin(&witness, &lin).unwrap();
|
||||
if lhs_value != rhs_value {
|
||||
return Err(Error::UnsatisfiedConstraint { error });
|
||||
return Err(Error::UnsatisfiedConstraint {
|
||||
error: error.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -92,7 +96,7 @@ impl Interpreter {
|
|||
}
|
||||
}
|
||||
Statement::Log(l, expressions) => {
|
||||
let mut parts = l.parts.into_iter();
|
||||
let mut parts = l.clone().parts.into_iter();
|
||||
|
||||
write!(log_stream, "{}", parts.next().unwrap())
|
||||
.map_err(|_| Error::LogStream)?;
|
||||
|
@ -103,8 +107,12 @@ impl Interpreter {
|
|||
.map(|e| evaluate_lin(&witness, e).unwrap())
|
||||
.collect();
|
||||
|
||||
write!(log_stream, "{}", Value::decode(values, t).into_serde_json())
|
||||
.map_err(|_| Error::LogStream)?;
|
||||
write!(
|
||||
log_stream,
|
||||
"{}",
|
||||
Value::decode(values, t.clone()).into_serde_json()
|
||||
)
|
||||
.map_err(|_| Error::LogStream)?;
|
||||
|
||||
write!(log_stream, "{}", part).map_err(|_| Error::LogStream)?;
|
||||
}
|
||||
|
@ -146,9 +154,9 @@ impl Interpreter {
|
|||
.collect()
|
||||
}
|
||||
|
||||
fn check_inputs<'ast, T: Field, I: IntoIterator<Item = Statement<'ast, T>>, U>(
|
||||
fn check_inputs<'ast, 'a, T: Field, I: IntoIterator<Item = &'a Statement<'ast, T>>, U>(
|
||||
&self,
|
||||
program: &ProgIterator<'ast, T, I>,
|
||||
program: &ProgRefIterator<'ast, 'a, T, I>,
|
||||
inputs: &[U],
|
||||
) -> Result<(), Error> {
|
||||
if program.arguments.len() == inputs.len() {
|
||||
|
|
|
@ -225,6 +225,7 @@ impl<'a> Write for LogWriter<'a> {
|
|||
mod internal {
|
||||
use super::*;
|
||||
use rand_0_8::{CryptoRng, RngCore};
|
||||
use zokrates_ast::ir::ProgRefIterator;
|
||||
|
||||
pub fn compile<T: Field>(
|
||||
source: JsValue,
|
||||
|
@ -325,6 +326,13 @@ mod internal {
|
|||
let public_inputs = program.public_inputs();
|
||||
|
||||
let mut writer = LogWriter::new(log_callback);
|
||||
|
||||
let program = ProgRefIterator {
|
||||
arguments: program.arguments,
|
||||
return_count: program.return_count,
|
||||
statements: program.statements.iter(),
|
||||
};
|
||||
|
||||
let witness = interpreter
|
||||
.execute_with_log_stream(program, &inputs.encode(), &mut writer)
|
||||
.map_err(|err| JsValue::from_str(&format!("Execution failed: {}", err)))?;
|
||||
|
|
|
@ -5,6 +5,7 @@ use std::fs::File;
|
|||
use std::io::BufReader;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use zokrates_ast::ir::ProgRefIterator;
|
||||
use zokrates_ast::typed::types::GTupleType;
|
||||
use zokrates_ast::typed::ConcreteSignature;
|
||||
use zokrates_ast::typed::ConcreteType;
|
||||
|
@ -178,7 +179,13 @@ fn compile_and_run<T: Field>(t: Tests) {
|
|||
.unwrap()
|
||||
};
|
||||
|
||||
let output = interpreter.execute(bin.clone(), &input);
|
||||
let bin = ProgRefIterator {
|
||||
arguments: bin.arguments.clone(),
|
||||
return_count: bin.return_count,
|
||||
statements: bin.statements.iter(),
|
||||
};
|
||||
|
||||
let output = interpreter.execute(bin, &input);
|
||||
|
||||
use zokrates_abi::Decode;
|
||||
|
||||
|
|
Loading…
Reference in a new issue