Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
JS syntax cleanups, Cmake Cleanups, mpz multiply benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryR authored and HarryR committed Oct 17, 2018
1 parent b6f960d commit 5b4f664
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 51 deletions.
34 changes: 19 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ project(ethsnarks)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)

if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# Common compilation flags and warning configuration
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wfatal-errors -Wno-unused-variable")
if("${MULTICORE}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
endif()
# Default optimizations flags (to override, use -DOPT_FLAGS=...)
endif()


set(
Expand Down Expand Up @@ -60,18 +52,25 @@ option(
OFF
)

option(
USE_MIXED_ADDITION
"Convert each element of the key pair to affine coordinates"
OFF
)

option(
BINARY_OUTPUT
"Use binary output for serialisation"
ON
)

option(
PERFORMANCE
"Enable link-time and aggressive optimizations"
OFF
MONTGOMERY_OUTPUT
"Serialize Fp elements as their Montgomery representations (faster but not human-readable)"
ON
)


option(
WITH_PROCPS
"Use procps for memory profiling"
Expand Down Expand Up @@ -103,8 +102,6 @@ endif()

if("${DEBUG}" OR "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
add_definitions(-DDEBUG=1)
else()
add_compile_options(-O3)
endif()


Expand Down Expand Up @@ -143,6 +140,15 @@ else()
)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# Common compilation flags and warning configuration
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wfatal-errors -Wno-unused-variable")
if("${MULTICORE}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
endif()
# Default optimizations flags (to override, use -DOPT_FLAGS=...)
endif()


find_path(GMP_INCLUDE_DIR NAMES gmp.h)
find_library(GMP_LIBRARY gmp)
Expand Down Expand Up @@ -194,6 +200,4 @@ add_library(

target_link_libraries(ff GMP::gmp gmpxx ${PROCPS_LIBRARIES})

#add_subdirectory(depends)
add_subdirectory(src)

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ bin/miximus_genKeys: build/Makefile
build/src/libmiximus.$(DLL_EXT): build/Makefile
make -C build

cmake-debug:
cmake-debug: build
cd build && cmake -DCMAKE_BUILD_TYPE=Debug ..

cmake-release:
cmake-release: build
cd build && cmake -DCMAKE_BUILD_TYPE=Release ..

release: cmake-release all
Expand Down
1 change: 1 addition & 0 deletions contracts/Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ library Verifier
return Verify(vk, pwi.proof, pwi.input);
}


function Verify (VerifyingKey memory vk, Proof memory proof, uint256[] memory input)
internal view returns (bool)
{
Expand Down
7 changes: 4 additions & 3 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
add_executable(benchmark_mpz_mul benchmark_mpz_mul.cpp)
target_link_libraries(benchmark_mpz_mul ff)

add_executable(benchmark_pairing benchmark_pairing.cpp)
target_link_libraries(benchmark_pairing ff)

add_executable(test_vk_raw2json test_vk_raw2json.cpp)
target_link_libraries(test_vk_raw2json ethsnarks_common)


add_executable(test_shootout test_shootout.cpp)
target_link_libraries(test_shootout ff)

add_executable(test_load_proofkey test_load_proofkey.cpp)
target_link_libraries(test_load_proofkey ethsnarks_common)

Expand Down
36 changes: 36 additions & 0 deletions src/test/benchmark_mpz_mul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "ethsnarks.hpp"

using namespace libsnark;
using namespace libff;

int main( )
{
bigint<alt_bn128_r_limbs> number_a;
bigint<alt_bn128_r_limbs> number_b;

number_a.randomize();
number_b.randomize();

mpz_t result;
mpz_init(result);

mpz_t number_a_mpz;
mpz_init(number_a_mpz);
number_a.to_mpz(number_a_mpz);

mpz_t number_b_mpz;
mpz_init(number_b_mpz);
number_b.to_mpz(number_b_mpz);

enter_block("Multiplying with MPZ, 1 million times");

for( int i = 0; i < 100000000; i++ ) {
mpz_mul(result, number_a_mpz, number_b_mpz);
}

leave_block("Multiplying with MPZ, 1 million times");

mpz_clear(number_a_mpz);
mpz_clear(number_b_mpz);
mpz_clear(result);
}
File renamed without changes.
64 changes: 34 additions & 30 deletions test/TestMiximus.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
const TestableMiximus = artifacts.require("TestableMiximus");

const crypto = require('crypto');
const crypto = require("crypto");

const fs = require('fs');
const ffi = require('ffi');
const ref = require('ref');
const ArrayType = require('ref-array');
const BigNumber = require('bignumber.js');
const fs = require("fs");
const ffi = require("ffi");
const ref = require("ref");
const ArrayType = require("ref-array");
const BigNumber = require("bignumber.js");

var StringArray = ArrayType(ref.types.CString);

var libmiximus = ffi.Library('build/src/libmiximus', {
var libmiximus = ffi.Library("build/src/libmiximus", {
// Retrieve depth of tree
'miximus_tree_depth': [
'size_t', []
"miximus_tree_depth": [
"size_t", []
],

// Create a proof for the parameters
'miximus_prove': [
'string', [
'string', // pk_file
'string', // in_root
'string', // in_nullifier
'string', // in_exthash
'string', // in_spend_preimage
'string', // in_address
"miximus_prove": [
"string", [
"string", // pk_file
"string", // in_root
"string", // in_nullifier
"string", // in_exthash
"string", // in_spend_preimage
"string", // in_address
StringArray, // in_path
]
],

// Verify a proof
'miximus_verify': [
'bool', [
'string', // vk_json
'string', // proof_json
"miximus_verify": [
"bool", [
"string", // vk_json
"string", // proof_json
]
]
});
Expand Down Expand Up @@ -67,15 +67,14 @@ let proof_to_flat = (proof) => {
};



contract("TestableMiximus", () => {
describe("Deposit", () => {
it("deposits then withdraws", async () => {
let obj = await TestableMiximus.deployed();

// Parameters for deposit
let spend_preimage = new BigNumber(crypto.randomBytes(30).toString('hex'), 16);
let nullifier = new BigNumber(crypto.randomBytes(30).toString('hex'), 16);
let spend_preimage = new BigNumber(crypto.randomBytes(30).toString("hex"), 16);
let nullifier = new BigNumber(crypto.randomBytes(30).toString("hex"), 16);
let leaf_hash = await obj.MakeLeafHash.call(spend_preimage, nullifier);


Expand All @@ -84,9 +83,12 @@ contract("TestableMiximus", () => {
await obj.Deposit.sendTransaction([leaf_hash], {value: 1000000000000000000});


// TODO: verify amount has been transferred


// Build parameters for proving
let tmp = await obj.GetPath.call(new_root_and_offset[1]);
let proof_address = tmp[1].map((_) => _ ? '1' : '0').join('');
let proof_address = tmp[1].map((_) => _ ? "1" : "0").join("");
let proof_path = [];
for( var i = 0; i < proof_address.length; i++ ) {
proof_path.push( tmp[0][i].toString(10) );
Expand All @@ -109,17 +111,16 @@ contract("TestableMiximus", () => {
let proof_json = libmiximus.miximus_prove(...args);
assert.notStrictEqual(proof_json, null);
let proof = JSON.parse(proof_json);
console.log("Proof:", proof);


// Ensure proof inputs match ours
assert.strictEqual('0x' + proof_root.toString(16), proof.input[0]);
assert.strictEqual('0x' + nullifier.toString(16), proof.input[1]);
assert.strictEqual('0x' + proof_exthash.toString(16), proof.input[2]);
assert.strictEqual("0x" + proof_root.toString(16), proof.input[0]);
assert.strictEqual("0x" + nullifier.toString(16), proof.input[1]);
assert.strictEqual("0x" + proof_exthash.toString(16), proof.input[2]);


// Re-verify proof using native library
let vk_json = fs.readFileSync('zksnark_element/miximus.vk.json');
let vk_json = fs.readFileSync("zksnark_element/miximus.vk.json");
let proof_valid_native = libmiximus.miximus_verify(vk_json, proof_json);
assert.strictEqual(proof_valid_native, true);
let vk = JSON.parse(vk_json);
Expand Down Expand Up @@ -165,6 +166,9 @@ contract("TestableMiximus", () => {
// Verify nullifier exists
let is_spent = await obj.IsSpent(nullifier.toString(10));
assert.strictEqual(is_spent, true);


// TODO: verify balance has been increased
});
});
});
1 change: 0 additions & 1 deletion test/test_merkle.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def test_known_2pow28(self):

proof_a = tree.proof(0)
self.assertTrue(proof_a.verify(tree.root))
print(proof_a.address)

proof_b = tree.proof(1)
self.assertTrue(proof_b.verify(tree.root))
Expand Down

0 comments on commit 5b4f664

Please sign in to comment.