fix buffer length issue
This commit is contained in:
parent
da062f0e27
commit
592d5dfd58
3 changed files with 100 additions and 47 deletions
|
@ -127,7 +127,7 @@ namespace gm17
|
|||
}
|
||||
}
|
||||
|
||||
void _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, uint8_t* vk_buf, uint8_t* pk_buf)
|
||||
gm17_setup_export_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;
|
||||
|
@ -149,11 +149,19 @@ void _gm17_setup(const uint8_t* A, const uint8_t* B, const uint8_t* C, int32_t A
|
|||
ss << keypair.pk;
|
||||
|
||||
std::string pk = ss.str();
|
||||
memcpy(pk_buf, pk.c_str(), pk.size());
|
||||
memcpy(vk_buf, vk.c_str(), vk.size());
|
||||
|
||||
buffer_t vk_buf, pk_buf;
|
||||
vk_buf.alloc(vk.size());
|
||||
pk_buf.alloc(pk.size());
|
||||
|
||||
vk.copy(reinterpret_cast<char*>(vk_buf.data), vk_buf.length);
|
||||
pk.copy(reinterpret_cast<char*>(pk_buf.data), pk_buf.length);
|
||||
|
||||
gm17_setup_export_t exp(vk_buf, pk_buf);
|
||||
return exp;
|
||||
}
|
||||
|
||||
void _gm17_generate_proof(const uint8_t* pk_buf, int32_t pk_buf_length, const uint8_t* public_inputs, int32_t public_inputs_length, const uint8_t* private_inputs, int32_t private_inputs_length, uint8_t* proof_buf)
|
||||
gm17_generate_proof_t _gm17_generate_proof(buffer_t* pk_buf, const uint8_t* public_inputs, int32_t public_inputs_length, const uint8_t* private_inputs, int32_t private_inputs_length)
|
||||
{
|
||||
libff::inhibit_profiling_info = true;
|
||||
libff::inhibit_profiling_counters = true;
|
||||
|
@ -163,7 +171,7 @@ void _gm17_generate_proof(const uint8_t* pk_buf, int32_t pk_buf_length, const ui
|
|||
r1cs_se_ppzksnark_proving_key<libff::alt_bn128_pp> pk;
|
||||
|
||||
std::stringstream ss;
|
||||
ss.write(reinterpret_cast<const char*>(pk_buf), pk_buf_length);
|
||||
ss.write(reinterpret_cast<const char*>(pk_buf->data), pk_buf->length);
|
||||
ss.rdbuf()->pubseekpos(0, std::ios_base::in);
|
||||
ss >> pk;
|
||||
|
||||
|
@ -189,5 +197,11 @@ void _gm17_generate_proof(const uint8_t* pk_buf, int32_t pk_buf_length, const ui
|
|||
// Proof Generation
|
||||
auto proof = r1cs_se_ppzksnark_prover<libff::alt_bn128_pp>(pk, primary_input, auxiliary_input);
|
||||
auto result = gm17::exportProof(proof, public_inputs, public_inputs_length);
|
||||
memcpy(proof_buf, result.c_str(), result.size());
|
||||
|
||||
buffer_t proof_buf;
|
||||
proof_buf.alloc(result.size());
|
||||
result.copy(reinterpret_cast<char*>(proof_buf.data), proof_buf.length);
|
||||
|
||||
gm17_generate_proof_t gp_exp(proof_buf);
|
||||
return gp_exp;
|
||||
}
|
|
@ -14,7 +14,30 @@ extern "C" {
|
|||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void _gm17_setup(const uint8_t* A,
|
||||
struct buffer_t {
|
||||
uint8_t* data;
|
||||
int32_t length;
|
||||
|
||||
void alloc(int32_t length) {
|
||||
this->data = (uint8_t*)malloc(length);
|
||||
this->length = length;
|
||||
}
|
||||
};
|
||||
|
||||
struct gm17_setup_export_t {
|
||||
buffer_t vk;
|
||||
buffer_t pk;
|
||||
|
||||
gm17_setup_export_t(buffer_t& vk_buf, buffer_t& pk_buf) : vk(vk_buf), pk(pk_buf) { }
|
||||
};
|
||||
|
||||
struct gm17_generate_proof_t {
|
||||
buffer_t proof;
|
||||
|
||||
gm17_generate_proof_t(buffer_t& proof_buf) : proof(proof_buf) { }
|
||||
};
|
||||
|
||||
gm17_setup_export_t _gm17_setup(const uint8_t* A,
|
||||
const uint8_t* B,
|
||||
const uint8_t* C,
|
||||
int32_t A_len,
|
||||
|
@ -22,19 +45,15 @@ void _gm17_setup(const uint8_t* A,
|
|||
int32_t C_len,
|
||||
int32_t constraints,
|
||||
int32_t variables,
|
||||
int32_t inputs,
|
||||
uint8_t* vk_buf,
|
||||
uint8_t* pk_buf
|
||||
);
|
||||
int32_t inputs
|
||||
);
|
||||
|
||||
void _gm17_generate_proof(
|
||||
const uint8_t* pk_buf,
|
||||
int32_t pk_buf_length,
|
||||
gm17_generate_proof_t _gm17_generate_proof(
|
||||
buffer_t* pk_buf,
|
||||
const uint8_t* public_inputs,
|
||||
int32_t public_inputs_length,
|
||||
const uint8_t* private_inputs,
|
||||
int32_t private_inputs_length,
|
||||
uint8_t* proof_buf
|
||||
int32_t private_inputs_length
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -7,9 +7,6 @@ use proof_system::ProofSystem;
|
|||
use regex::Regex;
|
||||
use zokrates_field::field::FieldPrime;
|
||||
|
||||
pub const BS_4096: usize = 4096;
|
||||
pub const BS_8192: usize = 8192;
|
||||
|
||||
pub struct GM17 {}
|
||||
|
||||
impl GM17 {
|
||||
|
@ -18,6 +15,33 @@ impl GM17 {
|
|||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
struct Buffer {
|
||||
data: *mut u8,
|
||||
length: i32
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
struct gm17_setup_export_t {
|
||||
vk: Buffer,
|
||||
pk: Buffer
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
struct gm17_generate_proof_t {
|
||||
proof: Buffer
|
||||
}
|
||||
|
||||
impl Buffer {
|
||||
fn from_vec(v: &mut Vec<u8>) -> Buffer {
|
||||
let length = v.len() as i32;
|
||||
Buffer {
|
||||
data: v.as_mut_ptr(),
|
||||
length
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn _gm17_setup(
|
||||
A: *const u8,
|
||||
|
@ -28,20 +52,16 @@ extern "C" {
|
|||
C_len: i32,
|
||||
constraints: i32,
|
||||
variables: i32,
|
||||
inputs: i32,
|
||||
vk_buf: *mut u8,
|
||||
pk_buf: *mut u8,
|
||||
);
|
||||
inputs: i32
|
||||
) -> gm17_setup_export_t;
|
||||
|
||||
fn _gm17_generate_proof(
|
||||
pk_buf: *const u8,
|
||||
pk_buf_length: i32,
|
||||
pk_buf: *mut Buffer,
|
||||
publquery_inputs: *const u8,
|
||||
publquery_inputs_length: i32,
|
||||
private_inputs: *const u8,
|
||||
private_inputs_length: i32,
|
||||
proof_buf: *mut u8
|
||||
);
|
||||
private_inputs_length: i32
|
||||
) -> gm17_generate_proof_t;
|
||||
}
|
||||
|
||||
impl ProofSystem for GM17 {
|
||||
|
@ -58,11 +78,8 @@ impl ProofSystem for GM17 {
|
|||
num_inputs
|
||||
) = prepare_setup(program);
|
||||
|
||||
let mut vk_buffer = vec![0u8; BS_4096];
|
||||
let mut pk_buffer = vec![0u8; BS_4096];
|
||||
|
||||
unsafe {
|
||||
_gm17_setup(
|
||||
let result: gm17_setup_export_t = _gm17_setup(
|
||||
a_arr.as_ptr(),
|
||||
b_arr.as_ptr(),
|
||||
c_arr.as_ptr(),
|
||||
|
@ -71,14 +88,17 @@ impl ProofSystem for GM17 {
|
|||
c_vec.len() as i32,
|
||||
num_constraints as i32,
|
||||
num_variables as i32,
|
||||
num_inputs as i32,
|
||||
vk_buffer.as_mut_ptr(),
|
||||
pk_buffer.as_mut_ptr()
|
||||
)
|
||||
};
|
||||
num_inputs as i32
|
||||
);
|
||||
|
||||
let vk_str: String = unsafe { String::from_utf8_unchecked(vk_buffer) };
|
||||
(vk_str, pk_buffer)
|
||||
let vk_length = result.vk.length as usize;
|
||||
let vk: String = String::from_raw_parts(result.vk.data, vk_length, vk_length);
|
||||
|
||||
let pk_length = result.pk.length as usize;
|
||||
let pk: Vec<u8> = Vec::from_raw_parts(result.pk.data, pk_length, pk_length);
|
||||
|
||||
(vk, pk)
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_proof(
|
||||
|
@ -94,20 +114,20 @@ impl ProofSystem for GM17 {
|
|||
private_inputs_length,
|
||||
) = prepare_generate_proof(program, witness);
|
||||
|
||||
let mut proof_buffer = vec![0u8; BS_8192];
|
||||
let pk_length = proving_key.len();
|
||||
let mut pk = proving_key.clone();
|
||||
let mut pk_buf = Buffer::from_vec(pk.as_mut());
|
||||
|
||||
unsafe {
|
||||
_gm17_generate_proof(
|
||||
proving_key.as_ptr(),
|
||||
pk_length as i32,
|
||||
let result: gm17_generate_proof_t = _gm17_generate_proof(
|
||||
&mut pk_buf as *mut _,
|
||||
public_inputs_arr[0].as_ptr(),
|
||||
public_inputs_length as i32,
|
||||
private_inputs_arr[0].as_ptr(),
|
||||
private_inputs_length as i32,
|
||||
proof_buffer.as_mut_ptr()
|
||||
private_inputs_length as i32
|
||||
);
|
||||
String::from_utf8_unchecked(proof_buffer)
|
||||
|
||||
let proof_length = result.proof.length as usize;
|
||||
String::from_raw_parts(result.proof.data, proof_length, proof_length)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue