1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00

Add setup keypair struct, add comments on memory allocation

This commit is contained in:
dark64 2019-11-10 18:35:17 +01:00
parent 2c29a0bed5
commit 012b5ee757
5 changed files with 72 additions and 34 deletions

View file

@ -3,21 +3,26 @@ 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::ProofSystem;
use crate::proof_system::{SetupKeypair, ProofSystem};
use bellman::groth16::Parameters;
use regex::Regex;
use std::io::{Cursor, Read};
use zokrates_field::field::FieldPrime;
const G16_WARNING: &str = "WARNING: You are using the G16 scheme which is subject to malleability. See zokrates.github.io/reference/proving_schemes.html#g16-malleability for implications.";
pub struct G16 {}
impl ProofSystem for G16 {
fn setup(&self, program: ir::Prog<FieldPrime>) -> (String, Vec<u8>) {
std::env::set_var("BELLMAN_VERBOSE", "0");
impl G16 {
pub fn new() -> G16 {
G16 {}
}
}
impl ProofSystem for G16 {
fn setup(&self, program: ir::Prog<FieldPrime>) -> SetupKeypair {
std::env::set_var("BELLMAN_VERBOSE", "0");
println!("{}", G16_WARNING);
let parameters = Computation::without_witness(program).setup();
@ -27,13 +32,12 @@ impl ProofSystem for G16 {
cursor.set_position(0);
let vk: String = serialize::serialize_vk(parameters.vk);
let mut pk: Vec<u8> = Vec::new();
cursor
.read_to_end(&mut pk)
.expect("Could not read cursor buffer");
(vk, pk)
SetupKeypair::from(vk, pk)
}
fn generate_proof(

View file

@ -4,7 +4,7 @@ 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::ProofSystem;
use proof_system::{SetupKeypair, ProofSystem};
use regex::Regex;
use zokrates_field::field::FieldPrime;
@ -39,7 +39,7 @@ extern "C" {
}
impl ProofSystem for GM17 {
fn setup(&self, program: ir::Prog<FieldPrime>) -> (String, Vec<u8>) {
fn setup(&self, program: ir::Prog<FieldPrime>) -> SetupKeypair {
let (
a_arr,
b_arr,
@ -52,8 +52,8 @@ impl ProofSystem for GM17 {
num_inputs
) = prepare_setup(program);
unsafe {
let result = gm17_setup(
let keypair = unsafe {
let result: SetupResult = gm17_setup(
a_arr.as_ptr(),
b_arr.as_ptr(),
c_arr.as_ptr(),
@ -65,15 +65,21 @@ impl ProofSystem for GM17 {
num_inputs as i32
);
let vk_buf: Vec<u8> = std::slice::from_raw_parts(result.vk.data, result.vk.length as usize).to_vec();
let vk: String = String::from_utf8(vk_buf).unwrap();
result.vk.free();
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
)
}
fn generate_proof(
@ -92,7 +98,7 @@ impl ProofSystem for GM17 {
let mut pk = proving_key.clone();
let mut pk_buf = Buffer::from_vec(pk.as_mut());
unsafe {
let proof_vec = unsafe {
let result = gm17_generate_proof(
&mut pk_buf as *mut _,
public_inputs_arr[0].as_ptr(),
@ -101,12 +107,16 @@ impl ProofSystem for GM17 {
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();
result.proof.free();
proof_vec
};
String::from_utf8(proof_vec).unwrap()
}
}
fn export_solidity_verifier(&self, vk: String, is_abiv2: bool) -> String {
let mut lines = vk.lines();

View file

@ -4,7 +4,7 @@ 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::ProofSystem;
use proof_system::{SetupKeypair, ProofSystem};
use regex::Regex;
use zokrates_field::field::FieldPrime;
@ -39,7 +39,7 @@ extern "C" {
}
impl ProofSystem for PGHR13 {
fn setup(&self, program: ir::Prog<FieldPrime>) -> (String, Vec<u8>) {
fn setup(&self, program: ir::Prog<FieldPrime>) -> SetupKeypair {
let (
a_arr,
b_arr,
@ -52,8 +52,8 @@ impl ProofSystem for PGHR13 {
num_inputs
) = prepare_setup(program);
unsafe {
let result = pghr13_setup(
let keypair = unsafe {
let result: SetupResult = pghr13_setup(
a_arr.as_ptr(),
b_arr.as_ptr(),
c_arr.as_ptr(),
@ -65,15 +65,21 @@ impl ProofSystem for PGHR13 {
num_inputs as i32
);
let vk_buf: Vec<u8> = std::slice::from_raw_parts(result.vk.data, result.vk.length as usize).to_vec();
let vk: String = String::from_utf8(vk_buf).unwrap();
result.vk.free();
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
)
}
fn generate_proof(
@ -92,7 +98,7 @@ impl ProofSystem for PGHR13 {
let mut pk = proving_key.clone();
let mut pk_buf = Buffer::from_vec(pk.as_mut());
unsafe {
let proof_vec = unsafe {
let result = pghr13_generate_proof(
&mut pk_buf as *mut _,
public_inputs_arr[0].as_ptr(),
@ -102,11 +108,16 @@ impl ProofSystem for PGHR13 {
);
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
};
String::from_utf8(proof_vec).unwrap()
}
}
fn export_solidity_verifier(&self, vk: String, is_abiv2: bool) -> String {
let mut lines = vk.lines();

View file

@ -28,6 +28,8 @@ impl Buffer {
}
}
/// 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) };
}

View file

@ -10,8 +10,19 @@ pub use self::bn128::PGHR13;
use crate::ir;
pub struct SetupKeypair {
pub vk: String,
pub pk: Vec<u8>
}
impl SetupKeypair {
pub fn from(vk: String, pk: Vec<u8>) -> SetupKeypair {
SetupKeypair { vk, pk }
}
}
pub trait ProofSystem {
fn setup(&self, program: ir::Prog<FieldPrime>) -> (String, Vec<u8>);
fn setup(&self, program: ir::Prog<FieldPrime>) -> SetupKeypair;
fn generate_proof(
&self,