From fc51968c476509f0d30755ece088889a1e4c64a3 Mon Sep 17 00:00:00 2001 From: Dennis Kuhnert Date: Thu, 23 Mar 2017 19:56:25 +0100 Subject: [PATCH] Format and inport changes --- lib/wraplibsnark.cpp | 169 ++++++++++++++++++++++--------------------- lib/wraplibsnark.hpp | 10 +-- src/field.rs | 6 -- src/libsnark.rs | 6 +- 4 files changed, 92 insertions(+), 99 deletions(-) diff --git a/lib/wraplibsnark.cpp b/lib/wraplibsnark.cpp index 246d1343..0ff189c7 100644 --- a/lib/wraplibsnark.cpp +++ b/lib/wraplibsnark.cpp @@ -32,120 +32,121 @@ using namespace libsnark; // conversion byte[32] <-> libsnark bigint. libsnark::bigint libsnarkBigintFromBytes(const uint8_t* _x) { - libsnark::bigint x; + libsnark::bigint 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 createConstraintSystem(const uint8_t* A, const uint8_t* B, const uint8_t* C, const uint8_t* witness, int constraints, int variables) { - r1cs_constraint_system > cs; - cs.primary_input_size = variables - 1; - cs.auxiliary_input_size = 0; + r1cs_constraint_system > cs; + cs.primary_input_size = variables - 1; + cs.auxiliary_input_size = 0; - cout << "num variables: " << variables < > lin_comb_A, lin_comb_B, lin_comb_C; - linear_combination > lin_comb_A, lin_comb_B, lin_comb_C; + for (int idx=0; idx 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 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 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 >(lin_comb_A, lin_comb_B, lin_comb_C)); + } + for (int idx=0; idx 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 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 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 >(lin_comb_A, lin_comb_B, lin_comb_C)); - } - for (int idx=0; idx generateKeypair(const r1cs_ppzksnark_constraint_system &cs){ - // from r1cs_ppzksnark.hpp - return r1cs_ppzksnark_generator(cs); + // from r1cs_ppzksnark.hpp + return r1cs_ppzksnark_generator(cs); } // TODO: Check with solidity format. Also, is IC_Query needed? void printVerificationKey(r1cs_ppzksnark_keypair 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 > cs; - cs = createConstraintSystem(A,B,C,witness,constraints,variables); + // Setup: + // create constraint system + r1cs_constraint_system > cs; + cs = createConstraintSystem(A,B,C,witness,constraints,variables); - // assign variables - r1cs_variable_assignment > full_variable_assignment; - for (int i = 1; i < variables; i++) { - full_variable_assignment.push_back(witness[i]); - } + // assign variables + r1cs_variable_assignment > 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 > primary_input(full_variable_assignment.begin(), full_variable_assignment.begin() + variables - 1); - r1cs_primary_input > 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 > primary_input(full_variable_assignment.begin(), full_variable_assignment.begin() + variables - 1); + r1cs_primary_input > 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 keypair = r1cs_ppzksnark_generator(cs); + // create keypair + r1cs_ppzksnark_keypair keypair = r1cs_ppzksnark_generator(cs); - // Print VerificationKey - printVerificationKey(keypair); + // Print VerificationKey + printVerificationKey(keypair); - // Proof Generation - r1cs_ppzksnark_proof proof = r1cs_ppzksnark_prover(keypair.pk, primary_input, auxiliary_input); + // Proof Generation + r1cs_ppzksnark_proof proof = r1cs_ppzksnark_prover(keypair.pk, primary_input, auxiliary_input); - // Verification - bool result = r1cs_ppzksnark_verifier_strong_IC(keypair.vk, primary_input, proof); + // Verification + bool result = r1cs_ppzksnark_verifier_strong_IC(keypair.vk, primary_input, proof); - return result; + return result; } diff --git a/lib/wraplibsnark.hpp b/lib/wraplibsnark.hpp index 5a660f12..234c3eed 100644 --- a/lib/wraplibsnark.hpp +++ b/lib/wraplibsnark.hpp @@ -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" diff --git a/src/field.rs b/src/field.rs index accf9896..085f508a 100644 --- a/src/field.rs +++ b/src/field.rs @@ -27,8 +27,6 @@ pub trait Field : From + From + From + for<'a> From<&'a str> + Div + for<'a> Div<&'a Self, Output=Self> + Pow + Pow + 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; /// Returns this `Field`'s contents as little-endian byte vector fn into_byte_vector(&self) -> Vec; /// 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 { - self.value.to_str_radix(10).to_string().into_bytes() - } - fn into_byte_vector(&self) -> Vec { ////for debugging //println!("uint dec: {}\n",self.value.to_biguint().unwrap().to_str_radix(10)); diff --git a/src/libsnark.rs b/src/libsnark.rs index be6e2bf6..4db9580e 100644 --- a/src/libsnark.rs +++ b/src/libsnark.rs @@ -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;32]{ #[cfg(test)] mod tests { use super::*; + use field::FieldPrime; + use num::bigint::{BigUint}; #[cfg(test)] mod libsnark_integration {