diff --git a/CMakeLists.txt b/CMakeLists.txt index ff8bd07..db7232a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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( @@ -60,6 +52,12 @@ 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" @@ -67,11 +65,12 @@ option( ) 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" @@ -103,8 +102,6 @@ endif() if("${DEBUG}" OR "${CMAKE_BUILD_TYPE}" STREQUAL "Debug") add_definitions(-DDEBUG=1) -else() - add_compile_options(-O3) endif() @@ -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) @@ -194,6 +200,4 @@ add_library( target_link_libraries(ff GMP::gmp gmpxx ${PROCPS_LIBRARIES}) -#add_subdirectory(depends) add_subdirectory(src) - diff --git a/Makefile b/Makefile index d4e421f..c452c65 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/contracts/Verifier.sol b/contracts/Verifier.sol index 7900631..b762444 100644 --- a/contracts/Verifier.sol +++ b/contracts/Verifier.sol @@ -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) { diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 418c59f..7cbb827 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -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) diff --git a/src/test/benchmark_mpz_mul.cpp b/src/test/benchmark_mpz_mul.cpp new file mode 100644 index 0000000..22adc26 --- /dev/null +++ b/src/test/benchmark_mpz_mul.cpp @@ -0,0 +1,36 @@ +#include "ethsnarks.hpp" + +using namespace libsnark; +using namespace libff; + +int main( ) +{ + bigint number_a; + bigint 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); +} \ No newline at end of file diff --git a/src/test/test_shootout.cpp b/src/test/benchmark_pairing.cpp similarity index 100% rename from src/test/test_shootout.cpp rename to src/test/benchmark_pairing.cpp diff --git a/test/TestMiximus.js b/test/TestMiximus.js index ed36856..812356b 100644 --- a/test/TestMiximus.js +++ b/test/TestMiximus.js @@ -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 ] ] }); @@ -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); @@ -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) ); @@ -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); @@ -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 }); }); }); diff --git a/test/test_merkle.py b/test/test_merkle.py index 981df29..f71f001 100644 --- a/test/test_merkle.py +++ b/test/test_merkle.py @@ -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))