fix warning, remove commented out libc, remove libsnark default
This commit is contained in:
parent
e549d3fddd
commit
1e14266123
14 changed files with 86 additions and 102 deletions
|
@ -6,7 +6,7 @@ repository = "https://github.com/JacobEberhardt/ZoKrates.git"
|
|||
edition = "2018"
|
||||
|
||||
[features]
|
||||
default = ["libsnark"]
|
||||
default = []
|
||||
libsnark = ["zokrates_core/libsnark"]
|
||||
|
||||
[dependencies]
|
||||
|
|
|
@ -271,7 +271,8 @@ fn cli() -> Result<(), String> {
|
|||
|
||||
let mut reader = BufReader::new(file);
|
||||
let mut source = String::new();
|
||||
reader.read_to_string(&mut source)
|
||||
reader
|
||||
.read_to_string(&mut source)
|
||||
.map_err(|why| format!("couldn't open input file {}: {}", path.display(), why))?;
|
||||
|
||||
let program_flattened: ir::Prog<FieldPrime> =
|
||||
|
@ -451,13 +452,15 @@ fn cli() -> Result<(), String> {
|
|||
// write verification key
|
||||
let mut vk_file = File::create(vk_path)
|
||||
.map_err(|why| format!("couldn't create {}: {}", vk_path.display(), why))?;
|
||||
vk_file.write(keypair.vk.as_ref())
|
||||
vk_file
|
||||
.write(keypair.vk.as_ref())
|
||||
.map_err(|why| format!("couldn't write to {}: {}", vk_path.display(), why))?;
|
||||
|
||||
// write proving key
|
||||
let mut pk_file = File::create(pk_path)
|
||||
.map_err(|why| format!("couldn't create {}: {}", pk_path.display(), why))?;
|
||||
pk_file.write(keypair.pk.as_ref())
|
||||
pk_file
|
||||
.write(keypair.pk.as_ref())
|
||||
.map_err(|why| format!("couldn't write to {}: {}", pk_path.display(), why))?;
|
||||
|
||||
println!("Setup completed.");
|
||||
|
@ -475,7 +478,8 @@ fn cli() -> Result<(), String> {
|
|||
let mut reader = BufReader::new(input_file);
|
||||
|
||||
let mut vk = String::new();
|
||||
reader.read_to_string(&mut vk)
|
||||
reader
|
||||
.read_to_string(&mut vk)
|
||||
.map_err(|why| format!("couldn't read {}: {}", input_path.display(), why))?;
|
||||
|
||||
let verifier = scheme.export_solidity_verifier(vk, is_abiv2);
|
||||
|
@ -525,15 +529,17 @@ fn cli() -> Result<(), String> {
|
|||
|
||||
let mut pk: Vec<u8> = Vec::new();
|
||||
let mut pk_reader = BufReader::new(pk_file);
|
||||
pk_reader.read_to_end(&mut pk)
|
||||
pk_reader
|
||||
.read_to_end(&mut pk)
|
||||
.map_err(|why| format!("couldn't read {}: {}", pk_path.display(), why))?;
|
||||
|
||||
let proof = scheme.generate_proof(program, witness, pk);
|
||||
let mut proof_file = File::create(proof_path).unwrap();
|
||||
|
||||
proof_file.write(proof.as_ref())
|
||||
proof_file
|
||||
.write(proof.as_ref())
|
||||
.map_err(|why| format!("couldn't write to {}: {}", proof_path.display(), why))?;
|
||||
|
||||
|
||||
println!("generate-proof successful: {}", format!("{}", proof));
|
||||
}
|
||||
("print-proof", Some(sub_matches)) => {
|
||||
|
@ -623,8 +629,7 @@ mod tests {
|
|||
let mut source = String::new();
|
||||
reader.read_to_string(&mut source).unwrap();
|
||||
|
||||
let _: ir::Prog<FieldPrime> =
|
||||
compile(source, location, Some(fs_resolve)).unwrap();
|
||||
let _: ir::Prog<FieldPrime> = compile(source, location, Some(fs_resolve)).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,6 @@ wasm = ["bellman_ce/wasm", "zokrates_embed/wasm"]
|
|||
multicore = ["bellman_ce/multicore"]
|
||||
|
||||
[dependencies]
|
||||
# libc = "0.2.0"
|
||||
num = {version = "0.1.36", default-features = false}
|
||||
num-bigint = {version = "0.1.36", default-features = false}
|
||||
lazy_static = "1.4"
|
||||
|
|
|
@ -20,7 +20,8 @@ fn main() {
|
|||
let out_path = env::var("OUT_DIR").unwrap();
|
||||
let libsnark_source_path = &PathBuf::from(out_path.clone()).join("libsnark");
|
||||
let libsnark_wrapper_a = String::from("libsnark_wrapper.a");
|
||||
let libsnark_wrapper_path = &PathBuf::from(out_path.clone()).join(PathBuf::from(libsnark_wrapper_a.clone()));
|
||||
let libsnark_wrapper_path =
|
||||
&PathBuf::from(out_path.clone()).join(PathBuf::from(libsnark_wrapper_a.clone()));
|
||||
|
||||
let repo = Repository::open(libsnark_source_path).unwrap_or_else(|_| {
|
||||
remove_dir(libsnark_source_path).ok();
|
||||
|
@ -61,8 +62,14 @@ fn main() {
|
|||
.file("lib/pghr13.cpp")
|
||||
.compile(libsnark_wrapper_a.as_str());
|
||||
|
||||
println!("cargo:rustc-link-search={}", libsnark_wrapper_path.display());
|
||||
println!("cargo:rustc-link-search=native={}", libsnark.join("lib").display());
|
||||
println!(
|
||||
"cargo:rustc-link-search={}",
|
||||
libsnark_wrapper_path.display()
|
||||
);
|
||||
println!(
|
||||
"cargo:rustc-link-search=native={}",
|
||||
libsnark.join("lib").display()
|
||||
);
|
||||
|
||||
println!("cargo:rustc-link-lib=gmp");
|
||||
println!("cargo:rustc-link-lib=gmpxx");
|
||||
|
|
|
@ -1185,7 +1185,7 @@ impl<'ast, T: Field> Flattener<'ast, T> {
|
|||
let ebytes_be = e.to_be_bytes();
|
||||
// convert the bytes to bits, remove leading zeroes (we only need powers up to the highest non-zero bit)
|
||||
let ebits_be: Vec<_> = ebytes_be
|
||||
.into_iter()
|
||||
.iter()
|
||||
.flat_map(|byte| (0..8).rev().map(move |i| byte & (1 << i) != 0)) // byte to bit, big endian
|
||||
.skip_while(|b| !b) // skip trailing false bits
|
||||
.collect();
|
||||
|
|
|
@ -40,4 +40,4 @@ pub mod compile;
|
|||
pub mod flat_absy;
|
||||
pub mod ir;
|
||||
pub mod proof_system;
|
||||
pub mod typed_absy;
|
||||
pub mod typed_absy;
|
||||
|
|
|
@ -3,7 +3,7 @@ use crate::proof_system::bn128::utils::bellman::Computation;
|
|||
use crate::proof_system::bn128::utils::solidity::{
|
||||
SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, SOLIDITY_PAIRING_LIB_V2,
|
||||
};
|
||||
use crate::proof_system::{SetupKeypair, ProofSystem};
|
||||
use crate::proof_system::{ProofSystem, SetupKeypair};
|
||||
use bellman::groth16::Parameters;
|
||||
use regex::Regex;
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use ir;
|
||||
use proof_system::bn128::utils::ffi::{Buffer, ProofResult, SetupResult};
|
||||
use proof_system::bn128::utils::libsnark::{prepare_generate_proof, prepare_setup};
|
||||
use proof_system::bn128::utils::solidity::{
|
||||
SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, SOLIDITY_PAIRING_LIB_V2,
|
||||
};
|
||||
use proof_system::bn128::utils::ffi::{Buffer, SetupResult, ProofResult};
|
||||
use proof_system::{SetupKeypair, ProofSystem};
|
||||
use proof_system::{ProofSystem, SetupKeypair};
|
||||
use regex::Regex;
|
||||
use zokrates_field::field::FieldPrime;
|
||||
|
||||
|
@ -26,7 +26,7 @@ extern "C" {
|
|||
C_len: i32,
|
||||
constraints: i32,
|
||||
variables: i32,
|
||||
inputs: i32
|
||||
inputs: i32,
|
||||
) -> SetupResult;
|
||||
|
||||
fn gm17_generate_proof(
|
||||
|
@ -34,23 +34,14 @@ extern "C" {
|
|||
publquery_inputs: *const u8,
|
||||
publquery_inputs_length: i32,
|
||||
private_inputs: *const u8,
|
||||
private_inputs_length: i32
|
||||
private_inputs_length: i32,
|
||||
) -> ProofResult;
|
||||
}
|
||||
|
||||
impl ProofSystem for GM17 {
|
||||
fn setup(&self, program: ir::Prog<FieldPrime>) -> SetupKeypair {
|
||||
let (
|
||||
a_arr,
|
||||
b_arr,
|
||||
c_arr,
|
||||
a_vec,
|
||||
b_vec,
|
||||
c_vec,
|
||||
num_constraints,
|
||||
num_variables,
|
||||
num_inputs
|
||||
) = prepare_setup(program);
|
||||
let (a_arr, b_arr, c_arr, a_vec, b_vec, c_vec, num_constraints, num_variables, num_inputs) =
|
||||
prepare_setup(program);
|
||||
|
||||
let keypair = unsafe {
|
||||
let result: SetupResult = gm17_setup(
|
||||
|
@ -62,11 +53,13 @@ impl ProofSystem for GM17 {
|
|||
c_vec.len() as i32,
|
||||
num_constraints as i32,
|
||||
num_variables as i32,
|
||||
num_inputs as i32
|
||||
num_inputs as i32,
|
||||
);
|
||||
|
||||
let vk: Vec<u8> = std::slice::from_raw_parts(result.vk.data, result.vk.length as usize).to_vec();
|
||||
let pk: Vec<u8> = std::slice::from_raw_parts(result.pk.data, result.pk.length as usize).to_vec();
|
||||
let vk: Vec<u8> =
|
||||
std::slice::from_raw_parts(result.vk.data, result.vk.length as usize).to_vec();
|
||||
let pk: Vec<u8> =
|
||||
std::slice::from_raw_parts(result.pk.data, result.pk.length as usize).to_vec();
|
||||
|
||||
// Memory is allocated in C and raw pointers are returned to Rust. The caller has to manually
|
||||
// free the memory.
|
||||
|
@ -76,10 +69,7 @@ impl ProofSystem for GM17 {
|
|||
(vk, pk)
|
||||
};
|
||||
|
||||
SetupKeypair::from(
|
||||
String::from_utf8(keypair.0).unwrap(),
|
||||
keypair.1
|
||||
)
|
||||
SetupKeypair::from(String::from_utf8(keypair.0).unwrap(), keypair.1)
|
||||
}
|
||||
|
||||
fn generate_proof(
|
||||
|
@ -88,12 +78,8 @@ impl ProofSystem for GM17 {
|
|||
witness: ir::Witness<FieldPrime>,
|
||||
proving_key: Vec<u8>,
|
||||
) -> String {
|
||||
let (
|
||||
public_inputs_arr,
|
||||
public_inputs_length,
|
||||
private_inputs_arr,
|
||||
private_inputs_length,
|
||||
) = prepare_generate_proof(program, witness);
|
||||
let (public_inputs_arr, public_inputs_length, private_inputs_arr, private_inputs_length) =
|
||||
prepare_generate_proof(program, witness);
|
||||
|
||||
let mut pk = proving_key.clone();
|
||||
let mut pk_buf = Buffer::from_vec(pk.as_mut());
|
||||
|
@ -104,12 +90,14 @@ impl ProofSystem for GM17 {
|
|||
public_inputs_arr[0].as_ptr(),
|
||||
public_inputs_length as i32,
|
||||
private_inputs_arr[0].as_ptr(),
|
||||
private_inputs_length as i32
|
||||
private_inputs_length as i32,
|
||||
);
|
||||
|
||||
// Memory is allocated in C and raw pointers are returned to Rust. The caller has to manually
|
||||
// free the memory.
|
||||
let proof_vec: Vec<u8> = std::slice::from_raw_parts(result.proof.data, result.proof.length as usize).to_vec();
|
||||
let proof_vec: Vec<u8> =
|
||||
std::slice::from_raw_parts(result.proof.data, result.proof.length as usize)
|
||||
.to_vec();
|
||||
result.proof.free();
|
||||
|
||||
proof_vec
|
||||
|
@ -162,10 +150,7 @@ impl ProofSystem for GM17 {
|
|||
let query_count: i32 = current_line_split[1].trim().parse().unwrap();
|
||||
|
||||
template_text = vk_query_len_regex
|
||||
.replace(
|
||||
template_text.as_str(),
|
||||
format!("{}", query_count).as_str()
|
||||
)
|
||||
.replace(template_text.as_str(), format!("{}", query_count).as_str())
|
||||
.into_owned();
|
||||
template_text = vk_input_len_regex
|
||||
.replace(
|
||||
|
@ -345,4 +330,4 @@ contract Verifier {
|
|||
}
|
||||
}
|
||||
}
|
||||
"#;
|
||||
"#;
|
||||
|
|
|
@ -10,4 +10,4 @@ pub use self::g16::G16;
|
|||
#[cfg(feature = "libsnark")]
|
||||
pub use self::gm17::GM17;
|
||||
#[cfg(feature = "libsnark")]
|
||||
pub use self::pghr13::PGHR13;
|
||||
pub use self::pghr13::PGHR13;
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use ir;
|
||||
use proof_system::bn128::utils::ffi::{Buffer, ProofResult, SetupResult};
|
||||
use proof_system::bn128::utils::libsnark::{prepare_generate_proof, prepare_setup};
|
||||
use proof_system::bn128::utils::solidity::{
|
||||
SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, SOLIDITY_PAIRING_LIB_V2,
|
||||
};
|
||||
use proof_system::bn128::utils::ffi::{Buffer, SetupResult, ProofResult};
|
||||
use proof_system::{SetupKeypair, ProofSystem};
|
||||
use proof_system::{ProofSystem, SetupKeypair};
|
||||
use regex::Regex;
|
||||
use zokrates_field::field::FieldPrime;
|
||||
|
||||
|
@ -26,7 +26,7 @@ extern "C" {
|
|||
C_len: i32,
|
||||
constraints: i32,
|
||||
variables: i32,
|
||||
inputs: i32
|
||||
inputs: i32,
|
||||
) -> SetupResult;
|
||||
|
||||
fn pghr13_generate_proof(
|
||||
|
@ -34,23 +34,14 @@ extern "C" {
|
|||
publquery_inputs: *const u8,
|
||||
publquery_inputs_length: i32,
|
||||
private_inputs: *const u8,
|
||||
private_inputs_length: i32
|
||||
private_inputs_length: i32,
|
||||
) -> ProofResult;
|
||||
}
|
||||
|
||||
impl ProofSystem for PGHR13 {
|
||||
fn setup(&self, program: ir::Prog<FieldPrime>) -> SetupKeypair {
|
||||
let (
|
||||
a_arr,
|
||||
b_arr,
|
||||
c_arr,
|
||||
a_vec,
|
||||
b_vec,
|
||||
c_vec,
|
||||
num_constraints,
|
||||
num_variables,
|
||||
num_inputs
|
||||
) = prepare_setup(program);
|
||||
let (a_arr, b_arr, c_arr, a_vec, b_vec, c_vec, num_constraints, num_variables, num_inputs) =
|
||||
prepare_setup(program);
|
||||
|
||||
let keypair = unsafe {
|
||||
let result: SetupResult = pghr13_setup(
|
||||
|
@ -62,24 +53,23 @@ impl ProofSystem for PGHR13 {
|
|||
c_vec.len() as i32,
|
||||
num_constraints as i32,
|
||||
num_variables as i32,
|
||||
num_inputs as i32
|
||||
num_inputs as i32,
|
||||
);
|
||||
|
||||
let vk: Vec<u8> = std::slice::from_raw_parts(result.vk.data, result.vk.length as usize).to_vec();
|
||||
let pk: Vec<u8> = std::slice::from_raw_parts(result.pk.data, result.pk.length as usize).to_vec();
|
||||
let vk: Vec<u8> =
|
||||
std::slice::from_raw_parts(result.vk.data, result.vk.length as usize).to_vec();
|
||||
let pk: Vec<u8> =
|
||||
std::slice::from_raw_parts(result.pk.data, result.pk.length as usize).to_vec();
|
||||
|
||||
// Memory is allocated in C and raw pointers are returned to Rust. The caller has to manually
|
||||
// free the memory.
|
||||
result.vk.free();
|
||||
result.pk.free();
|
||||
|
||||
|
||||
(vk, pk)
|
||||
};
|
||||
|
||||
SetupKeypair::from(
|
||||
String::from_utf8(keypair.0).unwrap(),
|
||||
keypair.1
|
||||
)
|
||||
SetupKeypair::from(String::from_utf8(keypair.0).unwrap(), keypair.1)
|
||||
}
|
||||
|
||||
fn generate_proof(
|
||||
|
@ -88,12 +78,8 @@ impl ProofSystem for PGHR13 {
|
|||
witness: ir::Witness<FieldPrime>,
|
||||
proving_key: Vec<u8>,
|
||||
) -> String {
|
||||
let (
|
||||
public_inputs_arr,
|
||||
public_inputs_length,
|
||||
private_inputs_arr,
|
||||
private_inputs_length,
|
||||
) = prepare_generate_proof(program, witness);
|
||||
let (public_inputs_arr, public_inputs_length, private_inputs_arr, private_inputs_length) =
|
||||
prepare_generate_proof(program, witness);
|
||||
|
||||
let mut pk = proving_key.clone();
|
||||
let mut pk_buf = Buffer::from_vec(pk.as_mut());
|
||||
|
@ -104,16 +90,18 @@ impl ProofSystem for PGHR13 {
|
|||
public_inputs_arr[0].as_ptr(),
|
||||
public_inputs_length as i32,
|
||||
private_inputs_arr[0].as_ptr(),
|
||||
private_inputs_length as i32
|
||||
private_inputs_length as i32,
|
||||
);
|
||||
|
||||
let proof_vec: Vec<u8> = std::slice::from_raw_parts(result.proof.data, result.proof.length as usize).to_vec();
|
||||
|
||||
let proof_vec: Vec<u8> =
|
||||
std::slice::from_raw_parts(result.proof.data, result.proof.length as usize)
|
||||
.to_vec();
|
||||
|
||||
// Memory is allocated in C and raw pointers are returned to Rust. The caller has to manually
|
||||
// free the memory.
|
||||
result.proof.free();
|
||||
|
||||
proof_vec
|
||||
proof_vec
|
||||
};
|
||||
|
||||
String::from_utf8(proof_vec).unwrap()
|
||||
|
@ -372,4 +360,4 @@ const CONTRACT_TEMPLATE: &str = r#"contract Verifier {
|
|||
}
|
||||
}
|
||||
}
|
||||
"#;
|
||||
"#;
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
#[repr(C)]
|
||||
pub struct Buffer {
|
||||
pub data: *mut u8,
|
||||
pub length: i32
|
||||
pub length: i32,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct SetupResult {
|
||||
pub vk: Buffer,
|
||||
pub pk: Buffer
|
||||
pub pk: Buffer,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct ProofResult {
|
||||
pub proof: Buffer
|
||||
pub proof: Buffer,
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
@ -24,13 +24,13 @@ impl Buffer {
|
|||
let length = v.len() as i32;
|
||||
Buffer {
|
||||
data: v.as_mut_ptr(),
|
||||
length
|
||||
length,
|
||||
}
|
||||
}
|
||||
|
||||
/// The purpose of this function is to free memory previously allocated by "malloc"
|
||||
/// The purpose of this function is to free memory previously allocated by "malloc"
|
||||
/// from C standard library. Do not use otherwise.
|
||||
pub fn free(self) {
|
||||
unsafe { __free(self.data) };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ fn vec_as_u8_32_array(vec: &Vec<u8>) -> [u8; 32] {
|
|||
|
||||
// proof-system-independent preparation for the setup phase
|
||||
pub fn prepare_setup<T: Field>(
|
||||
program: ir::Prog<T>
|
||||
program: ir::Prog<T>,
|
||||
) -> (
|
||||
Vec<u8>,
|
||||
Vec<u8>,
|
||||
|
@ -26,7 +26,7 @@ pub fn prepare_setup<T: Field>(
|
|||
Vec<(i32, i32, [u8; 32])>,
|
||||
usize,
|
||||
usize,
|
||||
usize
|
||||
usize,
|
||||
) {
|
||||
// transform to R1CS
|
||||
let (variables, public_variables_count, a, b, c) = r1cs_program(program);
|
||||
|
@ -133,7 +133,7 @@ pub fn prepare_setup<T: Field>(
|
|||
c_vec,
|
||||
num_constraints,
|
||||
num_variables,
|
||||
num_inputs
|
||||
num_inputs,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -170,7 +170,7 @@ pub fn prepare_generate_proof<T: Field>(
|
|||
public_inputs_arr,
|
||||
public_inputs_length,
|
||||
private_inputs_arr,
|
||||
private_inputs_length
|
||||
private_inputs_length,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
pub mod bellman;
|
||||
#[cfg(feature = "libsnark")]
|
||||
pub mod libsnark;
|
||||
#[cfg(feature = "libsnark")]
|
||||
pub mod ffi;
|
||||
pub mod solidity;
|
||||
#[cfg(feature = "libsnark")]
|
||||
pub mod libsnark;
|
||||
pub mod solidity;
|
||||
|
|
|
@ -12,7 +12,7 @@ use crate::ir;
|
|||
|
||||
pub struct SetupKeypair {
|
||||
pub vk: String,
|
||||
pub pk: Vec<u8>
|
||||
pub pk: Vec<u8>,
|
||||
}
|
||||
|
||||
impl SetupKeypair {
|
||||
|
@ -32,4 +32,4 @@ pub trait ProofSystem {
|
|||
) -> String;
|
||||
|
||||
fn export_solidity_verifier(&self, vk: String, is_abiv2: bool) -> String;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue