simplify vk parsing in export verifier, fix typos
This commit is contained in:
parent
8cf3084186
commit
071bcc6096
7 changed files with 155 additions and 182 deletions
|
@ -25,7 +25,7 @@ using std::endl;
|
|||
|
||||
namespace gm17
|
||||
{
|
||||
r1cs_se_ppzksnark_constraint_system<libff::alt_bn128_pp> createConstraintSystem(const uint8_t* A, const uint8_t* B, const uint8_t* C, int32_t A_len, int32_t B_len, int32_t C_len, int32_t constraints, int32_t variables, int32_t inputs)
|
||||
r1cs_se_ppzksnark_constraint_system<libff::alt_bn128_pp> createConstraintSystem(const uint8_t* a, const uint8_t* b, const uint8_t* c, int32_t a_len, int32_t b_len, int32_t c_len, int32_t constraints, int32_t variables, int32_t inputs)
|
||||
{
|
||||
r1cs_se_ppzksnark_constraint_system<libff::alt_bn128_pp> cs;
|
||||
cs.primary_input_size = inputs;
|
||||
|
@ -41,13 +41,13 @@ namespace gm17
|
|||
uint8_t variable_value[32];
|
||||
};
|
||||
|
||||
const VariableValueMapping* A_vvmap = (VariableValueMapping*) A;
|
||||
const VariableValueMapping* B_vvmap = (VariableValueMapping*) B;
|
||||
const VariableValueMapping* C_vvmap = (VariableValueMapping*) C;
|
||||
const VariableValueMapping* a_vvmap = (VariableValueMapping*) a;
|
||||
const VariableValueMapping* b_vvmap = (VariableValueMapping*) b;
|
||||
const VariableValueMapping* c_vvmap = (VariableValueMapping*) c;
|
||||
|
||||
int A_id = 0;
|
||||
int B_id = 0;
|
||||
int C_id = 0;
|
||||
int a_id = 0;
|
||||
int b_id = 0;
|
||||
int c_id = 0;
|
||||
|
||||
// initialize curve parameters
|
||||
libff::alt_bn128_pp::init_public_params();
|
||||
|
@ -55,26 +55,26 @@ namespace gm17
|
|||
for (int row = 0; row < constraints; row++)
|
||||
{
|
||||
linear_combination<libff::Fr<libff::alt_bn128_pp> > lin_comb_A, lin_comb_B, lin_comb_C;
|
||||
while (A_id < A_len && A_vvmap[A_id].constraint_id == row) {
|
||||
libff::bigint<libff::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(A_vvmap[A_id].variable_value);
|
||||
while (a_id < a_len && a_vvmap[a_id].constraint_id == row) {
|
||||
libff::bigint<libff::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(a_vvmap[a_id].variable_value);
|
||||
if (!value.is_zero()) {
|
||||
lin_comb_A.add_term(A_vvmap[A_id].variable_id, value);
|
||||
lin_comb_A.add_term(a_vvmap[a_id].variable_id, value);
|
||||
}
|
||||
A_id++;
|
||||
a_id++;
|
||||
}
|
||||
while (B_id < B_len && B_vvmap[B_id].constraint_id == row) {
|
||||
libff::bigint<libff::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(B_vvmap[B_id].variable_value);
|
||||
while (b_id < b_len && b_vvmap[b_id].constraint_id == row) {
|
||||
libff::bigint<libff::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(b_vvmap[b_id].variable_value);
|
||||
if (!value.is_zero()) {
|
||||
lin_comb_B.add_term(B_vvmap[B_id].variable_id, value);
|
||||
lin_comb_B.add_term(b_vvmap[b_id].variable_id, value);
|
||||
}
|
||||
B_id++;
|
||||
b_id++;
|
||||
}
|
||||
while (C_id < C_len && C_vvmap[C_id].constraint_id == row) {
|
||||
libff::bigint<libff::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(C_vvmap[C_id].variable_value);
|
||||
while (c_id < c_len && c_vvmap[c_id].constraint_id == row) {
|
||||
libff::bigint<libff::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(c_vvmap[c_id].variable_value);
|
||||
if (!value.is_zero()) {
|
||||
lin_comb_C.add_term(C_vvmap[C_id].variable_id, value);
|
||||
lin_comb_C.add_term(c_vvmap[c_id].variable_id, value);
|
||||
}
|
||||
C_id++;
|
||||
c_id++;
|
||||
}
|
||||
cs.add_constraint(r1cs_constraint<libff::Fr<libff::alt_bn128_pp> >(lin_comb_A, lin_comb_B, lin_comb_C));
|
||||
}
|
||||
|
@ -127,7 +127,7 @@ namespace gm17
|
|||
}
|
||||
}
|
||||
|
||||
setup_result_t gm17_setup(const uint8_t* A, const uint8_t* B, const uint8_t* C, int32_t A_len, int32_t B_len, int32_t C_len, int32_t constraints, int32_t variables, int32_t inputs)
|
||||
setup_result_t gm17_setup(const uint8_t* A, const uint8_t* B, const uint8_t* C, int32_t a_len, int32_t b_len, int32_t c_len, int32_t constraints, int32_t variables, int32_t inputs)
|
||||
{
|
||||
libff::inhibit_profiling_info = true;
|
||||
libff::inhibit_profiling_counters = true;
|
||||
|
@ -135,7 +135,7 @@ setup_result_t gm17_setup(const uint8_t* A, const uint8_t* B, const uint8_t* C,
|
|||
// initialize curve parameters
|
||||
libff::alt_bn128_pp::init_public_params();
|
||||
|
||||
auto cs = gm17::createConstraintSystem(A, B, C, A_len, B_len, C_len, constraints, variables, inputs);
|
||||
auto cs = gm17::createConstraintSystem(A, B, C, a_len, b_len, c_len, constraints, variables, inputs);
|
||||
assert(cs.num_variables() >= (unsigned)inputs);
|
||||
assert(cs.num_inputs() == (unsigned)inputs);
|
||||
assert(cs.num_constraints() == (unsigned)constraints);
|
||||
|
@ -175,7 +175,7 @@ proof_result_t gm17_generate_proof(buffer_t* pk_buf, const uint8_t* public_input
|
|||
ss >> proving_key;
|
||||
|
||||
// assign variables based on witness values, excludes ~one
|
||||
r1cs_variable_assignment<libff::Fr<libff::alt_bn128_pp> > full_variable_assignment;
|
||||
r1cs_variable_assignment<libff::Fr<libff::alt_bn128_pp>> full_variable_assignment;
|
||||
for (int i = 1; i < public_inputs_length; i++) {
|
||||
full_variable_assignment.push_back(libff::Fr<libff::alt_bn128_pp>(libsnarkBigintFromBytes(public_inputs + i * 32)));
|
||||
}
|
||||
|
|
|
@ -14,12 +14,12 @@ extern "C" {
|
|||
#include "ffi.hpp"
|
||||
|
||||
setup_result_t gm17_setup(
|
||||
const uint8_t* A,
|
||||
const uint8_t* B,
|
||||
const uint8_t* C,
|
||||
int32_t A_len,
|
||||
int32_t B_len,
|
||||
int32_t C_len,
|
||||
const uint8_t* a,
|
||||
const uint8_t* b,
|
||||
const uint8_t* c,
|
||||
int32_t a_len,
|
||||
int32_t b_len,
|
||||
int32_t c_len,
|
||||
int32_t constraints,
|
||||
int32_t variables,
|
||||
int32_t inputs
|
||||
|
|
|
@ -25,7 +25,7 @@ using std::endl;
|
|||
|
||||
namespace pghr13
|
||||
{
|
||||
r1cs_ppzksnark_constraint_system<libff::alt_bn128_pp> createConstraintSystem(const uint8_t* A, const uint8_t* B, const uint8_t* C, int A_len, int B_len, int C_len, int constraints, int variables, int inputs)
|
||||
r1cs_ppzksnark_constraint_system<libff::alt_bn128_pp> createConstraintSystem(const uint8_t* a, const uint8_t* b, const uint8_t* c, int a_len, int b_len, int c_len, int constraints, int variables, int inputs)
|
||||
{
|
||||
r1cs_ppzksnark_constraint_system<libff::alt_bn128_pp> cs;
|
||||
cs.primary_input_size = inputs;
|
||||
|
@ -41,41 +41,41 @@ namespace pghr13
|
|||
uint8_t variable_value[32];
|
||||
};
|
||||
|
||||
const VariableValueMapping* A_vvmap = (VariableValueMapping*) A;
|
||||
const VariableValueMapping* B_vvmap = (VariableValueMapping*) B;
|
||||
const VariableValueMapping* C_vvmap = (VariableValueMapping*) C;
|
||||
const VariableValueMapping* a_vvmap = (VariableValueMapping*) a;
|
||||
const VariableValueMapping* b_vvmap = (VariableValueMapping*) b;
|
||||
const VariableValueMapping* c_vvmap = (VariableValueMapping*) c;
|
||||
|
||||
int A_id = 0;
|
||||
int B_id = 0;
|
||||
int C_id = 0;
|
||||
int a_id = 0;
|
||||
int b_id = 0;
|
||||
int c_id = 0;
|
||||
|
||||
// initialize curve parameters
|
||||
libff::alt_bn128_pp::init_public_params();
|
||||
|
||||
for (int row = 0; row < constraints; row++) {
|
||||
linear_combination<libff::Fr<libff::alt_bn128_pp> > lin_comb_A, lin_comb_B, lin_comb_C;
|
||||
while (A_id < A_len && A_vvmap[A_id].constraint_id == row) {
|
||||
libff::bigint<libff::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(A_vvmap[A_id].variable_value);
|
||||
linear_combination<libff::Fr<libff::alt_bn128_pp>> lin_comb_a, lin_comb_b, lin_comb_c;
|
||||
while (a_id < a_len && a_vvmap[a_id].constraint_id == row) {
|
||||
libff::bigint<libff::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(a_vvmap[a_id].variable_value);
|
||||
if (!value.is_zero()) {
|
||||
lin_comb_A.add_term(A_vvmap[A_id].variable_id, value);
|
||||
lin_comb_a.add_term(a_vvmap[a_id].variable_id, value);
|
||||
}
|
||||
A_id++;
|
||||
a_id++;
|
||||
}
|
||||
while (B_id < B_len && B_vvmap[B_id].constraint_id == row) {
|
||||
libff::bigint<libff::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(B_vvmap[B_id].variable_value);
|
||||
while (b_id < b_len && b_vvmap[b_id].constraint_id == row) {
|
||||
libff::bigint<libff::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(b_vvmap[b_id].variable_value);
|
||||
if (!value.is_zero()) {
|
||||
lin_comb_B.add_term(B_vvmap[B_id].variable_id, value);
|
||||
lin_comb_b.add_term(b_vvmap[b_id].variable_id, value);
|
||||
}
|
||||
B_id++;
|
||||
b_id++;
|
||||
}
|
||||
while (C_id < C_len && C_vvmap[C_id].constraint_id == row) {
|
||||
libff::bigint<libff::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(C_vvmap[C_id].variable_value);
|
||||
while (c_id < c_len && c_vvmap[c_id].constraint_id == row) {
|
||||
libff::bigint<libff::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(c_vvmap[c_id].variable_value);
|
||||
if (!value.is_zero()) {
|
||||
lin_comb_C.add_term(C_vvmap[C_id].variable_id, value);
|
||||
lin_comb_c.add_term(c_vvmap[c_id].variable_id, value);
|
||||
}
|
||||
C_id++;
|
||||
c_id++;
|
||||
}
|
||||
cs.add_constraint(r1cs_constraint<libff::Fr<libff::alt_bn128_pp> >(lin_comb_A, lin_comb_B, lin_comb_C));
|
||||
cs.add_constraint(r1cs_constraint<libff::Fr<libff::alt_bn128_pp>>(lin_comb_a, lin_comb_b, lin_comb_c));
|
||||
}
|
||||
return cs;
|
||||
}
|
||||
|
@ -135,7 +135,7 @@ namespace pghr13
|
|||
}
|
||||
}
|
||||
|
||||
setup_result_t pghr13_setup(const uint8_t* A, const uint8_t* B, const uint8_t* C, int32_t A_len, int32_t B_len, int32_t C_len, int32_t constraints, int32_t variables, int32_t inputs)
|
||||
setup_result_t pghr13_setup(const uint8_t* A, const uint8_t* B, const uint8_t* C, int32_t a_len, int32_t b_len, int32_t c_len, int32_t constraints, int32_t variables, int32_t inputs)
|
||||
{
|
||||
libff::inhibit_profiling_info = true;
|
||||
libff::inhibit_profiling_counters = true;
|
||||
|
@ -143,7 +143,7 @@ setup_result_t pghr13_setup(const uint8_t* A, const uint8_t* B, const uint8_t* C
|
|||
// initialize curve parameters
|
||||
libff::alt_bn128_pp::init_public_params();
|
||||
|
||||
auto cs = pghr13::createConstraintSystem(A, B, C, A_len, B_len, C_len, constraints, variables, inputs);
|
||||
auto cs = pghr13::createConstraintSystem(A, B, C, a_len, b_len, c_len, constraints, variables, inputs);
|
||||
|
||||
assert(cs.num_variables() >= (unsigned)inputs);
|
||||
assert(cs.num_inputs() == (unsigned)inputs);
|
||||
|
@ -184,7 +184,7 @@ proof_result_t pghr13_generate_proof(buffer_t* pk_buf, const uint8_t* public_inp
|
|||
ss >> proving_key;
|
||||
|
||||
// assign variables based on witness values, excludes ~one
|
||||
r1cs_variable_assignment<libff::Fr<libff::alt_bn128_pp> > full_variable_assignment;
|
||||
r1cs_variable_assignment<libff::Fr<libff::alt_bn128_pp>> full_variable_assignment;
|
||||
for (int i = 1; i < public_inputs_length; i++) {
|
||||
full_variable_assignment.push_back(libff::Fr<libff::alt_bn128_pp>(libsnarkBigintFromBytes(public_inputs + i * 32)));
|
||||
}
|
||||
|
|
|
@ -14,12 +14,12 @@ extern "C" {
|
|||
#include "ffi.hpp"
|
||||
|
||||
setup_result_t pghr13_setup(
|
||||
const uint8_t* A,
|
||||
const uint8_t* B,
|
||||
const uint8_t* C,
|
||||
int32_t A_len,
|
||||
int32_t B_len,
|
||||
int32_t C_len,
|
||||
const uint8_t* a,
|
||||
const uint8_t* b,
|
||||
const uint8_t* c,
|
||||
int32_t a_len,
|
||||
int32_t b_len,
|
||||
int32_t c_len,
|
||||
int32_t constraints,
|
||||
int32_t variables,
|
||||
int32_t inputs
|
||||
|
|
|
@ -127,30 +127,12 @@ impl ProofSystem for G16 {
|
|||
let vk_gamma_abc_repeat_regex = Regex::new(r#"(<%vk_gamma_abc_pts%>)"#).unwrap();
|
||||
let vk_input_len_regex = Regex::new(r#"(<%vk_input_length%>)"#).unwrap();
|
||||
|
||||
template_text = vk_regex
|
||||
.replace(
|
||||
template_text.as_str(),
|
||||
vk_map.get("vk.alpha").unwrap().as_str(),
|
||||
)
|
||||
.into_owned();
|
||||
template_text = vk_regex
|
||||
.replace(
|
||||
template_text.as_str(),
|
||||
vk_map.get("vk.beta").unwrap().as_str(),
|
||||
)
|
||||
.into_owned();
|
||||
template_text = vk_regex
|
||||
.replace(
|
||||
template_text.as_str(),
|
||||
vk_map.get("vk.gamma").unwrap().as_str(),
|
||||
)
|
||||
.into_owned();
|
||||
template_text = vk_regex
|
||||
.replace(
|
||||
template_text.as_str(),
|
||||
vk_map.get("vk.delta").unwrap().as_str(),
|
||||
)
|
||||
.into_owned();
|
||||
let keys = vec!["vk.alpha", "vk.beta", "vk.gamma", "vk.delta"];
|
||||
for key in keys.iter() {
|
||||
template_text = vk_regex
|
||||
.replace(template_text.as_str(), vk_map.get(*key).unwrap().as_str())
|
||||
.into_owned();
|
||||
}
|
||||
|
||||
let gamma_abc_count: usize = vk_map.get("vk.gamma_abc.len()").unwrap().parse().unwrap();
|
||||
template_text = vk_gamma_abc_len_regex
|
||||
|
@ -208,8 +190,9 @@ impl ProofSystem for G16 {
|
|||
let pvk: PreparedVerifyingKey<Bn256> = prepare_verifying_key(&vk);
|
||||
let g16_proof: G16Proof = serde_json::from_str(proof.as_str()).unwrap();
|
||||
|
||||
let proof_bytes = base64::decode(g16_proof.raw.as_str()).unwrap();
|
||||
let bellman_proof: Proof<Bn256> = Proof::read(proof_bytes.as_slice()).unwrap();
|
||||
let bellman_proof: Proof<Bn256> =
|
||||
Proof::read(base64::decode(g16_proof.raw.as_str()).unwrap().as_slice())
|
||||
.expect("Could not read proof");
|
||||
|
||||
let public_inputs: Vec<Fr> = g16_proof
|
||||
.inputs
|
||||
|
@ -257,8 +240,8 @@ const CONTRACT_TEMPLATE_V2: &str = r#"
|
|||
contract Verifier {
|
||||
using Pairing for *;
|
||||
struct VerifyingKey {
|
||||
Pairing.G1Point a;
|
||||
Pairing.G2Point b;
|
||||
Pairing.G1Point alpha;
|
||||
Pairing.G2Point beta;
|
||||
Pairing.G2Point gamma;
|
||||
Pairing.G2Point delta;
|
||||
Pairing.G1Point[] gamma_abc;
|
||||
|
@ -269,8 +252,8 @@ contract Verifier {
|
|||
Pairing.G1Point c;
|
||||
}
|
||||
function verifyingKey() pure internal returns (VerifyingKey memory vk) {
|
||||
vk.a = Pairing.G1Point(<%vk_a%>);
|
||||
vk.b = Pairing.G2Point(<%vk_b%>);
|
||||
vk.alpha = Pairing.G1Point(<%vk_alpha%>);
|
||||
vk.beta = Pairing.G2Point(<%vk_beta%>);
|
||||
vk.gamma = Pairing.G2Point(<%vk_gamma%>);
|
||||
vk.delta = Pairing.G2Point(<%vk_delta%>);
|
||||
vk.gamma_abc = new Pairing.G1Point[](<%vk_gamma_abc_length%>);
|
||||
|
@ -291,7 +274,7 @@ contract Verifier {
|
|||
proof.a, proof.b,
|
||||
Pairing.negate(vk_x), vk.gamma,
|
||||
Pairing.negate(proof.c), vk.delta,
|
||||
Pairing.negate(vk.a), vk.b)) return 1;
|
||||
Pairing.negate(vk.alpha), vk.beta)) return 1;
|
||||
return 0;
|
||||
}
|
||||
event Verified(string s);
|
||||
|
@ -317,8 +300,8 @@ const CONTRACT_TEMPLATE: &str = r#"
|
|||
contract Verifier {
|
||||
using Pairing for *;
|
||||
struct VerifyingKey {
|
||||
Pairing.G1Point a;
|
||||
Pairing.G2Point b;
|
||||
Pairing.G1Point alpha;
|
||||
Pairing.G2Point beta;
|
||||
Pairing.G2Point gamma;
|
||||
Pairing.G2Point delta;
|
||||
Pairing.G1Point[] gamma_abc;
|
||||
|
@ -329,8 +312,8 @@ contract Verifier {
|
|||
Pairing.G1Point c;
|
||||
}
|
||||
function verifyingKey() pure internal returns (VerifyingKey memory vk) {
|
||||
vk.a = Pairing.G1Point(<%vk_a%>);
|
||||
vk.b = Pairing.G2Point(<%vk_b%>);
|
||||
vk.alpha = Pairing.G1Point(<%vk_alpha%>);
|
||||
vk.beta = Pairing.G2Point(<%vk_beta%>);
|
||||
vk.gamma = Pairing.G2Point(<%vk_gamma%>);
|
||||
vk.delta = Pairing.G2Point(<%vk_delta%>);
|
||||
vk.gamma_abc = new Pairing.G1Point[](<%vk_gamma_abc_length%>);
|
||||
|
@ -351,7 +334,7 @@ contract Verifier {
|
|||
proof.a, proof.b,
|
||||
Pairing.negate(vk_x), vk.gamma,
|
||||
Pairing.negate(proof.c), vk.delta,
|
||||
Pairing.negate(vk.a), vk.b)) return 1;
|
||||
Pairing.negate(vk.alpha), vk.beta)) return 1;
|
||||
return 0;
|
||||
}
|
||||
event Verified(string s);
|
||||
|
@ -381,10 +364,11 @@ contract Verifier {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::flat_absy::FlatVariable;
|
||||
use crate::ir::{Function, Prog, Statement};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn verify() {
|
||||
let program: Prog<FieldPrime> = Prog {
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
use regex::Regex;
|
||||
|
||||
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::parser::KeyValueParser;
|
||||
use proof_system::bn128::utils::solidity::{
|
||||
SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, SOLIDITY_PAIRING_LIB_V2,
|
||||
};
|
||||
use proof_system::{ProofSystem, SetupKeypair};
|
||||
use regex::Regex;
|
||||
use zokrates_field::field::FieldPrime;
|
||||
|
||||
pub struct GM17 {}
|
||||
|
@ -18,12 +20,12 @@ impl GM17 {
|
|||
|
||||
extern "C" {
|
||||
fn gm17_setup(
|
||||
A: *const u8,
|
||||
B: *const u8,
|
||||
C: *const u8,
|
||||
A_len: i32,
|
||||
B_len: i32,
|
||||
C_len: i32,
|
||||
a: *const u8,
|
||||
b: *const u8,
|
||||
c: *const u8,
|
||||
a_len: i32,
|
||||
b_len: i32,
|
||||
c_len: i32,
|
||||
constraints: i32,
|
||||
variables: i32,
|
||||
inputs: i32,
|
||||
|
@ -31,8 +33,8 @@ extern "C" {
|
|||
|
||||
fn gm17_generate_proof(
|
||||
pk_buf: *mut Buffer,
|
||||
publquery_inputs: *const u8,
|
||||
publquery_inputs_length: i32,
|
||||
public_query_inputs: *const u8,
|
||||
public_query_inputs_length: i32,
|
||||
private_inputs: *const u8,
|
||||
private_inputs_length: i32,
|
||||
) -> ProofResult;
|
||||
|
@ -107,8 +109,7 @@ impl ProofSystem for GM17 {
|
|||
}
|
||||
|
||||
fn export_solidity_verifier(&self, vk: String, abi_v2: bool) -> String {
|
||||
let mut lines = vk.lines();
|
||||
|
||||
let vk_map = KeyValueParser::parse(vk);
|
||||
let (mut template_text, solidity_pairing_lib) = if abi_v2 {
|
||||
(
|
||||
String::from(CONTRACT_TEMPLATE_V2),
|
||||
|
@ -121,37 +122,31 @@ impl ProofSystem for GM17 {
|
|||
)
|
||||
};
|
||||
|
||||
let query_template = String::from("vk.query[index] = Pairing.G1Point(points);"); //copy this for each entry
|
||||
|
||||
//replace things in template
|
||||
// replace things in template
|
||||
let vk_regex = Regex::new(r#"(<%vk_[^i%]*%>)"#).unwrap();
|
||||
let vk_query_len_regex = Regex::new(r#"(<%vk_query_length%>)"#).unwrap();
|
||||
let vk_query_index_regex = Regex::new(r#"index"#).unwrap();
|
||||
let vk_query_points_regex = Regex::new(r#"points"#).unwrap();
|
||||
let vk_query_repeat_regex = Regex::new(r#"(<%vk_query_pts%>)"#).unwrap();
|
||||
let vk_input_len_regex = Regex::new(r#"(<%vk_input_length%>)"#).unwrap();
|
||||
|
||||
for _ in 0..5 {
|
||||
let current_line: &str = lines
|
||||
.next()
|
||||
.expect("Unexpected end of file in verification key!");
|
||||
let current_line_split: Vec<&str> = current_line.split("=").collect();
|
||||
assert_eq!(current_line_split.len(), 2);
|
||||
let keys = vec![
|
||||
"vk.h",
|
||||
"vk.g_alpha",
|
||||
"vk.h_beta",
|
||||
"vk.g_gamma",
|
||||
"vk.h_gamma",
|
||||
];
|
||||
|
||||
for key in keys.iter() {
|
||||
template_text = vk_regex
|
||||
.replace(template_text.as_str(), current_line_split[1].trim())
|
||||
.replace(template_text.as_str(), vk_map.get(*key).unwrap().as_str())
|
||||
.into_owned();
|
||||
}
|
||||
|
||||
let current_line: &str = lines
|
||||
.next()
|
||||
.expect("Unexpected end of file in verification key!");
|
||||
let current_line_split: Vec<&str> = current_line.split("=").collect();
|
||||
assert_eq!(current_line_split.len(), 2);
|
||||
let query_count: i32 = current_line_split[1].trim().parse().unwrap();
|
||||
|
||||
let query_count: usize = vk_map.get("vk.query.len()").unwrap().parse().unwrap();
|
||||
template_text = vk_query_len_regex
|
||||
.replace(template_text.as_str(), format!("{}", query_count).as_str())
|
||||
.into_owned();
|
||||
|
||||
template_text = vk_input_len_regex
|
||||
.replace(
|
||||
template_text.as_str(),
|
||||
|
@ -161,23 +156,22 @@ impl ProofSystem for GM17 {
|
|||
|
||||
let mut query_repeat_text = String::new();
|
||||
for x in 0..query_count {
|
||||
let mut curr_template = query_template.clone();
|
||||
let current_line: &str = lines
|
||||
.next()
|
||||
.expect("Unexpected end of file in verification key!");
|
||||
let current_line_split: Vec<&str> = current_line.split("=").collect();
|
||||
assert_eq!(current_line_split.len(), 2);
|
||||
curr_template = vk_query_index_regex
|
||||
.replace(curr_template.as_str(), format!("{}", x).as_str())
|
||||
.into_owned();
|
||||
curr_template = vk_query_points_regex
|
||||
.replace(curr_template.as_str(), current_line_split[1].trim())
|
||||
.into_owned();
|
||||
query_repeat_text.push_str(curr_template.as_str());
|
||||
query_repeat_text.push_str(
|
||||
format!(
|
||||
"vk.query[{}] = Pairing.G1Point({});",
|
||||
x,
|
||||
vk_map
|
||||
.get(format!("vk.query[{}]", x).as_str())
|
||||
.unwrap()
|
||||
.as_str()
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
if x < query_count - 1 {
|
||||
query_repeat_text.push_str("\n ");
|
||||
}
|
||||
}
|
||||
|
||||
template_text = vk_query_repeat_regex
|
||||
.replace(template_text.as_str(), query_repeat_text.as_str())
|
||||
.into_owned();
|
||||
|
@ -191,7 +185,7 @@ impl ProofSystem for GM17 {
|
|||
)
|
||||
}
|
||||
|
||||
fn verify(&self, vk: String, proof: String) -> bool {
|
||||
fn verify(&self, _vk: String, _proof: String) -> bool {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
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::parser::KeyValueParser;
|
||||
use proof_system::bn128::utils::solidity::{
|
||||
SOLIDITY_G2_ADDITION_LIB, SOLIDITY_PAIRING_LIB, SOLIDITY_PAIRING_LIB_V2,
|
||||
};
|
||||
|
@ -18,12 +19,12 @@ impl PGHR13 {
|
|||
|
||||
extern "C" {
|
||||
fn pghr13_setup(
|
||||
A: *const u8,
|
||||
B: *const u8,
|
||||
C: *const u8,
|
||||
A_len: i32,
|
||||
B_len: i32,
|
||||
C_len: i32,
|
||||
a: *const u8,
|
||||
b: *const u8,
|
||||
c: *const u8,
|
||||
a_len: i32,
|
||||
b_len: i32,
|
||||
c_len: i32,
|
||||
constraints: i32,
|
||||
variables: i32,
|
||||
inputs: i32,
|
||||
|
@ -31,8 +32,8 @@ extern "C" {
|
|||
|
||||
fn pghr13_generate_proof(
|
||||
pk_buf: *mut Buffer,
|
||||
publquery_inputs: *const u8,
|
||||
publquery_inputs_length: i32,
|
||||
public_query_inputs: *const u8,
|
||||
public_query_inputs_length: i32,
|
||||
private_inputs: *const u8,
|
||||
private_inputs_length: i32,
|
||||
) -> ProofResult;
|
||||
|
@ -108,8 +109,7 @@ impl ProofSystem for PGHR13 {
|
|||
}
|
||||
|
||||
fn export_solidity_verifier(&self, vk: String, abi_v2: bool) -> String {
|
||||
let mut lines = vk.lines();
|
||||
|
||||
let vk_map = KeyValueParser::parse(vk);
|
||||
let (mut template_text, solidity_pairing_lib) = if abi_v2 {
|
||||
(
|
||||
String::from(CONTRACT_TEMPLATE_V2),
|
||||
|
@ -122,60 +122,55 @@ impl ProofSystem for PGHR13 {
|
|||
)
|
||||
};
|
||||
|
||||
let ic_template = String::from("vk.ic[index] = Pairing.G1Point(points);"); //copy this for each entry
|
||||
|
||||
//replace things in template
|
||||
// replace things in template
|
||||
let vk_regex = Regex::new(r#"(<%vk_[^i%]*%>)"#).unwrap();
|
||||
let vk_ic_len_regex = Regex::new(r#"(<%vk_ic_length%>)"#).unwrap();
|
||||
let vk_ic_index_regex = Regex::new(r#"index"#).unwrap();
|
||||
let vk_ic_points_regex = Regex::new(r#"points"#).unwrap();
|
||||
let vk_ic_repeat_regex = Regex::new(r#"(<%vk_ic_pts%>)"#).unwrap();
|
||||
let vk_input_len_regex = Regex::new(r#"(<%vk_input_length%>)"#).unwrap();
|
||||
|
||||
for _ in 0..7 {
|
||||
let current_line: &str = lines
|
||||
.next()
|
||||
.expect("Unexpected end of file in verification key!");
|
||||
let current_line_split: Vec<&str> = current_line.split("=").collect();
|
||||
assert_eq!(current_line_split.len(), 2);
|
||||
let keys = vec![
|
||||
"vk.a",
|
||||
"vk.b",
|
||||
"vk.c",
|
||||
"vk.gamma",
|
||||
"vk.gamma_beta_1",
|
||||
"vk.gamma_beta_2",
|
||||
"vk.z",
|
||||
];
|
||||
|
||||
for key in keys.iter() {
|
||||
template_text = vk_regex
|
||||
.replace(template_text.as_str(), current_line_split[1].trim())
|
||||
.replace(template_text.as_str(), vk_map.get(*key).unwrap().as_str())
|
||||
.into_owned();
|
||||
}
|
||||
|
||||
let current_line: &str = lines
|
||||
.next()
|
||||
.expect("Unexpected end of file in verification key!");
|
||||
let current_line_split: Vec<&str> = current_line.split("=").collect();
|
||||
assert_eq!(current_line_split.len(), 2);
|
||||
let ic_count: i32 = current_line_split[1].trim().parse().unwrap();
|
||||
|
||||
let ic_count: usize = vk_map.get("vk.ic.len()").unwrap().parse().unwrap();
|
||||
template_text = vk_ic_len_regex
|
||||
.replace(template_text.as_str(), format!("{}", ic_count).as_str())
|
||||
.into_owned();
|
||||
|
||||
template_text = vk_input_len_regex
|
||||
.replace(template_text.as_str(), format!("{}", ic_count - 1).as_str())
|
||||
.into_owned();
|
||||
|
||||
let mut ic_repeat_text = String::new();
|
||||
for x in 0..ic_count {
|
||||
let mut curr_template = ic_template.clone();
|
||||
let current_line: &str = lines
|
||||
.next()
|
||||
.expect("Unexpected end of file in verification key!");
|
||||
let current_line_split: Vec<&str> = current_line.split("=").collect();
|
||||
assert_eq!(current_line_split.len(), 2);
|
||||
curr_template = vk_ic_index_regex
|
||||
.replace(curr_template.as_str(), format!("{}", x).as_str())
|
||||
.into_owned();
|
||||
curr_template = vk_ic_points_regex
|
||||
.replace(curr_template.as_str(), current_line_split[1].trim())
|
||||
.into_owned();
|
||||
ic_repeat_text.push_str(curr_template.as_str());
|
||||
ic_repeat_text.push_str(
|
||||
format!(
|
||||
"vk.ic[{}] = Pairing.G1Point({});",
|
||||
x,
|
||||
vk_map
|
||||
.get(format!("vk.ic[{}]", x).as_str())
|
||||
.unwrap()
|
||||
.as_str()
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
if x < ic_count - 1 {
|
||||
ic_repeat_text.push_str("\n ");
|
||||
}
|
||||
}
|
||||
|
||||
template_text = vk_ic_repeat_regex
|
||||
.replace(template_text.as_str(), ic_repeat_text.as_str())
|
||||
.into_owned();
|
||||
|
@ -189,7 +184,7 @@ impl ProofSystem for PGHR13 {
|
|||
)
|
||||
}
|
||||
|
||||
fn verify(&self, vk: String, proof: String) -> bool {
|
||||
fn verify(&self, _vk: String, _proof: String) -> bool {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue