backup commit, not working
This commit is contained in:
parent
b15b961ff4
commit
30e62b13ec
2 changed files with 82 additions and 14 deletions
37
src/main.rs
37
src/main.rs
|
@ -11,6 +11,7 @@ extern crate clap;
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
extern crate num; // cli
|
extern crate num; // cli
|
||||||
extern crate serde; // serialization deserialization
|
extern crate serde; // serialization deserialization
|
||||||
|
extern crate serde_json;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
extern crate bincode;
|
extern crate bincode;
|
||||||
|
@ -36,7 +37,7 @@ use absy::Prog;
|
||||||
use parser::parse_program;
|
use parser::parse_program;
|
||||||
use semantics::Checker;
|
use semantics::Checker;
|
||||||
use flatten::Flattener;
|
use flatten::Flattener;
|
||||||
use r1cs::r1cs_program;
|
use r1cs::{r1cs_program, R1CS};
|
||||||
use clap::{App, AppSettings, Arg, SubCommand};
|
use clap::{App, AppSettings, Arg, SubCommand};
|
||||||
#[cfg(not(feature = "nolibsnark"))]
|
#[cfg(not(feature = "nolibsnark"))]
|
||||||
use libsnark::{setup, generate_proof};
|
use libsnark::{setup, generate_proof};
|
||||||
|
@ -50,6 +51,7 @@ fn main() {
|
||||||
const VERIFICATION_CONTRACT_DEFAULT_PATH: &str = "verifier.sol";
|
const VERIFICATION_CONTRACT_DEFAULT_PATH: &str = "verifier.sol";
|
||||||
const WITNESS_DEFAULT_PATH: &str = "witness";
|
const WITNESS_DEFAULT_PATH: &str = "witness";
|
||||||
const VARIABLES_INFORMATION_KEY_DEFAULT_PATH: &str = "variables.inf";
|
const VARIABLES_INFORMATION_KEY_DEFAULT_PATH: &str = "variables.inf";
|
||||||
|
const IMPORT_OUTPUT_DEFAULT_PATH: &str = "import.code";
|
||||||
|
|
||||||
// cli specification using clap library
|
// cli specification using clap library
|
||||||
let matches = App::new("ZoKrates")
|
let matches = App::new("ZoKrates")
|
||||||
|
@ -195,6 +197,26 @@ fn main() {
|
||||||
.default_value(VARIABLES_INFORMATION_KEY_DEFAULT_PATH)
|
.default_value(VARIABLES_INFORMATION_KEY_DEFAULT_PATH)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
.subcommand(SubCommand::with_name("import")
|
||||||
|
.about("Imports a standard R1CS file as .code.")
|
||||||
|
.arg(Arg::with_name("input")
|
||||||
|
.short("i")
|
||||||
|
.long("input")
|
||||||
|
.help("path of verifier.")
|
||||||
|
.value_name("FILE")
|
||||||
|
.takes_value(true)
|
||||||
|
.required(true)
|
||||||
|
)
|
||||||
|
.arg(Arg::with_name("output")
|
||||||
|
.short("o")
|
||||||
|
.long("output")
|
||||||
|
.help("output file path.")
|
||||||
|
.value_name("FILE")
|
||||||
|
.takes_value(true)
|
||||||
|
.required(false)
|
||||||
|
.default_value(IMPORT_OUTPUT_DEFAULT_PATH)
|
||||||
|
)
|
||||||
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
match matches.subcommand() {
|
match matches.subcommand() {
|
||||||
|
@ -569,6 +591,19 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
("import", Some(sub_matches)) => {
|
||||||
|
let path = Path::new(sub_matches.value_of("input").unwrap());
|
||||||
|
println!("Importing R1CS from {}", sub_matches.value_of("input").unwrap());
|
||||||
|
|
||||||
|
let file = match File::open(&path) {
|
||||||
|
Ok(file) => file,
|
||||||
|
Err(why) => panic!("couldn't open {}: {}", path.display(), why),
|
||||||
|
};
|
||||||
|
|
||||||
|
let r1cs: r1cs::R1CS = serde_json::from_reader(file).unwrap();
|
||||||
|
println!("R1CS: {:?}", r1cs);
|
||||||
|
let prog: Prog<FieldPrime> = r1cs::flattened_program(r1cs);
|
||||||
|
}
|
||||||
_ => unimplemented!(), // Either no subcommand or one not tested for...
|
_ => unimplemented!(), // Either no subcommand or one not tested for...
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
59
src/r1cs.rs
59
src/r1cs.rs
|
@ -6,10 +6,23 @@
|
||||||
//! @date 2017
|
//! @date 2017
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::collections::BTreeMap;
|
||||||
use absy::*;
|
use absy::*;
|
||||||
use absy::Expression::*;
|
use absy::Expression::*;
|
||||||
use field::Field;
|
use field::Field;
|
||||||
|
|
||||||
|
// for r1cs import, can be moved.
|
||||||
|
// r1cs data strucutre reflecting JSON standard format:
|
||||||
|
//{variables:["a","b", ... ],
|
||||||
|
//constraints:[
|
||||||
|
// [{offset_1:value_a1,offset2:value_a2,...},{offset1:value_b1,offset2:value_b2,...},{offset1:value_c1,offset2:value_c2,...}]
|
||||||
|
//]}
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct R1CS {
|
||||||
|
variables: Vec<String>,
|
||||||
|
constraints: Vec<Vec<BTreeMap<String, isize>>>,
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a vector of summands of the given `Expression`.
|
/// Returns a vector of summands of the given `Expression`.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
|
@ -338,25 +351,45 @@ pub fn r1cs_program<T: Field>(
|
||||||
/// * Since the matrices in R1CS are usually sparse, the following encoding is used:
|
/// * Since the matrices in R1CS are usually sparse, the following encoding is used:
|
||||||
/// * For each constraint (i.e., row in the R1CS), only non-zero values are supplied and encoded as a tuple (index, value).
|
/// * For each constraint (i.e., row in the R1CS), only non-zero values are supplied and encoded as a tuple (index, value).
|
||||||
///
|
///
|
||||||
/// Example for a row of Matrix A: 0 0 1 2 0 -> (2,1),(3,2)
|
|
||||||
///
|
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `inputs` - Input variables as (Index, Name) name tuples, where index is the column index.
|
/// * r1cs - R1CS in standard JSON data format
|
||||||
/// * `outputs` - Output variables as (Index, Name) name tuples, where index is the column index.
|
|
||||||
/// * `a` - Matrix A in the R1CS.
|
|
||||||
/// * `b` - Matrix B in the R1CS.
|
|
||||||
/// * `c` - Matrix C in the R1CS.
|
|
||||||
|
|
||||||
pub fn flattened_program<T: Field>(
|
pub fn flattened_program<T: Field>(
|
||||||
inputs: Vec<(usize, String)>,
|
r1cs: R1CS
|
||||||
outputs: Vec<(usize, String)>,
|
|
||||||
a: Vec<Vec<(usize, T)>>,
|
|
||||||
b: Vec<Vec<(usize, T)>>,
|
|
||||||
c: Vec<Vec<(usize, T)>>,
|
|
||||||
) -> Prog<T> {
|
) -> Prog<T> {
|
||||||
// initialize variable map with index->name
|
|
||||||
|
|
||||||
|
// statements that constrains are translated to
|
||||||
|
let mut statements: Vec<Statement<T>> = Vec::new();
|
||||||
|
|
||||||
|
for cons in r1cs.constraints {
|
||||||
|
assert!(cons.len() == 3); // entries for a,b,c
|
||||||
|
|
||||||
|
// right hand side of definition
|
||||||
|
let mut rhs: Expression<T>;
|
||||||
|
|
||||||
|
// Expression: c0+c1+c2...
|
||||||
|
for (c_var_offset, c_val) in cons[2] {
|
||||||
|
let counter = 0;
|
||||||
|
let var = r1cs.variables[c_var_offset.parse::<usize>().unwrap()]; // get variable name
|
||||||
|
if counter ==0{
|
||||||
|
rhs = Expression::Identifier(var);
|
||||||
|
} else {
|
||||||
|
rhs = Expression::Add(box rhs, box Expression::Identifier(var));
|
||||||
|
}
|
||||||
|
println!("rhs: {:?}", rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let a_entries = cons[0].clone();
|
||||||
|
let b_entries = cons[1].clone();
|
||||||
|
let c_entries = cons[2].clone();
|
||||||
|
println!("a entries: {:?}", a_entries);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut functs = Vec::new();
|
||||||
|
functs.push(Function{ id: "import".to_owned(), arguments: Vec::new() , statements: statements});
|
||||||
|
Prog{functions: functs}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in a new issue