Format and inport changes
This commit is contained in:
parent
d26f7c6382
commit
fc51968c47
4 changed files with 92 additions and 99 deletions
|
@ -32,120 +32,121 @@ using namespace libsnark;
|
|||
// conversion byte[32] <-> libsnark bigint.
|
||||
libsnark::bigint<libsnark::alt_bn128_r_limbs> libsnarkBigintFromBytes(const uint8_t* _x)
|
||||
{
|
||||
libsnark::bigint<libsnark::alt_bn128_r_limbs> x;
|
||||
libsnark::bigint<libsnark::alt_bn128_r_limbs> x;
|
||||
|
||||
for (unsigned i = 0; i < 4; i++)
|
||||
for (unsigned j = 0; j < 8; j++)
|
||||
x.data[3 - i] |= uint64_t(_x[i * 8 + j]) << (8 * (7-j));
|
||||
return x;
|
||||
for (unsigned i = 0; i < 4; i++) {
|
||||
for (unsigned j = 0; j < 8; j++) {
|
||||
x.data[3 - i] |= uint64_t(_x[i * 8 + j]) << (8 * (7-j));
|
||||
}
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
//takes input and puts it into constraint system
|
||||
r1cs_ppzksnark_constraint_system<alt_bn128_pp> createConstraintSystem(const uint8_t* A, const uint8_t* B, const uint8_t* C, const uint8_t* witness, int constraints, int variables)
|
||||
{
|
||||
r1cs_constraint_system<Fr<alt_bn128_pp> > cs;
|
||||
cs.primary_input_size = variables - 1;
|
||||
cs.auxiliary_input_size = 0;
|
||||
r1cs_constraint_system<Fr<alt_bn128_pp> > cs;
|
||||
cs.primary_input_size = variables - 1;
|
||||
cs.auxiliary_input_size = 0;
|
||||
|
||||
cout << "num variables: " << variables <<endl;
|
||||
cout << "num constraints: " << constraints <<endl;
|
||||
cout << "num variables: " << variables <<endl;
|
||||
cout << "num constraints: " << constraints <<endl;
|
||||
|
||||
for (int row = 0; row < constraints; row++) {
|
||||
for (int row = 0; row < constraints; row++) {
|
||||
linear_combination<Fr<alt_bn128_pp> > lin_comb_A, lin_comb_B, lin_comb_C;
|
||||
|
||||
linear_combination<Fr<alt_bn128_pp> > lin_comb_A, lin_comb_B, lin_comb_C;
|
||||
for (int idx=0; idx<variables; idx++) {
|
||||
libsnark::bigint<libsnark::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(A+row*variables*32 + idx*32);
|
||||
// cout << "C entry " << idx << " in row " << row << ": " << value << endl;
|
||||
if (!value.is_zero()) {
|
||||
cout << "A(" << idx << ", " << value << ")" << endl;
|
||||
lin_comb_A.add_term(idx, value);
|
||||
}
|
||||
}
|
||||
for (int idx=0; idx<variables; idx++) {
|
||||
libsnark::bigint<libsnark::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(B+row*variables*32 + idx*32);
|
||||
// cout << "B entry " << idx << " in row " << row << ": " << value << endl;
|
||||
if (!value.is_zero()) {
|
||||
cout << "B(" << idx << ", " << value << ")" << endl;
|
||||
lin_comb_B.add_term(idx, value);
|
||||
}
|
||||
}
|
||||
for (int idx=0; idx<variables; idx++) {
|
||||
libsnark::bigint<libsnark::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(C+row*variables*32 + idx*32);
|
||||
// cout << "C entry " << idx << " in row " << row << ": " << value << endl;
|
||||
if (!value.is_zero()) {
|
||||
cout << "C(" << idx << ", " << value << ")" << endl;
|
||||
lin_comb_C.add_term(idx, value);
|
||||
}
|
||||
}
|
||||
cs.add_constraint(r1cs_constraint<Fr<alt_bn128_pp> >(lin_comb_A, lin_comb_B, lin_comb_C));
|
||||
}
|
||||
for (int idx=0; idx<variables; idx++) {
|
||||
cout << "witness entry " << idx << ": " << libsnarkBigintFromBytes(witness + idx*32) << endl;
|
||||
}
|
||||
|
||||
for (int idx=0; idx<variables; idx++) {
|
||||
libsnark::bigint<libsnark::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(A+row*variables*32 + idx*32);
|
||||
// cout << "C entry " << idx << " in row " << row << ": " << value << endl;
|
||||
if (!value.is_zero()) {
|
||||
cout << "A(" << idx << ", " << value << ")" << endl;
|
||||
lin_comb_A.add_term(idx, value);
|
||||
}
|
||||
}
|
||||
for (int idx=0; idx<variables; idx++) {
|
||||
libsnark::bigint<libsnark::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(B+row*variables*32 + idx*32);
|
||||
// cout << "B entry " << idx << " in row " << row << ": " << value << endl;
|
||||
if (!value.is_zero()) {
|
||||
cout << "B(" << idx << ", " << value << ")" << endl;
|
||||
lin_comb_B.add_term(idx, value);
|
||||
}
|
||||
}
|
||||
for (int idx=0; idx<variables; idx++) {
|
||||
libsnark::bigint<libsnark::alt_bn128_r_limbs> value = libsnarkBigintFromBytes(C+row*variables*32 + idx*32);
|
||||
// cout << "C entry " << idx << " in row " << row << ": " << value << endl;
|
||||
if (!value.is_zero()) {
|
||||
cout << "C(" << idx << ", " << value << ")" << endl;
|
||||
lin_comb_C.add_term(idx, value);
|
||||
}
|
||||
}
|
||||
cs.add_constraint(r1cs_constraint<Fr<alt_bn128_pp> >(lin_comb_A, lin_comb_B, lin_comb_C));
|
||||
}
|
||||
for (int idx=0; idx<variables; idx++) {
|
||||
cout << "witness entry " << idx << ": " << libsnarkBigintFromBytes(witness + idx*32) << endl;
|
||||
}
|
||||
|
||||
return cs;
|
||||
return cs;
|
||||
}
|
||||
|
||||
// keypair generateKeypair(constraints)
|
||||
r1cs_ppzksnark_keypair<alt_bn128_pp> generateKeypair(const r1cs_ppzksnark_constraint_system<alt_bn128_pp> &cs){
|
||||
// from r1cs_ppzksnark.hpp
|
||||
return r1cs_ppzksnark_generator<alt_bn128_pp>(cs);
|
||||
// from r1cs_ppzksnark.hpp
|
||||
return r1cs_ppzksnark_generator<alt_bn128_pp>(cs);
|
||||
}
|
||||
|
||||
// TODO: Check with solidity format. Also, is IC_Query needed?
|
||||
void printVerificationKey(r1cs_ppzksnark_keypair<alt_bn128_pp> keypair){
|
||||
printf("Verification key:\n");
|
||||
printf("vk.alphaA_g2: "); keypair.vk.alphaA_g2.print();
|
||||
printf("\nvk.alphaB_g1: "); keypair.vk.alphaB_g1.print();
|
||||
printf("\nvk.alphaC_g2: "); keypair.vk.alphaC_g2.print();
|
||||
printf("\nvk.gamma_g2: "); keypair.vk.gamma_g2.print();
|
||||
printf("\nvk.gamma_beta_g1: "); keypair.vk.gamma_beta_g1.print();
|
||||
printf("\nvk.gamma_beta_g2: "); keypair.vk.gamma_beta_g2.print();
|
||||
printf("\nvk.rC_Z_g2: "); keypair.vk.rC_Z_g2.print();
|
||||
//printf("\nvk.encoded_IC_query: "); keypair.vk.encoded_IC_query.print();
|
||||
printf("Verification key:\n");
|
||||
printf("vk.alphaA_g2: "); keypair.vk.alphaA_g2.print();
|
||||
printf("\nvk.alphaB_g1: "); keypair.vk.alphaB_g1.print();
|
||||
printf("\nvk.alphaC_g2: "); keypair.vk.alphaC_g2.print();
|
||||
printf("\nvk.gamma_g2: "); keypair.vk.gamma_g2.print();
|
||||
printf("\nvk.gamma_beta_g1: "); keypair.vk.gamma_beta_g1.print();
|
||||
printf("\nvk.gamma_beta_g2: "); keypair.vk.gamma_beta_g2.print();
|
||||
printf("\nvk.rC_Z_g2: "); keypair.vk.rC_Z_g2.print();
|
||||
//printf("\nvk.encoded_IC_query: "); keypair.vk.encoded_IC_query.print();
|
||||
}
|
||||
|
||||
|
||||
bool _run_libsnark(const uint8_t* A, const uint8_t* B, const uint8_t* C, const uint8_t* witness, int constraints, int variables)
|
||||
{
|
||||
// Setup:
|
||||
// create constraint system
|
||||
r1cs_constraint_system<Fr<alt_bn128_pp> > cs;
|
||||
cs = createConstraintSystem(A,B,C,witness,constraints,variables);
|
||||
// Setup:
|
||||
// create constraint system
|
||||
r1cs_constraint_system<Fr<alt_bn128_pp> > cs;
|
||||
cs = createConstraintSystem(A,B,C,witness,constraints,variables);
|
||||
|
||||
// assign variables
|
||||
r1cs_variable_assignment<Fr<alt_bn128_pp> > full_variable_assignment;
|
||||
for (int i = 1; i < variables; i++) {
|
||||
full_variable_assignment.push_back(witness[i]);
|
||||
}
|
||||
// assign variables
|
||||
r1cs_variable_assignment<Fr<alt_bn128_pp> > full_variable_assignment;
|
||||
for (int i = 1; i < variables; i++) {
|
||||
full_variable_assignment.push_back(witness[i]);
|
||||
}
|
||||
|
||||
//split up variables into primary and auxiliary inputs
|
||||
// TODO: Check whether this is consistent with inputs from VerifiableStatementCompiler
|
||||
r1cs_primary_input<Fr<alt_bn128_pp> > primary_input(full_variable_assignment.begin(), full_variable_assignment.begin() + variables - 1);
|
||||
r1cs_primary_input<Fr<alt_bn128_pp> > auxiliary_input(full_variable_assignment.begin() + variables - 1, full_variable_assignment.end());
|
||||
//split up variables into primary and auxiliary inputs
|
||||
// TODO: Check whether this is consistent with inputs from VerifiableStatementCompiler
|
||||
r1cs_primary_input<Fr<alt_bn128_pp> > primary_input(full_variable_assignment.begin(), full_variable_assignment.begin() + variables - 1);
|
||||
r1cs_primary_input<Fr<alt_bn128_pp> > auxiliary_input(full_variable_assignment.begin() + variables - 1, full_variable_assignment.end());
|
||||
|
||||
// sanity checks
|
||||
assert(cs.num_variables() == full_variable_assignment.size());
|
||||
assert(cs.num_variables() >= variables - 1);
|
||||
assert(cs.num_inputs() == variables - 1);
|
||||
assert(cs.num_constraints() == constraints);
|
||||
assert(cs.is_satisfied(primary_input, auxiliary_input));
|
||||
// sanity checks
|
||||
assert(cs.num_variables() == full_variable_assignment.size());
|
||||
assert(cs.num_variables() >= variables - 1);
|
||||
assert(cs.num_inputs() == variables - 1);
|
||||
assert(cs.num_constraints() == constraints);
|
||||
assert(cs.is_satisfied(primary_input, auxiliary_input));
|
||||
|
||||
//initialize curve parameters
|
||||
alt_bn128_pp::init_public_params();
|
||||
//initialize curve parameters
|
||||
alt_bn128_pp::init_public_params();
|
||||
|
||||
// create keypair
|
||||
r1cs_ppzksnark_keypair<alt_bn128_pp> keypair = r1cs_ppzksnark_generator<alt_bn128_pp>(cs);
|
||||
// create keypair
|
||||
r1cs_ppzksnark_keypair<alt_bn128_pp> keypair = r1cs_ppzksnark_generator<alt_bn128_pp>(cs);
|
||||
|
||||
// Print VerificationKey
|
||||
printVerificationKey(keypair);
|
||||
// Print VerificationKey
|
||||
printVerificationKey(keypair);
|
||||
|
||||
// Proof Generation
|
||||
r1cs_ppzksnark_proof<alt_bn128_pp> proof = r1cs_ppzksnark_prover<alt_bn128_pp>(keypair.pk, primary_input, auxiliary_input);
|
||||
// Proof Generation
|
||||
r1cs_ppzksnark_proof<alt_bn128_pp> proof = r1cs_ppzksnark_prover<alt_bn128_pp>(keypair.pk, primary_input, auxiliary_input);
|
||||
|
||||
// Verification
|
||||
bool result = r1cs_ppzksnark_verifier_strong_IC<alt_bn128_pp>(keypair.vk, primary_input, proof);
|
||||
// Verification
|
||||
bool result = r1cs_ppzksnark_verifier_strong_IC<alt_bn128_pp>(keypair.vk, primary_input, proof);
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -13,11 +13,11 @@ extern "C" {
|
|||
|
||||
// entrypoint, wraps the whole process, probably should be removed later
|
||||
bool _run_libsnark(const uint8_t* A,
|
||||
const uint8_t* B,
|
||||
const uint8_t* C,
|
||||
const uint8_t* witness,
|
||||
int constraints,
|
||||
int variables);
|
||||
const uint8_t* B,
|
||||
const uint8_t* C,
|
||||
const uint8_t* witness,
|
||||
int constraints,
|
||||
int variables);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
|
|
|
@ -27,8 +27,6 @@ pub trait Field : From<i32> + From<u32> + From<usize> + for<'a> From<&'a str>
|
|||
+ Div<Self, Output=Self> + for<'a> Div<&'a Self, Output=Self>
|
||||
+ Pow<usize, Output=Self> + Pow<Self, Output=Self> + for<'a> Pow<&'a Self, Output=Self>
|
||||
{
|
||||
/// Returns a byte slice of this `Field`'s contents in decimal `String` representation.
|
||||
fn into_dec_bytes(&self) -> Vec<u8>;
|
||||
/// Returns this `Field`'s contents as little-endian byte vector
|
||||
fn into_byte_vector(&self) -> Vec<u8>;
|
||||
/// Returns the multiplicative inverse, i.e.: self * self.inverse_mul() = Self::one()
|
||||
|
@ -48,10 +46,6 @@ pub struct FieldPrime {
|
|||
}
|
||||
|
||||
impl Field for FieldPrime {
|
||||
fn into_dec_bytes(&self) -> Vec<u8> {
|
||||
self.value.to_str_radix(10).to_string().into_bytes()
|
||||
}
|
||||
|
||||
fn into_byte_vector(&self) -> Vec<u8> {
|
||||
////for debugging
|
||||
//println!("uint dec: {}\n",self.value.to_biguint().unwrap().to_str_radix(10));
|
||||
|
|
|
@ -9,10 +9,6 @@ use self::libc::c_int;
|
|||
use self::libc::uint8_t;
|
||||
use field::Field;
|
||||
|
||||
#[cfg(test)]
|
||||
use field::FieldPrime;
|
||||
use num::bigint::{BigUint};
|
||||
|
||||
#[link(name = "snark")]
|
||||
#[link(name = "supercop")]
|
||||
#[link(name = "gmp")]
|
||||
|
@ -74,6 +70,8 @@ fn vec_as_u8_32_array(vec: &Vec<u8>) -> [u8;32]{
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use field::FieldPrime;
|
||||
use num::bigint::{BigUint};
|
||||
|
||||
#[cfg(test)]
|
||||
mod libsnark_integration {
|
||||
|
|
Loading…
Reference in a new issue